Skip to content

Commit

Permalink
feat: add open-api codegen script and types
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed Feb 20, 2024
1 parent 2c8160f commit 759a476
Show file tree
Hide file tree
Showing 4 changed files with 1,004 additions and 1 deletion.
47 changes: 47 additions & 0 deletions scripts/codegen.open-api.sh
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

###############################################################################
Loading

0 comments on commit 759a476

Please sign in to comment.