1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.parseOperationArgs = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const openapi_v3_1 = require("@loopback/openapi-v3");
|
10 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
11 | const body_parsers_1 = require("./body-parsers");
|
12 | const coerce_parameter_1 = require("./coercion/coerce-parameter");
|
13 | const rest_http_error_1 = require("./rest-http-error");
|
14 | const ajv_factory_provider_1 = require("./validation/ajv-factory.provider");
|
15 | const request_body_validator_1 = require("./validation/request-body.validator");
|
16 | const debug = (0, debug_1.default)('loopback:rest:parser');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | async function parseOperationArgs(request, route, requestBodyParser = new body_parsers_1.RequestBodyParser(), options = ajv_factory_provider_1.DEFAULT_AJV_VALIDATION_OPTIONS) {
|
25 | debug('Parsing operation arguments for route %s', route.describe());
|
26 | const operationSpec = route.spec;
|
27 | const pathParams = route.pathParams;
|
28 | const body = await requestBodyParser.loadRequestBodyIfNeeded(operationSpec, request);
|
29 | return buildOperationArguments(operationSpec, request, pathParams, body, route.schemas, options);
|
30 | }
|
31 | exports.parseOperationArgs = parseOperationArgs;
|
32 | async function buildOperationArguments(operationSpec, request, pathParams, body, globalSchemas, options = ajv_factory_provider_1.DEFAULT_AJV_VALIDATION_OPTIONS) {
|
33 | var _a;
|
34 | let requestBodyIndex = -1;
|
35 | if (operationSpec.requestBody) {
|
36 |
|
37 |
|
38 | if ((0, openapi_v3_1.isReferenceObject)(operationSpec.requestBody)) {
|
39 | throw new Error('$ref requestBody is not supported yet.');
|
40 | }
|
41 | const i = operationSpec.requestBody[openapi_v3_1.REQUEST_BODY_INDEX];
|
42 | requestBodyIndex = i !== null && i !== void 0 ? i : 0;
|
43 | }
|
44 | const paramArgs = [];
|
45 | for (const paramSpec of (_a = operationSpec.parameters) !== null && _a !== void 0 ? _a : []) {
|
46 | if ((0, openapi_v3_1.isReferenceObject)(paramSpec)) {
|
47 |
|
48 |
|
49 | throw new Error('$ref parameters are not supported yet.');
|
50 | }
|
51 | const spec = paramSpec;
|
52 | const rawValue = getParamFromRequest(spec, request, pathParams);
|
53 | const coercedValue = await (0, coerce_parameter_1.coerceParameter)(rawValue, spec, options);
|
54 | paramArgs.push(coercedValue);
|
55 | }
|
56 | debug('Validating request body - value %j', body);
|
57 | await (0, request_body_validator_1.validateRequestBody)(body, operationSpec.requestBody, globalSchemas, options);
|
58 | if (requestBodyIndex >= 0) {
|
59 | paramArgs.splice(requestBodyIndex, 0, body.value);
|
60 | }
|
61 | return paramArgs;
|
62 | }
|
63 | function getParamFromRequest(spec, request, pathParams) {
|
64 | switch (spec.in) {
|
65 | case 'query':
|
66 | return request.query[spec.name];
|
67 | case 'path':
|
68 | return pathParams[spec.name];
|
69 | case 'header':
|
70 |
|
71 | return request.headers[spec.name.toLowerCase()];
|
72 |
|
73 |
|
74 | default:
|
75 | throw rest_http_error_1.RestHttpErrors.invalidParamLocation(spec.in);
|
76 | }
|
77 | }
|
78 |
|
\ | No newline at end of file |