api-def 0.12.0-alpha.1 → 0.12.0-alpha.3
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/bin/OpenApiToSourceCode.js +174 -0
- package/bin/index.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
45
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
46
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
47
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
48
|
+
function step(op) {
|
|
49
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
50
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
51
|
+
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;
|
|
52
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
53
|
+
switch (op[0]) {
|
|
54
|
+
case 0: case 1: t = op; break;
|
|
55
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
56
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
57
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
58
|
+
default:
|
|
59
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
60
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
61
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
62
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
63
|
+
if (t[2]) _.ops.pop();
|
|
64
|
+
_.trys.pop(); continue;
|
|
65
|
+
}
|
|
66
|
+
op = body.call(thisArg, _);
|
|
67
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
68
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
72
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
73
|
+
};
|
|
74
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
+
exports.openApiToSourceCode = void 0;
|
|
76
|
+
var fs = __importStar(require("node:fs"));
|
|
77
|
+
var openapi_core_1 = require("@redocly/openapi-core");
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
var chalk_1 = __importDefault(require("chalk"));
|
|
80
|
+
var lodash_1 = require("lodash");
|
|
81
|
+
var openapi_typescript_1 = __importStar(require("openapi-typescript"));
|
|
82
|
+
var METHOD_COLORS = {
|
|
83
|
+
get: chalk_1.default.green,
|
|
84
|
+
post: chalk_1.default.blue,
|
|
85
|
+
put: chalk_1.default.yellow,
|
|
86
|
+
delete: chalk_1.default.red,
|
|
87
|
+
patch: chalk_1.default.magenta,
|
|
88
|
+
};
|
|
89
|
+
var openApiToSourceCode = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
90
|
+
var openApiPath, inContents, ast, bundleResults, _a, routes, server, extraTypes, source, types;
|
|
91
|
+
var _b;
|
|
92
|
+
return __generator(this, function (_c) {
|
|
93
|
+
switch (_c.label) {
|
|
94
|
+
case 0:
|
|
95
|
+
openApiPath = options.openApiPath;
|
|
96
|
+
inContents = fs.readFileSync(openApiPath, "utf-8");
|
|
97
|
+
return [4 /*yield*/, (0, openapi_typescript_1.default)(inContents, {
|
|
98
|
+
rootTypes: true,
|
|
99
|
+
})];
|
|
100
|
+
case 1:
|
|
101
|
+
ast = _c.sent();
|
|
102
|
+
_a = openapi_core_1.bundle;
|
|
103
|
+
_b = {
|
|
104
|
+
ref: openApiPath
|
|
105
|
+
};
|
|
106
|
+
return [4 /*yield*/, (0, openapi_core_1.createConfig)({})];
|
|
107
|
+
case 2: return [4 /*yield*/, _a.apply(void 0, [(_b.config = _c.sent(),
|
|
108
|
+
_b)])];
|
|
109
|
+
case 3:
|
|
110
|
+
bundleResults = _c.sent();
|
|
111
|
+
routes = bundleResults.bundle.parsed.paths;
|
|
112
|
+
server = bundleResults.bundle.parsed.servers[0];
|
|
113
|
+
extraTypes = "";
|
|
114
|
+
source = "import { Api } from \"api-def\";\n\nconst API = new Api({\n name: \"".concat(bundleResults.bundle.parsed.info.title || "Generate Api", "\",\n baseUrl: \"").concat(server.url, "\",\n});\n\n").concat(Object.entries(routes)
|
|
115
|
+
.flatMap(function (_a) {
|
|
116
|
+
var path = _a[0], route = _a[1];
|
|
117
|
+
return Object.entries(route).map(function (_a) {
|
|
118
|
+
var _b, _c;
|
|
119
|
+
var method = _a[0], methodDef = _a[1];
|
|
120
|
+
var id = methodDef.operationId;
|
|
121
|
+
var responseStatuses = Object.keys(methodDef.responses);
|
|
122
|
+
var successfulResponse = responseStatuses.filter(function (status) { return status.startsWith("2") || status.startsWith("3"); });
|
|
123
|
+
var responseTypes = [];
|
|
124
|
+
for (var _i = 0, successfulResponse_1 = successfulResponse; _i < successfulResponse_1.length; _i++) {
|
|
125
|
+
var status_1 = successfulResponse_1[_i];
|
|
126
|
+
var response = methodDef.responses[status_1];
|
|
127
|
+
if (response.$ref) {
|
|
128
|
+
responseTypes.push("Response".concat(response.$ref.split("/").pop()));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
var bodyTypes = [];
|
|
132
|
+
if ((_b = methodDef.requestBody) === null || _b === void 0 ? void 0 : _b.$ref) {
|
|
133
|
+
bodyTypes.push("RequestBody".concat(methodDef.requestBody.$ref.split("/").pop()));
|
|
134
|
+
}
|
|
135
|
+
var queryTypes = [];
|
|
136
|
+
if ((_c = methodDef.parameters) === null || _c === void 0 ? void 0 : _c.length) {
|
|
137
|
+
var anyQueryParams = methodDef.parameters.some(function (param) {
|
|
138
|
+
if (param.in === "query") {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (param.$ref) {
|
|
142
|
+
var ref = param.$ref;
|
|
143
|
+
var paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
144
|
+
return paramDef.in === "query";
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
});
|
|
148
|
+
if (anyQueryParams) {
|
|
149
|
+
extraTypes += "export type Query".concat((0, lodash_1.upperFirst)(id), " = operations[\"").concat(id, "\"][\"parameters\"][\"query\"];\n");
|
|
150
|
+
queryTypes.push("Query".concat((0, lodash_1.upperFirst)(id)));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/*
|
|
154
|
+
const methodColor = METHOD_COLORS[method] || chalk.gray;
|
|
155
|
+
console.log(`Generating ${methodColor(method.toUpperCase())} '${id}'`);
|
|
156
|
+
*/
|
|
157
|
+
var endpointParts = [
|
|
158
|
+
responseTypes.length > 0 ? ".response<".concat(responseTypes.join("|"), ">()") : "",
|
|
159
|
+
bodyTypes.length > 0 ? ".body<".concat(bodyTypes.join("|"), ">()") : "",
|
|
160
|
+
queryTypes.length > 0 ? ".query<".concat(queryTypes.join("|"), ">()") : "",
|
|
161
|
+
];
|
|
162
|
+
return "export const ".concat(id, " = API.endpoint()\n").concat(endpointParts
|
|
163
|
+
.filter(Boolean)
|
|
164
|
+
.map(function (part) { return " ".concat(part); })
|
|
165
|
+
.join("\n"), "\n .build({\n method: \"").concat(method, "\",\n path: \"").concat(path, "\",\n id: \"").concat(id, "\",\n });");
|
|
166
|
+
});
|
|
167
|
+
})
|
|
168
|
+
.join("\n\n"), "\n\nexport default API;\n ");
|
|
169
|
+
types = (0, openapi_typescript_1.astToString)(ast);
|
|
170
|
+
return [2 /*return*/, "// Type Defs\n\n".concat(types, "\n").concat(extraTypes, "\n\n//API Def\n\n").concat(source)];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}); };
|
|
174
|
+
exports.openApiToSourceCode = openApiToSourceCode;
|
package/bin/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-def",
|
|
3
|
-
"version": "0.12.0-alpha.
|
|
3
|
+
"version": "0.12.0-alpha.3",
|
|
4
4
|
"description": "Typed API definitions with middleware support",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"types": "esm/index.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"keywords": ["typescript", "javascript", "node", "web", "api", "typed", "cache", "fetch", "retry", "middleware"],
|
|
27
27
|
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
28
28
|
"license": "MIT",
|
|
29
|
-
"files": ["LICENSE", "README.md", "esm/", "cjs/"],
|
|
29
|
+
"files": ["LICENSE", "README.md", "esm/", "cjs/", "bin/"],
|
|
30
30
|
"jest": {
|
|
31
31
|
"transform": {
|
|
32
32
|
"^.+\\.(t|j)sx?$": [
|