generated from acdh-oeaw/template-app-nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbdfa44
commit 3a8fde4
Showing
5 changed files
with
76 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,63 @@ | ||
import { load } from "js-yaml"; | ||
import { createUrl, createUrlSearchParams, log, request } from "@acdh-oeaw/lib"; | ||
import { generateZodClientFromOpenAPI } from "openapi-zod-client"; | ||
import type { OpenAPIObject, PathsObject } from "openapi3-ts/oas30"; | ||
import { z } from "zod"; | ||
|
||
console.log("Starting zod client generation"); | ||
const options = { | ||
url: "https://sicprod.acdh-dev.oeaw.ac.at/apis/swagger/schema/?format=json", | ||
prefixes: [ | ||
"/apis/api/apis_ontology.event", | ||
"/apis/api/apis_ontology.function", | ||
"/apis/api/apis_ontology.institution", | ||
"/apis/api/apis_ontology.person", | ||
"/apis/api/apis_ontology.place", | ||
"/apis/api/apis_ontology.salary", | ||
], | ||
dist: "./lib/api.ts", | ||
}; | ||
const outputPath = "./lib/api.ts"; | ||
async function main() { | ||
log.info("Starting zod client generation"); | ||
|
||
const apisBaseURL = z.string().url().parse(process.env.NUXT_PUBLIC_API_BASE_URL); | ||
|
||
const options = { | ||
url: createUrl({ | ||
baseUrl: apisBaseURL, | ||
pathname: "/apis/swagger/schema", | ||
searchParams: createUrlSearchParams({ format: "json" }), | ||
}), | ||
prefixes: [ | ||
"/apis/api/apis_ontology.event", | ||
"/apis/api/apis_ontology.function", | ||
"/apis/api/apis_ontology.institution", | ||
"/apis/api/apis_ontology.person", | ||
"/apis/api/apis_ontology.place", | ||
"/apis/api/apis_ontology.salary", | ||
], | ||
dist: outputPath, | ||
}; | ||
|
||
let data: string; | ||
if (options.url) { | ||
// download swagger file | ||
const response = await fetch(options.url); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Received a non-200 response when downloading from ${options.url}. Received ${response.statusText}. Please double check your setup.`, | ||
); | ||
} | ||
data = await response.text(); | ||
} else { | ||
throw new Error(`Found neither an input URL or an input file!`); | ||
} | ||
const data = await request(options.url, { responseType: "json" }); | ||
|
||
// trim swagger file to only contain the prefixes specified in options.prefixes | ||
const prefixes = options.prefixes; | ||
let openApiDoc: OpenAPIObject = load(data) as OpenAPIObject; | ||
const paths: PathsObject = {}; | ||
for (const path of Object.keys(openApiDoc.paths)) { | ||
if (prefixes.some((retain) => path.startsWith(retain))) { | ||
paths[path] = openApiDoc.paths[path] ?? {}; | ||
// trim swagger file to only contain the prefixes specified in options.prefixes | ||
const prefixes = options.prefixes; | ||
let openApiDoc: OpenAPIObject = data as OpenAPIObject; | ||
const paths: PathsObject = {}; | ||
for (const [key, value] of Object.entries(openApiDoc.paths)) { | ||
if (prefixes.some((retain) => key.startsWith(retain))) { | ||
paths[key] = value; | ||
} | ||
} | ||
} | ||
openApiDoc = { | ||
...openApiDoc, | ||
paths, | ||
}; | ||
openApiDoc = { | ||
...openApiDoc, | ||
paths, | ||
}; | ||
|
||
// use the trimmed openAPIDoc with openapi-zod-client | ||
await generateZodClientFromOpenAPI({ | ||
openApiDoc, | ||
distPath: options.dist, | ||
options: { | ||
shouldExportAllTypes: true, | ||
withAlias: true, | ||
}, | ||
}); | ||
// use the trimmed openAPIDoc with openapi-zod-client | ||
await generateZodClientFromOpenAPI({ | ||
openApiDoc, | ||
distPath: options.dist, | ||
options: { | ||
shouldExportAllTypes: true, | ||
withAlias: true, | ||
}, | ||
}); | ||
} | ||
|
||
console.log("Client creation completed"); | ||
console.log("Output was written to: ", options.dist); | ||
main() | ||
.then(() => { | ||
log.success("Client creation completed. \nOutput was written to: ", outputPath); | ||
}) | ||
.catch((err) => { | ||
log.error("Client creation failed", String(err)); | ||
}); |