1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6 | desc = { enumerable: true, get: function() { return m[k]; } };
|
7 | }
|
8 | Object.defineProperty(o, k2, desc);
|
9 | }) : (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | o[k2] = m[k];
|
12 | }));
|
13 | var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14 | for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15 | };
|
16 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
17 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
18 | };
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.serveStatic = exports.cors = exports.bodyParser = exports.Koa = void 0;
|
21 | exports.koa = koa;
|
22 | const koa_1 = __importDefault(require("koa"));
|
23 | exports.Koa = koa_1.default;
|
24 | const koa_qs_1 = __importDefault(require("koa-qs"));
|
25 | const transport_commons_1 = require("@feathersjs/transport-commons");
|
26 | const commons_1 = require("@feathersjs/commons");
|
27 | const koa_body_1 = require("koa-body");
|
28 | Object.defineProperty(exports, "bodyParser", { enumerable: true, get: function () { return koa_body_1.koaBody; } });
|
29 | const cors_1 = __importDefault(require("@koa/cors"));
|
30 | exports.cors = cors_1.default;
|
31 | const koa_static_1 = __importDefault(require("koa-static"));
|
32 | exports.serveStatic = koa_static_1.default;
|
33 | __exportStar(require("./authentication"), exports);
|
34 | __exportStar(require("./declarations"), exports);
|
35 | __exportStar(require("./handlers"), exports);
|
36 | __exportStar(require("./rest"), exports);
|
37 | const debug = (0, commons_1.createDebug)('@feathersjs/koa');
|
38 | function koa(feathersApp, koaApp = new koa_1.default()) {
|
39 | if (!feathersApp) {
|
40 | return koaApp;
|
41 | }
|
42 | if (typeof feathersApp.setup !== 'function') {
|
43 | throw new Error('@feathersjs/koa requires a valid Feathers application instance');
|
44 | }
|
45 | const app = feathersApp;
|
46 | const { listen: koaListen, use: koaUse } = koaApp;
|
47 | const { use: feathersUse, teardown: feathersTeardown } = feathersApp;
|
48 | Object.assign(app, {
|
49 | use(location, ...args) {
|
50 | if (typeof location === 'string') {
|
51 | return feathersUse.call(this, location, ...args);
|
52 | }
|
53 | return koaUse.call(this, location);
|
54 | },
|
55 | async listen(port, ...args) {
|
56 | const server = koaListen.call(this, port, ...args);
|
57 | this.server = server;
|
58 | await this.setup(server);
|
59 | debug('Feathers application listening');
|
60 | return server;
|
61 | },
|
62 | async teardown(server) {
|
63 | return feathersTeardown.call(this, server).then(() => new Promise((resolve, reject) => {
|
64 | if (this.server) {
|
65 | this.server.close((e) => (e ? reject(e) : resolve(this)));
|
66 | }
|
67 | else {
|
68 | resolve(this);
|
69 | }
|
70 | }));
|
71 | }
|
72 | });
|
73 | const appDescriptors = {
|
74 | ...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(app)),
|
75 | ...Object.getOwnPropertyDescriptors(app)
|
76 | };
|
77 | const newDescriptors = {
|
78 | ...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(koaApp)),
|
79 | ...Object.getOwnPropertyDescriptors(koaApp)
|
80 | };
|
81 |
|
82 |
|
83 | Object.keys(newDescriptors).forEach((prop) => {
|
84 | const appProp = appDescriptors[prop];
|
85 | const newProp = newDescriptors[prop];
|
86 | if (appProp === undefined && newProp !== undefined) {
|
87 | Object.defineProperty(app, prop, newProp);
|
88 | }
|
89 | });
|
90 | (0, koa_qs_1.default)(app);
|
91 | Object.getOwnPropertySymbols(koaApp).forEach((symbol) => {
|
92 | const target = app;
|
93 | const source = koaApp;
|
94 | target[symbol] = source[symbol];
|
95 | });
|
96 |
|
97 | app.setup = feathersApp.setup;
|
98 | app.teardown = feathersApp.teardown;
|
99 | app.configure((0, transport_commons_1.routing)());
|
100 | app.use((ctx, next) => {
|
101 | ctx.feathers = { ...ctx.feathers, provider: 'rest' };
|
102 | return next();
|
103 | });
|
104 | return app;
|
105 | }
|
106 |
|
\ | No newline at end of file |