Skip to content

Commit 39b0785

Browse files
committed
trpc-typecheck
1 parent 3f68aff commit 39b0785

7 files changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Hidden folders and files
2+
.*
3+
!.gitignore
4+
!.*.example
5+
6+
# Common generated folders
7+
logs/
8+
node_modules/
9+
out/
10+
dist/
11+
dist-ssr/
12+
build/
13+
coverage/
14+
temp/
15+
16+
# Common generated files
17+
*.log
18+
*.log.*
19+
*.tsbuildinfo
20+
*.vitest-temp.json
21+
vite.config.ts.timestamp-*
22+
vitest.config.ts.timestamp-*
23+
24+
# Common manual ignore files
25+
*.local
26+
*.pem
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "trpc-typecheck",
3+
"type": "module",
4+
"version": "0.0.0",
5+
"license": "MIT",
6+
"scripts": {
7+
"gen": "tsx scripts/gen.ts",
8+
"bench": "pnpm run gen && hyperfine 'tsc --noEmit'"
9+
},
10+
"dependencies": {
11+
"@orpc/client": "1.0.0-beta.2",
12+
"@orpc/server": "1.0.0-beta.2",
13+
"@standard-schema/spec": "^1.0.0",
14+
"@trpc/client": "11.0.0-rc.840",
15+
"@trpc/server": "11.0.0-rc.840"
16+
}
17+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import fs from 'node:fs/promises'
2+
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
5+
const __filename = fileURLToPath(import.meta.url)
6+
const __dirname = path.dirname(__filename)
7+
const NUM_ROUTERS = 500
8+
const ROUTER_DIR = path.resolve(__dirname, '../src/router')
9+
const CONSUME_DIR = path.resolve(__dirname, '../src/consume')
10+
11+
const PROCEDURE_1 = `procedure.input({} as Schema<{ who: string }, { who: string }>).query(({input}) => \`hello \${input.who}\`)`
12+
const PROCEDURE_2 = `procedure.input({} as Schema<{ id: string }, { id: string }>).mutation(({input}) => ({ id: input.id }))`
13+
14+
function createRouter(routerName: string): string {
15+
return `
16+
import { procedure, router, Schema } from '../trpc';
17+
18+
export const ${routerName} = router({
19+
procedure_1: ${PROCEDURE_1},
20+
procedure_2: ${PROCEDURE_2},
21+
nested: router({
22+
procedure_1: ${PROCEDURE_1},
23+
procedure_2: ${PROCEDURE_2},
24+
nested: router({
25+
procedure_1: ${PROCEDURE_1},
26+
procedure_2: ${PROCEDURE_2}
27+
}),
28+
}),
29+
})`.trim()
30+
}
31+
32+
function createConsume(routerName: string): string {
33+
return `
34+
import { client } from '../client';
35+
36+
const procedure_1: string = await client.${routerName}.procedure_1.query({ who: 'world' });
37+
const procedure_2: { id: string } = await client.${routerName}.procedure_2.mutate({ id: '123' });
38+
const nested_procedure_1: string = await client.${routerName}.nested.procedure_1.query({ who: 'world' });
39+
const nested_procedure_2: { id: string } = await client.${routerName}.nested.procedure_2.mutate({ id: '123' });
40+
const nested_nested_procedure_1: string = await client.${routerName}.nested.nested.procedure_1.query({ who: 'world' });
41+
const nested_nested_procedure_2: { id: string } = await client.${routerName}.nested.nested.procedure_2.mutate({ id: '123' });
42+
`.trim()
43+
}
44+
45+
const routerNames = Array.from({ length: NUM_ROUTERS }, (_, i) => `router_${i}`)
46+
47+
await fs.mkdir(ROUTER_DIR, { recursive: true })
48+
await fs.mkdir(CONSUME_DIR, { recursive: true })
49+
50+
routerNames.forEach(async (routerName) => {
51+
await fs.writeFile(path.join(ROUTER_DIR, `${routerName}.ts`), createRouter(routerName))
52+
await fs.writeFile(path.join(CONSUME_DIR, `${routerName}.ts`), createConsume(routerName))
53+
})
54+
55+
const routerIndex = `
56+
import { router as routerFn } from '../trpc'
57+
${routerNames.map(routerName => `import { ${routerName} } from './${routerName}';`).join('\n')}
58+
59+
export const router = routerFn({
60+
${routerNames.map(routerName => `${routerName},`).join('\n ')}
61+
})
62+
`
63+
64+
await fs.writeFile(path.join(ROUTER_DIR, 'index.ts'), routerIndex)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { router } from './router'
2+
import { createTRPCClient, httpBatchLink } from '@trpc/client'
3+
4+
export const client = createTRPCClient<typeof router>({
5+
links: [
6+
httpBatchLink({
7+
url: 'http://localhost:3000/trpc',
8+
}),
9+
],
10+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { initTRPC } from '@trpc/server'
2+
3+
/**
4+
* Initialization of tRPC backend
5+
* Should be done only once per backend!
6+
*/
7+
const t = initTRPC.create()
8+
9+
/**
10+
* Export reusable router and procedure helpers
11+
* that can be used throughout the router
12+
*/
13+
export const router = t.router
14+
export const procedure = t.procedure
15+
16+
export type { StandardSchemaV1 as Schema } from '@standard-schema/spec'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"incremental": false,
5+
"types": []
6+
},
7+
"references": [],
8+
"include": ["src"],
9+
"exclude": [
10+
"**/*.test.*",
11+
"**/*.test-d.ts",
12+
"**/*.bench.*",
13+
"**/__tests__/**",
14+
"**/__mocks__/**",
15+
"**/__snapshots__/**"
16+
]
17+
}

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)