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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15 | }) : function(o, v) {
|
16 | o["default"] = v;
|
17 | });
|
18 | var __importStar = (this && this.__importStar) || function (mod) {
|
19 | if (mod && mod.__esModule) return mod;
|
20 | var result = {};
|
21 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22 | __setModuleDefault(result, mod);
|
23 | return result;
|
24 | };
|
25 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
26 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
27 | };
|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
29 | exports.compression = exports.cors = exports.authenticate = exports.parseAuthentication = exports.notFound = exports.errorHandler = exports.formatter = exports.Router = exports.rest = exports.query = exports.urlencoded = exports.text = exports.raw = exports.json = exports.static = exports.serveStatic = exports.original = void 0;
|
30 | exports.default = feathersExpress;
|
31 | const express_1 = __importDefault(require("express"));
|
32 | const feathers_1 = require("@feathersjs/feathers");
|
33 | const transport_commons_1 = require("@feathersjs/transport-commons");
|
34 | const commons_1 = require("@feathersjs/commons");
|
35 | const cors_1 = __importDefault(require("cors"));
|
36 | exports.cors = cors_1.default;
|
37 | const compression_1 = __importDefault(require("compression"));
|
38 | exports.compression = compression_1.default;
|
39 | const rest_1 = require("./rest");
|
40 | Object.defineProperty(exports, "rest", { enumerable: true, get: function () { return rest_1.rest; } });
|
41 | Object.defineProperty(exports, "formatter", { enumerable: true, get: function () { return rest_1.formatter; } });
|
42 | const handlers_1 = require("./handlers");
|
43 | Object.defineProperty(exports, "errorHandler", { enumerable: true, get: function () { return handlers_1.errorHandler; } });
|
44 | Object.defineProperty(exports, "notFound", { enumerable: true, get: function () { return handlers_1.notFound; } });
|
45 | const authentication_1 = require("./authentication");
|
46 | Object.defineProperty(exports, "authenticate", { enumerable: true, get: function () { return authentication_1.authenticate; } });
|
47 | Object.defineProperty(exports, "parseAuthentication", { enumerable: true, get: function () { return authentication_1.parseAuthentication; } });
|
48 | const express_2 = __importStar(require("express"));
|
49 | Object.defineProperty(exports, "original", { enumerable: true, get: function () { return express_2.default; } });
|
50 | Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return express_2.static; } });
|
51 | Object.defineProperty(exports, "static", { enumerable: true, get: function () { return express_2.static; } });
|
52 | Object.defineProperty(exports, "json", { enumerable: true, get: function () { return express_2.json; } });
|
53 | Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return express_2.raw; } });
|
54 | Object.defineProperty(exports, "text", { enumerable: true, get: function () { return express_2.text; } });
|
55 | Object.defineProperty(exports, "urlencoded", { enumerable: true, get: function () { return express_2.urlencoded; } });
|
56 | Object.defineProperty(exports, "query", { enumerable: true, get: function () { return express_2.query; } });
|
57 | Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return express_2.Router; } });
|
58 | const debug = (0, commons_1.createDebug)('@feathersjs/express');
|
59 | function feathersExpress(feathersApp, expressApp = (0, express_1.default)()) {
|
60 | if (!feathersApp) {
|
61 | return expressApp;
|
62 | }
|
63 | if (typeof feathersApp.setup !== 'function') {
|
64 | throw new Error('@feathersjs/express requires a valid Feathers application instance');
|
65 | }
|
66 | const app = expressApp;
|
67 | const { use: expressUse, listen: expressListen } = expressApp;
|
68 | const { use: feathersUse, teardown: feathersTeardown } = feathersApp;
|
69 | Object.assign(app, {
|
70 | use(location, ...rest) {
|
71 | let service;
|
72 | let options = {};
|
73 | const middleware = rest.reduce(function (middleware, arg) {
|
74 | if (typeof arg === 'function' || Array.isArray(arg)) {
|
75 | middleware[service ? 'after' : 'before'].push(arg);
|
76 | }
|
77 | else if (!service) {
|
78 | service = arg;
|
79 | }
|
80 | else if (arg.methods || arg.events || arg.express || arg.koa) {
|
81 | options = arg;
|
82 | }
|
83 | else {
|
84 | throw new Error('Invalid options passed to app.use');
|
85 | }
|
86 | return middleware;
|
87 | }, {
|
88 | before: [],
|
89 | after: []
|
90 | });
|
91 | const hasMethod = (methods) => methods.some((name) => service && typeof service[name] === 'function');
|
92 |
|
93 | if (hasMethod(['handle', 'set']) || !hasMethod(feathers_1.defaultServiceMethods)) {
|
94 | debug('Passing app.use call to Express app');
|
95 | return expressUse.call(this, location, ...rest);
|
96 | }
|
97 | debug('Registering service with middleware', middleware);
|
98 |
|
99 | feathersUse.call(this, location, service, {
|
100 | express: middleware,
|
101 | ...options
|
102 | });
|
103 | return this;
|
104 | },
|
105 | async listen(...args) {
|
106 | const server = expressListen.call(this, ...args);
|
107 | this.server = server;
|
108 | await this.setup(server);
|
109 | debug('Feathers application listening');
|
110 | return server;
|
111 | }
|
112 | });
|
113 | const appDescriptors = {
|
114 | ...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(app)),
|
115 | ...Object.getOwnPropertyDescriptors(app)
|
116 | };
|
117 | const newDescriptors = {
|
118 | ...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(feathersApp)),
|
119 | ...Object.getOwnPropertyDescriptors(feathersApp)
|
120 | };
|
121 |
|
122 |
|
123 | Object.keys(newDescriptors).forEach((prop) => {
|
124 | const appProp = appDescriptors[prop];
|
125 | const newProp = newDescriptors[prop];
|
126 | if (appProp === undefined && newProp !== undefined) {
|
127 | Object.defineProperty(expressApp, prop, newProp);
|
128 | }
|
129 | });
|
130 |
|
131 | app.setup = feathersApp.setup;
|
132 | app.teardown = async function teardown(server) {
|
133 | return feathersTeardown.call(this, server).then(() => new Promise((resolve, reject) => {
|
134 | if (this.server) {
|
135 | this.server.close((e) => (e ? reject(e) : resolve(this)));
|
136 | }
|
137 | else {
|
138 | resolve(this);
|
139 | }
|
140 | }));
|
141 | };
|
142 | app.configure((0, transport_commons_1.routing)());
|
143 | app.use((req, _res, next) => {
|
144 | req.feathers = { ...req.feathers, provider: 'rest' };
|
145 | return next();
|
146 | });
|
147 | return app;
|
148 | }
|
149 | if (typeof module !== 'undefined') {
|
150 | module.exports = Object.assign(feathersExpress, module.exports);
|
151 | }
|
152 |
|
\ | No newline at end of file |