api 6.1.1 → 7.0.0-alpha.1
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/LICENSE +1 -1
- package/README.md +0 -12
- package/dist/bin.js +3 -3
- package/dist/{cli/codegen → codegen}/index.js +4 -4
- package/dist/{cli/codegen → codegen}/language.js +4 -3
- package/dist/codegen/languages/typescript/util.d.ts +10 -0
- package/dist/{cli/codegen → codegen}/languages/typescript/util.js +5 -6
- package/dist/{cli/codegen → codegen}/languages/typescript.d.ts +12 -12
- package/dist/{cli/codegen → codegen}/languages/typescript.js +75 -75
- package/dist/{cli/commands → commands}/index.js +3 -3
- package/dist/{cli/commands → commands}/install.js +37 -34
- package/dist/fetcher.d.ts +8 -9
- package/dist/fetcher.js +15 -15
- package/dist/{cli/lib → lib}/prompt.js +3 -3
- package/dist/{cli/logger.js → logger.js} +3 -3
- package/dist/packageInfo.d.ts +1 -1
- package/dist/packageInfo.js +2 -2
- package/dist/{cli/storage.d.ts → storage.d.ts} +5 -4
- package/dist/{cli/storage.js → storage.js} +39 -36
- package/package.json +14 -32
- package/src/bin.ts +1 -1
- package/src/{cli/codegen → codegen}/language.ts +3 -2
- package/src/{cli/codegen → codegen}/languages/typescript/util.ts +1 -1
- package/src/{cli/codegen → codegen}/languages/typescript.ts +31 -32
- package/src/{cli/commands → commands}/install.ts +1 -1
- package/src/fetcher.ts +4 -5
- package/src/packageInfo.ts +1 -1
- package/src/{cli/storage.ts → storage.ts} +13 -9
- package/tsconfig.json +3 -13
- package/dist/cache.d.ts +0 -68
- package/dist/cache.js +0 -198
- package/dist/cli/codegen/languages/typescript/util.d.ts +0 -20
- package/dist/core/errors/fetchError.d.ts +0 -12
- package/dist/core/errors/fetchError.js +0 -36
- package/dist/core/getJSONSchemaDefaults.d.ts +0 -14
- package/dist/core/getJSONSchemaDefaults.js +0 -61
- package/dist/core/index.d.ts +0 -40
- package/dist/core/index.js +0 -168
- package/dist/core/parseResponse.d.ts +0 -6
- package/dist/core/parseResponse.js +0 -71
- package/dist/core/prepareAuth.d.ts +0 -5
- package/dist/core/prepareAuth.js +0 -84
- package/dist/core/prepareParams.d.ts +0 -21
- package/dist/core/prepareParams.js +0 -425
- package/dist/core/prepareServer.d.ts +0 -10
- package/dist/core/prepareServer.js +0 -47
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -259
- package/src/.sink.d.ts +0 -1
- package/src/cache.ts +0 -193
- package/src/core/errors/fetchError.ts +0 -31
- package/src/core/getJSONSchemaDefaults.ts +0 -74
- package/src/core/index.ts +0 -148
- package/src/core/parseResponse.ts +0 -26
- package/src/core/prepareAuth.ts +0 -109
- package/src/core/prepareParams.ts +0 -415
- package/src/core/prepareServer.ts +0 -48
- package/src/index.ts +0 -203
- package/src/typings.d.ts +0 -2
- /package/dist/{cli/codegen → codegen}/index.d.ts +0 -0
- /package/dist/{cli/codegen → codegen}/language.d.ts +0 -0
- /package/dist/{cli/commands → commands}/index.d.ts +0 -0
- /package/dist/{cli/commands → commands}/install.d.ts +0 -0
- /package/dist/{cli/lib → lib}/prompt.d.ts +0 -0
- /package/dist/{cli/logger.d.ts → logger.d.ts} +0 -0
- /package/src/{cli/codegen → codegen}/index.ts +0 -0
- /package/src/{cli/commands → commands}/index.ts +0 -0
- /package/src/{cli/lib → lib}/prompt.ts +0 -0
- /package/src/{cli/logger.ts → logger.ts} +0 -0
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type Oas from 'oas';
|
|
2
|
-
|
|
3
|
-
function stripTrailingSlash(url: string) {
|
|
4
|
-
if (url[url.length - 1] === '/') {
|
|
5
|
-
return url.slice(0, -1);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return url;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* With an SDK server config and an instance of OAS we should extract and prepare the server and
|
|
13
|
-
* any server variables to be supplied to `@readme/oas-to-har`.
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export default function prepareServer(spec: Oas, url: string, variables: Record<string, string | number> = {}) {
|
|
17
|
-
let serverIdx;
|
|
18
|
-
const sanitizedUrl = stripTrailingSlash(url);
|
|
19
|
-
(spec.api.servers || []).forEach((server, i) => {
|
|
20
|
-
if (server.url === sanitizedUrl) {
|
|
21
|
-
serverIdx = i;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// If we were able to find the passed in server in the OAS servers, we should use that! If we
|
|
26
|
-
// couldn't and server variables were passed in we should try our best to handle that, otherwise
|
|
27
|
-
// we should ignore the passed in server and use whever the default from the OAS is.
|
|
28
|
-
if (serverIdx) {
|
|
29
|
-
return {
|
|
30
|
-
selected: serverIdx,
|
|
31
|
-
variables,
|
|
32
|
-
};
|
|
33
|
-
} else if (Object.keys(variables).length) {
|
|
34
|
-
// @todo we should run `oas.replaceUrl(url)` and pass that unto `@readme/oas-to-har`
|
|
35
|
-
} else {
|
|
36
|
-
const server = spec.splitVariables(url);
|
|
37
|
-
if (server) {
|
|
38
|
-
return {
|
|
39
|
-
selected: server.selected,
|
|
40
|
-
variables: server.variables,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// @todo we should pass `url` directly into `@readme/oas-to-har` as the base URL
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return false;
|
|
48
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import type { ConfigOptions } from './core';
|
|
2
|
-
import type { Operation } from 'oas';
|
|
3
|
-
import type { OASDocument } from 'oas/dist/rmoas.types';
|
|
4
|
-
|
|
5
|
-
import Oas from 'oas';
|
|
6
|
-
|
|
7
|
-
import Cache from './cache';
|
|
8
|
-
import APICore from './core';
|
|
9
|
-
import { PACKAGE_NAME, PACKAGE_VERSION } from './packageInfo';
|
|
10
|
-
|
|
11
|
-
interface SDKOptions {
|
|
12
|
-
cacheDir?: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class Sdk {
|
|
16
|
-
uri: string | OASDocument;
|
|
17
|
-
|
|
18
|
-
userAgent: string;
|
|
19
|
-
|
|
20
|
-
cacheDir: string | false;
|
|
21
|
-
|
|
22
|
-
constructor(uri: string | OASDocument, opts: SDKOptions = {}) {
|
|
23
|
-
this.uri = uri;
|
|
24
|
-
this.userAgent = `${PACKAGE_NAME} (node)/${PACKAGE_VERSION}`;
|
|
25
|
-
|
|
26
|
-
this.cacheDir = opts.cacheDir ? opts.cacheDir : false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
load() {
|
|
30
|
-
const cache = new Cache(this.uri, this.cacheDir);
|
|
31
|
-
const userAgent = this.userAgent;
|
|
32
|
-
|
|
33
|
-
const core = new APICore();
|
|
34
|
-
core.setUserAgent(userAgent);
|
|
35
|
-
|
|
36
|
-
let isLoaded = false;
|
|
37
|
-
let isCached = cache.isCached();
|
|
38
|
-
let sdk = {};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Create dynamic accessors for every operation with a defined operation ID. If an operation
|
|
42
|
-
* does not have an operation ID it can be accessed by its `.method('/path')` accessor instead.
|
|
43
|
-
*
|
|
44
|
-
*/
|
|
45
|
-
function loadOperations(spec: Oas) {
|
|
46
|
-
return Object.entries(spec.getPaths())
|
|
47
|
-
.map(([, operations]) => Object.values(operations))
|
|
48
|
-
.reduce((prev, next) => prev.concat(next), [])
|
|
49
|
-
.reduce((prev, next) => {
|
|
50
|
-
// `getOperationId()` creates dynamic operation IDs when one isn't available but we need
|
|
51
|
-
// to know here if we actually have one present or not. The `camelCase` option here also
|
|
52
|
-
// cleans up any `operationId` that we might have into something that can be used as a
|
|
53
|
-
// valid JS method.
|
|
54
|
-
const originalOperationId = next.getOperationId();
|
|
55
|
-
const operationId = next.getOperationId({ camelCase: true });
|
|
56
|
-
|
|
57
|
-
const op = {
|
|
58
|
-
[operationId]: ((operation: Operation, ...args: unknown[]) => {
|
|
59
|
-
return core.fetchOperation(operation, ...args);
|
|
60
|
-
}).bind(null, next),
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
if (operationId !== originalOperationId) {
|
|
64
|
-
// If we cleaned up their operation ID into a friendly method accessor (`findPetById`
|
|
65
|
-
// versus `find pet by id`) we should still let them use the non-friendly version if
|
|
66
|
-
// they want.
|
|
67
|
-
//
|
|
68
|
-
// This work is to maintain backwards compatibility with `api@4` and does not exist
|
|
69
|
-
// within our code generated SDKs -- those only allow the cleaner camelCase
|
|
70
|
-
// `operationId` to be used.
|
|
71
|
-
op[originalOperationId] = ((operation: Operation, ...args: unknown[]) => {
|
|
72
|
-
return core.fetchOperation(operation, ...args);
|
|
73
|
-
}).bind(null, next);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return Object.assign(prev, op);
|
|
77
|
-
}, {});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async function loadFromCache() {
|
|
81
|
-
let cachedSpec;
|
|
82
|
-
if (isCached) {
|
|
83
|
-
cachedSpec = await cache.get();
|
|
84
|
-
} else {
|
|
85
|
-
cachedSpec = await cache.load();
|
|
86
|
-
isCached = true;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const spec = new Oas(cachedSpec);
|
|
90
|
-
|
|
91
|
-
core.setSpec(spec);
|
|
92
|
-
|
|
93
|
-
sdk = Object.assign(sdk, loadOperations(spec));
|
|
94
|
-
isLoaded = true;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const sdkProxy = {
|
|
98
|
-
// @give this a better type than any
|
|
99
|
-
get(target: any, method: string) {
|
|
100
|
-
// Since auth returns a self-proxy, we **do not** want it to fall through into the async
|
|
101
|
-
// function below as when that'll happen, instead of returning a self-proxy, it'll end up
|
|
102
|
-
// returning a Promise. When that happens, chaining `sdk.auth().operationId()` will fail.
|
|
103
|
-
if (['auth', 'config'].includes(method)) {
|
|
104
|
-
// @todo split this up so we have better types for `auth` and `config`
|
|
105
|
-
return function authAndConfigHandler(...args: any) {
|
|
106
|
-
return target[method].apply(this, args);
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return async function accessorHandler(...args: unknown[]) {
|
|
111
|
-
if (!(method in target)) {
|
|
112
|
-
// If this method doesn't exist on the proxy, have we loaded the SDK? If we have, then
|
|
113
|
-
// this method isn't valid.
|
|
114
|
-
if (isLoaded) {
|
|
115
|
-
throw new Error(`Sorry, \`${method}\` does not appear to be a valid operation on this API.`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
await loadFromCache();
|
|
119
|
-
|
|
120
|
-
// If after loading the SDK and this method still doesn't exist, then it's not real!
|
|
121
|
-
if (!(method in sdk)) {
|
|
122
|
-
throw new Error(`Sorry, \`${method}\` does not appear to be a valid operation on this API.`);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// @todo give sdk a better type
|
|
126
|
-
return (sdk as any)[method].apply(this, args);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return target[method].apply(this, args);
|
|
130
|
-
};
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
sdk = {
|
|
135
|
-
/**
|
|
136
|
-
* If the API you're using requires authentication you can supply the required credentials
|
|
137
|
-
* through this method and the library will magically determine how they should be used
|
|
138
|
-
* within your API request.
|
|
139
|
-
*
|
|
140
|
-
* With the exception of OpenID and MutualTLS, it supports all forms of authentication
|
|
141
|
-
* supported by the OpenAPI specification.
|
|
142
|
-
*
|
|
143
|
-
* @example <caption>HTTP Basic auth</caption>
|
|
144
|
-
* sdk.auth('username', 'password');
|
|
145
|
-
*
|
|
146
|
-
* @example <caption>Bearer tokens (HTTP or OAuth 2)</caption>
|
|
147
|
-
* sdk.auth('myBearerToken');
|
|
148
|
-
*
|
|
149
|
-
* @example <caption>API Keys</caption>
|
|
150
|
-
* sdk.auth('myApiKey');
|
|
151
|
-
*
|
|
152
|
-
* @see {@link https://spec.openapis.org/oas/v3.0.3#fixed-fields-22}
|
|
153
|
-
* @see {@link https://spec.openapis.org/oas/v3.1.0#fixed-fields-22}
|
|
154
|
-
* @param values Your auth credentials for the API. Can specify up to two strings or numbers.
|
|
155
|
-
*/
|
|
156
|
-
auth: (...values: string[] | number[]) => {
|
|
157
|
-
core.setAuth(...values);
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Optionally configure various options that the SDK allows.
|
|
162
|
-
*
|
|
163
|
-
* @param config Object of supported SDK options and toggles.
|
|
164
|
-
* @param config.timeout Override the default `fetch` request timeout of 30 seconds (30000ms).
|
|
165
|
-
*/
|
|
166
|
-
config: (config: ConfigOptions) => {
|
|
167
|
-
core.setConfig(config);
|
|
168
|
-
},
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* If the API you're using offers alternate server URLs, and server variables, you can tell
|
|
172
|
-
* the SDK which one to use with this method. To use it you can supply either one of the
|
|
173
|
-
* server URLs that are contained within the OpenAPI definition (along with any server
|
|
174
|
-
* variables), or you can pass it a fully qualified URL to use (that may or may not exist
|
|
175
|
-
* within the OpenAPI definition).
|
|
176
|
-
*
|
|
177
|
-
* @example <caption>Server URL with server variables</caption>
|
|
178
|
-
* sdk.server('https://{region}.api.example.com/{basePath}', {
|
|
179
|
-
* name: 'eu',
|
|
180
|
-
* basePath: 'v14',
|
|
181
|
-
* });
|
|
182
|
-
*
|
|
183
|
-
* @example <caption>Fully qualified server URL</caption>
|
|
184
|
-
* sdk.server('https://eu.api.example.com/v14');
|
|
185
|
-
*
|
|
186
|
-
* @param url Server URL
|
|
187
|
-
* @param variables An object of variables to replace into the server URL.
|
|
188
|
-
*/
|
|
189
|
-
server: (url: string, variables = {}) => {
|
|
190
|
-
core.setServer(url, variables);
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
return new Proxy(sdk, sdkProxy);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Why `export` vs `export default`? If we leave this as `export` then TS will transpile it into
|
|
199
|
-
// a `module.exports` export so that when folks load this they don't need to load it as
|
|
200
|
-
// `require('api').default`.
|
|
201
|
-
export = (uri: string | OASDocument, opts: SDKOptions = {}) => {
|
|
202
|
-
return new Sdk(uri, opts).load();
|
|
203
|
-
};
|
package/src/typings.d.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|