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.
Files changed (50) hide show
  1. package/README.md +7 -8
  2. package/dist/bin.js +1 -1
  3. package/dist/cache.d.ts +38 -3
  4. package/dist/cache.js +7 -26
  5. package/dist/cli/codegen/index.d.ts +1 -1
  6. package/dist/cli/codegen/language.d.ts +1 -1
  7. package/dist/cli/codegen/language.js +13 -0
  8. package/dist/cli/codegen/languages/typescript/util.d.ts +21 -0
  9. package/dist/cli/codegen/languages/typescript/util.js +185 -0
  10. package/dist/cli/codegen/languages/typescript.d.ts +36 -41
  11. package/dist/cli/codegen/languages/typescript.js +394 -414
  12. package/dist/cli/commands/install.js +6 -6
  13. package/dist/cli/storage.d.ts +1 -1
  14. package/dist/cli/storage.js +2 -2
  15. package/dist/core/errors/fetchError.d.ts +12 -0
  16. package/dist/core/errors/fetchError.js +36 -0
  17. package/dist/core/getJSONSchemaDefaults.d.ts +1 -1
  18. package/dist/core/index.d.ts +12 -4
  19. package/dist/core/index.js +36 -11
  20. package/dist/core/parseResponse.d.ts +6 -1
  21. package/dist/core/parseResponse.js +9 -3
  22. package/dist/core/prepareAuth.js +47 -18
  23. package/dist/core/prepareParams.d.ts +0 -3
  24. package/dist/core/prepareParams.js +102 -41
  25. package/dist/fetcher.d.ts +1 -1
  26. package/dist/fetcher.js +3 -3
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.js +24 -40
  29. package/dist/packageInfo.d.ts +1 -1
  30. package/dist/packageInfo.js +1 -1
  31. package/package.json +31 -17
  32. package/src/bin.ts +2 -1
  33. package/src/cache.ts +9 -31
  34. package/src/cli/codegen/index.ts +1 -1
  35. package/src/cli/codegen/language.ts +18 -1
  36. package/src/cli/codegen/languages/typescript/util.ts +183 -0
  37. package/src/cli/codegen/languages/typescript.ts +348 -340
  38. package/src/cli/commands/install.ts +6 -8
  39. package/src/cli/storage.ts +4 -4
  40. package/src/core/errors/fetchError.ts +31 -0
  41. package/src/core/getJSONSchemaDefaults.ts +3 -2
  42. package/src/core/index.ts +53 -18
  43. package/src/core/parseResponse.ts +8 -2
  44. package/src/core/prepareAuth.ts +55 -31
  45. package/src/core/prepareParams.ts +112 -41
  46. package/src/fetcher.ts +5 -4
  47. package/src/index.ts +24 -32
  48. package/src/packageInfo.ts +1 -1
  49. package/src/typings.d.ts +0 -1
  50. package/tsconfig.json +1 -1
package/src/index.ts CHANGED
@@ -1,12 +1,11 @@
1
- import type { Operation } from 'oas';
2
- import type { HttpMethods, OASDocument } from 'oas/@types/rmoas.types';
3
1
  import type { ConfigOptions } from './core';
2
+ import type { Operation } from 'oas';
3
+ import type { OASDocument } from 'oas/dist/rmoas.types';
4
4
 
5
5
  import Oas from 'oas';
6
- import APICore from './core';
7
6
 
8
7
  import Cache from './cache';
9
-
8
+ import APICore from './core';
10
9
  import { PACKAGE_NAME, PACKAGE_VERSION } from './packageInfo';
11
10
 
12
11
  interface SDKOptions {
@@ -38,24 +37,6 @@ class Sdk {
38
37
  let isCached = cache.isCached();
39
38
  let sdk = {};
40
39
 
41
- /**
42
- * Create dynamic accessors for every HTTP method that the OpenAPI specification supports.
43
- *
44
- * @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#fixed-fields-7}
45
- * @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields-7}
46
- */
47
- function loadMethods() {
48
- return ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace']
49
- .map(httpVerb => {
50
- return {
51
- [httpVerb]: ((method: string, path: string, ...args: unknown[]) => {
52
- return core.fetch(path, method as HttpMethods, ...args);
53
- }).bind(null, httpVerb),
54
- };
55
- })
56
- .reduce((prev, next) => Object.assign(prev, next));
57
- }
58
-
59
40
  /**
60
41
  * Create dynamic accessors for every operation with a defined operation ID. If an operation
61
42
  * does not have an operation ID it can be accessed by its `.method('/path')` accessor instead.
@@ -66,19 +47,34 @@ class Sdk {
66
47
  return Object.entries(spec.getPaths())
67
48
  .map(([, operations]) => Object.values(operations))
68
49
  .reduce((prev, next) => prev.concat(next), [])
69
- .filter(operation => operation.hasOperationId())
70
50
  .reduce((prev, next) => {
71
51
  // `getOperationId()` creates dynamic operation IDs when one isn't available but we need
72
52
  // to know here if we actually have one present or not. The `camelCase` option here also
73
53
  // cleans up any `operationId` that we might have into something that can be used as a
74
54
  // valid JS method.
55
+ const originalOperationId = next.getOperationId();
75
56
  const operationId = next.getOperationId({ camelCase: true });
76
57
 
77
- return Object.assign(prev, {
58
+ const op = {
78
59
  [operationId]: ((operation: Operation, ...args: unknown[]) => {
79
60
  return core.fetchOperation(operation, ...args);
80
61
  }).bind(null, next),
81
- });
62
+ };
63
+
64
+ if (operationId !== originalOperationId) {
65
+ // If we cleaned up their operation ID into a friendly method accessor (`findPetById`
66
+ // versus `find pet by id`) we should still let them use the non-friendly version if
67
+ // they want.
68
+ //
69
+ // This work is to maintain backwards compatibility with `api@4` and does not exist
70
+ // within our code generated SDKs -- those only allow the cleaner camelCase
71
+ // `operationId` to be used.
72
+ op[originalOperationId] = ((operation: Operation, ...args: unknown[]) => {
73
+ return core.fetchOperation(operation, ...args);
74
+ }).bind(null, next);
75
+ }
76
+
77
+ return Object.assign(prev, op);
82
78
  }, {});
83
79
  }
84
80
 
@@ -95,11 +91,7 @@ class Sdk {
95
91
 
96
92
  core.setSpec(spec);
97
93
 
98
- sdk = Object.assign(sdk, {
99
- ...loadMethods(),
100
- ...loadOperations(spec),
101
- });
102
-
94
+ sdk = Object.assign(sdk, loadOperations(spec));
103
95
  isLoaded = true;
104
96
  }
105
97
 
@@ -167,10 +159,10 @@ class Sdk {
167
159
  },
168
160
 
169
161
  /**
170
- * Optionally configure various options, such as response parsing, that the SDK allows.
162
+ * Optionally configure various options that the SDK allows.
171
163
  *
172
164
  * @param config Object of supported SDK options and toggles.
173
- * @param config.parseResponse If responses are parsed according to its `Content-Type` header.
165
+ * @param config.timeout Override the default `fetch` request timeout of 30 seconds (30000ms).
174
166
  */
175
167
  config: (config: ConfigOptions) => {
176
168
  core.setConfig(config);
@@ -1,3 +1,3 @@
1
1
  // This file is automatically updated by the build script.
2
2
  export const PACKAGE_NAME = 'api';
3
- export const PACKAGE_VERSION = '5.0.0-beta.2';
3
+ export const PACKAGE_VERSION = '5.0.0';
package/src/typings.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  // These libraries don't have any types so we need to let TS know so we can use them.
2
2
  declare module 'fetch-har';
3
- declare module '@readme/oas-to-har';
package/tsconfig.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "baseUrl": "./src",
5
5
  "declaration": true,
6
6
  "esModuleInterop": true,
7
- "lib": ["dom", "es2020"],
7
+ "lib": ["dom", "dom.iterable", "es2020"],
8
8
  "noImplicitAny": true,
9
9
  "outDir": "dist/",
10
10
  "paths": {