UNPKG

3.5 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.builtinParsers = exports.getParserOptions = exports.DEFAULT_LIMIT = exports.invokeBodyParserMiddleware = exports.normalizeParsingError = exports.getContentType = void 0;
8const tslib_1 = require("tslib");
9const debug_1 = tslib_1.__importDefault(require("debug"));
10const debug = (0, debug_1.default)('loopback:rest:body-parser');
11/**
12 * Get the content-type header value from the request
13 * @param req - Http request
14 */
15function getContentType(req) {
16 return req.get('content-type');
17}
18exports.getContentType = getContentType;
19/**
20 * Normalize parsing errors as `4xx`
21 * @param err
22 */
23function normalizeParsingError(err) {
24 debug('Cannot parse request body %j', err);
25 if (!err.statusCode || err.statusCode >= 500) {
26 err.statusCode = 400;
27 }
28 return err;
29}
30exports.normalizeParsingError = normalizeParsingError;
31/* eslint-disable @typescript-eslint/no-explicit-any */
32/**
33 * Parse the request body asynchronously
34 * @param handle - The express middleware handler
35 * @param request - Http request
36 */
37function invokeBodyParserMiddleware(handle, request) {
38 // A hack to fool TypeScript as we don't need `response`
39 const response = {};
40 return new Promise((resolve, reject) => {
41 handle(request, response, err => {
42 if (err) {
43 reject(err);
44 return;
45 }
46 resolve(request.body);
47 });
48 });
49}
50exports.invokeBodyParserMiddleware = invokeBodyParserMiddleware;
51// Default limit of the body length
52exports.DEFAULT_LIMIT = '1mb';
53function getParserOptions(type, options) {
54 const opts = { limit: exports.DEFAULT_LIMIT };
55 switch (type) {
56 case 'json':
57 // Allow */json and */*+json
58 opts.type = ['*/json', '*/*+json'];
59 opts.strict = false;
60 break;
61 case 'urlencoded':
62 opts.type = type;
63 opts.extended = true;
64 break;
65 case 'text':
66 // Set media type to `text/*` to match `text/plain` or `text/html`
67 opts.type = 'text/*';
68 break;
69 case 'raw':
70 opts.type = ['application/octet-stream', '*/*'];
71 break;
72 }
73 Object.assign(opts, options[type], options);
74 for (const k of ['json', 'urlencoded', 'text', 'raw']) {
75 delete opts[k];
76 }
77 return opts;
78}
79exports.getParserOptions = getParserOptions;
80var builtinParsers;
81(function (builtinParsers) {
82 builtinParsers.json = Symbol('json');
83 builtinParsers.urlencoded = Symbol('urlencoded');
84 builtinParsers.text = Symbol('text');
85 builtinParsers.raw = Symbol('raw');
86 builtinParsers.stream = Symbol('stream');
87 builtinParsers.names = [
88 builtinParsers.json,
89 builtinParsers.urlencoded,
90 builtinParsers.text,
91 builtinParsers.raw,
92 builtinParsers.stream,
93 ];
94 builtinParsers.mapping = {
95 json: builtinParsers.json,
96 urlencoded: builtinParsers.urlencoded,
97 text: builtinParsers.text,
98 raw: builtinParsers.raw,
99 stream: builtinParsers.stream,
100 };
101})(builtinParsers = exports.builtinParsers || (exports.builtinParsers = {}));
102//# sourceMappingURL=body-parser.helpers.js.map
\No newline at end of file