httpsnippet-client-api 5.0.0-beta.2 → 5.0.0-beta.3

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { OASDocument } from 'oas/@types/rmoas.types';
1
+ import type { OASDocument } from 'oas/dist/rmoas.types';
2
2
  import type { Client } from '@readme/httpsnippet/dist/targets/targets';
3
3
  export interface APIOptions {
4
4
  apiDefinition: OASDocument;
package/dist/index.js CHANGED
@@ -10,6 +10,17 @@ 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
  };
@@ -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 (source, options) {
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(source.url, method);
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(source.url));
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 _a = new code_builder_1.CodeBuilder({ indent: opts.indent || ' ' }), blank = _a.blank, push = _a.push, join = _a.join;
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 = source.url.replace(path, '');
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,25 +137,25 @@ var client = {
126
137
  }
127
138
  }
128
139
  var metadata = {};
129
- Object.keys(source.queryObj).forEach(function (param) {
140
+ Object.keys(queryObj).forEach(function (param) {
130
141
  if (authSources.query.includes(param)) {
131
- authData.push(buildAuthSnippet(source.queryObj[param]));
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] = source.queryObj[param];
147
+ metadata[param] = queryObj[param];
137
148
  });
138
- Object.keys(source.cookiesObj).forEach(function (cookie) {
149
+ Object.keys(cookiesObj).forEach(function (cookie) {
139
150
  if (authSources.cookie.includes(cookie)) {
140
- authData.push(buildAuthSnippet(source.cookiesObj[cookie]));
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] = source.cookiesObj[cookie];
158
+ metadata[cookie] = cookiesObj[cookie];
148
159
  });
149
160
  // If we have path parameters present, we should only add them in if we have an `operationId` as
150
161
  // we don't want metadata to duplicate what we'll be setting the path in the snippet to.
@@ -156,22 +167,23 @@ var client = {
156
167
  metadata[param.substring(1)] = value;
157
168
  });
158
169
  }
159
- if (Object.keys(source.headersObj).length) {
160
- var headers_1 = source.headersObj;
170
+ if (Object.keys(headersObj).length) {
171
+ var headers_1 = headersObj;
172
+ var requestHeaders_1 = {};
161
173
  Object.keys(headers_1).forEach(function (header) {
162
174
  // Headers in HTTPSnippet are case-insensitive so we need to add in some special handling to
163
175
  // make sure we're able to match them properly.
164
- var headerLc = header.toLowerCase();
165
- if (headerLc in authSources.header) {
176
+ var headerLower = header.toLowerCase();
177
+ if (headerLower in authSources.header) {
166
178
  // If this header has been set up as an authentication header, let's remove it and add it
167
179
  // into our auth data so we can build up an `.auth()` snippet for the SDK.
168
- var authScheme = authSources.header[headerLc];
180
+ var authScheme = authSources.header[headerLower];
169
181
  if (authScheme === '*') {
170
182
  authData.push(buildAuthSnippet(headers_1[header]));
171
183
  }
172
184
  else {
173
185
  // @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
174
- var authKey = headers_1[header].replace("".concat(authSources.header[headerLc], " "), '');
186
+ var authKey = headers_1[header].replace("".concat(authSources.header[headerLower], " "), '');
175
187
  if (authScheme.toLowerCase() === 'basic') {
176
188
  authKey = Buffer.from(authKey, 'base64').toString('ascii');
177
189
  authKey = authKey.split(':');
@@ -179,41 +191,48 @@ var client = {
179
191
  authData.push(buildAuthSnippet(authKey));
180
192
  }
181
193
  delete headers_1[header];
194
+ return;
182
195
  }
183
- else if (headerLc === 'content-type') {
196
+ else if (headerLower === 'content-type') {
184
197
  // `Content-Type` headers are automatically added within the SDK so we can filter them out
185
198
  // if they don't have parameters attached to them.
186
199
  // @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
187
200
  var parsedContentType = content_type_1["default"].parse(headers_1[header]);
188
201
  if (!Object.keys(parsedContentType.parameters).length) {
189
202
  delete headers_1[header];
203
+ return;
190
204
  }
191
205
  }
192
- else if (headerLc === 'accept') {
206
+ else if (headerLower === 'accept') {
193
207
  // If the `Accept` header here is not the default or first `Accept` header for the
194
208
  // operations' request body then we should add it otherwise we can let the SDK handle it
195
209
  // itself.
196
210
  if (headers_1[header] === operation.getContentType()) {
197
211
  delete headers_1[header];
212
+ return;
198
213
  }
199
214
  }
215
+ // If we haven't used our header anywhere else, or we've deleted it from the payload
216
+ // because it'll be handled internally by `api` then we should add the lowercased version
217
+ // of our header into the generated code snippet.
218
+ requestHeaders_1[headerLower] = headers_1[header];
200
219
  });
201
- if (Object.keys(headers_1).length > 0) {
202
- metadata = Object.assign(metadata, headers_1);
220
+ if (Object.keys(requestHeaders_1).length > 0) {
221
+ metadata = Object.assign(metadata, requestHeaders_1);
203
222
  }
204
223
  }
205
224
  var body;
206
- switch (source.postData.mimeType) {
225
+ switch (postData.mimeType) {
207
226
  case 'application/x-www-form-urlencoded':
208
- body = source.postData.paramsObj;
227
+ body = postData.paramsObj;
209
228
  break;
210
229
  case 'application/json':
211
- if (source.postData.jsonObj) {
212
- body = source.postData.jsonObj;
230
+ if (postData.jsonObj) {
231
+ body = postData.jsonObj;
213
232
  }
214
233
  break;
215
234
  case 'multipart/form-data':
216
- if (source.postData.params) {
235
+ if (postData.params) {
217
236
  body = {};
218
237
  // If there's a `Content-Type` header present in the metadata, but it's for the
219
238
  // `multipart/form-data` request then dump it off the snippet. We shouldn't offload that
@@ -222,7 +241,7 @@ var client = {
222
241
  if ('content-type' in metadata && metadata['content-type'].indexOf('multipart/form-data') === 0) {
223
242
  delete metadata['content-type'];
224
243
  }
225
- source.postData.params.forEach(function (param) {
244
+ postData.params.forEach(function (param) {
226
245
  if (param.fileName) {
227
246
  body[param.name] = param.fileName;
228
247
  }
@@ -233,8 +252,8 @@ var client = {
233
252
  }
234
253
  break;
235
254
  default:
236
- if (source.postData.text) {
237
- body = source.postData.text;
255
+ if (postData.text) {
256
+ body = postData.text;
238
257
  }
239
258
  }
240
259
  var args = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "httpsnippet-client-api",
3
- "version": "5.0.0-beta.2",
3
+ "version": "5.0.0-beta.3",
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": "^18.0.1"
32
+ "oas": "^18.3.4"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@readme/oas-examples": "^5.4.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-beta.2",
39
+ "api": "^5.0.0-beta.3",
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": "aa738b1bf46b447afed38507d3fc49acbbad6309"
56
+ "gitHead": "24d5b83545735176786d212a69121a029cf6dea1"
57
57
  }
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { Operation } from 'oas';
2
- import type { HttpMethods, OASDocument } from 'oas/@types/rmoas.types';
2
+ import type { HttpMethods, OASDocument } from 'oas/dist/rmoas.types';
3
3
  import type { Client } from '@readme/httpsnippet/dist/targets/targets';
4
+ import type { ReducedHelperObject } from '@readme/httpsnippet/dist/helpers/reducer';
5
+
4
6
  import stringifyObject from 'stringify-object';
5
7
  import { CodeBuilder } from '@readme/httpsnippet/dist/helpers/code-builder';
6
8
  import contentType from 'content-type';
@@ -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(source.url, method);
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} ${source.url}`
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 = source.url.replace(path, '');
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(source.queryObj).forEach(param => {
142
+ Object.keys(queryObj).forEach(param => {
142
143
  if (authSources.query.includes(param)) {
143
- authData.push(buildAuthSnippet(source.queryObj[param]));
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] = source.queryObj[param];
151
+ metadata[param] = queryObj[param];
151
152
  });
152
153
 
153
- Object.keys(source.cookiesObj).forEach(cookie => {
154
+ Object.keys(cookiesObj).forEach(cookie => {
154
155
  if (authSources.cookie.includes(cookie)) {
155
- authData.push(buildAuthSnippet(source.cookiesObj[cookie]));
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,7 +162,7 @@ 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] = source.cookiesObj[cookie];
165
+ metadata[cookie] = cookiesObj[cookie];
165
166
  });
166
167
 
167
168
  // If we have path parameters present, we should only add them in if we have an `operationId` as
@@ -174,23 +175,24 @@ const client: Client<APIOptions> = {
174
175
  });
175
176
  }
176
177
 
177
- if (Object.keys(source.headersObj).length) {
178
- const headers = source.headersObj;
178
+ if (Object.keys(headersObj).length) {
179
+ const headers = headersObj;
180
+ const requestHeaders: ReducedHelperObject = {};
179
181
 
180
182
  Object.keys(headers).forEach(header => {
181
183
  // Headers in HTTPSnippet are case-insensitive so we need to add in some special handling to
182
184
  // make sure we're able to match them properly.
183
- const headerLc = header.toLowerCase();
185
+ const headerLower = header.toLowerCase();
184
186
 
185
- if (headerLc in authSources.header) {
187
+ if (headerLower in authSources.header) {
186
188
  // If this header has been set up as an authentication header, let's remove it and add it
187
189
  // into our auth data so we can build up an `.auth()` snippet for the SDK.
188
- const authScheme = authSources.header[headerLc];
190
+ const authScheme = authSources.header[headerLower];
189
191
  if (authScheme === '*') {
190
192
  authData.push(buildAuthSnippet(headers[header]));
191
193
  } else {
192
194
  // @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
193
- let authKey = headers[header].replace(`${authSources.header[headerLc]} `, '');
195
+ let authKey = headers[header].replace(`${authSources.header[headerLower]} `, '');
194
196
  if (authScheme.toLowerCase() === 'basic') {
195
197
  authKey = Buffer.from(authKey, 'base64').toString('ascii');
196
198
  authKey = authKey.split(':');
@@ -200,43 +202,51 @@ const client: Client<APIOptions> = {
200
202
  }
201
203
 
202
204
  delete headers[header];
203
- } else if (headerLc === 'content-type') {
205
+ return;
206
+ } else if (headerLower === 'content-type') {
204
207
  // `Content-Type` headers are automatically added within the SDK so we can filter them out
205
208
  // if they don't have parameters attached to them.
206
209
  // @ts-expect-error `headers[header]` is typed improperly in HTTPSnippet.
207
210
  const parsedContentType = contentType.parse(headers[header]);
208
211
  if (!Object.keys(parsedContentType.parameters).length) {
209
212
  delete headers[header];
213
+ return;
210
214
  }
211
- } else if (headerLc === 'accept') {
215
+ } else if (headerLower === 'accept') {
212
216
  // If the `Accept` header here is not the default or first `Accept` header for the
213
217
  // operations' request body then we should add it otherwise we can let the SDK handle it
214
218
  // itself.
215
219
  if (headers[header] === operation.getContentType()) {
216
220
  delete headers[header];
221
+ return;
217
222
  }
218
223
  }
224
+
225
+ // If we haven't used our header anywhere else, or we've deleted it from the payload
226
+ // because it'll be handled internally by `api` then we should add the lowercased version
227
+ // of our header into the generated code snippet.
228
+ requestHeaders[headerLower] = headers[header];
219
229
  });
220
230
 
221
- if (Object.keys(headers).length > 0) {
222
- metadata = Object.assign(metadata, headers);
231
+ if (Object.keys(requestHeaders).length > 0) {
232
+ metadata = Object.assign(metadata, requestHeaders);
223
233
  }
224
234
  }
225
235
 
226
236
  let body: any;
227
- switch (source.postData.mimeType) {
237
+ switch (postData.mimeType) {
228
238
  case 'application/x-www-form-urlencoded':
229
- body = source.postData.paramsObj;
239
+ body = postData.paramsObj;
230
240
  break;
231
241
 
232
242
  case 'application/json':
233
- if (source.postData.jsonObj) {
234
- body = source.postData.jsonObj;
243
+ if (postData.jsonObj) {
244
+ body = postData.jsonObj;
235
245
  }
236
246
  break;
237
247
 
238
248
  case 'multipart/form-data':
239
- if (source.postData.params) {
249
+ if (postData.params) {
240
250
  body = {};
241
251
 
242
252
  // If there's a `Content-Type` header present in the metadata, but it's for the
@@ -247,7 +257,7 @@ const client: Client<APIOptions> = {
247
257
  delete metadata['content-type'];
248
258
  }
249
259
 
250
- source.postData.params.forEach(function (param) {
260
+ postData.params.forEach(function (param) {
251
261
  if (param.fileName) {
252
262
  body[param.name] = param.fileName;
253
263
  } else {
@@ -258,8 +268,8 @@ const client: Client<APIOptions> = {
258
268
  break;
259
269
 
260
270
  default:
261
- if (source.postData.text) {
262
- body = source.postData.text;
271
+ if (postData.text) {
272
+ body = postData.text;
263
273
  }
264
274
  }
265
275