UNPKG

3.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.rest = exports.formatter = void 0;
4const express_1 = require("express");
5const errors_1 = require("@feathersjs/errors");
6const commons_1 = require("@feathersjs/commons");
7const transport_commons_1 = require("@feathersjs/transport-commons");
8const feathers_1 = require("@feathersjs/feathers");
9const authentication_1 = require("./authentication");
10const debug = (0, commons_1.createDebug)('@feathersjs/express/rest');
11const toHandler = (func) => {
12 return (req, res, next) => func(req, res, next).catch((error) => next(error));
13};
14const serviceMiddleware = () => {
15 return toHandler(async (req, res, next) => {
16 const { query, headers, path, body: data, method: httpMethod } = req;
17 const methodOverride = req.headers[transport_commons_1.http.METHOD_HEADER];
18 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
19 const { service, params: { __id: id = null, ...route } = {} } = req.lookup;
20 const method = transport_commons_1.http.getServiceMethod(httpMethod, id, methodOverride);
21 const { methods } = (0, feathers_1.getServiceOptions)(service);
22 debug(`Found service for path ${path}, attempting to run '${method}' service method`);
23 if (!methods.includes(method) || feathers_1.defaultServiceMethods.includes(methodOverride)) {
24 const error = new errors_1.MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`);
25 res.statusCode = error.code;
26 throw error;
27 }
28 const createArguments = transport_commons_1.http.argumentsFor[method] || transport_commons_1.http.argumentsFor.default;
29 const params = { query, headers, route, ...req.feathers };
30 const args = createArguments({ id, data, params });
31 const contextBase = (0, feathers_1.createContext)(service, method, { http: {} });
32 res.hook = contextBase;
33 const context = await service[method](...args, contextBase);
34 res.hook = context;
35 const response = transport_commons_1.http.getResponse(context);
36 res.statusCode = response.status;
37 res.set(response.headers);
38 res.data = response.body;
39 return next();
40 });
41};
42const servicesMiddleware = () => {
43 return toHandler(async (req, res, next) => {
44 const app = req.app;
45 const lookup = app.lookup(req.path);
46 if (!lookup) {
47 return next();
48 }
49 req.lookup = lookup;
50 const options = (0, feathers_1.getServiceOptions)(lookup.service);
51 const middleware = options.express.composed;
52 return middleware(req, res, next);
53 });
54};
55const formatter = (_req, res, next) => {
56 if (res.data === undefined) {
57 return next();
58 }
59 res.format({
60 'application/json'() {
61 res.json(res.data);
62 }
63 });
64};
65exports.formatter = formatter;
66const rest = (options) => {
67 options = typeof options === 'function' ? { formatter: options } : options || {};
68 const formatterMiddleware = options.formatter || exports.formatter;
69 const authenticationOptions = options.authentication;
70 return (app) => {
71 if (typeof app.route !== 'function') {
72 throw new Error('@feathersjs/express/rest needs an Express compatible app.');
73 }
74 app.use((0, authentication_1.parseAuthentication)(authenticationOptions));
75 app.use(servicesMiddleware());
76 app.mixins.push((_service, _path, options) => {
77 const { express: { before = [], after = [] } = {} } = options;
78 const middlewares = [].concat(before, serviceMiddleware(), after, formatterMiddleware);
79 const middleware = (0, express_1.Router)().use(middlewares);
80 options.express || (options.express = {});
81 options.express.composed = middleware;
82 });
83 };
84};
85exports.rest = rest;
86//# sourceMappingURL=rest.js.map
\No newline at end of file