From 50cc352eb690a6e71a0262cae4ca4a9d28e6986a Mon Sep 17 00:00:00 2001 From: unnoq Date: Tue, 19 Nov 2024 16:07:18 +0700 Subject: [PATCH] docs: used @whatwg-node/server instead of srvx --- apps/content/content/docs/index.mdx | 36 +++++---- .../content/docs/server/integrations.mdx | 34 +++++---- apps/content/examples/contract.ts | 36 +++++---- apps/content/examples/server.ts | 36 +++++---- apps/content/package.json | 8 +- pnpm-lock.yaml | 75 ++++++++++++++----- 6 files changed, 144 insertions(+), 81 deletions(-) diff --git a/apps/content/content/docs/index.mdx b/apps/content/content/docs/index.mdx index f2ba1baf..ca6e84fa 100644 --- a/apps/content/content/docs/index.mdx +++ b/apps/content/content/docs/index.mdx @@ -137,11 +137,12 @@ In oRPC middleware is very useful and fully typed you can find more info [here]( ## Start Your Server -This example uses [srvx](https://srvx.unjs.io/) to create a Node server with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). +This example uses [@whatwg-node/server](https://www.npmjs.com/package/@whatwg-node/server) to create a Node server with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). ```ts twoslash import { createFetchHandler } from '@orpc/server/fetch' -import { serve } from 'srvx' +import { createServer } from 'node:http' +import { createServerAdapter } from '@whatwg-node/server' import { router } from 'examples/server' const handler = createFetchHandler({ @@ -149,21 +150,26 @@ const handler = createFetchHandler({ serverless: false, // set true will improve cold start times }) -const server = serve({ - fetch(request) { - return handler({ - request, - context: {}, - prefix: '/api' - }) - }, - port: 2206, -}) +const server = createServer( + createServerAdapter((request: Request) => { + const url = new URL(request.url) -await server.ready() + if (url.pathname.startsWith('/api')) { + return handler({ + request, + prefix: '/api', + context: {}, + }) + } -// biome-ignore lint/suspicious/noConsole: -console.log(`🚀 Server ready at ${server.url}`) + return new Response('Not found', { status: 404 }) + }), +) + +server.listen(2026, () => { + // biome-ignore lint/suspicious/noConsole: + console.log('Server is available at http://localhost:2026') +}) ``` Start the server and visit http://localhost:2026/api/getting?name=yourname to see the result. diff --git a/apps/content/content/docs/server/integrations.mdx b/apps/content/content/docs/server/integrations.mdx index f29e32a6..0fee81da 100644 --- a/apps/content/content/docs/server/integrations.mdx +++ b/apps/content/content/docs/server/integrations.mdx @@ -10,7 +10,8 @@ so you can use it in any modern environment and framework. ```ts twoslash import { createFetchHandler } from '@orpc/server/fetch' -import { serve } from 'srvx' +import { createServer } from 'node:http' +import { createServerAdapter } from '@whatwg-node/server' import { router } from 'examples/server' const handler = createFetchHandler({ @@ -19,21 +20,26 @@ const handler = createFetchHandler({ }) -const server = serve({ - fetch(request) { - return handler({ - request, - context: {}, - prefix: '/api', - }) - }, - port: 2206, -}) +const server = createServer( + createServerAdapter((request: Request) => { + const url = new URL(request.url) -await server.ready() + if (url.pathname.startsWith('/api')) { + return handler({ + request, + prefix: '/api', + context: {}, + }) + } -// biome-ignore lint/suspicious/noConsole: -console.log(`🚀 Server ready at ${server.url}`) + return new Response('Not found', { status: 404 }) + }), +) + +server.listen(2026, () => { + // biome-ignore lint/suspicious/noConsole: + console.log('Server is available at http://localhost:2026') +}) ``` Under the hood, oRPC utilizes the RegExpRouter from [Hono](https://hono.dev/) (the fastest router in the JavaScript ecosystem). diff --git a/apps/content/examples/contract.ts b/apps/content/examples/contract.ts index 18636e19..450278f0 100644 --- a/apps/content/examples/contract.ts +++ b/apps/content/examples/contract.ts @@ -117,23 +117,29 @@ const handler = createFetchHandler({ // Modern runtime that support fetch api like deno, bun, cloudflare workers, even node can used -import { serve } from 'srvx' - -const server = serve({ - fetch(request) { - return handler({ - request, - context: {}, - prefix: '/api', - }) - }, - port: 2206, -}) +import { createServer } from 'node:http' +import { createServerAdapter } from '@whatwg-node/server' + +const server = createServer( + createServerAdapter((request: Request) => { + const url = new URL(request.url) + + if (url.pathname.startsWith('/api')) { + return handler({ + request, + prefix: '/api', + context: {}, + }) + } -await server.ready() + return new Response('Not found', { status: 404 }) + }), +) -// biome-ignore lint/suspicious/noConsole: -console.log(`🚀 Server ready at ${server.url}`) +server.listen(2026, () => { + // biome-ignore lint/suspicious/noConsole: + console.log('Server is available at http://localhost:2026') +}) // // diff --git a/apps/content/examples/server.ts b/apps/content/examples/server.ts index e048d22e..5e49ef8a 100644 --- a/apps/content/examples/server.ts +++ b/apps/content/examples/server.ts @@ -94,23 +94,29 @@ const handler = createFetchHandler({ // Modern runtime that support fetch api like deno, bun, cloudflare workers, even node can used -import { serve } from 'srvx' - -const server = serve({ - fetch(request) { - return handler({ - request, - context: {}, - prefix: '/api', - }) - }, - port: 2206, -}) +import { createServer } from 'node:http' +import { createServerAdapter } from '@whatwg-node/server' + +const server = createServer( + createServerAdapter((request: Request) => { + const url = new URL(request.url) + + if (url.pathname.startsWith('/api')) { + return handler({ + request, + prefix: '/api', + context: {}, + }) + } -await server.ready() + return new Response('Not found', { status: 404 }) + }), +) -// biome-ignore lint/suspicious/noConsole: -console.log(`🚀 Server ready at ${server.url}`) +server.listen(2026, () => { + // biome-ignore lint/suspicious/noConsole: + console.log('Server is available at http://localhost:2026') +}) // // diff --git a/apps/content/package.json b/apps/content/package.json index 79524d94..ddf9b1f1 100644 --- a/apps/content/package.json +++ b/apps/content/package.json @@ -18,9 +18,7 @@ "geist": "^1.3.1", "next": "15.0.3", "react": "^18.3.1", - "react-dom": "^18.3.1", - "srvx": "^0.1.1", - "zod": "^3.23.8" + "react-dom": "^18.3.1" }, "devDependencies": { "@orpc/client": "workspace:*", @@ -33,9 +31,11 @@ "@types/node": "22.9.0", "@types/react": "^18.3.12", "@types/react-dom": "^18.3.1", + "@whatwg-node/server": "^0.9.55", "autoprefixer": "^10.4.20", "postcss": "^8.4.48", "tailwindcss": "^3.4.14", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "zod": "^3.23.8" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ed53657..a1833ec9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,12 +80,6 @@ importers: react-dom: specifier: ^18.3.1 version: 18.3.1(react@18.3.1) - srvx: - specifier: ^0.1.1 - version: 0.1.1 - zod: - specifier: ^3.23.8 - version: 3.23.8 devDependencies: '@orpc/client': specifier: workspace:* @@ -117,6 +111,9 @@ importers: '@types/react-dom': specifier: ^18.3.1 version: 18.3.1 + '@whatwg-node/server': + specifier: ^0.9.55 + version: 0.9.55 autoprefixer: specifier: ^10.4.20 version: 10.4.20(postcss@8.4.49) @@ -129,6 +126,9 @@ importers: typescript: specifier: ^5.6.3 version: 5.6.3 + zod: + specifier: ^3.23.8 + version: 3.23.8 packages/client: dependencies: @@ -1100,6 +1100,9 @@ packages: '@jsdevtools/ono@7.1.3': resolution: {integrity: sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==} + '@kamilkisiela/fast-url-parser@1.1.4': + resolution: {integrity: sha512-gbkePEBupNydxCelHCESvFSFM8XPh1Zs/OAVRW/rKpEqPAl5PbOM90Si8mv9bvnR53uPD2s/FiRxdvSejpRJew==} + '@manypkg/find-root@1.1.0': resolution: {integrity: sha512-mki5uBvhHzO8kYYix/WRy2WX8S3B5wdVSc9D6KcU5lQNglP2yt58/VfLuAK49glRXChosY8ap2oJ1qgma3GUVA==} @@ -1854,6 +1857,18 @@ packages: '@vitest/utils@2.1.3': resolution: {integrity: sha512-xpiVfDSg1RrYT0tX6czgerkpcKFmFOF/gCr30+Mve5V2kewCy4Prn1/NDMSRwaSmT7PRaOF83wu+bEtsY1wrvA==} + '@whatwg-node/fetch@0.10.1': + resolution: {integrity: sha512-gmPOLrsjSZWEZlr9Oe5+wWFBq3CG6fN13rGlM91Jsj/vZ95G9CCvrORGBAxMXy0AJGiC83aYiHXn3JzTzXQmbA==} + engines: {node: '>=18.0.0'} + + '@whatwg-node/node-fetch@0.7.2': + resolution: {integrity: sha512-OAAEIbyspvQwkcRGutYN3D0a+hzQogvcZ7I3hf6vg742ZEq52yMJTGtkwjl3KZRmzzUltd/oEMxEGsXFLjnuLQ==} + engines: {node: '>=18.0.0'} + + '@whatwg-node/server@0.9.55': + resolution: {integrity: sha512-FW04dJZfgBGaGoHQosCTeSOXKksCVzMLMV5YZPMpUfEmkH8VeDjCIMguvw2cKgrjnLjwQ1J3irLg2eNQbLxLNg==} + engines: {node: '>=18.0.0'} + acorn-jsx@5.3.2: resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} peerDependencies: @@ -2123,9 +2138,6 @@ packages: resolution: {integrity: sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==} engines: {node: '>= 0.6'} - cookie-es@1.2.2: - resolution: {integrity: sha512-+W7VmiVINB+ywl1HGXJXmrqkOhpKrIiVZV6tQuV54ZyQC7MMuBt81Vc336GMLoHBq5hV/F9eXgt5Mnx0Rha5Fg==} - copy-anything@3.0.5: resolution: {integrity: sha512-yCEafptTtb4bk7GLEQoM8KVJpxAfdBJYaXyzQEgQQQgYrZiDp8SJmGKlYza6CYjEDNstAdNdKA3UuoULlEbS6w==} engines: {node: '>=12.13'} @@ -2323,6 +2335,9 @@ packages: fast-content-type-parse@2.0.0: resolution: {integrity: sha512-fCqg/6Sps8tqk8p+kqyKqYfOF0VjPNYrqpLiqNl0RBKmD80B080AJWVV6EkSkscjToNExcXg1+Mfzftrx6+iSA==} + fast-decode-uri-component@1.0.1: + resolution: {integrity: sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg==} + fast-deep-equal@3.1.3: resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} @@ -2330,6 +2345,9 @@ packages: resolution: {integrity: sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==} engines: {node: '>=8.6.0'} + fast-querystring@1.1.2: + resolution: {integrity: sha512-g6KuKWmFXc0fID8WWH0jit4g0AGBoJhCkJMb1RmbsSEUNvQ+ZC8D6CUZ+GtF8nMzSPXnhiePyyqqipzNNEnHjg==} + fast-uri@3.0.3: resolution: {integrity: sha512-aLrHthzCjH5He4Z2H9YZ+v6Ujb9ocRuW6ZzkJQOrTxleEijANq4v1TsaPaVG1PZcuurEzrLcWRyYBYXD5cEiaw==} @@ -3536,9 +3554,6 @@ packages: sprintf-js@1.0.3: resolution: {integrity: sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==} - srvx@0.1.1: - resolution: {integrity: sha512-SSQJNUQygHso16xWOurmc0o0+tcQRZcnJy4sb+eJqjLu/IVAPB+bUSSL+qIqlmstNhFw1bjbVjWGgzLiNV/fNg==} - stackback@0.0.2: resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==} @@ -3833,6 +3848,9 @@ packages: uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} + urlpattern-polyfill@10.0.0: + resolution: {integrity: sha512-H/A06tKD7sS1O1X2SshBVeA5FLycRpjqiBeqGKmBwBDBy28EnRjORxTNe269KSSr5un5qyWi1iL61wLxpd+ZOg==} + use-callback-ref@1.3.2: resolution: {integrity: sha512-elOQwe6Q8gqZgDA8mrh44qRTQqpIHDcZ3hXTLjBe1i4ph8XpNJnO+aQf3NaG+lriLopI4HMx9VjQLfPQ6vhnoA==} engines: {node: '>=10'} @@ -4706,6 +4724,8 @@ snapshots: '@jsdevtools/ono@7.1.3': {} + '@kamilkisiela/fast-url-parser@1.1.4': {} + '@manypkg/find-root@1.1.0': dependencies: '@babel/runtime': 7.26.0 @@ -5549,6 +5569,23 @@ snapshots: loupe: 3.1.2 tinyrainbow: 1.2.0 + '@whatwg-node/fetch@0.10.1': + dependencies: + '@whatwg-node/node-fetch': 0.7.2 + urlpattern-polyfill: 10.0.0 + + '@whatwg-node/node-fetch@0.7.2': + dependencies: + '@kamilkisiela/fast-url-parser': 1.1.4 + busboy: 1.6.0 + fast-querystring: 1.1.2 + tslib: 2.8.0 + + '@whatwg-node/server@0.9.55': + dependencies: + '@whatwg-node/fetch': 0.10.1 + tslib: 2.8.0 + acorn-jsx@5.3.2(acorn@8.13.0): dependencies: acorn: 8.13.0 @@ -5821,8 +5858,6 @@ snapshots: dependencies: safe-buffer: 5.2.1 - cookie-es@1.2.2: {} - copy-anything@3.0.5: dependencies: is-what: 4.1.16 @@ -6079,6 +6114,8 @@ snapshots: fast-content-type-parse@2.0.0: {} + fast-decode-uri-component@1.0.1: {} + fast-deep-equal@3.1.3: {} fast-glob@3.3.2: @@ -6089,6 +6126,10 @@ snapshots: merge2: 1.4.1 micromatch: 4.0.8 + fast-querystring@1.1.2: + dependencies: + fast-decode-uri-component: 1.0.1 + fast-uri@3.0.3: {} fastq@1.17.1: @@ -7684,10 +7725,6 @@ snapshots: sprintf-js@1.0.3: {} - srvx@0.1.1: - dependencies: - cookie-es: 1.2.2 - stackback@0.0.2: {} std-env@3.7.0: {} @@ -7994,6 +8031,8 @@ snapshots: punycode: 2.3.1 optional: true + urlpattern-polyfill@10.0.0: {} + use-callback-ref@1.3.2(@types/react@18.3.12)(react@18.3.1): dependencies: react: 18.3.1