diff --git a/client/.eslintignore b/client/.eslintignore index 544edd9..0eab8e5 100644 --- a/client/.eslintignore +++ b/client/.eslintignore @@ -1 +1,2 @@ -.eslintrc.cjs \ No newline at end of file +.eslintrc.cjs +src/server-types.d.ts \ No newline at end of file diff --git a/client/package.json b/client/package.json index 837d1f9..6957e4c 100644 --- a/client/package.json +++ b/client/package.json @@ -4,7 +4,8 @@ "version": "0.0.0", "type": "module", "scripts": { - "build": "tsc -b && vite build", + "build": "pnpm run build:server-types && tsc -b && vite build", + "build:server-types": "tsc -p ../server/tsconfig.shared.json --declaration --emitDeclarationOnly --outFile ./src/server-types.d.ts", "build:test": "tsc -b && E2E=true vite build", "deploy:stage": "pnpm wrangler pages deploy ./dist --project-name simon --branch \"stage\" --commit-hash \"$GITHUB_SHA\" --commit-message \"stage deployment\"", "deploy:prod": "pnpm wrangler pages deploy ./dist --project-name simon", @@ -25,7 +26,6 @@ "tone": "^15.0.4" }, "devDependencies": { - "@simon/server": "workspace:*", "@testing-library/jest-dom": "^6.4.6", "@testing-library/react": "^16.0.0", "@types/react": "^18.3.3", diff --git a/client/src/components/use-game-machine.ts b/client/src/components/use-game-machine.ts index bc197da..23fd7bc 100644 --- a/client/src/components/use-game-machine.ts +++ b/client/src/components/use-game-machine.ts @@ -5,7 +5,7 @@ import { gameLogic, NEW_GAME_STATE } from './use-game-machine.logic'; import { useOnEntry } from './use-game-machine.use-on-entry'; import { melodyPlayer } from '../services/melody-player'; import type { PadId } from '../types'; -import type { HighScoreEntry } from '@simon/server/src/types'; +import type { HighScoreEntry } from 'types.shared'; export type GameMachine = ReturnType; diff --git a/client/src/server-types.d.ts b/client/src/server-types.d.ts new file mode 100644 index 0000000..c9de397 --- /dev/null +++ b/client/src/server-types.d.ts @@ -0,0 +1,137 @@ +declare module "types" { + import type { KVNamespace } from '@cloudflare/workers-types'; + import type { Context as HonoContext } from 'hono'; + export interface Env { + ALLOWED_HOST: string; + DB: KVNamespace; + LOCAL_DB: KVNamespace; + TEST_DB: KVNamespace; + ENV: 'dev' | 'prod' | 'stage' | 'test'; + GITHUB_REF_NAME: string; + GITHUB_SHA: string; + } + export type Context = HonoContext<{ + Bindings: Env; + }>; +} +declare module "test" { + import type { Env } from "types"; + export const testRoute: import("hono/hono-base").HonoBase<{ + Bindings: Env; + }, { + "/reset": { + $post: { + input: {}; + output: {}; + outputFormat: string; + status: import("hono/utils/http-status").StatusCode; + }; + }; + }, "/">; +} +declare module "utils" { + import type { KVNamespace } from '@cloudflare/workers-types'; + import type { Context } from "types"; + export const getDb: (c: Context) => KVNamespace; +} +declare module "high-score" { + import type { Env } from "types"; + export const highScoreRoute: import("hono/hono-base").HonoBase<{ + Bindings: Env; + }, { + "/": { + $get: { + input: {}; + output: { + highScore: any; + }; + outputFormat: "json"; + status: 200; + }; + }; + } & { + "/": { + $post: { + input: { + json: { + name: string; + score: number; + }; + }; + output: { + newHighScore: { + score: number; + name: string; + timestamp: number; + }; + }; + outputFormat: "json"; + status: 200; + }; + }; + }, "/">; +} +declare module "index" { + import type { Env } from "types"; + const app: import("hono/hono-base").HonoBase<{ + Bindings: Env; + }, { + "/high-score": { + $get: { + input: {}; + output: { + highScore: any; + }; + outputFormat: "json"; + status: 200; + }; + $post: { + input: { + json: { + name: string; + score: number; + }; + }; + output: { + newHighScore: { + score: number; + name: string; + timestamp: number; + }; + }; + outputFormat: "json"; + status: 200; + }; + }; + } & { + "/test/reset": { + $post: { + input: {}; + output: {}; + outputFormat: string; + status: import("hono/utils/http-status").StatusCode; + }; + }; + } & { + "*": {}; + } & { + "/": { + $get: { + input: {}; + output: "ok"; + outputFormat: "text"; + status: 200; + }; + }; + }, "/">; + export default app; +} +declare module "types.shared" { + import type app from "index"; + export type ServerApi = typeof app; + export interface HighScoreEntry { + name: string; + score: number; + timestamp: number; + } +} diff --git a/client/src/services/api.high-score.ts b/client/src/services/api.high-score.ts index ebadeb3..484ae4e 100644 --- a/client/src/services/api.high-score.ts +++ b/client/src/services/api.high-score.ts @@ -1,9 +1,9 @@ -import type { ServerApi } from '@simon/server/src/types'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import type { InferRequestType, InferResponseType } from 'hono/client'; import { hc } from 'hono/client'; import { useMonitor } from './monitor.use-monitor'; import { getServerUrl } from '../config'; +import type { ServerApi } from 'types.shared'; const serverUrl = getServerUrl(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0ebb3a0..051afb4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -64,9 +64,6 @@ importers: specifier: ^15.0.4 version: 15.0.4 devDependencies: - '@simon/server': - specifier: workspace:* - version: link:../server '@testing-library/jest-dom': specifier: ^6.4.6 version: 6.4.8 diff --git a/server/src/high-score.ts b/server/src/high-score.ts index 83316fa..ceddc19 100644 --- a/server/src/high-score.ts +++ b/server/src/high-score.ts @@ -2,7 +2,7 @@ import { Hono } from 'hono'; import { HTTPException } from 'hono/http-exception'; import { zValidator } from '@hono/zod-validator'; import type { Context, Env } from './types'; -import type { HighScoreEntry } from './types'; +import type { HighScoreEntry } from './types.shared'; import { z } from 'zod'; import { getDb } from './utils'; diff --git a/server/src/types.shared.ts b/server/src/types.shared.ts new file mode 100644 index 0000000..ed1f855 --- /dev/null +++ b/server/src/types.shared.ts @@ -0,0 +1,9 @@ +import type app from './index'; + +export type ServerApi = typeof app; + +export interface HighScoreEntry { + name: string; + score: number; + timestamp: number; +} diff --git a/server/src/types.ts b/server/src/types.ts index 49792f8..8b21019 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -1,6 +1,5 @@ import type { KVNamespace } from '@cloudflare/workers-types'; import type { Context as HonoContext } from 'hono'; -import type app from './index'; export interface Env { ALLOWED_HOST: string; @@ -15,11 +14,3 @@ export interface Env { export type Context = HonoContext<{ Bindings: Env; }>; - -export type ServerApi = typeof app; - -export interface HighScoreEntry { - name: string; - score: number; - timestamp: number; -} diff --git a/server/tsconfig.shared.json b/server/tsconfig.shared.json new file mode 100644 index 0000000..46273a5 --- /dev/null +++ b/server/tsconfig.shared.json @@ -0,0 +1,14 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "noEmit": false, + "types": [ + "@cloudflare/workers-types", + "@cloudflare/workers-types/experimental", + "@cloudflare/vitest-pool-workers" + ], + "noUnusedLocals": true, + "noUnusedParameters": true + }, + "include": ["src/types.shared.ts"] +}