httpsnippet-client-api 5.0.0-beta.2 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +59 -52
- package/package.json +5 -5
- package/src/index.ts +50 -54
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { OASDocument } from 'oas/@types/rmoas.types';
|
|
2
1
|
import type { Client } from '@readme/httpsnippet/dist/targets/targets';
|
|
2
|
+
import type { OASDocument } from 'oas/dist/rmoas.types';
|
|
3
3
|
export interface APIOptions {
|
|
4
4
|
apiDefinition: OASDocument;
|
|
5
5
|
apiDefinitionUri: string;
|
package/dist/index.js
CHANGED
|
@@ -10,14 +10,25 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
+
var t = {};
|
|
15
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
+
t[p] = s[p];
|
|
17
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
+
t[p[i]] = s[p[i]];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
13
24
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
25
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
26
|
};
|
|
16
27
|
exports.__esModule = true;
|
|
17
|
-
var stringify_object_1 = __importDefault(require("stringify-object"));
|
|
18
28
|
var code_builder_1 = require("@readme/httpsnippet/dist/helpers/code-builder");
|
|
19
29
|
var content_type_1 = __importDefault(require("content-type"));
|
|
20
30
|
var oas_1 = __importDefault(require("oas"));
|
|
31
|
+
var stringify_object_1 = __importDefault(require("stringify-object"));
|
|
21
32
|
function stringify(obj, opts) {
|
|
22
33
|
if (opts === void 0) { opts = {}; }
|
|
23
34
|
return (0, stringify_object_1["default"])(obj, __assign({ indent: ' ' }, opts));
|
|
@@ -87,7 +98,8 @@ var client = {
|
|
|
87
98
|
link: 'https://npm.im/api',
|
|
88
99
|
description: 'Automatic SDK generation from an OpenAPI definition.'
|
|
89
100
|
},
|
|
90
|
-
convert: function (
|
|
101
|
+
convert: function (_a, options) {
|
|
102
|
+
var cookiesObj = _a.cookiesObj, headersObj = _a.headersObj, postData = _a.postData, queryObj = _a.queryObj, url = _a.url, source = __rest(_a, ["cookiesObj", "headersObj", "postData", "queryObj", "url"]);
|
|
91
103
|
var opts = __assign({}, options);
|
|
92
104
|
if (!('apiDefinitionUri' in opts)) {
|
|
93
105
|
throw new Error('This HTTP Snippet client must have an `apiDefinitionUri` option supplied to it.');
|
|
@@ -98,17 +110,16 @@ var client = {
|
|
|
98
110
|
var method = source.method.toLowerCase();
|
|
99
111
|
var oas = new oas_1["default"](opts.apiDefinition);
|
|
100
112
|
var apiDefinition = oas.getDefinition();
|
|
101
|
-
var foundOperation = oas.findOperation(
|
|
113
|
+
var foundOperation = oas.findOperation(url, method);
|
|
102
114
|
if (!foundOperation) {
|
|
103
|
-
throw new Error("Unable to locate a matching operation in the supplied `apiDefinition` for: ".concat(source.method, " ").concat(
|
|
115
|
+
throw new Error("Unable to locate a matching operation in the supplied `apiDefinition` for: ".concat(source.method, " ").concat(url));
|
|
104
116
|
}
|
|
105
117
|
var operationSlugs = foundOperation.url.slugs;
|
|
106
118
|
var operation = oas.operation(foundOperation.url.nonNormalizedPath, method);
|
|
107
119
|
var path = operation.path;
|
|
108
120
|
var authData = [];
|
|
109
121
|
var authSources = getAuthSources(operation);
|
|
110
|
-
var
|
|
111
|
-
// const code = new CodeBuilder(opts.indent);
|
|
122
|
+
var _b = new code_builder_1.CodeBuilder({ indent: opts.indent || ' ' }), blank = _b.blank, push = _b.push, join = _b.join;
|
|
112
123
|
push("const sdk = require('api')('".concat(opts.apiDefinitionUri, "');"));
|
|
113
124
|
blank();
|
|
114
125
|
// If we have multiple servers configured and our source URL differs from the stock URL that we
|
|
@@ -118,7 +129,7 @@ var client = {
|
|
|
118
129
|
var configData = [];
|
|
119
130
|
if ((apiDefinition.servers || []).length > 1) {
|
|
120
131
|
var stockUrl = oas.url();
|
|
121
|
-
var baseUrl =
|
|
132
|
+
var baseUrl = url.replace(path, '');
|
|
122
133
|
if (baseUrl !== stockUrl) {
|
|
123
134
|
var serverVars = oas.splitVariables(baseUrl);
|
|
124
135
|
var serverUrl = serverVars ? oas.url(serverVars.selected, serverVars.variables) : baseUrl;
|
|
@@ -126,52 +137,50 @@ var client = {
|
|
|
126
137
|
}
|
|
127
138
|
}
|
|
128
139
|
var metadata = {};
|
|
129
|
-
Object.keys(
|
|
140
|
+
Object.keys(queryObj).forEach(function (param) {
|
|
130
141
|
if (authSources.query.includes(param)) {
|
|
131
|
-
authData.push(buildAuthSnippet(
|
|
142
|
+
authData.push(buildAuthSnippet(queryObj[param]));
|
|
132
143
|
// If this query param is part of an auth source then we don't want it doubled up in the
|
|
133
144
|
// snippet.
|
|
134
145
|
return;
|
|
135
146
|
}
|
|
136
|
-
metadata[param] =
|
|
147
|
+
metadata[param] = queryObj[param];
|
|
137
148
|
});
|
|
138
|
-
Object.keys(
|
|
149
|
+
Object.keys(cookiesObj).forEach(function (cookie) {
|
|
139
150
|
if (authSources.cookie.includes(cookie)) {
|
|
140
|
-
authData.push(buildAuthSnippet(
|
|
151
|
+
authData.push(buildAuthSnippet(cookiesObj[cookie]));
|
|
141
152
|
// If this cookie is part of an auth source then we don't want it doubled up.
|
|
142
153
|
return;
|
|
143
154
|
}
|
|
144
155
|
// Note that we may have the potential to overlap any cookie that also shares the name as
|
|
145
156
|
// another metadata parameter. This problem is currently inherent to `api` and not this
|
|
146
157
|
// snippet generator.
|
|
147
|
-
metadata[cookie] =
|
|
158
|
+
metadata[cookie] = cookiesObj[cookie];
|
|
148
159
|
});
|
|
149
|
-
// If we have path parameters present
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (Object.keys(source.headersObj).length) {
|
|
160
|
-
var headers_1 = source.headersObj;
|
|
160
|
+
// If we have path parameters present we should add them into the metadata object.
|
|
161
|
+
Array.from(Object.entries(operationSlugs)).forEach(function (_a) {
|
|
162
|
+
var param = _a[0], value = _a[1];
|
|
163
|
+
// The keys in `operationSlugs` will always be prefixed with a `:` in the `oas` library so
|
|
164
|
+
// we can safely do this substring here without asserting this context.
|
|
165
|
+
metadata[param.substring(1)] = value;
|
|
166
|
+
});
|
|
167
|
+
if (Object.keys(headersObj).length) {
|
|
168
|
+
var headers_1 = headersObj;
|
|
169
|
+
var requestHeaders_1 = {};
|
|
161
170
|
Object.keys(headers_1).forEach(function (header) {
|
|
162
171
|
// Headers in HTTPSnippet are case-insensitive so we need to add in some special handling to
|
|
163
172
|
// make sure we're able to match them properly.
|
|
164
|
-
var
|
|
165
|
-
if (
|
|
173
|
+
var headerLower = header.toLowerCase();
|
|
174
|
+
if (headerLower in authSources.header) {
|
|
166
175
|
// If this header has been set up as an authentication header, let's remove it and add it
|
|
167
176
|
// into our auth data so we can build up an `.auth()` snippet for the SDK.
|
|
168
|
-
var authScheme = authSources.header[
|
|
177
|
+
var authScheme = authSources.header[headerLower];
|
|
169
178
|
if (authScheme === '*') {
|
|
170
179
|
authData.push(buildAuthSnippet(headers_1[header]));
|
|
171
180
|
}
|
|
172
181
|
else {
|
|
173
182
|
// @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
|
|
174
|
-
var authKey = headers_1[header].replace("".concat(authSources.header[
|
|
183
|
+
var authKey = headers_1[header].replace("".concat(authSources.header[headerLower], " "), '');
|
|
175
184
|
if (authScheme.toLowerCase() === 'basic') {
|
|
176
185
|
authKey = Buffer.from(authKey, 'base64').toString('ascii');
|
|
177
186
|
authKey = authKey.split(':');
|
|
@@ -179,41 +188,48 @@ var client = {
|
|
|
179
188
|
authData.push(buildAuthSnippet(authKey));
|
|
180
189
|
}
|
|
181
190
|
delete headers_1[header];
|
|
191
|
+
return;
|
|
182
192
|
}
|
|
183
|
-
else if (
|
|
193
|
+
else if (headerLower === 'content-type') {
|
|
184
194
|
// `Content-Type` headers are automatically added within the SDK so we can filter them out
|
|
185
195
|
// if they don't have parameters attached to them.
|
|
186
196
|
// @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
|
|
187
197
|
var parsedContentType = content_type_1["default"].parse(headers_1[header]);
|
|
188
198
|
if (!Object.keys(parsedContentType.parameters).length) {
|
|
189
199
|
delete headers_1[header];
|
|
200
|
+
return;
|
|
190
201
|
}
|
|
191
202
|
}
|
|
192
|
-
else if (
|
|
203
|
+
else if (headerLower === 'accept') {
|
|
193
204
|
// If the `Accept` header here is not the default or first `Accept` header for the
|
|
194
205
|
// operations' request body then we should add it otherwise we can let the SDK handle it
|
|
195
206
|
// itself.
|
|
196
207
|
if (headers_1[header] === operation.getContentType()) {
|
|
197
208
|
delete headers_1[header];
|
|
209
|
+
return;
|
|
198
210
|
}
|
|
199
211
|
}
|
|
212
|
+
// If we haven't used our header anywhere else, or we've deleted it from the payload
|
|
213
|
+
// because it'll be handled internally by `api` then we should add the lowercased version
|
|
214
|
+
// of our header into the generated code snippet.
|
|
215
|
+
requestHeaders_1[headerLower] = headers_1[header];
|
|
200
216
|
});
|
|
201
|
-
if (Object.keys(
|
|
202
|
-
metadata = Object.assign(metadata,
|
|
217
|
+
if (Object.keys(requestHeaders_1).length > 0) {
|
|
218
|
+
metadata = Object.assign(metadata, requestHeaders_1);
|
|
203
219
|
}
|
|
204
220
|
}
|
|
205
221
|
var body;
|
|
206
|
-
switch (
|
|
222
|
+
switch (postData.mimeType) {
|
|
207
223
|
case 'application/x-www-form-urlencoded':
|
|
208
|
-
body =
|
|
224
|
+
body = postData.paramsObj;
|
|
209
225
|
break;
|
|
210
226
|
case 'application/json':
|
|
211
|
-
if (
|
|
212
|
-
body =
|
|
227
|
+
if (postData.jsonObj) {
|
|
228
|
+
body = postData.jsonObj;
|
|
213
229
|
}
|
|
214
230
|
break;
|
|
215
231
|
case 'multipart/form-data':
|
|
216
|
-
if (
|
|
232
|
+
if (postData.params) {
|
|
217
233
|
body = {};
|
|
218
234
|
// If there's a `Content-Type` header present in the metadata, but it's for the
|
|
219
235
|
// `multipart/form-data` request then dump it off the snippet. We shouldn't offload that
|
|
@@ -222,7 +238,7 @@ var client = {
|
|
|
222
238
|
if ('content-type' in metadata && metadata['content-type'].indexOf('multipart/form-data') === 0) {
|
|
223
239
|
delete metadata['content-type'];
|
|
224
240
|
}
|
|
225
|
-
|
|
241
|
+
postData.params.forEach(function (param) {
|
|
226
242
|
if (param.fileName) {
|
|
227
243
|
body[param.name] = param.fileName;
|
|
228
244
|
}
|
|
@@ -233,21 +249,12 @@ var client = {
|
|
|
233
249
|
}
|
|
234
250
|
break;
|
|
235
251
|
default:
|
|
236
|
-
if (
|
|
237
|
-
body =
|
|
252
|
+
if (postData.text) {
|
|
253
|
+
body = postData.text;
|
|
238
254
|
}
|
|
239
255
|
}
|
|
240
256
|
var args = [];
|
|
241
|
-
var accessor =
|
|
242
|
-
if (operation.hasOperationId()) {
|
|
243
|
-
accessor = operation.getOperationId({ camelCase: true });
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
// Since we're not using an operationId as our primary accessor we need to take the current
|
|
247
|
-
// operation that we're working with and transpile back our path parameters on top of it.
|
|
248
|
-
var slugs = Object.fromEntries(Object.keys(operationSlugs).map(function (slug) { return [slug.replace(/:(.*)/, '$1'), operationSlugs[slug]]; }));
|
|
249
|
-
args.push("'".concat(decodeURIComponent(oas.replaceUrl(path, slugs)), "'"));
|
|
250
|
-
}
|
|
257
|
+
var accessor = operation.getOperationId({ camelCase: true });
|
|
251
258
|
// If we're going to be rendering out body params and metadata we should cut their character
|
|
252
259
|
// limit in half because we'll be rendering them in their own lines.
|
|
253
260
|
var inlineCharacterLimit = typeof body !== 'undefined' && Object.keys(metadata).length > 0 ? 40 : 80;
|
|
@@ -264,7 +271,7 @@ var client = {
|
|
|
264
271
|
push(configData.join('\n'));
|
|
265
272
|
}
|
|
266
273
|
push("sdk.".concat(accessor, "(").concat(args.join(', '), ")"));
|
|
267
|
-
push('.then(
|
|
274
|
+
push('.then(({ data }) => console.log(data))', 1);
|
|
268
275
|
push('.catch(err => console.error(err));', 1);
|
|
269
276
|
return join();
|
|
270
277
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "httpsnippet-client-api",
|
|
3
|
-
"version": "5.0.0
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "An HTTPSnippet client for generating snippets for the `api` module.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -29,14 +29,14 @@
|
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@readme/httpsnippet": "^4.0.3",
|
|
32
|
-
"oas": "^
|
|
32
|
+
"oas": "^20.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@readme/oas-examples": "^5.
|
|
35
|
+
"@readme/oas-examples": "^5.7.1",
|
|
36
36
|
"@readme/openapi-parser": "^2.2.0",
|
|
37
37
|
"@types/content-type": "^1.1.5",
|
|
38
38
|
"@types/stringify-object": "^4.0.1",
|
|
39
|
-
"api": "^5.0.0
|
|
39
|
+
"api": "^5.0.0",
|
|
40
40
|
"chai": "^4.3.6",
|
|
41
41
|
"fetch-mock": "^9.11.0",
|
|
42
42
|
"isomorphic-fetch": "^3.0.0",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"test/"
|
|
54
54
|
]
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "bb044ecdb3296c24838056764553fa7f18e6d228"
|
|
57
57
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { HttpMethods, OASDocument } from 'oas/@types/rmoas.types';
|
|
1
|
+
import type { ReducedHelperObject } from '@readme/httpsnippet/dist/helpers/reducer';
|
|
3
2
|
import type { Client } from '@readme/httpsnippet/dist/targets/targets';
|
|
4
|
-
import
|
|
3
|
+
import type { Operation } from 'oas';
|
|
4
|
+
import type { HttpMethods, OASDocument } from 'oas/dist/rmoas.types';
|
|
5
|
+
|
|
5
6
|
import { CodeBuilder } from '@readme/httpsnippet/dist/helpers/code-builder';
|
|
6
7
|
import contentType from 'content-type';
|
|
7
8
|
import Oas from 'oas';
|
|
9
|
+
import stringifyObject from 'stringify-object';
|
|
8
10
|
|
|
9
11
|
// This should really be an exported type in `oas`.
|
|
10
12
|
type SecurityType = 'Basic' | 'Bearer' | 'Query' | 'Header' | 'Cookie' | 'OAuth2' | 'http' | 'apiKey';
|
|
@@ -88,7 +90,7 @@ const client: Client<APIOptions> = {
|
|
|
88
90
|
link: 'https://npm.im/api',
|
|
89
91
|
description: 'Automatic SDK generation from an OpenAPI definition.',
|
|
90
92
|
},
|
|
91
|
-
convert: (source, options) => {
|
|
93
|
+
convert: ({ cookiesObj, headersObj, postData, queryObj, url, ...source }, options) => {
|
|
92
94
|
const opts = {
|
|
93
95
|
...options,
|
|
94
96
|
};
|
|
@@ -102,10 +104,10 @@ const client: Client<APIOptions> = {
|
|
|
102
104
|
const method = source.method.toLowerCase() as HttpMethods;
|
|
103
105
|
const oas = new Oas(opts.apiDefinition);
|
|
104
106
|
const apiDefinition = oas.getDefinition();
|
|
105
|
-
const foundOperation = oas.findOperation(
|
|
107
|
+
const foundOperation = oas.findOperation(url, method);
|
|
106
108
|
if (!foundOperation) {
|
|
107
109
|
throw new Error(
|
|
108
|
-
`Unable to locate a matching operation in the supplied \`apiDefinition\` for: ${source.method} ${
|
|
110
|
+
`Unable to locate a matching operation in the supplied \`apiDefinition\` for: ${source.method} ${url}`
|
|
109
111
|
);
|
|
110
112
|
}
|
|
111
113
|
|
|
@@ -116,7 +118,6 @@ const client: Client<APIOptions> = {
|
|
|
116
118
|
const authSources = getAuthSources(operation);
|
|
117
119
|
|
|
118
120
|
const { blank, push, join } = new CodeBuilder({ indent: opts.indent || ' ' });
|
|
119
|
-
// const code = new CodeBuilder(opts.indent);
|
|
120
121
|
|
|
121
122
|
push(`const sdk = require('api')('${opts.apiDefinitionUri}');`);
|
|
122
123
|
blank();
|
|
@@ -128,7 +129,7 @@ const client: Client<APIOptions> = {
|
|
|
128
129
|
const configData = [];
|
|
129
130
|
if ((apiDefinition.servers || []).length > 1) {
|
|
130
131
|
const stockUrl = oas.url();
|
|
131
|
-
const baseUrl =
|
|
132
|
+
const baseUrl = url.replace(path, '');
|
|
132
133
|
if (baseUrl !== stockUrl) {
|
|
133
134
|
const serverVars = oas.splitVariables(baseUrl);
|
|
134
135
|
const serverUrl = serverVars ? oas.url(serverVars.selected, serverVars.variables) : baseUrl;
|
|
@@ -138,21 +139,21 @@ const client: Client<APIOptions> = {
|
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
let metadata: Record<string, string | string[]> = {};
|
|
141
|
-
Object.keys(
|
|
142
|
+
Object.keys(queryObj).forEach(param => {
|
|
142
143
|
if (authSources.query.includes(param)) {
|
|
143
|
-
authData.push(buildAuthSnippet(
|
|
144
|
+
authData.push(buildAuthSnippet(queryObj[param]));
|
|
144
145
|
|
|
145
146
|
// If this query param is part of an auth source then we don't want it doubled up in the
|
|
146
147
|
// snippet.
|
|
147
148
|
return;
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
metadata[param] =
|
|
151
|
+
metadata[param] = queryObj[param];
|
|
151
152
|
});
|
|
152
153
|
|
|
153
|
-
Object.keys(
|
|
154
|
+
Object.keys(cookiesObj).forEach(cookie => {
|
|
154
155
|
if (authSources.cookie.includes(cookie)) {
|
|
155
|
-
authData.push(buildAuthSnippet(
|
|
156
|
+
authData.push(buildAuthSnippet(cookiesObj[cookie]));
|
|
156
157
|
|
|
157
158
|
// If this cookie is part of an auth source then we don't want it doubled up.
|
|
158
159
|
return;
|
|
@@ -161,36 +162,34 @@ const client: Client<APIOptions> = {
|
|
|
161
162
|
// Note that we may have the potential to overlap any cookie that also shares the name as
|
|
162
163
|
// another metadata parameter. This problem is currently inherent to `api` and not this
|
|
163
164
|
// snippet generator.
|
|
164
|
-
metadata[cookie] =
|
|
165
|
+
metadata[cookie] = cookiesObj[cookie];
|
|
165
166
|
});
|
|
166
167
|
|
|
167
|
-
// If we have path parameters present
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
metadata[param.substring(1)] = value;
|
|
174
|
-
});
|
|
175
|
-
}
|
|
168
|
+
// If we have path parameters present we should add them into the metadata object.
|
|
169
|
+
Array.from(Object.entries(operationSlugs)).forEach(([param, value]) => {
|
|
170
|
+
// The keys in `operationSlugs` will always be prefixed with a `:` in the `oas` library so
|
|
171
|
+
// we can safely do this substring here without asserting this context.
|
|
172
|
+
metadata[param.substring(1)] = value;
|
|
173
|
+
});
|
|
176
174
|
|
|
177
|
-
if (Object.keys(
|
|
178
|
-
const headers =
|
|
175
|
+
if (Object.keys(headersObj).length) {
|
|
176
|
+
const headers = headersObj;
|
|
177
|
+
const requestHeaders: ReducedHelperObject = {};
|
|
179
178
|
|
|
180
179
|
Object.keys(headers).forEach(header => {
|
|
181
180
|
// Headers in HTTPSnippet are case-insensitive so we need to add in some special handling to
|
|
182
181
|
// make sure we're able to match them properly.
|
|
183
|
-
const
|
|
182
|
+
const headerLower = header.toLowerCase();
|
|
184
183
|
|
|
185
|
-
if (
|
|
184
|
+
if (headerLower in authSources.header) {
|
|
186
185
|
// If this header has been set up as an authentication header, let's remove it and add it
|
|
187
186
|
// into our auth data so we can build up an `.auth()` snippet for the SDK.
|
|
188
|
-
const authScheme = authSources.header[
|
|
187
|
+
const authScheme = authSources.header[headerLower];
|
|
189
188
|
if (authScheme === '*') {
|
|
190
189
|
authData.push(buildAuthSnippet(headers[header]));
|
|
191
190
|
} else {
|
|
192
191
|
// @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
|
|
193
|
-
let authKey = headers[header].replace(`${authSources.header[
|
|
192
|
+
let authKey = headers[header].replace(`${authSources.header[headerLower]} `, '');
|
|
194
193
|
if (authScheme.toLowerCase() === 'basic') {
|
|
195
194
|
authKey = Buffer.from(authKey, 'base64').toString('ascii');
|
|
196
195
|
authKey = authKey.split(':');
|
|
@@ -200,43 +199,51 @@ const client: Client<APIOptions> = {
|
|
|
200
199
|
}
|
|
201
200
|
|
|
202
201
|
delete headers[header];
|
|
203
|
-
|
|
202
|
+
return;
|
|
203
|
+
} else if (headerLower === 'content-type') {
|
|
204
204
|
// `Content-Type` headers are automatically added within the SDK so we can filter them out
|
|
205
205
|
// if they don't have parameters attached to them.
|
|
206
206
|
// @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
|
|
207
207
|
const parsedContentType = contentType.parse(headers[header]);
|
|
208
208
|
if (!Object.keys(parsedContentType.parameters).length) {
|
|
209
209
|
delete headers[header];
|
|
210
|
+
return;
|
|
210
211
|
}
|
|
211
|
-
} else if (
|
|
212
|
+
} else if (headerLower === 'accept') {
|
|
212
213
|
// If the `Accept` header here is not the default or first `Accept` header for the
|
|
213
214
|
// operations' request body then we should add it otherwise we can let the SDK handle it
|
|
214
215
|
// itself.
|
|
215
216
|
if (headers[header] === operation.getContentType()) {
|
|
216
217
|
delete headers[header];
|
|
218
|
+
return;
|
|
217
219
|
}
|
|
218
220
|
}
|
|
221
|
+
|
|
222
|
+
// If we haven't used our header anywhere else, or we've deleted it from the payload
|
|
223
|
+
// because it'll be handled internally by `api` then we should add the lowercased version
|
|
224
|
+
// of our header into the generated code snippet.
|
|
225
|
+
requestHeaders[headerLower] = headers[header];
|
|
219
226
|
});
|
|
220
227
|
|
|
221
|
-
if (Object.keys(
|
|
222
|
-
metadata = Object.assign(metadata,
|
|
228
|
+
if (Object.keys(requestHeaders).length > 0) {
|
|
229
|
+
metadata = Object.assign(metadata, requestHeaders);
|
|
223
230
|
}
|
|
224
231
|
}
|
|
225
232
|
|
|
226
233
|
let body: any;
|
|
227
|
-
switch (
|
|
234
|
+
switch (postData.mimeType) {
|
|
228
235
|
case 'application/x-www-form-urlencoded':
|
|
229
|
-
body =
|
|
236
|
+
body = postData.paramsObj;
|
|
230
237
|
break;
|
|
231
238
|
|
|
232
239
|
case 'application/json':
|
|
233
|
-
if (
|
|
234
|
-
body =
|
|
240
|
+
if (postData.jsonObj) {
|
|
241
|
+
body = postData.jsonObj;
|
|
235
242
|
}
|
|
236
243
|
break;
|
|
237
244
|
|
|
238
245
|
case 'multipart/form-data':
|
|
239
|
-
if (
|
|
246
|
+
if (postData.params) {
|
|
240
247
|
body = {};
|
|
241
248
|
|
|
242
249
|
// If there's a `Content-Type` header present in the metadata, but it's for the
|
|
@@ -247,7 +254,7 @@ const client: Client<APIOptions> = {
|
|
|
247
254
|
delete metadata['content-type'];
|
|
248
255
|
}
|
|
249
256
|
|
|
250
|
-
|
|
257
|
+
postData.params.forEach(function (param) {
|
|
251
258
|
if (param.fileName) {
|
|
252
259
|
body[param.name] = param.fileName;
|
|
253
260
|
} else {
|
|
@@ -258,25 +265,14 @@ const client: Client<APIOptions> = {
|
|
|
258
265
|
break;
|
|
259
266
|
|
|
260
267
|
default:
|
|
261
|
-
if (
|
|
262
|
-
body =
|
|
268
|
+
if (postData.text) {
|
|
269
|
+
body = postData.text;
|
|
263
270
|
}
|
|
264
271
|
}
|
|
265
272
|
|
|
266
273
|
const args = [];
|
|
267
274
|
|
|
268
|
-
|
|
269
|
-
if (operation.hasOperationId()) {
|
|
270
|
-
accessor = operation.getOperationId({ camelCase: true });
|
|
271
|
-
} else {
|
|
272
|
-
// Since we're not using an operationId as our primary accessor we need to take the current
|
|
273
|
-
// operation that we're working with and transpile back our path parameters on top of it.
|
|
274
|
-
const slugs = Object.fromEntries(
|
|
275
|
-
Object.keys(operationSlugs).map(slug => [slug.replace(/:(.*)/, '$1'), operationSlugs[slug]])
|
|
276
|
-
);
|
|
277
|
-
|
|
278
|
-
args.push(`'${decodeURIComponent(oas.replaceUrl(path, slugs))}'`);
|
|
279
|
-
}
|
|
275
|
+
const accessor = operation.getOperationId({ camelCase: true });
|
|
280
276
|
|
|
281
277
|
// If we're going to be rendering out body params and metadata we should cut their character
|
|
282
278
|
// limit in half because we'll be rendering them in their own lines.
|
|
@@ -298,7 +294,7 @@ const client: Client<APIOptions> = {
|
|
|
298
294
|
}
|
|
299
295
|
|
|
300
296
|
push(`sdk.${accessor}(${args.join(', ')})`);
|
|
301
|
-
push('.then(
|
|
297
|
+
push('.then(({ data }) => console.log(data))', 1);
|
|
302
298
|
push('.catch(err => console.error(err));', 1);
|
|
303
299
|
|
|
304
300
|
return join();
|