Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): update rollups http interface #35

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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