diff --git a/.gitignore b/.gitignore index c449246..75b3c59 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ repo.txt /playwright-report/ /blob-report/ /playwright/.cache/ +*.tsbuildinfo \ No newline at end of file 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 ac72434..9146f5a 100644 --- a/client/package.json +++ b/client/package.json @@ -3,7 +3,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 todo:rename --branch \"stage\" --commit-hash \"$GITHUB_SHA\" --commit-message \"stage deployment\"", "deploy:prod": "pnpm wrangler pages deploy ./dist --project-name todo:rename", diff --git a/client/src/example.test.tsx b/client/src/example.test.tsx index c3567e1..6252dec 100644 --- a/client/src/example.test.tsx +++ b/client/src/example.test.tsx @@ -10,6 +10,7 @@ import { Example } from './example'; // Mock only the react-query hook vi.mock('@tanstack/react-query', async (importOriginal) => { + // eslint-disable-next-line @typescript-eslint/consistent-type-imports const mod = await importOriginal(); return { ...mod, diff --git a/client/src/example.tsx b/client/src/example.tsx index ecdaba5..eac0650 100644 --- a/client/src/example.tsx +++ b/client/src/example.tsx @@ -4,8 +4,8 @@ import { Modal } from './components/ui.modal'; import { useQuery } from '@tanstack/react-query'; import { getServerUrl } from './config'; import { hc } from 'hono/client'; -import type { ServerApi } from '../../server/src/types'; import { useMonitor } from './services/monitor.use-monitor'; +import type { ServerApi } from 'types.shared'; const serverUrl = getServerUrl(); diff --git a/client/src/server-types.d.ts b/client/src/server-types.d.ts new file mode 100644 index 0000000..c60fc09 --- /dev/null +++ b/client/src/server-types.d.ts @@ -0,0 +1,62 @@ +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 "index" { + import type { Env } from "types"; + const app: import("hono/hono-base").HonoBase<{ + Bindings: Env; + }, ({ + "*": {}; + } & { + "/": { + $get: { + input: {}; + output: "ok"; + outputFormat: "text"; + status: 200; + }; + }; + }) | import("hono/types").MergeSchemaPath<{ + "/reset": { + $post: { + input: {}; + output: {}; + outputFormat: string; + status: import("hono/utils/http-status").StatusCode; + }; + }; + }, "/test">, "/">; + export default app; +} +declare module "types.shared" { + import type app from "index"; + export type ServerApi = typeof app; +} diff --git a/package.json b/package.json index e0ad0ff..73598b2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "create-react-spa-cloudflare", "type": "module", - "version": "0.0.19", + "version": "0.0.20", "description": "Starter package for react spa with cloudflare pages, workers, and kv", "bin": "./bin/cli.js", "scripts": { diff --git a/server/src/types.shared.ts b/server/src/types.shared.ts new file mode 100644 index 0000000..a1397f7 --- /dev/null +++ b/server/src/types.shared.ts @@ -0,0 +1,3 @@ +import type app from './index'; + +export type ServerApi = typeof app; diff --git a/server/src/types.ts b/server/src/types.ts index 58c421e..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,5 +14,3 @@ export interface Env { export type Context = HonoContext<{ Bindings: Env; }>; - -export type ServerApi = typeof app; 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"] +}