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/README.md
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
- [Usage](https://api.readme.dev/docs/usage)
|
|
18
18
|
- [Authentication](https://api.readme.dev/docs/authentication)
|
|
19
19
|
- [Parameters and Payloads](https://api.readme.dev/docs/parameters-and-payloads)
|
|
20
|
-
- [
|
|
20
|
+
- [Making requests](https://api.readme.dev/docs/making-requests)
|
|
21
21
|
- [Server configurations](https://api.readme.dev/docs/server-configurations)
|
|
22
22
|
- [How does it work?](https://api.readme.dev/docs/how-it-works)
|
|
23
23
|
- [FAQ](https://api.readme.dev/docs/faq)
|
|
@@ -29,22 +29,21 @@ $ npx api install https://raw.githubusercontent.com/OAI/OpenAPI-Specification/ma
|
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
```js
|
|
32
|
-
const
|
|
32
|
+
const petstore = require('@api/petstore');
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
console.log(`My pets name is ${res[0].name}!`);
|
|
34
|
+
petstore.listPets().then(({ data }) => {
|
|
35
|
+
console.log(`My pets name is ${data[0].name}!`);
|
|
37
36
|
});
|
|
38
37
|
```
|
|
39
38
|
|
|
40
|
-
Or you can use it dynamically (though you won't have fancy TypeScript types):
|
|
39
|
+
Or you can use it dynamically (though you won't have fancy TypeScript types to help you out!):
|
|
41
40
|
|
|
42
41
|
```js
|
|
43
42
|
const petstore = require('api')(
|
|
44
43
|
'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json'
|
|
45
44
|
);
|
|
46
45
|
|
|
47
|
-
petstore.listPets().then(
|
|
48
|
-
console.log(`My pets name is ${
|
|
46
|
+
petstore.listPets().then(({ data })) => {
|
|
47
|
+
console.log(`My pets name is ${data[0].name}!`);
|
|
49
48
|
});
|
|
50
49
|
```
|
package/dist/bin.js
CHANGED
|
@@ -63,8 +63,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
63
63
|
};
|
|
64
64
|
exports.__esModule = true;
|
|
65
65
|
var commander_1 = require("commander");
|
|
66
|
-
var pkg = __importStar(require("./packageInfo"));
|
|
67
66
|
var commands_1 = __importDefault(require("./cli/commands"));
|
|
67
|
+
var pkg = __importStar(require("./packageInfo"));
|
|
68
68
|
(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
69
69
|
var program;
|
|
70
70
|
return __generator(this, function (_a) {
|
package/dist/cache.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { OASDocument } from 'oas
|
|
1
|
+
import type { OASDocument } from 'oas/dist/rmoas.types';
|
|
2
2
|
import 'isomorphic-fetch';
|
|
3
3
|
import Fetcher from './fetcher';
|
|
4
4
|
declare type CacheStore = Record<string, {
|
|
@@ -20,11 +20,46 @@ export default class Cache {
|
|
|
20
20
|
static getCacheHash(file: string | OASDocument): string;
|
|
21
21
|
static setCacheDir(dir?: string | false): void;
|
|
22
22
|
static reset(): Promise<void>;
|
|
23
|
-
static validate(json: any): Promise<import("openapi-types").OpenAPI.Document<{}>>;
|
|
24
23
|
isCached(): boolean;
|
|
25
24
|
getCache(): CacheStore;
|
|
26
25
|
get(): any;
|
|
27
|
-
load(): Promise<
|
|
26
|
+
load(): Promise<import("openapi-types").OpenAPIV3.Document<{}> | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
27
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
28
|
+
jsonSchemaDialect?: string;
|
|
29
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
30
|
+
} & Pick<{
|
|
31
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
32
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
33
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
34
|
+
}, "paths"> & Omit<Partial<{
|
|
35
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
36
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
37
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
38
|
+
}>, "paths">) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
39
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
40
|
+
jsonSchemaDialect?: string;
|
|
41
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
42
|
+
} & Pick<{
|
|
43
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
44
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
45
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
46
|
+
}, "webhooks"> & Omit<Partial<{
|
|
47
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
48
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
49
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
50
|
+
}>, "webhooks">) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
51
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
52
|
+
jsonSchemaDialect?: string;
|
|
53
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
54
|
+
} & Pick<{
|
|
55
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
56
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
57
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
58
|
+
}, "components"> & Omit<Partial<{
|
|
59
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
60
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
61
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
62
|
+
}>, "components">) | import("openapi-types").OpenAPIV2.Document<{}>>;
|
|
28
63
|
save(spec: OASDocument): OASDocument;
|
|
29
64
|
}
|
|
30
65
|
export {};
|
package/dist/cache.js
CHANGED
|
@@ -39,16 +39,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
exports.__esModule = true;
|
|
42
|
-
require("isomorphic-fetch");
|
|
43
|
-
var openapi_parser_1 = __importDefault(require("@readme/openapi-parser"));
|
|
44
42
|
var crypto_1 = __importDefault(require("crypto"));
|
|
45
|
-
var find_cache_dir_1 = __importDefault(require("find-cache-dir"));
|
|
46
43
|
var fs_1 = __importDefault(require("fs"));
|
|
47
44
|
var os_1 = __importDefault(require("os"));
|
|
48
45
|
var path_1 = __importDefault(require("path"));
|
|
46
|
+
var find_cache_dir_1 = __importDefault(require("find-cache-dir"));
|
|
47
|
+
require("isomorphic-fetch");
|
|
49
48
|
var make_dir_1 = __importDefault(require("make-dir"));
|
|
50
|
-
var packageInfo_1 = require("./packageInfo");
|
|
51
49
|
var fetcher_1 = __importDefault(require("./fetcher"));
|
|
50
|
+
var packageInfo_1 = require("./packageInfo");
|
|
52
51
|
var Cache = /** @class */ (function () {
|
|
53
52
|
function Cache(uri, cacheDir) {
|
|
54
53
|
if (cacheDir === void 0) { cacheDir = false; }
|
|
@@ -120,25 +119,6 @@ var Cache = /** @class */ (function () {
|
|
|
120
119
|
});
|
|
121
120
|
});
|
|
122
121
|
};
|
|
123
|
-
Cache.validate = function (json) {
|
|
124
|
-
if (json.swagger) {
|
|
125
|
-
throw new Error('Sorry, this module only supports OpenAPI definitions.');
|
|
126
|
-
}
|
|
127
|
-
// The `validate` method handles dereferencing for us.
|
|
128
|
-
return openapi_parser_1["default"].validate(json, {
|
|
129
|
-
dereference: {
|
|
130
|
-
// If circular `$refs` are ignored they'll remain in the API definition as `$ref: String`.
|
|
131
|
-
// This allows us to not only do easy circular reference detection but also stringify and
|
|
132
|
-
// save dereferenced API definitions back into the cache directory.
|
|
133
|
-
circular: 'ignore'
|
|
134
|
-
}
|
|
135
|
-
})["catch"](function (err) {
|
|
136
|
-
if (/is not a valid openapi definition/i.test(err.message)) {
|
|
137
|
-
throw new Error("Sorry, that doesn't look like a valid OpenAPI definition.");
|
|
138
|
-
}
|
|
139
|
-
throw err;
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
122
|
Cache.prototype.isCached = function () {
|
|
143
123
|
var cache = this.getCache();
|
|
144
124
|
return cache && this.uriHash in cache;
|
|
@@ -178,10 +158,11 @@ var Cache = /** @class */ (function () {
|
|
|
178
158
|
return __awaiter(this, void 0, void 0, function () {
|
|
179
159
|
var _this = this;
|
|
180
160
|
return __generator(this, function (_a) {
|
|
181
|
-
// If the class was supplied a raw object
|
|
182
|
-
//
|
|
161
|
+
// If the class was supplied a raw object we should still validate and make sure that it's
|
|
162
|
+
// dereferenced in order for everything to function, but we shouldn't worry about saving it
|
|
163
|
+
// into the cache directory architecture.
|
|
183
164
|
if (typeof this.uri === 'object') {
|
|
184
|
-
return [2 /*return*/, this.uri];
|
|
165
|
+
return [2 /*return*/, fetcher_1["default"].validate(this.uri)];
|
|
185
166
|
}
|
|
186
167
|
return [2 /*return*/, this.fetcher.load().then(function (spec) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
187
168
|
return [2 /*return*/, this.save(spec)];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type Oas from 'oas';
|
|
2
1
|
import type CodeGeneratorLanguage from './language';
|
|
2
|
+
import type Oas from 'oas';
|
|
3
3
|
export declare type SupportedLanguages = 'js' | 'js-cjs' | 'js-esm' | 'ts';
|
|
4
4
|
export default function codegen(language: SupportedLanguages, spec: Oas, specPath: string, identifier: string): CodeGeneratorLanguage;
|
|
@@ -10,6 +10,19 @@ var CodeGeneratorLanguage = /** @class */ (function () {
|
|
|
10
10
|
// a `petstore` spec installed on api@4.2.0.
|
|
11
11
|
var info = spec.getDefinition().info;
|
|
12
12
|
this.userAgent = "".concat(identifier, "/").concat(info.version, " (").concat(packageInfo_1.PACKAGE_NAME, "/").concat(packageInfo_1.PACKAGE_VERSION, ")");
|
|
13
|
+
/**
|
|
14
|
+
* This check is barbaric but there are a number of issues with how the `transformer` work we
|
|
15
|
+
* have in `oas` and in `.getParametersAsJSONSchema()` and `.getResponseAsJSONSchema()` that
|
|
16
|
+
* are fully crashing when attempting to codegen an SDK for an API definition that has a
|
|
17
|
+
* circular reference.
|
|
18
|
+
*
|
|
19
|
+
* In order to get v5 out the door we're not going to support this case initialy.
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://github.com/readmeio/api/issues/549}
|
|
22
|
+
*/
|
|
23
|
+
if (JSON.stringify(spec.api).includes('"$ref":"#/')) {
|
|
24
|
+
throw new Error('Sorry, this library does not yet support generating an SDK for an OpenAPI definition that contains circular references.');
|
|
25
|
+
}
|
|
13
26
|
}
|
|
14
27
|
CodeGeneratorLanguage.prototype.hasRequiredPackages = function () {
|
|
15
28
|
return Boolean(Object.keys(this.requiredPackages));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare function formatter(content: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* @see {@link https://www.30secondsofcode.org/js/s/word-wrap}
|
|
4
|
+
*/
|
|
5
|
+
export declare function wordWrap(str: string, max?: number): string;
|
|
6
|
+
/**
|
|
7
|
+
* Safely escape some string characters that may break a docblock.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare function docblockEscape(str: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Convert a string that might contain spaces or special characters to one that can safely be used
|
|
13
|
+
* as a TypeScript interface or enum name.
|
|
14
|
+
*
|
|
15
|
+
* This function has been adapted and slighty modified from `json-schema-to-typescript`.
|
|
16
|
+
*
|
|
17
|
+
* @license MIT
|
|
18
|
+
* @see {@link https://github.com/bcherny/json-schema-to-typescript}
|
|
19
|
+
*/
|
|
20
|
+
export declare function toSafeString(str: string): string;
|
|
21
|
+
export declare function generateTypeName(...parts: string[]): string;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.generateTypeName = exports.toSafeString = exports.docblockEscape = exports.wordWrap = exports.formatter = void 0;
|
|
7
|
+
var lodash_camelcase_1 = __importDefault(require("lodash.camelcase"));
|
|
8
|
+
var lodash_deburr_1 = __importDefault(require("lodash.deburr"));
|
|
9
|
+
var lodash_startcase_1 = __importDefault(require("lodash.startcase"));
|
|
10
|
+
var prettier_1 = require("prettier");
|
|
11
|
+
/**
|
|
12
|
+
* This is a mix of reserved JS words and keywords in TypeScript that might be reserved or
|
|
13
|
+
* allowable but functionally confusing (like `let any = 'buster';`)
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar}
|
|
16
|
+
*/
|
|
17
|
+
var RESERVED_WORDS = [
|
|
18
|
+
'abstract',
|
|
19
|
+
'any',
|
|
20
|
+
'arguments',
|
|
21
|
+
'as',
|
|
22
|
+
'async',
|
|
23
|
+
'await',
|
|
24
|
+
'boolean',
|
|
25
|
+
'break',
|
|
26
|
+
'byte',
|
|
27
|
+
'case',
|
|
28
|
+
'catch',
|
|
29
|
+
'char',
|
|
30
|
+
'class',
|
|
31
|
+
'const',
|
|
32
|
+
'continue',
|
|
33
|
+
'constructor',
|
|
34
|
+
'debugger',
|
|
35
|
+
'default',
|
|
36
|
+
'delete',
|
|
37
|
+
'do',
|
|
38
|
+
'double',
|
|
39
|
+
'else',
|
|
40
|
+
'enum',
|
|
41
|
+
'eval',
|
|
42
|
+
'export',
|
|
43
|
+
'extends',
|
|
44
|
+
'false',
|
|
45
|
+
'final',
|
|
46
|
+
'finally',
|
|
47
|
+
'float',
|
|
48
|
+
'for',
|
|
49
|
+
'from',
|
|
50
|
+
'function',
|
|
51
|
+
'get',
|
|
52
|
+
'goto',
|
|
53
|
+
'if',
|
|
54
|
+
'implements',
|
|
55
|
+
'import',
|
|
56
|
+
'interface',
|
|
57
|
+
'in',
|
|
58
|
+
'instanceof',
|
|
59
|
+
'int',
|
|
60
|
+
'let',
|
|
61
|
+
'long',
|
|
62
|
+
'native',
|
|
63
|
+
'new',
|
|
64
|
+
'null',
|
|
65
|
+
'number',
|
|
66
|
+
'of',
|
|
67
|
+
'package',
|
|
68
|
+
'private',
|
|
69
|
+
'protected',
|
|
70
|
+
'public',
|
|
71
|
+
'module',
|
|
72
|
+
'namespace',
|
|
73
|
+
'return',
|
|
74
|
+
'set',
|
|
75
|
+
'short',
|
|
76
|
+
'static',
|
|
77
|
+
'string',
|
|
78
|
+
'super',
|
|
79
|
+
'switch',
|
|
80
|
+
'synchronized',
|
|
81
|
+
'this',
|
|
82
|
+
'throw',
|
|
83
|
+
'throws',
|
|
84
|
+
'transient',
|
|
85
|
+
'true',
|
|
86
|
+
'try',
|
|
87
|
+
'type',
|
|
88
|
+
'typeof',
|
|
89
|
+
'var',
|
|
90
|
+
'void',
|
|
91
|
+
'while',
|
|
92
|
+
'with',
|
|
93
|
+
'volatile',
|
|
94
|
+
'yield',
|
|
95
|
+
// These aren't reserved keywords but because we maybe codegen'ing an SDK to be used in the
|
|
96
|
+
// browser it'd be very bad if we overwrote these. This obviously doesn't account for browser APIs
|
|
97
|
+
// you can access outside of the `Window` API (like `alert()`), but we can add checks for those
|
|
98
|
+
// later if we need to.
|
|
99
|
+
'frames',
|
|
100
|
+
'global',
|
|
101
|
+
'globalThis',
|
|
102
|
+
'navigator',
|
|
103
|
+
'self',
|
|
104
|
+
'window',
|
|
105
|
+
];
|
|
106
|
+
function formatter(content) {
|
|
107
|
+
return (0, prettier_1.format)(content, {
|
|
108
|
+
parser: 'typescript',
|
|
109
|
+
printWidth: 100,
|
|
110
|
+
singleQuote: true
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
exports.formatter = formatter;
|
|
114
|
+
/**
|
|
115
|
+
* @see {@link https://www.30secondsofcode.org/js/s/word-wrap}
|
|
116
|
+
*/
|
|
117
|
+
function wordWrap(str, max) {
|
|
118
|
+
if (max === void 0) { max = 88; }
|
|
119
|
+
return str.replace(new RegExp("(?![^\\n]{1,".concat(max, "}$)([^\\n]{1,").concat(max, "})\\s"), 'g'), '$1\n');
|
|
120
|
+
}
|
|
121
|
+
exports.wordWrap = wordWrap;
|
|
122
|
+
/**
|
|
123
|
+
* Safely escape some string characters that may break a docblock.
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
function docblockEscape(str) {
|
|
127
|
+
return str.replace(/\/\*/g, '/\\*').replace(/\*\//g, '*\\/');
|
|
128
|
+
}
|
|
129
|
+
exports.docblockEscape = docblockEscape;
|
|
130
|
+
/**
|
|
131
|
+
* Convert a string that might contain spaces or special characters to one that can safely be used
|
|
132
|
+
* as a TypeScript interface or enum name.
|
|
133
|
+
*
|
|
134
|
+
* This function has been adapted and slighty modified from `json-schema-to-typescript`.
|
|
135
|
+
*
|
|
136
|
+
* @license MIT
|
|
137
|
+
* @see {@link https://github.com/bcherny/json-schema-to-typescript}
|
|
138
|
+
*/
|
|
139
|
+
function toSafeString(str) {
|
|
140
|
+
// identifiers in javaScript/ts:
|
|
141
|
+
// First character: a-zA-Z | _ | $
|
|
142
|
+
// Rest: a-zA-Z | _ | $ | 0-9
|
|
143
|
+
// remove accents, umlauts, ... by their basic latin letters
|
|
144
|
+
return ((0, lodash_deburr_1["default"])(str)
|
|
145
|
+
// if the string starts with a number, prefix it with character that typescript can accept
|
|
146
|
+
// https://github.com/bcherny/json-schema-to-typescript/issues/489
|
|
147
|
+
.replace(/^(\d){1}/, '$$1')
|
|
148
|
+
// replace chars which are not valid for typescript identifiers with whitespace
|
|
149
|
+
.replace(/(^\s*[^a-zA-Z_$])|([^a-zA-Z_$\d])/g, ' ')
|
|
150
|
+
// uppercase leading underscores followed by lowercase
|
|
151
|
+
.replace(/^_[a-z]/g, function (match) { return match.toUpperCase(); })
|
|
152
|
+
// remove non-leading underscores followed by lowercase (convert snake_case)
|
|
153
|
+
.replace(/_[a-z]/g, function (match) { return match.substr(1, match.length).toUpperCase(); })
|
|
154
|
+
// uppercase letters after digits, dollars
|
|
155
|
+
.replace(/([\d$]+[a-zA-Z])/g, function (match) { return match.toUpperCase(); })
|
|
156
|
+
// uppercase first letter after whitespace
|
|
157
|
+
.replace(/\s+([a-zA-Z])/g, function (match) { return match.toUpperCase().trim(); })
|
|
158
|
+
// remove remaining whitespace
|
|
159
|
+
.replace(/\s/g, ''));
|
|
160
|
+
}
|
|
161
|
+
exports.toSafeString = toSafeString;
|
|
162
|
+
function generateTypeName() {
|
|
163
|
+
var parts = [];
|
|
164
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
165
|
+
parts[_i] = arguments[_i];
|
|
166
|
+
}
|
|
167
|
+
var str;
|
|
168
|
+
// If the end of our string ends with something like `2XX`, the combination of `startCase` and
|
|
169
|
+
// `camelCase` will transform it into `2Xx`.
|
|
170
|
+
if (parts.length > 1) {
|
|
171
|
+
var last = parts[parts.length - 1];
|
|
172
|
+
if (last.match(/^(\d)XX$/)) {
|
|
173
|
+
str = (0, lodash_startcase_1["default"])((0, lodash_camelcase_1["default"])(parts.slice(0, -1).join(' ')));
|
|
174
|
+
str += " ".concat(last);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!str) {
|
|
178
|
+
str = (0, lodash_startcase_1["default"])((0, lodash_camelcase_1["default"])(parts.join(' ')));
|
|
179
|
+
}
|
|
180
|
+
if (RESERVED_WORDS.includes(str.toLowerCase())) {
|
|
181
|
+
str = "$".concat(str);
|
|
182
|
+
}
|
|
183
|
+
return toSafeString(str);
|
|
184
|
+
}
|
|
185
|
+
exports.generateTypeName = generateTypeName;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import type Oas from 'oas';
|
|
2
|
-
import type { Operation } from 'oas';
|
|
3
|
-
import type { JSONSchema, SchemaObject } from 'oas/@types/rmoas.types';
|
|
4
|
-
import type { ClassDeclaration, MethodDeclaration } from 'ts-morph';
|
|
5
1
|
import type Storage from '../../storage';
|
|
6
2
|
import type { InstallerOptions } from '../language';
|
|
7
|
-
import
|
|
3
|
+
import type Oas from 'oas';
|
|
4
|
+
import type { Operation } from 'oas';
|
|
5
|
+
import type { ClassDeclaration } from 'ts-morph';
|
|
8
6
|
import { Project } from 'ts-morph';
|
|
7
|
+
import CodeGeneratorLanguage from '../language';
|
|
8
|
+
export declare type TSGeneratorOptions = {
|
|
9
|
+
outputJS?: boolean;
|
|
10
|
+
compilerTarget?: 'cjs' | 'esm';
|
|
11
|
+
};
|
|
9
12
|
declare type OperationTypeHousing = {
|
|
10
13
|
types: {
|
|
11
14
|
params?: false | Record<'body' | 'formData' | 'metadata', string>;
|
|
@@ -19,18 +22,14 @@ export default class TSGenerator extends CodeGeneratorLanguage {
|
|
|
19
22
|
compilerTarget: 'cjs' | 'esm';
|
|
20
23
|
types: Map<string, string>;
|
|
21
24
|
files: Record<string, string>;
|
|
22
|
-
methodGenerics: Map<string, MethodDeclaration>;
|
|
23
25
|
sdk: ClassDeclaration;
|
|
24
|
-
schemas:
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
compilerTarget?: 'cjs' | 'esm';
|
|
32
|
-
});
|
|
33
|
-
static formatter(content: string): string;
|
|
26
|
+
schemas: Record<string, {
|
|
27
|
+
body?: any;
|
|
28
|
+
metadata?: any;
|
|
29
|
+
response?: Record<string, any>;
|
|
30
|
+
} | Record<string, any>>;
|
|
31
|
+
usesHTTPMethodRangeInterface: boolean;
|
|
32
|
+
constructor(spec: Oas, specPath: string, identifier: string, opts?: TSGeneratorOptions);
|
|
34
33
|
installer(storage: Storage, opts?: InstallerOptions): Promise<void>;
|
|
35
34
|
/**
|
|
36
35
|
* Compile the current OpenAPI definition into a TypeScript library.
|
|
@@ -40,60 +39,56 @@ export default class TSGenerator extends CodeGeneratorLanguage {
|
|
|
40
39
|
[x: string]: string;
|
|
41
40
|
}>;
|
|
42
41
|
/**
|
|
43
|
-
* Create
|
|
42
|
+
* Create our main SDK source file.
|
|
44
43
|
*
|
|
45
|
-
* @param method
|
|
46
44
|
*/
|
|
47
|
-
|
|
45
|
+
createSourceFile(): import("ts-morph").SourceFile;
|
|
48
46
|
/**
|
|
49
|
-
* Create
|
|
47
|
+
* Create our main schemas file. This is where all of the JSON Schema that our TypeScript typing
|
|
48
|
+
* infrastructure sources its data from. Without this there are no types.
|
|
50
49
|
*
|
|
51
|
-
* @param operation
|
|
52
|
-
* @param operationId
|
|
53
|
-
* @param paramTypes
|
|
54
|
-
* @param responseTypes
|
|
55
50
|
*/
|
|
56
|
-
|
|
51
|
+
createSchemasFile(): import("ts-morph").SourceFile;
|
|
57
52
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
53
|
+
* Create our main types file. This sources its data from the JSON Schema `schemas.ts` file and
|
|
54
|
+
* will re-export types to be used in TypeScript implementations and IDE intellisense. This
|
|
55
|
+
* typing work is functional with the `json-schema-to-ts` library.
|
|
60
56
|
*
|
|
61
|
-
*
|
|
62
|
-
|
|
57
|
+
* @see {@link https://npm.im/json-schema-to-ts}
|
|
58
|
+
*/
|
|
59
|
+
createTypesFile(): import("ts-morph").SourceFile;
|
|
60
|
+
/**
|
|
61
|
+
* Create operation accessors on the SDK.
|
|
63
62
|
*
|
|
64
|
-
* @param schema
|
|
65
|
-
* @param name
|
|
66
63
|
*/
|
|
67
|
-
|
|
68
|
-
primaryType: string;
|
|
69
|
-
}>;
|
|
64
|
+
createOperationAccessor(operation: Operation, operationId: string, paramTypes?: OperationTypeHousing['types']['params'], responseTypes?: OperationTypeHousing['types']['responses']): void;
|
|
70
65
|
/**
|
|
71
66
|
* Scour through the current OpenAPI definition and compile a store of every operation, along
|
|
72
67
|
* with every HTTP method that's in use, and their available TypeScript types that we can use,
|
|
73
68
|
* along with every HTTP method that's in use.
|
|
74
69
|
*
|
|
75
70
|
*/
|
|
76
|
-
loadOperationsAndMethods():
|
|
71
|
+
loadOperationsAndMethods(): {
|
|
77
72
|
operations: Record<string, OperationTypeHousing>;
|
|
78
73
|
methods: Set<unknown>;
|
|
79
|
-
}
|
|
74
|
+
};
|
|
80
75
|
/**
|
|
81
76
|
* Compile the parameter (path, query, cookie, and header) schemas for an API operation into
|
|
82
77
|
* usable TypeScript types.
|
|
83
78
|
*
|
|
84
|
-
* @param operation
|
|
85
|
-
* @param operationId
|
|
86
79
|
*/
|
|
87
80
|
prepareParameterTypesForOperation(operation: Operation, operationId: string): false | Record<"formData" | "body" | "metadata", string>;
|
|
88
81
|
/**
|
|
89
82
|
* Compile the response schemas for an API operation into usable TypeScript types.
|
|
90
83
|
*
|
|
91
|
-
* @todo what does this do for a spec that has no responses?
|
|
92
|
-
* @param operation
|
|
93
|
-
* @param operationId
|
|
94
84
|
*/
|
|
95
85
|
prepareResponseTypesForOperation(operation: Operation, operationId: string): {
|
|
96
86
|
[x: string]: string;
|
|
97
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* Add a given schema into our schema dataset that we'll be be exporting as types.
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
addSchemaToExport(schema: any, typeName: string, pointer: string): void;
|
|
98
93
|
}
|
|
99
94
|
export {};
|