UNPKG

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