oas 18.0.3 → 18.0.6

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.
@@ -79,7 +79,8 @@ export default class Operation {
79
79
  prepareSecurity(): Record<SecurityType, RMOAS.KeyedSecuritySchemeObject[]>;
80
80
  getHeaders(): Operation['headers'];
81
81
  /**
82
- * Determine if the operation has an operation present in its schema.
82
+ * Determine if the operation has an operation present in its schema. Note that if one is present
83
+ * in the schema but is an empty string then this will return false.
83
84
  *
84
85
  */
85
86
  hasOperationId(): boolean;
package/CHANGELOG.md CHANGED
@@ -1,3 +1,29 @@
1
+ ## <small>18.0.6 (2022-03-21)</small>
2
+
3
+ * feat: `camelCase` opt on `getOperationId()` should clean IDs if present (#625) ([6ab85df](https://github.com/readmeio/oas/commit/6ab85df)), closes [#625](https://github.com/readmeio/oas/issues/625)
4
+
5
+
6
+
7
+ ## <small>18.0.5 (2022-03-21)</small>
8
+
9
+ * fix: `operation.hasOperationId` should return false for an empty string (#624) ([bc23a43](https://github.com/readmeio/oas/commit/bc23a43)), closes [#624](https://github.com/readmeio/oas/issues/624)
10
+
11
+
12
+
13
+ ## <small>18.0.4 (2022-03-21)</small>
14
+
15
+ * docs: updating the changelog for 17.8.2 ([18c853c](https://github.com/readmeio/oas/commit/18c853c))
16
+ * chore: removing some docs from this repo as they're now in our .github/ repo ([776dbe5](https://github.com/readmeio/oas/commit/776dbe5))
17
+ * fix: issue where hostname server variables wouldn't match subdomains or ports (#623) ([9e84ded](https://github.com/readmeio/oas/commit/9e84ded)), closes [#623](https://github.com/readmeio/oas/issues/623)
18
+
19
+
20
+
21
+ ## <small>17.8.2 (2022-03-21)</small>
22
+
23
+ * fix: issue where hostname server variables wouldn't match subdomains or ports (#623) ([0630600](https://github.com/readmeio/oas/commit/0630600)), closes [#623](https://github.com/readmeio/oas/issues/623)
24
+
25
+
26
+
1
27
  ## <small>18.0.3 (2022-03-18)</small>
2
28
 
3
29
  * fix: bumping oas-normalize to fix a nested babel dep issue ([3f45c98](https://github.com/readmeio/oas/commit/3f45c98))
package/dist/index.js CHANGED
@@ -84,6 +84,7 @@ exports.Callback = operation_1.Callback;
84
84
  exports.Webhook = operation_1.Webhook;
85
85
  var utils_1 = __importStar(require("./utils"));
86
86
  exports.utils = utils_1["default"];
87
+ var SERVER_VARIABLE_REGEX = /{([-_a-zA-Z0-9:.[\]]+)}/g;
87
88
  function ensureProtocol(url) {
88
89
  // Add protocol to urls starting with // e.g. //example.com
89
90
  // This is because httpsnippet throws a HARError when it doesnt have a protocol
@@ -137,12 +138,12 @@ function normalizedUrl(api, selected) {
137
138
  *
138
139
  * For example, when given `https://{region}.node.example.com/v14` this will return back:
139
140
  *
140
- * https://([-_a-zA-Z0-9[\\]]+).node.example.com/v14
141
+ * https://([-_a-zA-Z0-9:.[\\]]+).node.example.com/v14
141
142
  *
142
143
  * @param url URL to transform
143
144
  */
144
145
  function transformUrlIntoRegex(url) {
145
- return stripTrailingSlash(url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, '([-_a-zA-Z0-9[\\]]+)'));
146
+ return stripTrailingSlash(url.replace(SERVER_VARIABLE_REGEX, '([-_a-zA-Z0-9:.[\\]]+)'));
146
147
  }
147
148
  /**
148
149
  * Normalize a path so that we can use it with `path-to-regexp` to do operation lookups.
@@ -383,7 +384,7 @@ var Oas = /** @class */ (function () {
383
384
  // way we'll be able to extract the parameter names and match them up with the matched server that we obtained
384
385
  // above.
385
386
  var variables = {};
386
- Array.from(server.url.matchAll(/{([-_a-zA-Z0-9[\]]+)}/g)).forEach(function (variable, y) {
387
+ Array.from(server.url.matchAll(SERVER_VARIABLE_REGEX)).forEach(function (variable, y) {
387
388
  variables[variable[1]] = found[y + 1];
388
389
  });
389
390
  return {
@@ -417,7 +418,7 @@ var Oas = /** @class */ (function () {
417
418
  if (variables === void 0) { variables = {}; }
418
419
  // When we're constructing URLs, server URLs with trailing slashes cause problems with doing lookups, so if we have
419
420
  // one here on, slice it off.
420
- return stripTrailingSlash(url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, function (original, key) {
421
+ return stripTrailingSlash(url.replace(SERVER_VARIABLE_REGEX, function (original, key) {
421
422
  var userVariable = (0, get_user_variable_1["default"])(_this.user, key);
422
423
  if (userVariable) {
423
424
  return userVariable;
package/dist/operation.js CHANGED
@@ -279,11 +279,12 @@ var Operation = /** @class */ (function () {
279
279
  return this.headers;
280
280
  };
281
281
  /**
282
- * Determine if the operation has an operation present in its schema.
282
+ * Determine if the operation has an operation present in its schema. Note that if one is present
283
+ * in the schema but is an empty string then this will return false.
283
284
  *
284
285
  */
285
286
  Operation.prototype.hasOperationId = function () {
286
- return 'operationId' in this.schema;
287
+ return Boolean('operationId' in this.schema && this.schema.operationId.length);
287
288
  };
288
289
  /**
289
290
  * Get an `operationId` for this operation. If one is not present (it's not required by the spec!)
@@ -293,25 +294,36 @@ var Operation = /** @class */ (function () {
293
294
  * @param opts.camelCase Generate a JS method-friendly operation ID when one isn't present.
294
295
  */
295
296
  Operation.prototype.getOperationId = function (opts) {
296
- if ('operationId' in this.schema) {
297
- return this.schema.operationId;
297
+ var operationId;
298
+ if (this.hasOperationId()) {
299
+ operationId = this.schema.operationId;
300
+ }
301
+ else {
302
+ operationId = this.path
303
+ .replace(/[^a-zA-Z0-9]/g, '-') // Remove weird characters
304
+ .replace(/^-|-$/g, '') // Don't start or end with -
305
+ .replace(/--+/g, '-') // Remove double --'s
306
+ .toLowerCase();
298
307
  }
299
308
  var method = this.method.toLowerCase();
300
- var operationId = this.path
301
- .replace(/[^a-zA-Z0-9]/g, '-') // Remove weird characters
302
- .replace(/^-|-$/g, '') // Don't start or end with -
303
- .replace(/--+/g, '-') // Remove double --'s
304
- .toLowerCase();
305
309
  if (opts === null || opts === void 0 ? void 0 : opts.camelCase) {
306
310
  operationId = operationId.replace(/[^a-zA-Z0-9]+(.)/g, function (_, chr) { return chr.toUpperCase(); });
307
- // If the generated operationId already starts with the method (eg. `getPets`) we don't want
311
+ // If the generated `operationId` already starts with the method (eg. `getPets`) we don't want
308
312
  // to double it up into `getGetPets`.
309
313
  if (operationId.startsWith(method)) {
310
314
  return operationId;
311
315
  }
316
+ // If this operation already has an operationId and we just cleaned it up then we shouldn't
317
+ // prefix it with an HTTP method.
318
+ if (this.hasOperationId()) {
319
+ return operationId;
320
+ }
312
321
  operationId = operationId.charAt(0).toUpperCase() + operationId.slice(1);
313
322
  return "".concat(method).concat(operationId);
314
323
  }
324
+ else if (this.hasOperationId()) {
325
+ return operationId;
326
+ }
315
327
  return "".concat(method, "_").concat(operationId);
316
328
  };
317
329
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oas",
3
- "version": "18.0.3",
3
+ "version": "18.0.6",
4
4
  "description": "Working with OpenAPI definitions is hard. This makes it easier.",
5
5
  "license": "MIT",
6
6
  "author": "ReadMe <support@readme.io> (https://readme.com)",
package/src/index.ts CHANGED
@@ -24,6 +24,8 @@ type PathMatches = PathMatch[];
24
24
 
25
25
  type Variables = Record<string, string | number | { default?: string | number }[] | { default?: string | number }>;
26
26
 
27
+ const SERVER_VARIABLE_REGEX = /{([-_a-zA-Z0-9:.[\]]+)}/g;
28
+
27
29
  function ensureProtocol(url: string) {
28
30
  // Add protocol to urls starting with // e.g. //example.com
29
31
  // This is because httpsnippet throws a HARError when it doesnt have a protocol
@@ -84,12 +86,12 @@ function normalizedUrl(api: RMOAS.OASDocument, selected: number) {
84
86
  *
85
87
  * For example, when given `https://{region}.node.example.com/v14` this will return back:
86
88
  *
87
- * https://([-_a-zA-Z0-9[\\]]+).node.example.com/v14
89
+ * https://([-_a-zA-Z0-9:.[\\]]+).node.example.com/v14
88
90
  *
89
91
  * @param url URL to transform
90
92
  */
91
93
  function transformUrlIntoRegex(url: string) {
92
- return stripTrailingSlash(url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, '([-_a-zA-Z0-9[\\]]+)'));
94
+ return stripTrailingSlash(url.replace(SERVER_VARIABLE_REGEX, '([-_a-zA-Z0-9:.[\\]]+)'));
93
95
  }
94
96
 
95
97
  /**
@@ -384,7 +386,7 @@ export default class Oas {
384
386
  // way we'll be able to extract the parameter names and match them up with the matched server that we obtained
385
387
  // above.
386
388
  const variables: Record<string, string | number> = {};
387
- Array.from(server.url.matchAll(/{([-_a-zA-Z0-9[\]]+)}/g)).forEach((variable, y) => {
389
+ Array.from(server.url.matchAll(SERVER_VARIABLE_REGEX)).forEach((variable, y) => {
388
390
  variables[variable[1]] = found[y + 1];
389
391
  });
390
392
 
@@ -420,7 +422,7 @@ export default class Oas {
420
422
  // When we're constructing URLs, server URLs with trailing slashes cause problems with doing lookups, so if we have
421
423
  // one here on, slice it off.
422
424
  return stripTrailingSlash(
423
- url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, (original: string, key: string) => {
425
+ url.replace(SERVER_VARIABLE_REGEX, (original: string, key: string) => {
424
426
  const userVariable = getUserVariable(this.user, key);
425
427
  if (userVariable) {
426
428
  return userVariable as string;
package/src/operation.ts CHANGED
@@ -315,11 +315,12 @@ export default class Operation {
315
315
  }
316
316
 
317
317
  /**
318
- * Determine if the operation has an operation present in its schema.
318
+ * Determine if the operation has an operation present in its schema. Note that if one is present
319
+ * in the schema but is an empty string then this will return false.
319
320
  *
320
321
  */
321
322
  hasOperationId(): boolean {
322
- return 'operationId' in this.schema;
323
+ return Boolean('operationId' in this.schema && this.schema.operationId.length);
323
324
  }
324
325
 
325
326
  /**
@@ -330,28 +331,37 @@ export default class Operation {
330
331
  * @param opts.camelCase Generate a JS method-friendly operation ID when one isn't present.
331
332
  */
332
333
  getOperationId(opts?: { camelCase: boolean }): string {
333
- if ('operationId' in this.schema) {
334
- return this.schema.operationId;
334
+ let operationId;
335
+ if (this.hasOperationId()) {
336
+ operationId = this.schema.operationId;
337
+ } else {
338
+ operationId = this.path
339
+ .replace(/[^a-zA-Z0-9]/g, '-') // Remove weird characters
340
+ .replace(/^-|-$/g, '') // Don't start or end with -
341
+ .replace(/--+/g, '-') // Remove double --'s
342
+ .toLowerCase();
335
343
  }
336
344
 
337
345
  const method = this.method.toLowerCase();
338
- let operationId = this.path
339
- .replace(/[^a-zA-Z0-9]/g, '-') // Remove weird characters
340
- .replace(/^-|-$/g, '') // Don't start or end with -
341
- .replace(/--+/g, '-') // Remove double --'s
342
- .toLowerCase();
343
-
344
346
  if (opts?.camelCase) {
345
347
  operationId = operationId.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase());
346
348
 
347
- // If the generated operationId already starts with the method (eg. `getPets`) we don't want
349
+ // If the generated `operationId` already starts with the method (eg. `getPets`) we don't want
348
350
  // to double it up into `getGetPets`.
349
351
  if (operationId.startsWith(method)) {
350
352
  return operationId;
351
353
  }
352
354
 
355
+ // If this operation already has an operationId and we just cleaned it up then we shouldn't
356
+ // prefix it with an HTTP method.
357
+ if (this.hasOperationId()) {
358
+ return operationId;
359
+ }
360
+
353
361
  operationId = operationId.charAt(0).toUpperCase() + operationId.slice(1);
354
362
  return `${method}${operationId}`;
363
+ } else if (this.hasOperationId()) {
364
+ return operationId;
355
365
  }
356
366
 
357
367
  return `${method}_${operationId}`;