Skip to content

Commit 768f83d

Browse files
committed
Fix type export & lint
1 parent 20ee39a commit 768f83d

35 files changed

Lines changed: 525 additions & 676 deletions

.github/workflows/docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- main
77
tags:
8-
- "v*"
8+
- 'v*'
99
pull_request:
1010
branches:
1111
- main

.github/workflows/npm-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Publish to npm
33
on:
44
push:
55
tags:
6-
- "v*"
6+
- 'v*'
77

88
permissions:
99
id-token: write # Required for OIDC
@@ -31,8 +31,8 @@ jobs:
3131
- name: Setup Node.js
3232
uses: actions/setup-node@v4
3333
with:
34-
node-version: "20"
35-
registry-url: "https://registry.npmjs.org/"
34+
node-version: '20'
35+
registry-url: 'https://registry.npmjs.org/'
3636

3737
# Ensure npm 11.5.1 or later for trusted publishing
3838
# https://github.com/orgs/community/discussions/173102#discussioncomment-14367428

.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"singleQuote": true,
5+
"trailingComma": "none",
6+
"endOfLine": "lf",
7+
"arrowParens": "always",
8+
"printWidth": 120
9+
}

CLAUDE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Default to using Bun instead of Node.js.
32

43
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The `db.ts` service can be swapped between implementations:
136136
**Current**: Bun native Postgres
137137

138138
```typescript
139-
import { sql } from "bun";
139+
import { sql } from 'bun';
140140
```
141141

142142
**Future**: Postgres Gateway (HTTP-based)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"start": "bun src/index.ts",
1111
"compile": "bun build --compile --minify --sourcemap server.ts --outfile dist/openworkers-api",
1212
"test": "bun test",
13+
"lint": "prettier --write .",
1314
"generate-types": "bun scripts/generate-types.ts"
1415
},
1516
"dependencies": {

scripts/generate-types.ts

Lines changed: 61 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,86 @@
1-
import { version } from "../package.json";
2-
import { zodToTs } from "zod-to-ts";
3-
import ts from "typescript";
4-
import prettier from "prettier";
1+
import { version } from '../package.json';
2+
import { zodToTs } from 'zod-to-ts';
3+
import ts from 'typescript';
4+
import prettier from 'prettier';
55

66
// Helper to print TypeScript node as string
77
function printNode(node: ts.Node): string {
88
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
9-
const sourceFile = ts.createSourceFile(
10-
"temp.ts",
11-
"",
12-
ts.ScriptTarget.Latest,
13-
false,
14-
ts.ScriptKind.TS
15-
);
9+
const sourceFile = ts.createSourceFile('temp.ts', '', ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
1610
return printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);
1711
}
1812

1913
// Import all schemas
20-
import {
21-
ResourceSchema,
22-
TimestampsSchema,
23-
} from "../src/types/schemas/base.schema";
14+
import { ResourceSchema, TimestampsSchema } from '../src/types/schemas/base.schema';
2415

25-
import { LoginResponseSchema } from "../src/types/schemas/auth.schema";
16+
import { LoginResponseSchema } from '../src/types/schemas/auth.schema';
2617

27-
import {
28-
SelfSchema,
29-
ResourceLimitsSchema,
30-
} from "../src/types/schemas/user.schema";
18+
import { SelfSchema, ResourceLimitsSchema } from '../src/types/schemas/user.schema';
3119

3220
import {
3321
EnvironmentSchema,
3422
EnvironmentCreateInputSchema,
3523
EnvironmentUpdateInputSchema,
3624
EnvironmentValueSchema,
37-
EnvironmentValueUpdateInputSchema,
38-
} from "../src/types/schemas/environment.schema";
25+
EnvironmentValueUpdateInputSchema
26+
} from '../src/types/schemas/environment.schema';
3927

40-
import {
41-
CronSchema,
42-
CronCreateInputSchema,
43-
CronUpdateInputSchema,
44-
} from "../src/types/schemas/cron.schema";
28+
import { CronSchema, CronCreateInputSchema, CronUpdateInputSchema } from '../src/types/schemas/cron.schema';
4529

46-
import {
47-
DomainSchema,
48-
DomainCreateInputSchema,
49-
} from "../src/types/schemas/domain.schema";
30+
import { DomainSchema, DomainCreateInputSchema } from '../src/types/schemas/domain.schema';
5031

5132
import {
5233
WorkerSchema,
5334
WorkerCreateInputSchema,
5435
WorkerUpdateInputSchema,
55-
WorkerLanguageSchema,
56-
} from "../src/types/schemas/worker.schema";
36+
WorkerLanguageSchema
37+
} from '../src/types/schemas/worker.schema';
5738

5839
// Schema definitions to generate
5940
const schemas = [
6041
// Base schemas
61-
{ schema: ResourceSchema, name: "Resource" },
62-
{ schema: TimestampsSchema, name: "Timestamps" },
42+
{ schema: ResourceSchema, name: 'Resource' },
43+
{ schema: TimestampsSchema, name: 'Timestamps' },
6344

6445
// Auth
65-
{ schema: LoginResponseSchema, name: "LoginResponse" },
46+
{ schema: LoginResponseSchema, name: 'LoginResponse' },
6647

6748
// User
68-
{ schema: SelfSchema, name: "Self" },
69-
{ schema: ResourceLimitsSchema, name: "ResourceLimits" },
49+
{ schema: SelfSchema, name: 'Self' },
50+
{ schema: ResourceLimitsSchema, name: 'ResourceLimits' },
7051

7152
// Environment
72-
{ schema: EnvironmentSchema, name: "Environment" },
73-
{ schema: EnvironmentCreateInputSchema, name: "EnvironmentCreateInput" },
74-
{ schema: EnvironmentUpdateInputSchema, name: "EnvironmentUpdateInput" },
75-
{ schema: EnvironmentValueSchema, name: "EnvironmentValue" },
53+
{ schema: EnvironmentSchema, name: 'Environment' },
54+
{ schema: EnvironmentCreateInputSchema, name: 'EnvironmentCreateInput' },
55+
{ schema: EnvironmentUpdateInputSchema, name: 'EnvironmentUpdateInput' },
56+
{ schema: EnvironmentValueSchema, name: 'EnvironmentValue' },
7657
{
7758
schema: EnvironmentValueUpdateInputSchema,
78-
name: "EnvironmentValueUpdateInput",
59+
name: 'EnvironmentValueUpdateInput'
7960
},
8061

8162
// Cron
82-
{ schema: CronSchema, name: "Cron" },
83-
{ schema: CronCreateInputSchema, name: "CronCreateInput" },
84-
{ schema: CronUpdateInputSchema, name: "CronUpdateInput" },
63+
{ schema: CronSchema, name: 'Cron' },
64+
{ schema: CronCreateInputSchema, name: 'CronCreateInput' },
65+
{ schema: CronUpdateInputSchema, name: 'CronUpdateInput' },
8566

8667
// Domain
87-
{ schema: DomainSchema, name: "Domain" },
88-
{ schema: DomainCreateInputSchema, name: "DomainCreateInput" },
68+
{ schema: DomainSchema, name: 'Domain' },
69+
{ schema: DomainCreateInputSchema, name: 'DomainCreateInput' },
8970

9071
// Worker
91-
{ schema: WorkerSchema, name: "Worker" },
92-
{ schema: WorkerCreateInputSchema, name: "WorkerCreateInput" },
93-
{ schema: WorkerUpdateInputSchema, name: "WorkerUpdateInput" },
94-
{ schema: WorkerLanguageSchema, name: "WorkerLanguage" },
72+
{ schema: WorkerSchema, name: 'Worker' },
73+
{ schema: WorkerCreateInputSchema, name: 'WorkerCreateInput' },
74+
{ schema: WorkerUpdateInputSchema, name: 'WorkerUpdateInput' },
75+
{ schema: WorkerLanguageSchema, name: 'WorkerLanguage' }
9576
];
9677

9778
async function generateTypes() {
98-
console.log("🔄 Generating TypeScript types from Zod schemas...");
79+
console.log('🔄 Generating TypeScript types from Zod schemas...');
9980

10081
// Clean dist directory
10182
const distDir = `${import.meta.dir}/../dist`;
102-
console.log(" 🧹 Cleaning dist directory...");
83+
console.log(' 🧹 Cleaning dist directory...');
10384
try {
10485
await Bun.$`rm -rf ${distDir}`;
10586
} catch (error) {
@@ -111,30 +92,28 @@ async function generateTypes() {
11192
let idCounter = 0;
11293
const auxiliaryTypeStore = {
11394
nextId: () => `T${idCounter++}`,
114-
definitions: new Map(),
95+
definitions: new Map()
11596
};
11697

117-
console.log(" 📋 First pass: building types map...");
98+
console.log(' 📋 First pass: building types map...');
11899
for (const { schema, name } of schemas) {
119100
try {
120101
const { node } = zodToTs(schema, { auxiliaryTypeStore });
121102
let typeStr = printNode(node);
122103
// Normalize whitespace for better matching
123-
typeStr = typeStr.replace(/\s+/g, " ").trim();
104+
typeStr = typeStr.replace(/\s+/g, ' ').trim();
124105
typesMap.set(typeStr, name);
125106
} catch (error) {
126107
console.error(`❌ Failed to generate type for ${name}:`, error);
127108
}
128109
}
129110

130111
// SECOND PASS: Replace nested types with references
131-
console.log(" 🔄 Second pass: replacing nested types...");
112+
console.log(' 🔄 Second pass: replacing nested types...');
132113
const finalTypesMap = new Map<string, string>();
133114

134115
// Sort types by length (longest first) for better matching
135-
const sortedTypes = Array.from(typesMap.entries()).sort(
136-
([a], [b]) => b.length - a.length
137-
);
116+
const sortedTypes = Array.from(typesMap.entries()).sort(([a], [b]) => b.length - a.length);
138117

139118
for (const [typeStr, name] of typesMap) {
140119
let updatedTypeStr = typeStr;
@@ -143,18 +122,15 @@ async function generateTypes() {
143122
for (const [otherTypeStr, otherName] of sortedTypes) {
144123
if (otherName !== name && updatedTypeStr.includes(otherTypeStr)) {
145124
// Use a more precise replacement to avoid partial matches
146-
updatedTypeStr = updatedTypeStr.replaceAll(
147-
otherTypeStr,
148-
`I${otherName}`
149-
);
125+
updatedTypeStr = updatedTypeStr.replaceAll(otherTypeStr, `I${otherName}`);
150126
}
151127
}
152128

153129
finalTypesMap.set(name, updatedTypeStr);
154130
}
155131

156132
// THIRD PASS: Recursive replacement until no more changes
157-
console.log(" 🔁 Third pass: recursive replacement...");
133+
console.log(' 🔁 Third pass: recursive replacement...');
158134
let changed = true;
159135
let iterations = 0;
160136
const maxIterations = 10;
@@ -168,10 +144,7 @@ async function generateTypes() {
168144

169145
for (const [otherTypeStr, otherName] of sortedTypes) {
170146
if (otherName !== name && updatedTypeStr.includes(otherTypeStr)) {
171-
const newStr = updatedTypeStr.replaceAll(
172-
otherTypeStr,
173-
`I${otherName}`
174-
);
147+
const newStr = updatedTypeStr.replaceAll(otherTypeStr, `I${otherName}`);
175148
if (newStr !== updatedTypeStr) {
176149
updatedTypeStr = newStr;
177150
changed = true;
@@ -214,46 +187,43 @@ export type Dictionary<T> = Record<string, T>;
214187
}
215188

216189
// Format with prettier
217-
console.log(" 🎨 Formatting with prettier...");
190+
console.log(' 🎨 Formatting with prettier...');
218191
const formatted = await prettier.format(output, {
219-
parser: "typescript",
192+
parser: 'typescript',
220193
semi: true,
221194
singleQuote: true,
222-
trailingComma: "all",
195+
trailingComma: 'all'
223196
});
224197

225198
// Write types file
226199
await Bun.write(`${distDir}/types.d.ts`, formatted);
227200

228201
// Generate package.json for npm publish
229202
const packageJson = {
230-
name: "@openworkers/api-types",
203+
name: '@openworkers/api-types',
231204
version,
232-
license: "MIT",
233-
type: "module",
205+
license: 'MIT',
206+
type: 'module',
234207
private: false,
235-
main: "./types.d.ts",
236-
types: "./types.d.ts",
208+
main: './types.d.ts',
209+
types: './types.d.ts',
237210
exports: {
238-
".": "./types.d.ts",
211+
'.': './types.d.ts'
239212
},
240-
description: "TypeScript types for OpenWorkers API",
241-
keywords: ["openworkers", "types", "typescript"],
213+
description: 'TypeScript types for OpenWorkers API',
214+
keywords: ['openworkers', 'types', 'typescript'],
242215
repository: {
243-
type: "git",
244-
url: "git+https://github.com/openworkers/openworkers-api.git",
216+
type: 'git',
217+
url: 'git+https://github.com/openworkers/openworkers-api.git'
245218
},
246219
publishConfig: {
247-
access: "public",
248-
registry: "https://registry.npmjs.org/",
249-
provenance: true,
250-
},
220+
access: 'public',
221+
registry: 'https://registry.npmjs.org/',
222+
provenance: true
223+
}
251224
};
252225

253-
await Bun.write(
254-
`${distDir}/package.json`,
255-
JSON.stringify(packageJson, null, 2)
256-
);
226+
await Bun.write(`${distDir}/package.json`, JSON.stringify(packageJson, null, 2));
257227

258228
// Copy LICENSE
259229
const license = await Bun.file(`${import.meta.dir}/../LICENSE`).text();

server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* For development, use `bun --hot src/index.ts` which relies on
1212
* the default export and lets Bun handle hot reload lifecycle.
1313
*/
14-
import app from "./src";
14+
import app from './src';
1515

1616
const server = Bun.serve(app);
1717

18-
process.on("SIGTERM", () => {
19-
console.log("SIGTERM received, shutting down gracefully...");
18+
process.on('SIGTERM', () => {
19+
console.log('SIGTERM received, shutting down gracefully...');
2020
server.stop(true);
2121
});
2222

23-
process.on("SIGINT", () => {
24-
console.log("SIGINT received, shutting down gracefully...");
23+
process.on('SIGINT', () => {
24+
console.log('SIGINT received, shutting down gracefully...');
2525
server.stop(true);
2626
});

0 commit comments

Comments
 (0)