api 7.0.0-beta.0 → 7.0.0-beta.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/dist/codegen/codegenerator.d.ts +9 -11
- package/dist/codegen/codegenerator.d.ts.map +1 -1
- package/dist/codegen/codegenerator.js +11 -0
- package/dist/codegen/codegenerator.js.map +1 -1
- package/dist/codegen/factory.d.ts +19 -3
- package/dist/codegen/factory.d.ts.map +1 -1
- package/dist/codegen/factory.js +12 -5
- package/dist/codegen/factory.js.map +1 -1
- package/dist/codegen/languages/typescript/index.d.ts +8 -2
- package/dist/codegen/languages/typescript/index.d.ts.map +1 -1
- package/dist/codegen/languages/typescript/index.js +61 -15
- package/dist/codegen/languages/typescript/index.js.map +1 -1
- package/dist/commands/index.d.ts +2 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +4 -0
- package/dist/commands/index.js.map +1 -1
- package/dist/commands/install.d.ts.map +1 -1
- package/dist/commands/install.js +5 -5
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/list.d.ts +4 -0
- package/dist/commands/list.d.ts.map +1 -0
- package/dist/commands/list.js +37 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/uninstall.d.ts +4 -0
- package/dist/commands/uninstall.d.ts.map +1 -0
- package/dist/commands/uninstall.js +72 -0
- package/dist/commands/uninstall.js.map +1 -0
- package/dist/lockfileSchema.d.ts +125 -0
- package/dist/lockfileSchema.d.ts.map +1 -0
- package/dist/lockfileSchema.js +78 -0
- package/dist/lockfileSchema.js.map +1 -0
- package/dist/packageInfo.d.ts +1 -1
- package/dist/packageInfo.js +1 -1
- package/dist/storage.d.ts +75 -60
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +85 -25
- package/dist/storage.js.map +1 -1
- package/package.json +14 -7
- package/schema.json +69 -0
- package/src/bin.ts +0 -21
- package/src/codegen/codegenerator.ts +0 -75
- package/src/codegen/factory.ts +0 -23
- package/src/codegen/languages/typescript/index.ts +0 -984
- package/src/codegen/languages/typescript/util.ts +0 -174
- package/src/commands/index.ts +0 -5
- package/src/commands/install.ts +0 -196
- package/src/fetcher.ts +0 -145
- package/src/lib/prompt.ts +0 -29
- package/src/logger.ts +0 -10
- package/src/packageInfo.ts +0 -3
- package/src/storage.ts +0 -333
- package/tsconfig.json +0 -10
package/dist/storage.js
CHANGED
|
@@ -1,26 +1,42 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import semver from 'semver';
|
|
3
4
|
import ssri from 'ssri';
|
|
4
5
|
import validateNPMPackageName from 'validate-npm-package-name';
|
|
6
|
+
import { SupportedLanguages } from './codegen/factory.js';
|
|
5
7
|
import Fetcher from './fetcher.js';
|
|
6
8
|
import { PACKAGE_VERSION } from './packageInfo.js';
|
|
7
9
|
export default class Storage {
|
|
8
10
|
static dir;
|
|
9
11
|
static lockfile;
|
|
12
|
+
fetcher;
|
|
10
13
|
/**
|
|
11
14
|
* This is the original source that the file came from (relative/absolute file path, URL, ReadMe
|
|
12
15
|
* registry UUID, etc.).
|
|
16
|
+
*
|
|
17
|
+
* @example @developers/v2.0#nysezql0wwo236
|
|
18
|
+
* @example https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore-simple.json
|
|
19
|
+
* @example ./petstore.json
|
|
13
20
|
*/
|
|
14
21
|
source;
|
|
22
|
+
/**
|
|
23
|
+
* The language that this SDK was generated for.
|
|
24
|
+
*/
|
|
25
|
+
language;
|
|
26
|
+
/**
|
|
27
|
+
* The identifier that this was installed as.
|
|
28
|
+
*
|
|
29
|
+
* @example petstore
|
|
30
|
+
*/
|
|
15
31
|
identifier;
|
|
16
|
-
|
|
17
|
-
constructor(source, identifier) {
|
|
32
|
+
constructor(source, language, identifier) {
|
|
18
33
|
Storage.setStorageDir();
|
|
19
34
|
this.fetcher = new Fetcher(source);
|
|
20
35
|
this.source = source;
|
|
21
|
-
if (
|
|
36
|
+
if (language)
|
|
37
|
+
this.language = language;
|
|
38
|
+
if (identifier)
|
|
22
39
|
this.identifier = identifier;
|
|
23
|
-
}
|
|
24
40
|
// This should default to false so we have awareness if we've looked at the lockfile yet.
|
|
25
41
|
Storage.lockfile = false;
|
|
26
42
|
}
|
|
@@ -59,8 +75,9 @@ export default class Storage {
|
|
|
59
75
|
}
|
|
60
76
|
}
|
|
61
77
|
static getDefaultLockfile() {
|
|
78
|
+
const majorVersion = semver.parse(PACKAGE_VERSION)?.major || 'latest';
|
|
62
79
|
return {
|
|
63
|
-
|
|
80
|
+
$schema: `https://unpkg.com/api@${majorVersion}/schema.json`,
|
|
64
81
|
apis: [],
|
|
65
82
|
};
|
|
66
83
|
}
|
|
@@ -116,6 +133,15 @@ export default class Storage {
|
|
|
116
133
|
});
|
|
117
134
|
return res === undefined ? false : res;
|
|
118
135
|
}
|
|
136
|
+
setLanguage(language) {
|
|
137
|
+
// `language` wasn't always present in the lockfile so if we don't have one we should default
|
|
138
|
+
// to JS.
|
|
139
|
+
if (!language) {
|
|
140
|
+
this.language = SupportedLanguages.JS;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this.language = language;
|
|
144
|
+
}
|
|
119
145
|
setIdentifier(identifier) {
|
|
120
146
|
this.identifier = identifier;
|
|
121
147
|
}
|
|
@@ -127,19 +153,44 @@ export default class Storage {
|
|
|
127
153
|
}
|
|
128
154
|
/**
|
|
129
155
|
* Retrieve the lockfile record for the current spec + identifier if it exists in the lockfile.
|
|
156
|
+
*
|
|
130
157
|
*/
|
|
131
158
|
getFromLockfile() {
|
|
132
159
|
const lockfile = Storage.getLockfile();
|
|
133
160
|
return lockfile.apis.find(a => a.identifier === this.identifier);
|
|
134
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Retrieve the lockfile record, if it exists, for a given identifier.
|
|
164
|
+
*
|
|
165
|
+
*/
|
|
166
|
+
static getFromLockfile(identifier) {
|
|
167
|
+
const lockfile = Storage.getLockfile();
|
|
168
|
+
return lockfile.apis.find(a => a.identifier === identifier);
|
|
169
|
+
}
|
|
170
|
+
getSDKLanguage() {
|
|
171
|
+
const entry = this.getFromLockfile();
|
|
172
|
+
// We may not have `language` in the lockfile for old users but we default to JS so we can
|
|
173
|
+
// safely return that if this isn't present.
|
|
174
|
+
return entry?.language || SupportedLanguages.JS;
|
|
175
|
+
}
|
|
176
|
+
getPackageName() {
|
|
177
|
+
const entry = this.getFromLockfile();
|
|
178
|
+
if (entry?.private) {
|
|
179
|
+
return `@api/${entry.identifier}`;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
135
183
|
getIdentifierStorageDir() {
|
|
136
184
|
if (!this.isInLockfile()) {
|
|
137
185
|
throw new Error(`${this.source} has not been saved to storage yet and must do so before being retrieved.`);
|
|
138
186
|
}
|
|
139
187
|
return path.join(Storage.getAPIsDir(), this.identifier);
|
|
140
188
|
}
|
|
189
|
+
getAPIDefinitionPath() {
|
|
190
|
+
return path.join(this.getIdentifierStorageDir(), 'openapi.json');
|
|
191
|
+
}
|
|
141
192
|
getAPIDefinition() {
|
|
142
|
-
const file = fs.readFileSync(
|
|
193
|
+
const file = fs.readFileSync(this.getAPIDefinitionPath(), 'utf8');
|
|
143
194
|
return JSON.parse(file);
|
|
144
195
|
}
|
|
145
196
|
saveSourceFiles(files) {
|
|
@@ -173,31 +224,19 @@ export default class Storage {
|
|
|
173
224
|
});
|
|
174
225
|
}
|
|
175
226
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* | // installed, when it was installed, what the original source was,
|
|
180
|
-
* | // and what version of `api` was used.
|
|
181
|
-
* └── apis/
|
|
182
|
-
* ├── readme/
|
|
183
|
-
* | ├── node_modules/
|
|
184
|
-
* │ ├── index.js // We may offer the option to export a raw TS file for folks who want
|
|
185
|
-
* | | // that, but for now it'll be a compiled JS file.
|
|
186
|
-
* │ ├── index.d.ts // All types for their SDK, ready to use in an IDE.
|
|
187
|
-
* │ |── openapi.json
|
|
188
|
-
* │ └── package.json
|
|
189
|
-
* └── petstore/
|
|
190
|
-
* ├── node_modules/
|
|
191
|
-
* ├── index.js
|
|
192
|
-
* ├── index.d.ts
|
|
193
|
-
* ├── openapi.json
|
|
194
|
-
* └── package.json
|
|
227
|
+
* Initialize a directory in the storage system for this identifier and add it into the lockfile.
|
|
228
|
+
* This does not create or save the source code for this SDK, that work happens within the
|
|
229
|
+
* code generation system.
|
|
195
230
|
*
|
|
231
|
+
* @see {@link https://api.readme.dev/docs/how-it-works#api-directory}
|
|
196
232
|
*/
|
|
197
233
|
save(spec) {
|
|
198
234
|
if (!this.identifier) {
|
|
199
235
|
throw new TypeError('An identifier must be set before saving the API definition into storage.');
|
|
200
236
|
}
|
|
237
|
+
else if (!this.language) {
|
|
238
|
+
throw new TypeError('A language must be set before saving the API definition into storage.');
|
|
239
|
+
}
|
|
201
240
|
// Create our main `.api/` directory.
|
|
202
241
|
if (!fs.existsSync(Storage.dir)) {
|
|
203
242
|
fs.mkdirSync(Storage.dir, { recursive: true });
|
|
@@ -217,10 +256,13 @@ export default class Storage {
|
|
|
217
256
|
fs.mkdirSync(identifierStorageDir, { recursive: true });
|
|
218
257
|
}
|
|
219
258
|
Storage.lockfile.apis.push({
|
|
259
|
+
private: true,
|
|
220
260
|
identifier: this.identifier,
|
|
221
261
|
source: this.source,
|
|
222
262
|
integrity: Storage.generateIntegrityHash(spec),
|
|
223
263
|
installerVersion: PACKAGE_VERSION,
|
|
264
|
+
language: this.language,
|
|
265
|
+
createdAt: new Date().toISOString(),
|
|
224
266
|
});
|
|
225
267
|
fs.writeFileSync(path.join(identifierStorageDir, 'openapi.json'), saved);
|
|
226
268
|
fs.writeFileSync(Storage.getLockfilePath(), JSON.stringify(Storage.lockfile, null, 2));
|
|
@@ -230,5 +272,23 @@ export default class Storage {
|
|
|
230
272
|
}
|
|
231
273
|
return spec;
|
|
232
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Delete the stored source code for the given identifier and purge it from the lockfile.
|
|
277
|
+
*
|
|
278
|
+
*/
|
|
279
|
+
async remove() {
|
|
280
|
+
// Delete the codegen'd SDK source code.
|
|
281
|
+
const identifierDir = this.getIdentifierStorageDir();
|
|
282
|
+
await fs.promises.rm(identifierDir, { recursive: true }).catch(() => {
|
|
283
|
+
// If the identifier directory doesn't exist for some reason we can continue on and remove it
|
|
284
|
+
// from the lockfile because some sort of corruption happened.
|
|
285
|
+
});
|
|
286
|
+
// Remove the SDK from the lockfile.
|
|
287
|
+
const lockfile = Storage.lockfile;
|
|
288
|
+
const idx = lockfile.apis.findIndex(api => api.identifier === this.identifier);
|
|
289
|
+
lockfile.apis.splice(idx, 1);
|
|
290
|
+
Storage.lockfile = lockfile;
|
|
291
|
+
fs.writeFileSync(Storage.getLockfilePath(), JSON.stringify(Storage.lockfile, null, 2));
|
|
292
|
+
}
|
|
233
293
|
}
|
|
234
294
|
//# sourceMappingURL=storage.js.map
|
package/dist/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,sBAAsB,MAAM,2BAA2B,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,MAAM,CAAC,GAAG,CAAS;IAEnB,MAAM,CAAC,QAAQ,CAAmB;IAElC,OAAO,CAAU;IAEjB;;;;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EAAE,KAAK,CAAC,CAAC;YACzE,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;SACxF;aAAM;YACL,4EAA4E;SAC7E;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM;QACV,wCAAwC;QACxC,MAAM,aAAa,GAAG,IAAI,CAAC,uBAAuB,EAAE,CAAC;QACrD,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;YAClE,6FAA6F;YAC7F,8DAA8D;QAChE,CAAC,CAAC,CAAC;QAEH,oCAAoC;QACpC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAoB,CAAC;QAC9C,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,UAAU,KAAK,IAAI,CAAC,UAAU,CAAC,CAAC;QAE/E,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QAE7B,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAE5B,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IACzF,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.1",
|
|
4
4
|
"description": "Magical SDK generation from an OpenAPI definition 🪄",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"attw": "attw --pack --format table-flipped",
|
|
11
11
|
"build": "tsc",
|
|
12
|
+
"build:versionedfiles": "NODE_OPTIONS=--no-warnings node --loader ts-node/esm bin/buildVersionedFiles.ts",
|
|
12
13
|
"debug:bin": "NODE_OPTIONS=--no-warnings node --loader ts-node/esm src/bin.ts",
|
|
13
14
|
"lint:types": "tsc --noEmit",
|
|
14
|
-
"prebuild": "rm -rf dist
|
|
15
|
+
"prebuild": "rm -rf dist/ && npm run build:versionedfiles",
|
|
15
16
|
"prepack": "npm run build",
|
|
16
17
|
"test": "vitest run --coverage",
|
|
17
|
-
"test:smoke": "vitest --config=vitest-smoketest.config.ts
|
|
18
|
-
"version": "
|
|
18
|
+
"test:smoke": "vitest --config=vitest-smoketest.config.ts",
|
|
19
|
+
"version": "npm run build:versionedfiles && git add schema.json src/packageInfo.ts"
|
|
19
20
|
},
|
|
20
21
|
"repository": {
|
|
21
22
|
"type": "git",
|
|
@@ -31,6 +32,10 @@
|
|
|
31
32
|
"engines": {
|
|
32
33
|
"node": ">=18"
|
|
33
34
|
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"schema.json"
|
|
38
|
+
],
|
|
34
39
|
"keywords": [
|
|
35
40
|
"api",
|
|
36
41
|
"openapi",
|
|
@@ -38,7 +43,7 @@
|
|
|
38
43
|
"swagger"
|
|
39
44
|
],
|
|
40
45
|
"dependencies": {
|
|
41
|
-
"@readme/api-core": "^7.0.0-beta.
|
|
46
|
+
"@readme/api-core": "^7.0.0-beta.1",
|
|
42
47
|
"@readme/openapi-parser": "^2.4.0",
|
|
43
48
|
"chalk": "^5.3.0",
|
|
44
49
|
"commander": "^11.1.0",
|
|
@@ -59,7 +64,7 @@
|
|
|
59
64
|
"validate-npm-package-name": "^5.0.0"
|
|
60
65
|
},
|
|
61
66
|
"devDependencies": {
|
|
62
|
-
"@api/test-utils": "^7.0.0-beta.
|
|
67
|
+
"@api/test-utils": "^7.0.0-beta.1",
|
|
63
68
|
"@readme/oas-examples": "^5.12.0",
|
|
64
69
|
"@types/js-yaml": "^4.0.7",
|
|
65
70
|
"@types/lodash.camelcase": "^4.3.7",
|
|
@@ -72,6 +77,8 @@
|
|
|
72
77
|
"@types/uslug": "^1.0.2",
|
|
73
78
|
"@types/validate-npm-package-name": "^4.0.0",
|
|
74
79
|
"@vitest/coverage-v8": "^0.34.4",
|
|
80
|
+
"ajv": "^8.12.0",
|
|
81
|
+
"ajv-formats": "^2.1.1",
|
|
75
82
|
"fetch-mock": "^9.11.0",
|
|
76
83
|
"oas-normalize": "^11.0.1",
|
|
77
84
|
"tsup": "^7.2.0",
|
|
@@ -81,5 +88,5 @@
|
|
|
81
88
|
"vitest": "^0.34.5"
|
|
82
89
|
},
|
|
83
90
|
"prettier": "@readme/eslint-config/prettier",
|
|
84
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "e31539d88b5af53ed7be05495329de1c2a70c93f"
|
|
85
92
|
}
|
package/schema.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "api storage lockfile",
|
|
4
|
+
"description": "See https://api.readme.dev/docs",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": ["apis"],
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"properties": {
|
|
9
|
+
"$schema": { "type": "string" },
|
|
10
|
+
"apis": { "type": "array", "description": "The list of installed APIs", "items": { "$ref": "#/definitions/api" } }
|
|
11
|
+
},
|
|
12
|
+
"definitions": {
|
|
13
|
+
"api": {
|
|
14
|
+
"type": "object",
|
|
15
|
+
"required": ["createdAt", "identifier", "installerVersion", "integrity"],
|
|
16
|
+
"properties": {
|
|
17
|
+
"createdAt": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"format": "date-time",
|
|
20
|
+
"description": "The date that this SDK was installed.",
|
|
21
|
+
"examples": ["2023-10-19T20:35:39.268Z"]
|
|
22
|
+
},
|
|
23
|
+
"identifier": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "A unique identifier of the API. This'll be used to do imports on `@api/<identifier>` and also where the SDK code will be located in `.api/apis/<identifier>`",
|
|
26
|
+
"examples": ["petstore"]
|
|
27
|
+
},
|
|
28
|
+
"installerVersion": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"description": "The version of `api` that was used to install this SDK.",
|
|
31
|
+
"examples": ["7.0.0"]
|
|
32
|
+
},
|
|
33
|
+
"integrity": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"description": "An integrity hash that will be used to determine on `npx api update` calls if the API has changed since the SDK was last generated.",
|
|
36
|
+
"examples": [
|
|
37
|
+
"sha512-otRF5TLMeDczSJlrmWLNDHLfmXg+C98oa/I/X2WWycwngh+a6WsbnjTbfwKGRU5DFbagOn2qX2SRvtBGOBRVGg=="
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"language": {
|
|
41
|
+
"type": "string",
|
|
42
|
+
"description": "The language that this SDK was generated for.",
|
|
43
|
+
"default": "js",
|
|
44
|
+
"enum": ["js"]
|
|
45
|
+
},
|
|
46
|
+
"private": {
|
|
47
|
+
"type": "boolean",
|
|
48
|
+
"description": "Was this SDK installed as a private, unpublished, package to the filesystem?"
|
|
49
|
+
},
|
|
50
|
+
"source": {
|
|
51
|
+
"type": "string",
|
|
52
|
+
"description": "The original source that was used to generate the SDK with.",
|
|
53
|
+
"examples": [
|
|
54
|
+
"https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore-simple.json",
|
|
55
|
+
"./petstore.json",
|
|
56
|
+
"@developers/v2.0#nysezql0wwo236"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
"updatedAt": {
|
|
60
|
+
"type": "string",
|
|
61
|
+
"format": "date-time",
|
|
62
|
+
"description": "The date that this SDK was last rebuilt or updated.",
|
|
63
|
+
"examples": ["2023-10-19T20:35:39.268Z"]
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"additionalProperties": false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/bin.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
|
|
3
|
-
import commands from './commands/index.js';
|
|
4
|
-
import * as pkg from './packageInfo.js';
|
|
5
|
-
|
|
6
|
-
(async () => {
|
|
7
|
-
const program = new Command();
|
|
8
|
-
program.name(pkg.PACKAGE_NAME);
|
|
9
|
-
program.version(pkg.PACKAGE_VERSION);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Instead of using Commander's `executableDir` API for loading in external command files we're
|
|
13
|
-
* programatically doing it like this because it's cleaner for us to let Commander manage option
|
|
14
|
-
* and argument parsing within this file than having each command manage that itself.
|
|
15
|
-
*/
|
|
16
|
-
Object.entries(commands).forEach(([, cmd]: [string, Command]) => {
|
|
17
|
-
program.addCommand(cmd);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
await program.parseAsync(process.argv);
|
|
21
|
-
})();
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type Storage from '../storage.js';
|
|
2
|
-
import type Oas from 'oas';
|
|
3
|
-
|
|
4
|
-
import { PACKAGE_NAME, PACKAGE_VERSION } from '../packageInfo.js';
|
|
5
|
-
|
|
6
|
-
export interface InstallerOptions {
|
|
7
|
-
/**
|
|
8
|
-
* Will initiate a dry run install process. Used for simulating installations within a unit test.
|
|
9
|
-
*/
|
|
10
|
-
dryRun?: boolean;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Used for stubbing out the logger that we use within the installation process so it can be
|
|
14
|
-
* easily introspected without having to mock out `console.*`.
|
|
15
|
-
*/
|
|
16
|
-
logger?: (msg: string) => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default abstract class CodeGenerator {
|
|
20
|
-
spec: Oas;
|
|
21
|
-
|
|
22
|
-
specPath: string;
|
|
23
|
-
|
|
24
|
-
identifier: string;
|
|
25
|
-
|
|
26
|
-
userAgent: string;
|
|
27
|
-
|
|
28
|
-
requiredPackages!: Record<
|
|
29
|
-
string,
|
|
30
|
-
{
|
|
31
|
-
dependencyType: 'production' | 'development';
|
|
32
|
-
reason: string;
|
|
33
|
-
url?: string;
|
|
34
|
-
version: string;
|
|
35
|
-
}
|
|
36
|
-
>;
|
|
37
|
-
|
|
38
|
-
constructor(spec: Oas, specPath: string, identifier: string) {
|
|
39
|
-
this.spec = spec;
|
|
40
|
-
this.specPath = specPath;
|
|
41
|
-
this.identifier = identifier;
|
|
42
|
-
|
|
43
|
-
// User agents should be contextual to the spec in question and the version of `api` that was
|
|
44
|
-
// used to generate the SDK. For example, this'll look like `petstore/1.0.0 (api/4.2.0)` for
|
|
45
|
-
// a `petstore` spec installed on api@4.2.0.
|
|
46
|
-
const info = spec.getDefinition().info;
|
|
47
|
-
this.userAgent = `${identifier}/${info.version} (${PACKAGE_NAME}/${PACKAGE_VERSION})`;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* This check is barbaric but there are a number of issues with how the `transformer` work we
|
|
51
|
-
* have in `oas` and in `.getParametersAsJSONSchema()` and `.getResponseAsJSONSchema()` that
|
|
52
|
-
* are fully crashing when attempting to codegen an SDK for an API definition that has a
|
|
53
|
-
* circular reference.
|
|
54
|
-
*
|
|
55
|
-
* In order to get v5 out the door we're not going to support this case initialy.
|
|
56
|
-
*
|
|
57
|
-
* @see {@link https://github.com/readmeio/api/issues/549}
|
|
58
|
-
*/
|
|
59
|
-
if (JSON.stringify(spec.api).includes('"$ref":"#/')) {
|
|
60
|
-
throw new Error(
|
|
61
|
-
'Sorry, this library does not yet support generating an SDK for an OpenAPI definition that contains circular references.',
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
abstract generate(): Promise<Record<string, string>>;
|
|
67
|
-
|
|
68
|
-
abstract install(storage: Storage, opts?: InstallerOptions): Promise<void>;
|
|
69
|
-
|
|
70
|
-
abstract compile(storage: Storage, opts?: InstallerOptions): Promise<void>;
|
|
71
|
-
|
|
72
|
-
hasRequiredPackages() {
|
|
73
|
-
return Boolean(Object.keys(this.requiredPackages));
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/codegen/factory.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type CodeGenerator from './codegenerator.js';
|
|
2
|
-
import type Oas from 'oas';
|
|
3
|
-
|
|
4
|
-
import TSGenerator from './languages/typescript/index.js';
|
|
5
|
-
|
|
6
|
-
export enum SupportedLanguages {
|
|
7
|
-
JS = 'js',
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default function codegenFactory(
|
|
11
|
-
language: SupportedLanguages,
|
|
12
|
-
spec: Oas,
|
|
13
|
-
specPath: string,
|
|
14
|
-
identifier: string,
|
|
15
|
-
): CodeGenerator {
|
|
16
|
-
switch (language) {
|
|
17
|
-
case SupportedLanguages.JS:
|
|
18
|
-
return new TSGenerator(spec, specPath, identifier);
|
|
19
|
-
|
|
20
|
-
default:
|
|
21
|
-
throw new TypeError(`Unsupported language supplied: ${language}`);
|
|
22
|
-
}
|
|
23
|
-
}
|