1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.rest = exports.formatter = void 0;
|
7 | const koa_compose_1 = __importDefault(require("koa-compose"));
|
8 | const transport_commons_1 = require("@feathersjs/transport-commons");
|
9 | const commons_1 = require("@feathersjs/commons");
|
10 | const feathers_1 = require("@feathersjs/feathers");
|
11 | const errors_1 = require("@feathersjs/errors");
|
12 | const authentication_1 = require("./authentication");
|
13 | const debug = (0, commons_1.createDebug)('@feathersjs/koa/rest');
|
14 | const serviceMiddleware = () => {
|
15 | return async (ctx, next) => {
|
16 | const { query, headers, path, body: data, method: httpMethod } = ctx.request;
|
17 | const methodOverride = ctx.request.headers[transport_commons_1.http.METHOD_HEADER];
|
18 |
|
19 | const { service, params: { __id: id = null, ...route } = {} } = ctx.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 | ctx.response.status = 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, ...ctx.feathers };
|
30 | const args = createArguments({ id, data, params });
|
31 | const contextBase = (0, feathers_1.createContext)(service, method, { http: {} });
|
32 | ctx.hook = contextBase;
|
33 | const context = await service[method](...args, contextBase);
|
34 | ctx.hook = context;
|
35 | const response = transport_commons_1.http.getResponse(context);
|
36 | ctx.status = response.status;
|
37 | ctx.set(response.headers);
|
38 | ctx.body = response.body;
|
39 | return next();
|
40 | };
|
41 | };
|
42 | const servicesMiddleware = () => {
|
43 | return async (ctx, next) => {
|
44 | const app = ctx.app;
|
45 | const lookup = app.lookup(ctx.request.path);
|
46 | if (!lookup) {
|
47 | return next();
|
48 | }
|
49 | ctx.lookup = lookup;
|
50 | const options = (0, feathers_1.getServiceOptions)(lookup.service);
|
51 | const middleware = options.koa.composed;
|
52 | return middleware(ctx, next);
|
53 | };
|
54 | };
|
55 |
|
56 | const formatter = (_ctx, _next) => { };
|
57 | exports.formatter = formatter;
|
58 | const rest = (options) => {
|
59 | options = typeof options === 'function' ? { formatter: options } : options || {};
|
60 | const formatterMiddleware = options.formatter || exports.formatter;
|
61 | const authenticationOptions = options.authentication;
|
62 | return (app) => {
|
63 | app.use((0, authentication_1.parseAuthentication)(authenticationOptions));
|
64 | app.use(servicesMiddleware());
|
65 | app.mixins.push((_service, _path, options) => {
|
66 | const { koa: { before = [], after = [] } = {} } = options;
|
67 | const middlewares = [].concat(before, serviceMiddleware(), after, formatterMiddleware);
|
68 | const middleware = (0, koa_compose_1.default)(middlewares);
|
69 | options.koa || (options.koa = {});
|
70 | options.koa.composed = middleware;
|
71 | });
|
72 | };
|
73 | };
|
74 | exports.rest = rest;
|
75 |
|
\ | No newline at end of file |