UNPKG

2.69 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.getResponse = exports.getStatusCode = exports.argumentsFor = exports.getServiceMethod = exports.knownMethods = exports.statusCodes = exports.METHOD_HEADER = void 0;
7const lib_1 = require("@feathersjs/errors/lib");
8const encodeurl_1 = __importDefault(require("encodeurl"));
9exports.METHOD_HEADER = 'x-service-method';
10exports.statusCodes = {
11 created: 201,
12 noContent: 204,
13 methodNotAllowed: 405,
14 success: 200,
15 seeOther: 303
16};
17exports.knownMethods = {
18 post: 'create',
19 patch: 'patch',
20 put: 'update',
21 delete: 'remove'
22};
23function 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}
37exports.getServiceMethod = getServiceMethod;
38exports.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};
47function 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}
63exports.getStatusCode = getStatusCode;
64function 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}
76exports.getResponse = getResponse;
77//# sourceMappingURL=http.js.map
\No newline at end of file