UNPKG

7.82 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 __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});
18var __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};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.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;
30exports.default = feathersExpress;
31const express_1 = __importDefault(require("express"));
32const feathers_1 = require("@feathersjs/feathers");
33const transport_commons_1 = require("@feathersjs/transport-commons");
34const commons_1 = require("@feathersjs/commons");
35const cors_1 = __importDefault(require("cors"));
36exports.cors = cors_1.default;
37const compression_1 = __importDefault(require("compression"));
38exports.compression = compression_1.default;
39const rest_1 = require("./rest");
40Object.defineProperty(exports, "rest", { enumerable: true, get: function () { return rest_1.rest; } });
41Object.defineProperty(exports, "formatter", { enumerable: true, get: function () { return rest_1.formatter; } });
42const handlers_1 = require("./handlers");
43Object.defineProperty(exports, "errorHandler", { enumerable: true, get: function () { return handlers_1.errorHandler; } });
44Object.defineProperty(exports, "notFound", { enumerable: true, get: function () { return handlers_1.notFound; } });
45const authentication_1 = require("./authentication");
46Object.defineProperty(exports, "authenticate", { enumerable: true, get: function () { return authentication_1.authenticate; } });
47Object.defineProperty(exports, "parseAuthentication", { enumerable: true, get: function () { return authentication_1.parseAuthentication; } });
48const express_2 = __importStar(require("express"));
49Object.defineProperty(exports, "original", { enumerable: true, get: function () { return express_2.default; } });
50Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return express_2.static; } });
51Object.defineProperty(exports, "static", { enumerable: true, get: function () { return express_2.static; } });
52Object.defineProperty(exports, "json", { enumerable: true, get: function () { return express_2.json; } });
53Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return express_2.raw; } });
54Object.defineProperty(exports, "text", { enumerable: true, get: function () { return express_2.text; } });
55Object.defineProperty(exports, "urlencoded", { enumerable: true, get: function () { return express_2.urlencoded; } });
56Object.defineProperty(exports, "query", { enumerable: true, get: function () { return express_2.query; } });
57Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return express_2.Router; } });
58const debug = (0, commons_1.createDebug)('@feathersjs/express');
59function 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 // Check for service (any object with at least one service method)
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 // Since this is a service, call Feathers `.use`
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 // Copy all non-existing properties (including non-enumerables)
122 // that don't already exist on the Express app
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 // Assign teardown and setup which will also make sure that hooks are initialized
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}
149if (typeof module !== 'undefined') {
150 module.exports = Object.assign(feathersExpress, module.exports);
151}
152//# sourceMappingURL=index.js.map
\No newline at end of file