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.getResponse = exports.getStatusCode = exports.argumentsFor = exports.getServiceMethod = exports.knownMethods = exports.statusCodes = exports.METHOD_HEADER = void 0;
|
7 | const lib_1 = require("@feathersjs/errors/lib");
|
8 | const encodeurl_1 = __importDefault(require("encodeurl"));
|
9 | exports.METHOD_HEADER = 'x-service-method';
|
10 | exports.statusCodes = {
|
11 | created: 201,
|
12 | noContent: 204,
|
13 | methodNotAllowed: 405,
|
14 | success: 200,
|
15 | seeOther: 303
|
16 | };
|
17 | exports.knownMethods = {
|
18 | post: 'create',
|
19 | patch: 'patch',
|
20 | put: 'update',
|
21 | delete: 'remove'
|
22 | };
|
23 | function getServiceMethod(_httpMethod, id, headerOverride) {
|
24 | const httpMethod = _httpMethod.toLowerCase();
|
25 | if (httpMethod === 'post' && headerOverride) {
|
26 | return headerOverride;
|
27 | }
|
28 | const mappedMethod = exports.knownMethods[httpMethod];
|
29 | if (mappedMethod) {
|
30 | return mappedMethod;
|
31 | }
|
32 | if (httpMethod === 'get') {
|
33 | return id === null ? 'find' : 'get';
|
34 | }
|
35 | throw new lib_1.MethodNotAllowed(`Method ${_httpMethod} not allowed`);
|
36 | }
|
37 | exports.getServiceMethod = getServiceMethod;
|
38 | exports.argumentsFor = {
|
39 | get: ({ id, params }) => [id, params],
|
40 | find: ({ params }) => [params],
|
41 | create: ({ data, params }) => [data, params],
|
42 | update: ({ id, data, params }) => [id, data, params],
|
43 | patch: ({ id, data, params }) => [id, data, params],
|
44 | remove: ({ id, params }) => [id, params],
|
45 | default: ({ data, params }) => [data, params]
|
46 | };
|
47 | function getStatusCode(context, body, location) {
|
48 | const { http = {} } = context;
|
49 | if (http.status) {
|
50 | return http.status;
|
51 | }
|
52 | if (context.method === 'create') {
|
53 | return exports.statusCodes.created;
|
54 | }
|
55 | if (location !== undefined) {
|
56 | return exports.statusCodes.seeOther;
|
57 | }
|
58 | if (!body) {
|
59 | return exports.statusCodes.noContent;
|
60 | }
|
61 | return exports.statusCodes.success;
|
62 | }
|
63 | exports.getStatusCode = getStatusCode;
|
64 | function getResponse(context) {
|
65 | const { http = {} } = context;
|
66 | const body = context.dispatch !== undefined ? context.dispatch : context.result;
|
67 | let headers = http.headers || {};
|
68 | let location = headers.Location;
|
69 | if (http.location !== undefined) {
|
70 | location = (0, encodeurl_1.default)(http.location);
|
71 | headers = { ...headers, Location: location };
|
72 | }
|
73 | const status = getStatusCode(context, body, location);
|
74 | return { status, headers, body };
|
75 | }
|
76 | exports.getResponse = getResponse;
|
77 |
|
\ | No newline at end of file |