-
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.
feat: add open-api codegen script and types
- Loading branch information
1 parent
2c8160f
commit 759a476
Showing
4 changed files
with
1,004 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env bash | ||
#################################################################################### | ||
# This script downloads the Fixit OpenAPI Schema and uses it to generate types. | ||
#################################################################################### | ||
|
||
# Download the latest version of the Fixit OpenAPI Schema | ||
npx swaggerhub api:get Nerdware/Fixit > open-api.yaml | ||
|
||
# Update OpenAPI types using NodeJS API from openapi-typescript to fix `Date` types. | ||
# (Their CLI does not convert `format: date-time` values to `Date` types) | ||
|
||
node --input-type=module -e " | ||
import fs from 'node:fs'; | ||
import ts from 'typescript'; | ||
import openapiTS, { astToString } from 'openapi-typescript'; | ||
const DATE = ts.factory.createIdentifier('Date'); | ||
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull()); | ||
const ast = await openapiTS( | ||
new URL('file://$PWD/open-api.yaml'), | ||
{ | ||
transform(schemaObject, metadata) { | ||
if (schemaObject.format === 'date-time') { | ||
return Array.isArray(schemaObject.type) && schemaObject.type.includes('null') | ||
? ts.factory.createUnionTypeNode([DATE, NULL]) | ||
: DATE; | ||
} | ||
}, | ||
} | ||
); | ||
const tsFileContents = \`\ | ||
/** | ||
* This file was auto-generated by openapi-typescript. | ||
* Do not make direct changes to the file. | ||
*/ | ||
\${astToString(ast)} | ||
\`; | ||
fs.writeFileSync('src/types/__codegen__/open-api.ts', tsFileContents);" | ||
|
||
# Finally, delete the open-api.yaml file | ||
rm open-api.yaml | ||
|
||
############################################################################### |
Oops, something went wrong.