{"id":18582096,"url":"https://github.com/lorefnon/ts-json-rpc","last_synced_at":"2025-08-03T10:04:43.978Z","repository":{"id":182498777,"uuid":"651853075","full_name":"lorefnon/ts-json-rpc","owner":"lorefnon","description":"Type-safe codegen-free isomorphic RPC solution for Typescript","archived":false,"fork":false,"pushed_at":"2025-06-09T08:11:05.000Z","size":246,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-27T04:21:50.711Z","etag":null,"topics":["api","nodejs","rpc","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lorefnon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-06-10T09:45:09.000Z","updated_at":"2025-06-09T08:11:09.000Z","dependencies_parsed_at":"2025-04-10T23:42:50.134Z","dependency_job_id":"12f09655-6d37-41bd-8c49-415eb09f0ac4","html_url":"https://github.com/lorefnon/ts-json-rpc","commit_stats":null,"previous_names":["lorefnon/ts-json-rpc"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/lorefnon/ts-json-rpc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Fts-json-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Fts-json-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Fts-json-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Fts-json-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorefnon","download_url":"https://codeload.github.com/lorefnon/ts-json-rpc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Fts-json-rpc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268525073,"owners_count":24264095,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","nodejs","rpc","typescript"],"created_at":"2024-11-07T00:09:14.044Z","updated_at":"2025-08-03T10:04:43.925Z","avatar_url":"https://github.com/lorefnon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-json-rpc\n\nLightweight [JSON-RPC](https://www.jsonrpc.org/specification) solution for TypeScript projects\n\n## Features:\n\n- 👩‍🔧 Service definition through Zod-based contracts\n- 📜 JSON-RPC 2.0 protocol\n- 🕵️ Full IDE autocompletion\n- 🪶 Tiny footprint (\u003c 1kB)\n- 🌎 Support for Deno and edge runtimes\n- 🚫 No code generation step\n\n## Basic Usage\n\nDefine a shared service contract and export the type of service:\n\n```ts\nimport { z } from \"zod\";\nimport type { ZServiceType } from \"@lorefnon/ts-json-rpc/lib/zod\";\nimport { ZService } from \"@lorefnon/ts-json-rpc/lib/zod\";\n\nexport const MyServiceDef = ZService.define({\n\n  hello: z.function()\n    .args(z.string())\n    .returns(z.string().promise()),\n\n  // More methods here ...\n});\n\nexport type MyService = ZServiceType\u003ctypeof MyServiceDef\u003e\n// { hello: (arg: string) =\u003e Promise\u003cstring\u003e, ... }\n```\n\nDefine a server-side implementation of this service:\n\n```ts\nexport const MyServiceImpl = MyServiceDef.implement(() =\u003e ({\n\n  async hello(name) { // name is inferred as string\n    return `Hello ${name}!`;\n  },\n\n  // Implement other methods...\n}));\n```\n\nCreate a server with a route to handle the API requests:\n\n```ts\nimport express from \"express\";\nimport { rpcHandler } from \"@lorefnon/ts-json-rpc/lib/express\";\n\nconst app = express();\napp.use(express.json());\napp.post(\"/api\", rpcHandler(MyServiceImpl));\napp.listen(3000);\n```\n\n\u003e **Note**\n\u003e You can also use @lorefnon/ts-json-rpc in servers other than Express.\n\u003e Check out to docs below for [examples](#support-for-other-runtimes).\n\n\nOn the client-side, import the shared type and create a typed `rpcClient` with it:\n\n```ts\nimport { rpcClient, HttpPostTransport } from \"@lorefnon/ts-json-rpc/lib/client\";\n\n// Import the type (not the implementation!)\nimport type { MyService } from \"../shared/MyService\";\n\n// Create a typed client:\nconst client = rpcClient\u003cMyService\u003e({\n  transport: new HttpPostTransport({\n    url: \"http://localhost:3000/api\"\n  })\n});\n\n// Call a remote method:\nconsole.log(await client.hello(\"world\"));\n```\n\nThat's all it takes to create a type-safe JSON-RPC API. 🎉\n\n## Demo\n\nYou can play with a live example over at StackBlitz:\n\n[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/typed-rpc-express-qeemgh?file=main.ts)\n\n# Advanced Usage\n\n## Service factories vs singletons\n\nIt is common for services to be created per request. `DefaultServiceImpl` in above example\nis a service factory ie. a function that creates and returns an implementation of the service contract.\n\nrpcHandler will invoke this function for every request to create a service object that handles that particular\nrequest.\n\nThis is convenient if you need to access the request (more on this below) but if you don't, instead of a function\nyou could also create an instance and pass that to rpcHandler:\n\n```ts\napp.post(\"/api\", rpcHandler(DefaultServiceImpl({})));\n```\n\nNow, we have a singleton service that handles all requests.\n\n## Service context\n\nThe function passed to `ServiceDef.implement` can accept a context argument which is available to all the methods.\n\n```ts\ninterface ServiceContext {\n  currentUser?: { name: string }\n}\n\nexport const MyServiceImpl = ServiceDef.implement((context: ServiceContext) =\u003e ({\n\n  async hello() {\n    return `Hello ${context.currentUser?.name ?? \"Stranger\"}!`;\n  },\n\n  // Implement other methods...\n}));\n```\n\nYou are responsible for passing this `context` to `DefaultServiceImpl`.\n\n### Accessing the request\n\nMost common use case for context is to get access to the request object.\n\nSo, by default rpcHandler will simply pass the request object to service factory as context.\n\nHowever, if you want to ensure that your service implementation is not tied to a specific server implementation (eg. express) you can also\nextract what you need from the request and pass it to the service factory.\n\n```ts\napp.post(\n  \"/api\",\n  rpcHandler((req) =\u003e MyServiceImpl(req.headers))\n);\n```\n\nThis is also useful if you need to inject any additional objects (eg. database pool instance) into the service.\n\n## Support for other runtimes\n\nThe generic `@lorefnon/ts-json-rpc/server` package can be used with any server framework or (edge-) runtime.\n\n### Fastify\n\nWith [Fastify](https://www.fastify.io/), you would use `@lorefnon/ts-json-rpc` like this:\n\n```ts\nimport { handleRpc, isJsonRpcRequest } from \"@lorefnon/ts-json-rpc/lib/server\";\n\nfastify.post(\"/api\", async (req, reply) =\u003e {\n  if (isJsonRpcRequest(req.body)) {\n    const res = await handleRpc(req.body, Service(req));\n    reply.send(res);\n  }\n});\n```\n\n\n## Sending custom headers\n\nA client can send custom request headers by providing a `getHeaders` function:\n\n```ts\nconst client = rpcClient\u003cMyService\u003e(apiUrl, {\n  getHeaders() {\n    return {\n      Authorization: auth,\n    };\n  },\n});\n```\n\n\u003e **Note**\n\u003e The `getHeaders` function can also be `async`.\n\n## CORS credentials\n\nTo include credentials in cross-origin requests, pass `credentials: 'include'` as option.\n\n## React hooks\n\nWhile `@lorefnon/ts-json-rpc` itself does not provide any built-in UI framework integrations,\nyou can pair it with [react-api-query](https://www.npmjs.com/package/react-api-query),\na thin wrapper around _TanStack Query_. A type-safe match made in heaven. 💕\n\n# License\n\nMIT\n\n# Lineage\n\nThis implementation is based on past work by [Felix Gnass](https://indieweb.social/@fgnass)\nin [typed-rpc](https://github.com/fgnass/typed-rpc).\n\nThe typed-rpc repo is more minimal in its focus (eg. runtime type checking is explicitly not a goal)\nand does not appear to be accepting pull requests.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Fts-json-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florefnon%2Fts-json-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Fts-json-rpc/lists"}