source: node_modules/swagger-client/lib/execute/oas3/build-request.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.5 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.applySecurities = applySecurities;
6exports.default = buildRequest;
7var _isPlainObject = require("is-plain-object");
8var _btoaNode = _interopRequireDefault(require("../../helpers/btoa.node.js"));
9// This function runs after the common function,
10// `src/execute/index.js#buildRequest`
11
12function buildRequest(options, req) {
13 const {
14 operation,
15 requestBody,
16 securities,
17 spec,
18 attachContentTypeForEmptyPayload
19 } = options;
20 let {
21 requestContentType
22 } = options;
23 req = applySecurities({
24 request: req,
25 securities,
26 operation,
27 spec
28 });
29 const requestBodyDef = operation.requestBody || {};
30 const requestBodyMediaTypes = Object.keys(requestBodyDef.content || {});
31 const isExplicitContentTypeValid = requestContentType && requestBodyMediaTypes.indexOf(requestContentType) > -1;
32
33 // for OAS3: set the Content-Type
34 if (requestBody || attachContentTypeForEmptyPayload) {
35 // does the passed requestContentType appear in the requestBody definition?
36
37 if (requestContentType && isExplicitContentTypeValid) {
38 req.headers['Content-Type'] = requestContentType;
39 } else if (!requestContentType) {
40 const firstMediaType = requestBodyMediaTypes[0];
41 if (firstMediaType) {
42 req.headers['Content-Type'] = firstMediaType;
43 requestContentType = firstMediaType;
44 }
45 }
46 } else if (requestContentType && isExplicitContentTypeValid) {
47 req.headers['Content-Type'] = requestContentType;
48 }
49 if (!options.responseContentType && operation.responses) {
50 const mediaTypes = Object.entries(operation.responses).filter(([key, value]) => {
51 const code = parseInt(key, 10);
52 return code >= 200 && code < 300 && (0, _isPlainObject.isPlainObject)(value.content);
53 }).reduce((acc, [, value]) => acc.concat(Object.keys(value.content)), []);
54 if (mediaTypes.length > 0) {
55 req.headers.accept = mediaTypes.join(', ');
56 }
57 }
58
59 // for OAS3: add requestBody to request
60 if (requestBody) {
61 if (requestContentType) {
62 if (requestBodyMediaTypes.indexOf(requestContentType) > -1) {
63 // only attach body if the requestBody has a definition for the
64 // contentType that has been explicitly set
65 if (requestContentType === 'application/x-www-form-urlencoded' || requestContentType === 'multipart/form-data') {
66 if (typeof requestBody === 'object') {
67 var _requestBodyDef$conte, _requestBodyDef$conte2;
68 const encoding = (_requestBodyDef$conte = (_requestBodyDef$conte2 = requestBodyDef.content[requestContentType]) == null ? void 0 : _requestBodyDef$conte2.encoding) != null ? _requestBodyDef$conte : {};
69 req.form = {};
70 Object.keys(requestBody).forEach(k => {
71 req.form[k] = {
72 value: requestBody[k],
73 encoding: encoding[k] || {}
74 };
75 });
76 } else {
77 req.form = requestBody;
78 }
79 } else {
80 req.body = requestBody;
81 }
82 }
83 } else {
84 req.body = requestBody;
85 }
86 }
87 return req;
88}
89
90// Add security values, to operations - that declare their need on them
91// Adapted from the Swagger2 implementation
92function applySecurities({
93 request,
94 securities = {},
95 operation = {},
96 spec
97}) {
98 var _spec$components;
99 const result = {
100 ...request
101 };
102 const {
103 authorized = {}
104 } = securities;
105 const security = operation.security || spec.security || [];
106 const isAuthorized = authorized && !!Object.keys(authorized).length;
107 const securityDef = (spec == null || (_spec$components = spec.components) == null ? void 0 : _spec$components.securitySchemes) || {};
108 result.headers = result.headers || {};
109 result.query = result.query || {};
110 if (!Object.keys(securities).length || !isAuthorized || !security || Array.isArray(operation.security) && !operation.security.length) {
111 return request;
112 }
113 security.forEach(securityObj => {
114 Object.keys(securityObj).forEach(key => {
115 const auth = authorized[key];
116 const schema = securityDef[key];
117 if (!auth) {
118 return;
119 }
120 const value = auth.value || auth;
121 const {
122 type
123 } = schema;
124 if (auth) {
125 if (type === 'apiKey') {
126 if (schema.in === 'query') {
127 result.query[schema.name] = value;
128 }
129 if (schema.in === 'header') {
130 result.headers[schema.name] = value;
131 }
132 if (schema.in === 'cookie') {
133 result.cookies[schema.name] = value;
134 }
135 } else if (type === 'http') {
136 if (/^basic$/i.test(schema.scheme)) {
137 const username = value.username || '';
138 const password = value.password || '';
139 const encoded = (0, _btoaNode.default)(`${username}:${password}`);
140 result.headers.Authorization = `Basic ${encoded}`;
141 }
142 if (/^bearer$/i.test(schema.scheme)) {
143 result.headers.Authorization = `Bearer ${value}`;
144 }
145 } else if (type === 'oauth2' || type === 'openIdConnect') {
146 const token = auth.token || {};
147 const tokenName = schema['x-tokenName'] || 'access_token';
148 const tokenValue = token[tokenName];
149 let tokenType = token.token_type;
150 if (!tokenType || tokenType.toLowerCase() === 'bearer') {
151 tokenType = 'Bearer';
152 }
153 result.headers.Authorization = `${tokenType} ${tokenValue}`;
154 }
155 }
156 });
157 });
158 return result;
159}
Note: See TracBrowser for help on using the repository browser.