{"id":26647705,"url":"https://github.com/ledouxm/pocket-rpc","last_synced_at":"2025-03-24T23:56:34.845Z","repository":{"id":189491134,"uuid":"680751025","full_name":"ledouxm/pocket-rpc","owner":"ledouxm","description":null,"archived":false,"fork":false,"pushed_at":"2023-09-04T14:31:07.000Z","size":171,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-28T15:45:16.936Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ledouxm.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}},"created_at":"2023-08-20T09:28:57.000Z","updated_at":"2024-05-28T15:45:16.936Z","dependencies_parsed_at":"2023-08-20T12:42:37.636Z","dependency_job_id":"5856a240-4f93-4632-8568-eff5c3ae918f","html_url":"https://github.com/ledouxm/pocket-rpc","commit_stats":null,"previous_names":["ledouxm/pocket-rpc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledouxm%2Fpocket-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledouxm%2Fpocket-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledouxm%2Fpocket-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledouxm%2Fpocket-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ledouxm","download_url":"https://codeload.github.com/ledouxm/pocket-rpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245372338,"owners_count":20604490,"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","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":[],"created_at":"2025-03-24T23:56:34.265Z","updated_at":"2025-03-24T23:56:34.836Z","avatar_url":"https://github.com/ledouxm.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pocket-rpc\n\nEasy to build full typesafe api router\n\nLargely inspired by [tRPC](https://github.com/trpc/trpc)\n\n# Installation\n\n## Server-side:\n\n```\npnpm install @pocket-rpc/server @pocket-rpc/fastify @sinclair/typebox\n```\n\n## Client-side:\n\n```\npnpm install @pocket-rpc/ofetch\n```\n\nAlternatively, you can use @pocket-rpc/ky or @pocket-rpc/axios, or even create a custom one with @pocket-rpc/client\n\n# Usage\n\n## Defining router\n\n```ts\nimport { makeRouter, route } from \"@pocket-rpc/server\";\nimport { Type } from \"@sinclair/typebox\";\n\nconst loginRoute = route({\n    method: \"post\",\n    schema: {\n        body: Type.Object({\n            username: Type.String(),\n            password: Type.String(),\n        }),\n    },\n    handler: (ctx) =\u003e {\n        const { body } = ctx;\n        //       ^? type { username: string, password: string }\n\n        return login(body);\n    },\n});\n\nconst router = makeRouter({\n    // path of this route will be /login\n    login: loginRoute,\n});\n\n// this will be used client-side\nexport type AppRouter = typeof router;\n```\n\nA router can contain multiple routes and/or multiple routers, it will get flatten with a \"/\" separator\n\n```ts\nconst authRouter = makeRouter({ login: loginRoute });\nconst dataRouter = makeRouter({ getData: getDataRoute });\n\nconst router = makeRouter({\n    auth: authRouter,\n    data: dataRouter,\n});\n```\n\nIn this case, the paths will be :\n\n-   /auth/login\n-   /data/getData\n\n## Usage with Fastify\n\n```ts\nconst fastifyInstance = makeFastifyRouter(router);\n\n// You can use fastify as usual for example:\nfastifyInstance.register(cors);\nfastifyInstance.listen({ port: 8080 });\n```\n\n## Client side usage\n\nYou can choose between the 3 existing clients (ofetch, axios or ky), or create a custom one\n\n### Ofetch\n\n```ts\nimport type { AppRouter } from \"../path/to/server/router\";\nimport { makeOfetchClient } from \"@pocket-rpc/ofetch\";\n\nexport const api = makeOfetchClient\u003cAppRouter\u003e({\n    baseURL: \"http://localhost:8080\",\n});\n\napi.post(\"auth/login\", {\n    body: {\n        username: \"...\",\n        password: \"...\",\n    },\n});\n```\n\n### Axios\n\n```ts\nimport { makeAxiosClient } from \"@pocket-rpc/axios\";\n```\n\n### Ky\n\n```ts\nimport { makeKyClient } from \"@pocket-rpc/ky\";\n```\n\n### Custom\n\nYou must define a Fetcher such as:\n\n```ts\nimport { type Fetcher, type Payload, ApiClient } from \"@pocket-rpc/client\";\n\nconst fetcher = async (method: Method, path: string, payload: Payload) =\u003e {\n    const response = await axios.request({\n        method,\n        url: path,\n        params: payload?.query,\n        data: payload?.body,\n        headers: payload?.headers,\n    });\n\n    return response.data;\n};\n```\n\nThen you can use it with ApiClient\n\n```ts\nconst api = new ApiClient\u003cAppRouter\u003e(fetcher);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledouxm%2Fpocket-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fledouxm%2Fpocket-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledouxm%2Fpocket-rpc/lists"}