Skip to content

Commit

Permalink
feat(typescript): update rollups http interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed Oct 24, 2024
1 parent bc99c1c commit cfdf7ed
Show file tree
Hide file tree
Showing 3 changed files with 899 additions and 781 deletions.
16 changes: 8 additions & 8 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "0.1.0",
"description": "TypeScript DApp",
"dependencies": {
"openapi-fetch": "^0.7",
"viem": "^1"
"openapi-fetch": "^0.12.2",
"viem": "^2.21.34"
},
"devDependencies": {
"@types/node": "^20",
"esbuild": "^0.19",
"@types/node": "^22.7.9",
"esbuild": "^0.24.0",
"npm-run-all": "^4",
"openapi-typescript": "^6",
"ts-node": "^10",
"typescript": "^5",
"vitest": "^0.34"
"openapi-typescript": "^7.4.1",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"vitest": "^2.1.3"
},
"scripts": {
"build": "run-s codegen compile",
Expand Down
27 changes: 20 additions & 7 deletions typescript/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from "fs";
import openapiTS from "openapi-typescript";
import openapiTS, { astToString } from "openapi-typescript";
import ts from "typescript";
const ADDRESS = ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Address")
);
const HEX = ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Hex")
);
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());

/*
This code customizes the TypeScript schema generation using openapi-typescript
Expand All @@ -8,22 +16,27 @@ viem types Hex and Address instead of simple strings for some schema properties.
*/

const inputFile =
"https://raw.githubusercontent.com/cartesi/openapi-interfaces/fce8cc7fcf2d2fcc1940e048cd16fb8550b09779/rollup.yaml";
"https://raw.githubusercontent.com/cartesi/openapi-interfaces/refs/tags/v0.9.0/rollup.yaml";
const outputFile = "src/schema.d.ts";

// import types from viem in generated code
const inject = "import { Address, Hex } from 'viem';\n";

console.log(`${inputFile} -> ${outputFile}`);
openapiTS(inputFile, {
inject,
transform: (schemaObject, _options) => {
transform: (schemaObject, _metadata) => {
if ("format" in schemaObject && schemaObject.format === "hex") {
// use viem.Hex if format is hex
return schemaObject.nullable ? "Hex | null" : "Hex";
return schemaObject.nullable
? ts.factory.createUnionTypeNode([HEX, NULL])
: HEX;
} else if ("format" in schemaObject && schemaObject.format === "address") {
// use viem.Address if format is address
return schemaObject.nullable ? "Address | null" : "Address";
return schemaObject.nullable
? ts.factory.createUnionTypeNode([ADDRESS, NULL])
: ADDRESS;
}
},
}).then((output) => fs.writeFileSync(outputFile, output));
}).then((output) =>
fs.writeFileSync(outputFile, `${inject}${astToString(output)}`)
);
Loading

0 comments on commit cfdf7ed

Please sign in to comment.