Skip to content

Commit

Permalink
TypeScript OpenAPI code generation (#24)
Browse files Browse the repository at this point in the history
* add openapi typescript definitions (partly 3rd party)

* add simple typescript generation script

* fix deprecation warning

* Update README.md

* remove commented code blocks
  • Loading branch information
lkleuver committed Apr 26, 2024
1 parent bbc9a27 commit 482d3c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
21 changes: 21 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,24 @@ The application uses the following dependencies:
- `prettier`: Opinionated code formatter
- `eslint`: Linter


### Scripts

#### gen_openapi_types

Generate Typescript types from `/backend/openapi.json`.

```sh
npm run gen:openapi
```
This results in `/frontend/lib/api/gen/openapi.ts`

#### gen_icons

Generate React components from icons located in `/frontend/lib/ui/svg`

```sh
npm run gen:icons
```
This results in `/frontend/lib/icon/gen.tsx`

2 changes: 1 addition & 1 deletion frontend/lib/api/gen/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface POLLING_STATION_DATA_ENTRY_REQUEST_PARAMS {
id: number;
entry_number: number;
}
export type POLLING_STATION_DATA_ENTRY_REQUEST_PATH = `/api/polling_stations/${number}/data_entries/${number}`;
export type POLLING_STATION_DATA_ENTRY_REQUEST_PATH = `/api/polling_stations/${number}/data_entries/${number};`;
export type POLLING_STATION_DATA_ENTRY_REQUEST_BODY = DataEntryRequest;

/** TYPES **/
Expand Down
10 changes: 2 additions & 8 deletions frontend/scripts/gen_openapi_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function addPath(path: string, v: PathsObject | undefined) {
});
}
result.push("}");
result.push(`export type ${id}_REQUEST_PATH = \`${requestPath}\`;`);
result.push(`export type ${id}_REQUEST_PATH = \`${requestPath};\``);

if (post.requestBody) {
if ("$ref" in post.requestBody) {
Expand All @@ -89,8 +89,6 @@ function addPath(path: string, v: PathsObject | undefined) {
);
}
}
// result.push(`export interface ${id}_REQUEST_BODY {`);
// result.push('}');
}
}
}
Expand Down Expand Up @@ -120,10 +118,6 @@ function addDefinition(name: string, v: ReferenceObject | SchemaObject) {
result.push("}");

return result.join("\n");

// props.forEach(([k, v2]: [string, any]) => {
// ar.push(` ${k}${isRequired(k, v.required)}: ${tsType(v2.type)};`);
// });
}

function tsType(s: ReferenceObject | SchemaObject | undefined): string {
Expand All @@ -141,7 +135,7 @@ function tsType(s: ReferenceObject | SchemaObject | undefined): string {
case "number":
return "number";
default:
//TODO: catch all types, any is not allowd
//TODO: catch all types, any is not allowed
return "any";
}
}
Expand Down

0 comments on commit 482d3c2

Please sign in to comment.