api-def 0.12.0-alpha.5 → 0.12.0-alpha.7
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/index.js +30 -14
- package/cjs/Endpoint.js +3 -2
- package/esm/Endpoint.js +3 -2
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -258551,16 +258551,14 @@ var METHOD_COLORS = {
|
|
|
258551
258551
|
var openApiToSourceCode = async (options) => {
|
|
258552
258552
|
const { openApiPath } = options;
|
|
258553
258553
|
const inContents = fs.readFileSync(openApiPath, "utf-8");
|
|
258554
|
-
const ast = await openapiTS(inContents, {
|
|
258555
|
-
rootTypes: true
|
|
258556
|
-
});
|
|
258554
|
+
const ast = await openapiTS(inContents, {});
|
|
258557
258555
|
const bundleResults = await (0, import_openapi_core3.bundle)({
|
|
258558
258556
|
ref: openApiPath,
|
|
258559
258557
|
config: await (0, import_openapi_core3.createConfig)({})
|
|
258560
258558
|
});
|
|
258561
258559
|
const routes = bundleResults.bundle.parsed.paths;
|
|
258562
258560
|
const server2 = bundleResults.bundle.parsed.servers[0];
|
|
258563
|
-
|
|
258561
|
+
const extraTypes = {};
|
|
258564
258562
|
const source = `import { Api } from "api-def";
|
|
258565
258563
|
|
|
258566
258564
|
const API = new Api({
|
|
@@ -258585,14 +258583,18 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258585
258583
|
responseType = mediaType.split(";")[0];
|
|
258586
258584
|
}
|
|
258587
258585
|
if (schema?.$ref) {
|
|
258588
|
-
|
|
258586
|
+
const name = schema.$ref.split("/").pop();
|
|
258587
|
+
extraTypes[`Response${name}`] = `components["schemas"]["${name}"]`;
|
|
258588
|
+
responseTypes.push(`Response${name}`);
|
|
258589
258589
|
}
|
|
258590
258590
|
}
|
|
258591
258591
|
}
|
|
258592
258592
|
}
|
|
258593
258593
|
const bodyTypes = [];
|
|
258594
258594
|
if (methodDef.requestBody?.$ref) {
|
|
258595
|
-
|
|
258595
|
+
const name = methodDef.requestBody.$ref.split("/").pop();
|
|
258596
|
+
extraTypes[`Body${name}`] = `components["schemas"]["${name}"]`;
|
|
258597
|
+
bodyTypes.push(`Body${name}`);
|
|
258596
258598
|
}
|
|
258597
258599
|
const queryTypes = [];
|
|
258598
258600
|
if (methodDef.parameters?.length) {
|
|
@@ -258608,8 +258610,7 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258608
258610
|
return false;
|
|
258609
258611
|
});
|
|
258610
258612
|
if (anyQueryParams) {
|
|
258611
|
-
extraTypes
|
|
258612
|
-
`;
|
|
258613
|
+
extraTypes[`Query${(0, import_lodash.upperFirst)(id)}`] = `operations["${id}"]["parameters"]["query"]`;
|
|
258613
258614
|
queryTypes.push(`Query${(0, import_lodash.upperFirst)(id)}`);
|
|
258614
258615
|
}
|
|
258615
258616
|
}
|
|
@@ -258627,13 +258628,28 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258627
258628
|
return false;
|
|
258628
258629
|
});
|
|
258629
258630
|
if (anyHeaderParams) {
|
|
258630
|
-
extraTypes
|
|
258631
|
-
|
|
258632
|
-
requestHeaderTypes.push(`Header${(0, import_lodash.upperFirst)(id)}`);
|
|
258631
|
+
extraTypes[`Headers${(0, import_lodash.upperFirst)(id)}`] = `operations["${id}"]["parameters"]["header"]`;
|
|
258632
|
+
requestHeaderTypes.push(`Headers${(0, import_lodash.upperFirst)(id)}`);
|
|
258633
258633
|
}
|
|
258634
258634
|
}
|
|
258635
258635
|
const responseHeaderTypes = [];
|
|
258636
|
+
let pathParams = [];
|
|
258637
|
+
if (methodDef.parameters?.length) {
|
|
258638
|
+
pathParams = methodDef.parameters.reduce((pathParams2, param) => {
|
|
258639
|
+
if (param.in === "path") {
|
|
258640
|
+
pathParams2.push(param.name);
|
|
258641
|
+
} else if (param.$ref) {
|
|
258642
|
+
const ref = param.$ref;
|
|
258643
|
+
const paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
258644
|
+
if (paramDef.in === "path") {
|
|
258645
|
+
pathParams2.push(paramDef.name);
|
|
258646
|
+
}
|
|
258647
|
+
}
|
|
258648
|
+
return pathParams2;
|
|
258649
|
+
}, []);
|
|
258650
|
+
}
|
|
258636
258651
|
const endpointParts = [
|
|
258652
|
+
pathParams.length > 0 ? `.paramsOf<"${pathParams.join("|")}">()` : "",
|
|
258637
258653
|
responseTypes.length > 0 ? `.responseOf<${responseTypes.join("|")}>()` : "",
|
|
258638
258654
|
bodyTypes.length > 0 ? `.bodyOf<${bodyTypes.join("|")}>()` : "",
|
|
258639
258655
|
queryTypes.length > 0 ? `.queryOf<${queryTypes.join("|")}>()` : "",
|
|
@@ -258650,13 +258666,13 @@ ${endpointParts.filter(Boolean).map((part) => ` ${part}`).join("\n")}
|
|
|
258650
258666
|
});
|
|
258651
258667
|
}).join("\n\n")}
|
|
258652
258668
|
|
|
258653
|
-
export default API
|
|
258654
|
-
`;
|
|
258669
|
+
export default API;`;
|
|
258655
258670
|
const types = astToString(ast);
|
|
258671
|
+
const extraTypesSource = Object.keys(extraTypes).sort().map((key) => `export type ${key} = ${extraTypes[key]};`).join("\n");
|
|
258656
258672
|
return `// Type Defs
|
|
258657
258673
|
|
|
258658
258674
|
${types}
|
|
258659
|
-
${
|
|
258675
|
+
${extraTypesSource}
|
|
258660
258676
|
|
|
258661
258677
|
//API Def
|
|
258662
258678
|
|
package/cjs/Endpoint.js
CHANGED
|
@@ -143,10 +143,11 @@ var Endpoint = /** @class */ (function () {
|
|
|
143
143
|
var keys = Object.keys(request.params);
|
|
144
144
|
for (var i = 0; i < keys.length; i++) {
|
|
145
145
|
var argName = keys[i];
|
|
146
|
-
|
|
146
|
+
var argValue = request.params[argName];
|
|
147
|
+
computedPath = computedPath.replace(":".concat(argName), argValue).replace("{".concat(argName, "}"), argValue);
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
|
-
if (computedPath.includes(":")) {
|
|
150
|
+
if (computedPath.includes(":") || computedPath.includes("{")) {
|
|
150
151
|
throw new Error("[api-def] Not all path params have been resolved: '".concat(computedPath, "'"));
|
|
151
152
|
}
|
|
152
153
|
return computedPath;
|
package/esm/Endpoint.js
CHANGED
|
@@ -64,10 +64,11 @@ export default class Endpoint {
|
|
|
64
64
|
const keys = Object.keys(request.params);
|
|
65
65
|
for (let i = 0; i < keys.length; i++) {
|
|
66
66
|
const argName = keys[i];
|
|
67
|
-
|
|
67
|
+
const argValue = request.params[argName];
|
|
68
|
+
computedPath = computedPath.replace(`:${argName}`, argValue).replace(`{${argName}}`, argValue);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
|
-
if (computedPath.includes(":")) {
|
|
71
|
+
if (computedPath.includes(":") || computedPath.includes("{")) {
|
|
71
72
|
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
72
73
|
}
|
|
73
74
|
return computedPath;
|