@crumbjs/core
Version:
<img src="https://raw.githubusercontent.com/tuplescompany/crumbjs/refs/heads/main/logo/crumbjs.png" alt="CrumbJS Logo" width="200"/> - The tasty way to build fast apis.
28 lines (27 loc) • 1.38 kB
JavaScript
import openapiTS, { astToString } from 'openapi-typescript';
import ts from 'typescript';
const DATE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Date')); // `Date`
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull()); // `null`
const BLOB = ts.factory.createTypeReferenceNode('Blob');
const union = (...types) => ts.factory.createUnionTypeNode(types);
const arr = (t) => ts.factory.createArrayTypeNode(t);
export async function createClientSpecs(jsonSpec) {
const ast = await openapiTS(jsonSpec, {
transform(schemaObject, metadata) {
if (schemaObject.format === 'date-time') {
return schemaObject.nullable ? union(DATE, NULL) : DATE;
}
// string(binary) -> FileLike (| null)
if ((schemaObject.type === 'string' && schemaObject.format === 'binary') || schemaObject.format === 'byte') {
return schemaObject.nullable ? union(BLOB, NULL) : BLOB;
}
// array of binaries -> FileLike[] (| null)
if (schemaObject.type === 'array' && schemaObject.items && schemaObject.items.format === 'binary') {
const t = arr(BLOB);
return schemaObject.nullable ? union(t, NULL) : t;
}
},
});
const contents = astToString(ast);
await Bun.write('./client.d.ts', contents);
}