Setup Namespaces

This commit is contained in:
Steve Faulkner
2020-07-22 22:40:29 -05:00
parent 769a2e7d1c
commit 9db8d11801
2 changed files with 1531 additions and 1596 deletions

1330
models.ts

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,12 @@
/// <reference types="node" /> /// <reference types="node" />
const { writeFileSync } = require("fs"); const { writeFileSync } = require("fs");
const schema = require("./schema.json"); const schema = require("./schema.json");
const file: string[] = [""]; const file: string[] = [""];
const namespaces: { [key: string]: string[] } = {};
const propertyMap: { [key: string]: string } = { const propertyMap: { [key: string]: string } = {
integer: "number" integer: "number"
}; };
@@ -16,6 +19,14 @@ function refToType(path: string | undefined) {
return "unknown"; return "unknown";
} }
function camelize(str: string) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word: any, index: any) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, "");
}
function bodyParam(parameter: any) { function bodyParam(parameter: any) {
if (!parameter) { if (!parameter) {
return ""; return "";
@@ -131,9 +142,13 @@ async function main() {
const bodyParameter = operation.parameters.find( const bodyParameter = operation.parameters.find(
(parameter: any) => parameter.in === "body" && parameter.required === true (parameter: any) => parameter.in === "body" && parameter.required === true
); );
file.push(` const [namespace, operationName] = operation.operationId.split("_");
if (namespaces[namespace] === undefined) {
namespaces[namespace] = [];
}
namespaces[namespace].push(`
/* ${operation.description} */ /* ${operation.description} */
export async function ${operation.operationId} ( async ${camelize(operationName)} (
${parametersFromPath(path)} ${parametersFromPath(path)}
${bodyParam(bodyParameter)} ${bodyParam(bodyParameter)}
) : Promise<${responseType(operation)}> { ) : Promise<${responseType(operation)}> {
@@ -145,6 +160,12 @@ async function main() {
} }
} }
for (const namespace in namespaces) {
file.push(`export const ${namespace} = {`);
file.push(namespaces[namespace].join(",\n"));
file.push(`}\n`);
}
writeFileSync("./models.ts", file.join("")); writeFileSync("./models.ts", file.join(""));
} }