source: node_modules/swagger-client/es/execute/swagger2/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: 3.8 KB
Line 
1import btoa from '../../helpers/btoa.node.js';
2
3// This function runs after the common function,
4// `src/execute/index.js#buildRequest`
5export default function buildRequest(options, req) {
6 const {
7 spec,
8 operation,
9 securities,
10 requestContentType,
11 responseContentType,
12 attachContentTypeForEmptyPayload
13 } = options;
14 // Add securities, which are applicable
15 req = applySecurities({
16 request: req,
17 securities,
18 operation,
19 spec
20 });
21 if (req.body || req.form || attachContentTypeForEmptyPayload) {
22 // all following conditionals are Swagger2 only
23 if (requestContentType) {
24 req.headers['Content-Type'] = requestContentType;
25 } else if (Array.isArray(operation.consumes)) {
26 [req.headers['Content-Type']] = operation.consumes;
27 } else if (Array.isArray(spec.consumes)) {
28 [req.headers['Content-Type']] = spec.consumes;
29 } else if (operation.parameters && operation.parameters.filter(p => p.type === 'file').length) {
30 req.headers['Content-Type'] = 'multipart/form-data';
31 } else if (operation.parameters && operation.parameters.filter(p => p.in === 'formData').length) {
32 req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
33 }
34 } else if (requestContentType) {
35 const isBodyParamPresent = operation.parameters && operation.parameters.filter(p => p.in === 'body').length > 0;
36 const isFormDataParamPresent = operation.parameters && operation.parameters.filter(p => p.in === 'formData').length > 0;
37 if (isBodyParamPresent || isFormDataParamPresent) {
38 req.headers['Content-Type'] = requestContentType;
39 }
40 }
41 if (!responseContentType && Array.isArray(operation.produces) && operation.produces.length > 0) {
42 req.headers.accept = operation.produces.join(', ');
43 }
44 return req;
45}
46
47// Add security values, to operations - that declare their need on them
48export function applySecurities({
49 request,
50 securities = {},
51 operation = {},
52 spec
53}) {
54 const result = {
55 ...request
56 };
57 const {
58 authorized = {},
59 specSecurity = []
60 } = securities;
61 const security = operation.security || specSecurity;
62 const isAuthorized = authorized && !!Object.keys(authorized).length;
63 const securityDef = spec.securityDefinitions;
64 result.headers = result.headers || {};
65 result.query = result.query || {};
66 if (!Object.keys(securities).length || !isAuthorized || !security || Array.isArray(operation.security) && !operation.security.length) {
67 return request;
68 }
69 security.forEach(securityObj => {
70 Object.keys(securityObj).forEach(key => {
71 const auth = authorized[key];
72 if (!auth) {
73 return;
74 }
75 const {
76 token
77 } = auth;
78 const value = auth.value || auth;
79 const schema = securityDef[key];
80 const {
81 type
82 } = schema;
83 const tokenName = schema['x-tokenName'] || 'access_token';
84 const oauthToken = token && token[tokenName];
85 let tokenType = token && token.token_type;
86 if (auth) {
87 if (type === 'apiKey') {
88 const inType = schema.in === 'query' ? 'query' : 'headers';
89 result[inType] = result[inType] || {};
90 result[inType][schema.name] = value;
91 } else if (type === 'basic') {
92 if (value.header) {
93 result.headers.authorization = value.header;
94 } else {
95 const username = value.username || '';
96 const password = value.password || '';
97 value.base64 = btoa(`${username}:${password}`);
98 result.headers.authorization = `Basic ${value.base64}`;
99 }
100 } else if (type === 'oauth2' && oauthToken) {
101 tokenType = !tokenType || tokenType.toLowerCase() === 'bearer' ? 'Bearer' : tokenType;
102 result.headers.authorization = `${tokenType} ${oauthToken}`;
103 }
104 }
105 });
106 });
107 return result;
108}
Note: See TracBrowser for help on using the repository browser.