Skip to content

Commit 1ec070c

Browse files
committed
build server types
1 parent c8d1573 commit 1ec070c

File tree

8 files changed

+173
-13
lines changed

8 files changed

+173
-13
lines changed

client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.0",
44
"type": "module",
55
"scripts": {
6-
"build": "tsc -b && vite build",
6+
"build": "pnpm run build:server-types && tsc -b && vite build",
7+
"build:server-types": "tsc -p ../server/tsconfig.shared.json --declaration --emitDeclarationOnly --outFile ./src/server-types.d.ts",
78
"build:test": "tsc -b && E2E=true vite build",
89
"deploy:stage": "pnpm wrangler pages deploy ./dist --project-name simon --branch \"stage\" --commit-hash \"$GITHUB_SHA\" --commit-message \"stage deployment\"",
910
"deploy:prod": "pnpm wrangler pages deploy ./dist --project-name simon",

client/src/components/use-game-machine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { gameLogic, NEW_GAME_STATE } from './use-game-machine.logic';
55
import { useOnEntry } from './use-game-machine.use-on-entry';
66
import { melodyPlayer } from '../services/melody-player';
77
import type { PadId } from '../types';
8-
import type { HighScoreEntry } from '../../../server/src/types';
8+
import type { HighScoreEntry } from 'types.shared';
99

1010
export type GameMachine = ReturnType<typeof useGameMachine>;
1111

client/src/server-types.d.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
declare module "types" {
2+
import type { KVNamespace } from '@cloudflare/workers-types';
3+
import type { Context as HonoContext } from 'hono';
4+
export interface Env {
5+
ALLOWED_HOST: string;
6+
DB: KVNamespace;
7+
LOCAL_DB: KVNamespace;
8+
TEST_DB: KVNamespace;
9+
ENV: 'dev' | 'prod' | 'stage' | 'test';
10+
GITHUB_REF_NAME: string;
11+
GITHUB_SHA: string;
12+
}
13+
export type Context = HonoContext<{
14+
Bindings: Env;
15+
}>;
16+
}
17+
declare module "test" {
18+
import type { Env } from "types";
19+
export const testRoute: import("hono/hono-base").HonoBase<{
20+
Bindings: Env;
21+
}, {
22+
"/reset": {
23+
$post: {
24+
input: {};
25+
output: {};
26+
outputFormat: string;
27+
status: import("hono/utils/http-status").StatusCode;
28+
};
29+
};
30+
}, "/">;
31+
}
32+
declare module "utils" {
33+
import type { KVNamespace } from '@cloudflare/workers-types';
34+
import type { Context } from "types";
35+
export const getDb: (c: Context) => KVNamespace;
36+
}
37+
declare module "high-score" {
38+
import type { Env } from "types";
39+
export const highScoreRoute: import("hono/hono-base").HonoBase<{
40+
Bindings: Env;
41+
}, {
42+
"/": {
43+
$get: {
44+
input: {};
45+
output: {
46+
highScore: {
47+
name: string;
48+
score: number;
49+
timestamp: number;
50+
};
51+
};
52+
outputFormat: "json";
53+
status: 200;
54+
};
55+
};
56+
} & {
57+
"/": {
58+
$post: {
59+
input: {
60+
json: {
61+
name: string;
62+
score: number;
63+
};
64+
};
65+
output: {
66+
newHighScore: {
67+
score: number;
68+
name: string;
69+
timestamp: number;
70+
};
71+
};
72+
outputFormat: "json";
73+
status: 200;
74+
};
75+
};
76+
}, "/">;
77+
}
78+
declare module "index" {
79+
import type { Env } from "types";
80+
const app: import("hono/hono-base").HonoBase<{
81+
Bindings: Env;
82+
}, {
83+
"/high-score": {
84+
$get: {
85+
input: {};
86+
output: {
87+
highScore: {
88+
name: string;
89+
score: number;
90+
timestamp: number;
91+
};
92+
};
93+
outputFormat: "json";
94+
status: 200;
95+
};
96+
$post: {
97+
input: {
98+
json: {
99+
name: string;
100+
score: number;
101+
};
102+
};
103+
output: {
104+
newHighScore: {
105+
score: number;
106+
name: string;
107+
timestamp: number;
108+
};
109+
};
110+
outputFormat: "json";
111+
status: 200;
112+
};
113+
};
114+
} & {
115+
"/test/reset": {
116+
$post: {
117+
input: {};
118+
output: {};
119+
outputFormat: string;
120+
status: import("hono/utils/http-status").StatusCode;
121+
};
122+
};
123+
} & {
124+
"*": {};
125+
} & {
126+
"/": {
127+
$get: {
128+
input: {};
129+
output: "ok";
130+
outputFormat: "text";
131+
status: 200;
132+
};
133+
};
134+
}, "/">;
135+
export default app;
136+
}
137+
declare module "types.shared" {
138+
import type app from "index";
139+
export type ServerApi = typeof app;
140+
export interface HighScoreEntry {
141+
name: string;
142+
score: number;
143+
timestamp: number;
144+
}
145+
}

client/src/services/api.high-score.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { ServerApi } from '../../../server/src/types';
21
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
32
import type { InferRequestType, InferResponseType } from 'hono/client';
43
import { hc } from 'hono/client';
54
import { useMonitor } from './monitor.use-monitor';
65
import { getServerUrl } from '../config';
6+
import type { ServerApi } from 'types.shared';
77

88
const serverUrl = getServerUrl();
99

server/src/high-score.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hono } from 'hono';
22
import { HTTPException } from 'hono/http-exception';
33
import { zValidator } from '@hono/zod-validator';
44
import type { Context, Env } from './types';
5-
import type { HighScoreEntry } from './types';
5+
import type { HighScoreEntry } from './types.shared';
66
import { z } from 'zod';
77
import { getDb } from './utils';
88

server/src/types.shared.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type app from './index';
2+
3+
export type ServerApi = typeof app;
4+
5+
export interface HighScoreEntry {
6+
name: string;
7+
score: number;
8+
timestamp: number;
9+
}

server/src/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { KVNamespace } from '@cloudflare/workers-types';
22
import type { Context as HonoContext } from 'hono';
3-
import type app from './index';
43

54
export interface Env {
65
ALLOWED_HOST: string;
@@ -15,11 +14,3 @@ export interface Env {
1514
export type Context = HonoContext<{
1615
Bindings: Env;
1716
}>;
18-
19-
export type ServerApi = typeof app;
20-
21-
export interface HighScoreEntry {
22-
name: string;
23-
score: number;
24-
timestamp: number;
25-
}

server/tsconfig.shared.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"types": [
6+
"@cloudflare/workers-types",
7+
"@cloudflare/workers-types/experimental",
8+
"@cloudflare/vitest-pool-workers"
9+
],
10+
"noUnusedLocals": true,
11+
"noUnusedParameters": true
12+
},
13+
"include": ["src/types.shared.ts"]
14+
}

0 commit comments

Comments
 (0)