UNPKG

4.41 kBJavaScriptView Raw
1"use strict";
2var __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}));
13var __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};
16var __importDefault = (this && this.__importDefault) || function (mod) {
17 return (mod && mod.__esModule) ? mod : { "default": mod };
18};
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.serveStatic = exports.cors = exports.bodyParser = exports.Koa = void 0;
21exports.koa = koa;
22const koa_1 = __importDefault(require("koa"));
23exports.Koa = koa_1.default;
24const koa_qs_1 = __importDefault(require("koa-qs"));
25const transport_commons_1 = require("@feathersjs/transport-commons");
26const commons_1 = require("@feathersjs/commons");
27const koa_body_1 = require("koa-body");
28Object.defineProperty(exports, "bodyParser", { enumerable: true, get: function () { return koa_body_1.koaBody; } });
29const cors_1 = __importDefault(require("@koa/cors"));
30exports.cors = cors_1.default;
31const koa_static_1 = __importDefault(require("koa-static"));
32exports.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);
37const debug = (0, commons_1.createDebug)('@feathersjs/koa');
38function 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 // Copy all non-existing properties (including non-enumerables)
82 // that don't already exist on the Express app
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 // This reinitializes hooks
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//# sourceMappingURL=index.js.map
\No newline at end of file