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 +7 -8
- package/dist/bin.js +1 -1
- package/dist/cache.d.ts +38 -3
- package/dist/cache.js +7 -26
- package/dist/cli/codegen/index.d.ts +1 -1
- package/dist/cli/codegen/language.d.ts +1 -1
- package/dist/cli/codegen/language.js +13 -0
- package/dist/cli/codegen/languages/typescript/util.d.ts +21 -0
- package/dist/cli/codegen/languages/typescript/util.js +185 -0
- package/dist/cli/codegen/languages/typescript.d.ts +36 -41
- package/dist/cli/codegen/languages/typescript.js +394 -414
- package/dist/cli/commands/install.js +6 -6
- package/dist/cli/storage.d.ts +1 -1
- package/dist/cli/storage.js +2 -2
- package/dist/core/errors/fetchError.d.ts +12 -0
- package/dist/core/errors/fetchError.js +36 -0
- package/dist/core/getJSONSchemaDefaults.d.ts +1 -1
- package/dist/core/index.d.ts +12 -4
- package/dist/core/index.js +36 -11
- package/dist/core/parseResponse.d.ts +6 -1
- package/dist/core/parseResponse.js +9 -3
- package/dist/core/prepareAuth.js +47 -18
- package/dist/core/prepareParams.d.ts +0 -3
- package/dist/core/prepareParams.js +102 -41
- package/dist/fetcher.d.ts +1 -1
- package/dist/fetcher.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +24 -40
- package/dist/packageInfo.d.ts +1 -1
- package/dist/packageInfo.js +1 -1
- package/package.json +31 -17
- package/src/bin.ts +2 -1
- package/src/cache.ts +9 -31
- package/src/cli/codegen/index.ts +1 -1
- package/src/cli/codegen/language.ts +18 -1
- package/src/cli/codegen/languages/typescript/util.ts +183 -0
- package/src/cli/codegen/languages/typescript.ts +348 -340
- package/src/cli/commands/install.ts +6 -8
- package/src/cli/storage.ts +4 -4
- package/src/core/errors/fetchError.ts +31 -0
- package/src/core/getJSONSchemaDefaults.ts +3 -2
- package/src/core/index.ts +53 -18
- package/src/core/parseResponse.ts +8 -2
- package/src/core/prepareAuth.ts +55 -31
- package/src/core/prepareParams.ts +112 -41
- package/src/fetcher.ts +5 -4
- package/src/index.ts +24 -32
- package/src/packageInfo.ts +1 -1
- package/src/typings.d.ts +0 -1
- 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
|
-
|
|
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
|
|
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.
|
|
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);
|
package/src/packageInfo.ts
CHANGED
package/src/typings.d.ts
CHANGED