api 7.0.0-alpha.0 → 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/dist/bin.d.ts +1 -0
- package/dist/bin.js +91 -0
- package/dist/codegen/index.d.ts +4 -0
- package/dist/codegen/index.js +23 -0
- package/dist/codegen/language.d.ts +27 -0
- package/dist/codegen/language.js +33 -0
- package/dist/codegen/languages/typescript/util.d.ts +10 -0
- package/dist/codegen/languages/typescript/util.js +175 -0
- package/dist/codegen/languages/typescript.d.ts +111 -0
- package/dist/codegen/languages/typescript.js +821 -0
- package/dist/commands/index.d.ts +4 -0
- package/dist/commands/index.js +9 -0
- package/dist/commands/install.d.ts +3 -0
- package/dist/commands/install.js +239 -0
- package/dist/fetcher.d.ts +53 -0
- package/dist/fetcher.js +164 -0
- package/dist/lib/prompt.d.ts +9 -0
- package/dist/lib/prompt.js +81 -0
- package/dist/logger.d.ts +1 -0
- package/dist/logger.js +16 -0
- package/dist/packageInfo.d.ts +2 -0
- package/dist/packageInfo.js +6 -0
- package/dist/storage.d.ts +106 -0
- package/dist/storage.js +280 -0
- package/package.json +2 -2
- package/src/codegen/languages/typescript.ts +6 -6
- package/src/packageInfo.ts +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var install_1 = __importDefault(require("./install"));
|
|
7
|
+
exports.default = {
|
|
8
|
+
install: install_1.default,
|
|
9
|
+
};
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var commander_1 = require("commander");
|
|
43
|
+
var figures_1 = __importDefault(require("figures"));
|
|
44
|
+
var oas_1 = __importDefault(require("oas"));
|
|
45
|
+
var ora_1 = __importDefault(require("ora"));
|
|
46
|
+
var codegen_1 = __importDefault(require("../codegen"));
|
|
47
|
+
var fetcher_1 = __importDefault(require("../fetcher"));
|
|
48
|
+
var prompt_1 = __importDefault(require("../lib/prompt"));
|
|
49
|
+
var logger_1 = __importDefault(require("../logger"));
|
|
50
|
+
var storage_1 = __importDefault(require("../storage"));
|
|
51
|
+
// @todo log logs to `.api/.logs` and have `.logs` ignored
|
|
52
|
+
var cmd = new commander_1.Command();
|
|
53
|
+
cmd
|
|
54
|
+
.name('install')
|
|
55
|
+
.description('install an API SDK into your codebase')
|
|
56
|
+
.argument('<uri>', 'an API to install')
|
|
57
|
+
.option('-i, --identifier <identifier>', 'API identifier (eg. `@api/petstore`)')
|
|
58
|
+
.addOption(new commander_1.Option('-l, --lang <language>', 'SDK language').choices([
|
|
59
|
+
'js',
|
|
60
|
+
'js-cjs',
|
|
61
|
+
'js-esm',
|
|
62
|
+
'ts',
|
|
63
|
+
]))
|
|
64
|
+
.addOption(new commander_1.Option('-y, --yes', 'Automatically answer "yes" to any prompts printed'))
|
|
65
|
+
.action(function (uri, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
66
|
+
var language, identifier, spinner, storage, oas, generator, sdkSource, err_1;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
if (!options.lang) return [3 /*break*/, 1];
|
|
71
|
+
language = options.lang;
|
|
72
|
+
return [3 /*break*/, 3];
|
|
73
|
+
case 1: return [4 /*yield*/, (0, prompt_1.default)({
|
|
74
|
+
type: 'select',
|
|
75
|
+
name: 'value',
|
|
76
|
+
message: 'What language would you like to generate an SDK for?',
|
|
77
|
+
choices: [
|
|
78
|
+
{ title: 'TypeScript', value: 'ts' },
|
|
79
|
+
{ title: 'JavaScript', value: 'js' },
|
|
80
|
+
],
|
|
81
|
+
initial: 1,
|
|
82
|
+
})];
|
|
83
|
+
case 2:
|
|
84
|
+
(language = (_a.sent()).value);
|
|
85
|
+
_a.label = 3;
|
|
86
|
+
case 3:
|
|
87
|
+
if (!(language === 'js')) return [3 /*break*/, 5];
|
|
88
|
+
return [4 /*yield*/, (0, prompt_1.default)({
|
|
89
|
+
type: 'select',
|
|
90
|
+
name: 'value',
|
|
91
|
+
message: 'How are your project imports and exports structured?',
|
|
92
|
+
choices: [
|
|
93
|
+
{ title: 'CommonJS', description: 'require/exports', value: 'cjs' },
|
|
94
|
+
{ title: 'ECMAScript Modules', description: 'import/export', value: 'esm' },
|
|
95
|
+
],
|
|
96
|
+
initial: 0,
|
|
97
|
+
format: function (sel) { return (sel === 'cjs' ? 'js-cjs' : 'js-esm'); },
|
|
98
|
+
})];
|
|
99
|
+
case 4:
|
|
100
|
+
(language = (_a.sent()).value);
|
|
101
|
+
_a.label = 5;
|
|
102
|
+
case 5:
|
|
103
|
+
// @todo let them know that we're going to be creating a `.api/ directory
|
|
104
|
+
// @todo detect if they have a gitigore and .npmignore and if .api woudl be ignored by that
|
|
105
|
+
// @todo don't support swagger files without upconverting them
|
|
106
|
+
if (storage_1.default.isInLockFile({ source: uri })) {
|
|
107
|
+
// @todo
|
|
108
|
+
// logger(`It looks like you already have this API installed. Would you like to update it?`);
|
|
109
|
+
}
|
|
110
|
+
if (!options.identifier) return [3 /*break*/, 6];
|
|
111
|
+
// `Storage.isIdentifierValid` will throw an exception if an identifier is invalid.
|
|
112
|
+
if (storage_1.default.isIdentifierValid(options.identifier)) {
|
|
113
|
+
identifier = options.identifier;
|
|
114
|
+
}
|
|
115
|
+
return [3 /*break*/, 9];
|
|
116
|
+
case 6:
|
|
117
|
+
if (!fetcher_1.default.isAPIRegistryUUID(uri)) return [3 /*break*/, 7];
|
|
118
|
+
identifier = fetcher_1.default.getProjectPrefixFromRegistryUUID(uri);
|
|
119
|
+
return [3 /*break*/, 9];
|
|
120
|
+
case 7: return [4 /*yield*/, (0, prompt_1.default)({
|
|
121
|
+
type: 'text',
|
|
122
|
+
name: 'value',
|
|
123
|
+
message: 'What would you like to identify this API as? This will be how you import the SDK. (e.g. entering `petstore` would result in `@api/petstore`)',
|
|
124
|
+
validate: function (value) {
|
|
125
|
+
if (!value) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
return storage_1.default.isIdentifierValid(value, true);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
return err.message;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
})];
|
|
136
|
+
case 8:
|
|
137
|
+
(identifier = (_a.sent()).value);
|
|
138
|
+
_a.label = 9;
|
|
139
|
+
case 9:
|
|
140
|
+
if (!identifier) {
|
|
141
|
+
(0, logger_1.default)('You must tell us what you would like to identify this API as in order to install it.', true);
|
|
142
|
+
process.exit(1);
|
|
143
|
+
}
|
|
144
|
+
spinner = (0, ora_1.default)('Fetching your API').start();
|
|
145
|
+
storage = new storage_1.default(uri, identifier);
|
|
146
|
+
return [4 /*yield*/, storage
|
|
147
|
+
.load()
|
|
148
|
+
.then(function (res) {
|
|
149
|
+
spinner.succeed(spinner.text);
|
|
150
|
+
return res;
|
|
151
|
+
})
|
|
152
|
+
.then(oas_1.default.init)
|
|
153
|
+
.catch(function (err) {
|
|
154
|
+
// @todo cleanup installed files
|
|
155
|
+
spinner.fail(spinner.text);
|
|
156
|
+
(0, logger_1.default)(err.message, true);
|
|
157
|
+
process.exit(1);
|
|
158
|
+
})];
|
|
159
|
+
case 10:
|
|
160
|
+
oas = _a.sent();
|
|
161
|
+
// @todo look for a prettier config and if we find one ask them if we should use it
|
|
162
|
+
spinner = (0, ora_1.default)('Generating your SDK').start();
|
|
163
|
+
generator = (0, codegen_1.default)(language, oas, './openapi.json', identifier);
|
|
164
|
+
return [4 /*yield*/, generator
|
|
165
|
+
.generator()
|
|
166
|
+
.then(function (res) {
|
|
167
|
+
spinner.succeed(spinner.text);
|
|
168
|
+
return res;
|
|
169
|
+
})
|
|
170
|
+
.catch(function (err) {
|
|
171
|
+
// @todo cleanup installed files
|
|
172
|
+
spinner.fail(spinner.text);
|
|
173
|
+
(0, logger_1.default)(err.message, true);
|
|
174
|
+
process.exit(1);
|
|
175
|
+
})];
|
|
176
|
+
case 11:
|
|
177
|
+
sdkSource = _a.sent();
|
|
178
|
+
spinner = (0, ora_1.default)('Saving your SDK into your codebase').start();
|
|
179
|
+
return [4 /*yield*/, storage
|
|
180
|
+
.saveSourceFiles(sdkSource)
|
|
181
|
+
.then(function () {
|
|
182
|
+
spinner.succeed(spinner.text);
|
|
183
|
+
})
|
|
184
|
+
.catch(function (err) {
|
|
185
|
+
// @todo cleanup installed files
|
|
186
|
+
spinner.fail(spinner.text);
|
|
187
|
+
(0, logger_1.default)(err.message, true);
|
|
188
|
+
process.exit(1);
|
|
189
|
+
})];
|
|
190
|
+
case 12:
|
|
191
|
+
_a.sent();
|
|
192
|
+
if (!generator.hasRequiredPackages()) return [3 /*break*/, 18];
|
|
193
|
+
(0, logger_1.default)("".concat(figures_1.default.warning, " This generator requires some packages to be installed alongside it:"));
|
|
194
|
+
Object.entries(generator.requiredPackages).forEach(function (_a) {
|
|
195
|
+
var pkg = _a[0], pkgInfo = _a[1];
|
|
196
|
+
(0, logger_1.default)(" ".concat(figures_1.default.pointerSmall, " ").concat(pkg, ": ").concat(pkgInfo.reason, " ").concat(pkgInfo.url));
|
|
197
|
+
});
|
|
198
|
+
if (!!options.yes) return [3 /*break*/, 14];
|
|
199
|
+
return [4 /*yield*/, (0, prompt_1.default)({
|
|
200
|
+
type: 'confirm',
|
|
201
|
+
name: 'value',
|
|
202
|
+
message: 'OK to proceed with package installation?',
|
|
203
|
+
initial: true,
|
|
204
|
+
}).then(function (_a) {
|
|
205
|
+
var value = _a.value;
|
|
206
|
+
if (!value) {
|
|
207
|
+
// @todo cleanup installed files
|
|
208
|
+
(0, logger_1.default)('Installation cancelled.', true);
|
|
209
|
+
process.exit(1);
|
|
210
|
+
}
|
|
211
|
+
})];
|
|
212
|
+
case 13:
|
|
213
|
+
_a.sent();
|
|
214
|
+
_a.label = 14;
|
|
215
|
+
case 14:
|
|
216
|
+
spinner = (0, ora_1.default)('Installing required packages').start();
|
|
217
|
+
_a.label = 15;
|
|
218
|
+
case 15:
|
|
219
|
+
_a.trys.push([15, 17, , 18]);
|
|
220
|
+
return [4 /*yield*/, generator.installer(storage)];
|
|
221
|
+
case 16:
|
|
222
|
+
_a.sent();
|
|
223
|
+
spinner.succeed(spinner.text);
|
|
224
|
+
return [3 /*break*/, 18];
|
|
225
|
+
case 17:
|
|
226
|
+
err_1 = _a.sent();
|
|
227
|
+
// @todo cleanup installed files
|
|
228
|
+
spinner.fail(spinner.text);
|
|
229
|
+
(0, logger_1.default)(err_1.message, true);
|
|
230
|
+
process.exit(1);
|
|
231
|
+
return [3 /*break*/, 18];
|
|
232
|
+
case 18:
|
|
233
|
+
(0, logger_1.default)('🚀 All done!');
|
|
234
|
+
return [2 /*return*/];
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}); })
|
|
238
|
+
.addHelpText('after', "\nExamples:\n $ api install @developers/v2.0#nysezql0wwo236\n $ api install https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore-simple.json\n $ api install ./petstore.json");
|
|
239
|
+
exports.default = cmd;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { OASDocument } from 'oas/dist/rmoas.types';
|
|
2
|
+
export default class Fetcher {
|
|
3
|
+
uri: string | OASDocument;
|
|
4
|
+
/**
|
|
5
|
+
* @example @petstore/v1.0#n6kvf10vakpemvplx
|
|
6
|
+
* @example @petstore#n6kvf10vakpemvplx
|
|
7
|
+
*/
|
|
8
|
+
static registryUUIDRegex: RegExp;
|
|
9
|
+
constructor(uri: string | OASDocument);
|
|
10
|
+
static isAPIRegistryUUID(uri: string): boolean;
|
|
11
|
+
static isGitHubBlobURL(uri: string): boolean;
|
|
12
|
+
static getProjectPrefixFromRegistryUUID(uri: string): string | undefined;
|
|
13
|
+
load(): Promise<(Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
14
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
15
|
+
jsonSchemaDialect?: string | undefined;
|
|
16
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[] | undefined;
|
|
17
|
+
} & Pick<{
|
|
18
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
19
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
20
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
21
|
+
}, "paths"> & Omit<Partial<{
|
|
22
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
23
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
24
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
25
|
+
}>, "paths"> & Record<string, unknown>) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
26
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
27
|
+
jsonSchemaDialect?: string | undefined;
|
|
28
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[] | undefined;
|
|
29
|
+
} & Pick<{
|
|
30
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
31
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
32
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
33
|
+
}, "webhooks"> & Omit<Partial<{
|
|
34
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
35
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
36
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
37
|
+
}>, "webhooks"> & Record<string, unknown>) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
38
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
39
|
+
jsonSchemaDialect?: string | undefined;
|
|
40
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[] | undefined;
|
|
41
|
+
} & Pick<{
|
|
42
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
43
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
44
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
45
|
+
}, "components"> & Omit<Partial<{
|
|
46
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
47
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
48
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
49
|
+
}>, "components"> & Record<string, unknown>) | (import("openapi-types").OpenAPIV3.Document<{}> & Record<string, unknown>)>;
|
|
50
|
+
static getURL(url: string): Promise<any>;
|
|
51
|
+
static getFile(uri: string): Promise<any>;
|
|
52
|
+
static validate(json: OASDocument): Promise<import("openapi-types").OpenAPI.Document<{}>>;
|
|
53
|
+
}
|
package/dist/fetcher.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var node_fs_1 = __importDefault(require("node:fs"));
|
|
43
|
+
var node_path_1 = __importDefault(require("node:path"));
|
|
44
|
+
var openapi_parser_1 = __importDefault(require("@readme/openapi-parser"));
|
|
45
|
+
var js_yaml_1 = __importDefault(require("js-yaml"));
|
|
46
|
+
var Fetcher = /** @class */ (function () {
|
|
47
|
+
function Fetcher(uri) {
|
|
48
|
+
if (typeof uri === 'string') {
|
|
49
|
+
if (Fetcher.isAPIRegistryUUID(uri)) {
|
|
50
|
+
// Resolve OpenAPI definition shorthand accessors from within the ReadMe API Registry.
|
|
51
|
+
this.uri = uri.replace(Fetcher.registryUUIDRegex, 'https://dash.readme.com/api/v1/api-registry/$4');
|
|
52
|
+
}
|
|
53
|
+
else if (Fetcher.isGitHubBlobURL(uri)) {
|
|
54
|
+
/**
|
|
55
|
+
* People may try to use a public repository URL to the source viewer on GitHub not knowing
|
|
56
|
+
* that this page actually serves HTML. In this case we want to rewrite these to the "raw"
|
|
57
|
+
* version of this page that'll allow us to access the API definition.
|
|
58
|
+
*
|
|
59
|
+
* @example https://github.com/readmeio/oas-examples/blob/main/3.1/json/petstore.json
|
|
60
|
+
*/
|
|
61
|
+
this.uri = uri.replace(/\/\/github.com/, '//raw.githubusercontent.com').replace(/\/blob\//, '/');
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.uri = uri;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.uri = uri;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
Fetcher.isAPIRegistryUUID = function (uri) {
|
|
72
|
+
return Fetcher.registryUUIDRegex.test(uri);
|
|
73
|
+
};
|
|
74
|
+
Fetcher.isGitHubBlobURL = function (uri) {
|
|
75
|
+
return /\/\/github.com\/[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9]+\/blob\/(.*).(yaml|json|yml)/.test(uri);
|
|
76
|
+
};
|
|
77
|
+
Fetcher.getProjectPrefixFromRegistryUUID = function (uri) {
|
|
78
|
+
var _a;
|
|
79
|
+
var matches = uri.match(Fetcher.registryUUIDRegex);
|
|
80
|
+
if (!matches) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
return (_a = matches.groups) === null || _a === void 0 ? void 0 : _a.project;
|
|
84
|
+
};
|
|
85
|
+
Fetcher.prototype.load = function () {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
if (typeof this.uri !== 'string') {
|
|
89
|
+
throw new TypeError("Something disastrous occurred and a non-string URI was supplied to the Fetcher library. This shouldn't have happened!");
|
|
90
|
+
}
|
|
91
|
+
return [2 /*return*/, Promise.resolve(this.uri)
|
|
92
|
+
.then(function (uri) {
|
|
93
|
+
var url;
|
|
94
|
+
try {
|
|
95
|
+
url = new URL(uri);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
// If that try fails for whatever reason than the URI that we have isn't a real URL and
|
|
99
|
+
// we can safely attempt to look for it on the filesystem.
|
|
100
|
+
return Fetcher.getFile(uri);
|
|
101
|
+
}
|
|
102
|
+
return Fetcher.getURL(url.href);
|
|
103
|
+
})
|
|
104
|
+
.then(function (res) { return Fetcher.validate(res); })
|
|
105
|
+
.then(function (res) { return res; })];
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
Fetcher.getURL = function (url) {
|
|
110
|
+
// @todo maybe include our user-agent here to identify our request
|
|
111
|
+
return fetch(url).then(function (res) {
|
|
112
|
+
if (!res.ok) {
|
|
113
|
+
throw new Error("Unable to retrieve URL (".concat(url, "). Reason: ").concat(res.statusText));
|
|
114
|
+
}
|
|
115
|
+
if (res.headers.get('content-type') === 'application/yaml' || /\.(yaml|yml)/.test(url)) {
|
|
116
|
+
return res.text().then(function (text) {
|
|
117
|
+
return js_yaml_1.default.load(text);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return res.json();
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
Fetcher.getFile = function (uri) {
|
|
124
|
+
// Support relative paths by resolving them against the cwd.
|
|
125
|
+
var file = node_path_1.default.resolve(process.cwd(), uri);
|
|
126
|
+
if (!node_fs_1.default.existsSync(file)) {
|
|
127
|
+
throw new Error("Sorry, we were unable to load an API definition from ".concat(file, ". Please either supply a URL or a path on your filesystem."));
|
|
128
|
+
}
|
|
129
|
+
return Promise.resolve(node_fs_1.default.readFileSync(file, 'utf8')).then(function (res) {
|
|
130
|
+
if (/\.(yaml|yml)/.test(file)) {
|
|
131
|
+
return js_yaml_1.default.load(res);
|
|
132
|
+
}
|
|
133
|
+
return JSON.parse(res);
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
Fetcher.validate = function (json) {
|
|
137
|
+
if (json.swagger) {
|
|
138
|
+
throw new Error('Sorry, this module only supports OpenAPI definitions.');
|
|
139
|
+
}
|
|
140
|
+
// The `validate` method handles dereferencing for us.
|
|
141
|
+
return openapi_parser_1.default.validate(json, {
|
|
142
|
+
dereference: {
|
|
143
|
+
/**
|
|
144
|
+
* If circular `$refs` are ignored they'll remain in the API definition as `$ref: String`.
|
|
145
|
+
* This allows us to not only do easy circular reference detection but also stringify and
|
|
146
|
+
* save dereferenced API definitions back into the cache directory.
|
|
147
|
+
*/
|
|
148
|
+
circular: 'ignore',
|
|
149
|
+
},
|
|
150
|
+
}).catch(function (err) {
|
|
151
|
+
if (/is not a valid openapi definition/i.test(err.message)) {
|
|
152
|
+
throw new Error("Sorry, that doesn't look like a valid OpenAPI definition.");
|
|
153
|
+
}
|
|
154
|
+
throw err;
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* @example @petstore/v1.0#n6kvf10vakpemvplx
|
|
159
|
+
* @example @petstore#n6kvf10vakpemvplx
|
|
160
|
+
*/
|
|
161
|
+
Fetcher.registryUUIDRegex = /^@(?<project>[a-zA-Z0-9-_]+)(\/?(?<version>.+))?#(?<uuid>[a-z0-9]+)$/;
|
|
162
|
+
return Fetcher;
|
|
163
|
+
}());
|
|
164
|
+
exports.default = Fetcher;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import prompts from 'prompts';
|
|
2
|
+
/**
|
|
3
|
+
* The `prompts` library doesn't always interpret CTRL+C and release the terminal back to the user
|
|
4
|
+
* so we need handle this ourselves. This function is just a simple overload of the main `prompts`
|
|
5
|
+
* import that we use.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://github.com/terkelg/prompts/issues/252}
|
|
8
|
+
*/
|
|
9
|
+
export default function promptTerminal<T extends string = string>(question: prompts.PromptObject<T>, options?: prompts.Options): Promise<prompts.Answers<T>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var prompts_1 = __importDefault(require("prompts"));
|
|
54
|
+
/**
|
|
55
|
+
* The `prompts` library doesn't always interpret CTRL+C and release the terminal back to the user
|
|
56
|
+
* so we need handle this ourselves. This function is just a simple overload of the main `prompts`
|
|
57
|
+
* import that we use.
|
|
58
|
+
*
|
|
59
|
+
* @see {@link https://github.com/terkelg/prompts/issues/252}
|
|
60
|
+
*/
|
|
61
|
+
function promptTerminal(question, options) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
var enableTerminalCursor, onState;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
enableTerminalCursor = function () {
|
|
66
|
+
process.stdout.write('\x1B[?25h');
|
|
67
|
+
};
|
|
68
|
+
onState = function (state) {
|
|
69
|
+
if (state.aborted) {
|
|
70
|
+
// If we don't re-enable the terminal cursor before exiting the program, the cursor will
|
|
71
|
+
// remain hidden.
|
|
72
|
+
enableTerminalCursor();
|
|
73
|
+
process.stdout.write('\n');
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return [2 /*return*/, (0, prompts_1.default)(__assign(__assign({}, question), { onState: onState }), options)];
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
exports.default = promptTerminal;
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function logger(log: string, error?: boolean): void;
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable no-console */
|
|
7
|
+
var chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
function logger(log, error) {
|
|
9
|
+
if (error) {
|
|
10
|
+
console.error(chalk_1.default.red(log));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
console.log(log);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.default = logger;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PACKAGE_VERSION = exports.PACKAGE_NAME = void 0;
|
|
4
|
+
// This file is automatically updated by the build script.
|
|
5
|
+
exports.PACKAGE_NAME = 'api';
|
|
6
|
+
exports.PACKAGE_VERSION = '7.0.0-alpha.1';
|