Skip to content

Commit

Permalink
Refactoring and cleanup with Stefan
Browse files Browse the repository at this point in the history
  • Loading branch information
katharinawuensche committed Jul 3, 2024
1 parent dbdfa44 commit 3a8fde4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 108 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ ARG NUXT_PUBLIC_MATOMO_BASE_URL
ARG NUXT_PUBLIC_MATOMO_ID
ARG NUXT_PUBLIC_REDMINE_ID
ARG NUXT_PUBLIC_API_BASE_URL
ARG NUXT_PUBLIC_API_SPEC_URL

RUN pnpm install --frozen-lockfile --offline

Expand Down
3 changes: 1 addition & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ export default defineNuxtConfig({
runtimeConfig: {
NODE_ENV: process.env.NODE_ENV,
public: {
NUXT_PUBLIC_APP_BASE_URL: process.env.NUXT_PUBLIC_APP_BASE_URL,
NUXT_PUBLIC_API_BASE_URL: process.env.NUXT_PUBLIC_API_BASE_URL,
NUXT_PUBLIC_API_SPEC_URL: process.env.NUXT_PUBLIC_API_SPEC_URL,
NUXT_PUBLIC_APP_BASE_URL: process.env.NUXT_PUBLIC_APP_BASE_URL,
NUXT_PUBLIC_BOTS: process.env.NUXT_PUBLIC_BOTS,
NUXT_PUBLIC_MATOMO_BASE_URL: process.env.NUXT_PUBLIC_MATOMO_BASE_URL,
NUXT_PUBLIC_MATOMO_ID: process.env.NUXT_PUBLIC_MATOMO_ID,
Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test:e2e:ui": "playwright test --ui",
"types:check": "nuxt typecheck",
"validate": "run-p format:check lint:check types:check test test:e2e",
"createclient": "npx tsx ./scripts/generate-api-client.ts"
"createclient": "dotenv -c -- tsx ./scripts/generate-api-client.ts"
},
"dependencies": {
"@acdh-oeaw/lib": "^0.1.7",
Expand All @@ -55,10 +55,8 @@
"lodash": "^4.17.21",
"lucide-vue-next": "^0.356.0",
"maplibre-gl": "^4.1.3",
"mkdirp": "^3.0.1",
"openapi-zod-client": "^1.16.2",
"pino-http": "^9.0.0",
"rimraf": "^5.0.7",
"satori": "^0.10.13",
"vue": "^3.4.21",
"vue-i18n": "^9.10.1",
Expand All @@ -79,22 +77,20 @@
"@playwright/test": "^1.42.1",
"@tailwindcss/typography": "^0.5.12",
"@types/d3": "^7.4.3",
"@types/js-yaml": "^4.0.9",
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.26",
"axe-core": "^4.8.4",
"axe-playwright": "^2.0.1",
"ci-info": "^4.0.0",
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.2",
"dotenv-expand": "^11.0.6",
"eslint": "^8.57.0",
"eslint-plugin-tailwindcss": "^3.15.1",
"is-ci": "^3.0.1",
"js-yaml": "^4.1.0",
"lint-staged": "^15.2.2",
"npm-run-all2": "^6.1.2",
"nuxt": "^3.10.3",
"openapi-endpoint-trimmer": "^2.0.0",
"openapi3-ts": "^4.3.3",
"postcss": "^8.4.35",
"prettier": "^3.2.5",
Expand Down
69 changes: 19 additions & 50 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 54 additions & 49 deletions scripts/generate-api-client.ts
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));
});

0 comments on commit 3a8fde4

Please sign in to comment.