{"id":21419079,"url":"https://github.com/graywolftech/rest-ts","last_synced_at":"2026-07-06T01:31:33.633Z","repository":{"id":40718288,"uuid":"262834357","full_name":"graywolftech/rest-ts","owner":"graywolftech","description":"End-to-end REST API typings using TypeScript","archived":false,"fork":false,"pushed_at":"2022-12-12T18:00:47.000Z","size":638,"stargazers_count":1,"open_issues_count":17,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-29T13:21:44.938Z","etag":null,"topics":[],"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/graywolftech.png","metadata":{"files":{"readme":"README.md","changelog":"changelogs/v0.1.1-CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-10T16:59:20.000Z","updated_at":"2021-02-25T18:22:13.000Z","dependencies_parsed_at":"2023-01-27T23:16:31.637Z","dependency_job_id":null,"html_url":"https://github.com/graywolftech/rest-ts","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graywolftech%2Frest-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graywolftech%2Frest-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graywolftech%2Frest-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graywolftech%2Frest-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graywolftech","download_url":"https://codeload.github.com/graywolftech/rest-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243918998,"owners_count":20368807,"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":"2024-11-22T19:35:17.839Z","updated_at":"2025-10-24T04:51:28.265Z","avatar_url":"https://github.com/graywolftech.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rest-ts\nEnd-to-end REST API typings using TypeScript.\n\n\u003e Original idea taken from @rawrmaan [restyped](https://github.com/rawrmaan/restyped). This library offers stricter type checking and updated dependencies. Eventually, it will also incorporate [`io-ts`](https://github.com/gcanti/io-ts) for automatic boundary type-checking.\n\n## The Idea\nDefine your REST API such that it can be consumed by your frontend and backend. Use simple type wrappers around client libraries such as [axios](https://github.com/axios/axios) and router libraries such as [express](https://expressjs.com/) which consume your API definition and then type check your requests / route definitions.\n\nThe API definition would look something like this:\n\n```typescript\nexport type API = {\n  \"/plant-potato\": {\n    POST: {\n      body: { size: number; weight: number };\n      response: { result: \"one-potato\" | \"two-potato\" };\n    };\n  };\n};\n```\n\nSubsequently, when defining your router, everything is type checked:\n\n```typescript\n// ERROR: Argument of type '\"/plan-potato\"' is not assignable to parameter of type '\"/plant-potato\"'.ts(2345)\nrouter.post(\"/plan-potato\", async (req) =\u003e {\n  ...\n})\n\nrouter.post(\"/plant-potato\", async (req) =\u003e {\n  ...\n  // ERROR Property 'height' does not exist on type '{ size: number; weight: number; }'. Did you mean 'weight'?ts(2551)\n  plantPotato(req.body.height);\n  ...\n});\n\n// ERROR: Type '\"three-potato\"' is not assignable to type '\"one-potato\" | \"two-potato\"'.ts(2345)\nrouter.post(\"/plant-potato\", async (req) =\u003e {\n  return {\n    result: \"three-potato\"\n  }\n});\n```\n\nNext, when calling your API from your client, everything is also type checked:\n\n```typescript\n// ERROR Argument of type '\"/plan-potato\"' is not assignable to parameter of type '\"/plant-potato\"'.ts(2345)\nclient.post(\"/plan-potato\", { size: 2, weight: 55 });\n\n// ERROR Property 'weight' is missing in type '{ size: number; }' but required in type '{ size: number; weight: number; }'.ts(2345)\nclient.post(\"/plant-potato\", { size: 2 });\n\nconst response = await client.post(\"/plant-potato\", { size: 2, weight: 55 });\n// ERROR: This condition will always return 'false' since the types '\"one-potato\" | \"two-potato\"' and '\"three-potato\"' have no overlap.ts(2367)\nresponse.data.result === \"three-potato\";\n```\n\n## Why use Type REST?\n\u003e **End-to-end typing**: Share request and response types between your client and server for ease of use and peace of mind.  \n\u003e **Unopinionated**: Works with any new or existing REST API.  \n\u003e **Universal**: Can support any server framework or REST client.  \n\u003e **~~Lightweight~~ Weightless**: Client and server implementations add no runtime code--It's Just Types™.  \n\u003e **Use existing syntax**: Declare and call your routes the same way you always have.  \n\u003e **Great for private APIs**: Keep API clients across your organization in sync with the latest changes.  \n\u003e **Great for public APIs**: Create a RESTyped definition so TypeScript users can consume your API fully typed.  \n\u003e **Easy to learn and use**: Start using RESTyped in less than one minute per route.\n\nQuote taken from [restyped](https://github.com/rawrmaan/restyped#benefits).\n\n## Installation \u0026 Usage\nThis will talk you through the steps to install and use `rest-ts`. Note that there are examples below after you complete these steps.\n\n1. Make sure your TypeScript version is at least `3.0` as the `unknown` type is used.\n\n2. Define your common API. Ensure that you are using a `type` and not an `interface` or you will you receive a `TypeScript` error.\n\n```typescript\nexport type RestAPI = {\n  ...\n};\n```\n\n3. Install [`@graywolfai/rest-ts-express`](http://npmjs.com/package/@graywolfai/rest-ts-express) in your backend.\n\n```shell\nnpm install --save @graywolfai/rest-ts-express express\n```\n\n\u003e Note: [`express`](https://www.npmjs.com/package/express) is a peer dependency.\n\n4. Import and create your `express` router.\n\n```typescript\nimport * as express from \"express\";\nimport { TypedAsyncRouter } from \"rest-ts-express\";\nimport { RestAPI } from \"path/to/api\";\n\nconst app = express();\nconst router = TypedAsyncRouter\u003cRestAPI\u003e(app);\n\n// Define your routes like normal\nrouter.get(\"/some/route\", async (req) =\u003e {\n  ...\n  return {\n    // Your response goes here\n    ...\n  }\n});\n```\n\n\u003e Typically, you send your response using `router.send` or `router.json` but that doesn't work that well for type checking. How can we be *sure* that you actually return the expected response? Furthermore, using `async/await` is a *pain* as you need to [explicitly catch all errors](https://zellwk.com/blog/async-await-express/) and send them using `next(error)`. We define a light wrapper around your `async` handler to remedy both of these issues.\n\n```typescript\n// method = \"get\" | \"post\" | \"put\" | \"delete\" | \"patch\" | \"options\" | \"head\"\nrouter[method](path, (req, res, next) =\u003e {\n  handler(req, res, next)\n    .then((result) =\u003e {\n      if (!res.headersSent) {\n        res.send(result);\n      }\n    })\n    .catch(next);\n});\n```\n\n5. Install [`@graywolfai/rest-ts-axios`](http://npmjs.com/package/@graywolfai/rest-ts-axios) in your frontend.\n\n```shell\nnpm install --save @graywolfai/rest-ts-axios axios\n```\n\n\u003e Note: [`axios`](https://www.npmjs.com/package/axios) is a peer dependency.\n\n6. Import and create an `axios` instance.\n\n```typescript\nimport axios from \"rest-ts-axios\";\nimport { RestAPI } from \"path/to/api\";\n\n// All of the normal configuration (e.g. `baseUrl`) is supported\n// See the axios documentation for more information\nconst client = axios.create\u003cRestAPI\u003e();\n\n// Use the axios client like normal\nconst results = await client.get(\"/some/route\");\n```\n\n\u003e NOTE: Currently, only `express` and `axios` are supported. Other libraries can easily be added, just create an issue and/or PR!\n\n## Specification\nHere is the specification as defined in [`@graywolfai/rest-ts`](https://npmjs.com/package/@graywolfai/rest-ts).\n\n```typescript\nexport interface RestTSRoute {\n  params?: { [k: string]: string };\n  query?: { [k: string]: string };\n  body?: any;\n  response?: any;\n}\n\n// This is the type that your API must conform to.\n// Note that you do NOT need to import this or use it in your code in any way.\nexport type RestTSBase = {\n  // e.g. '/orders'\n  [route: string]: {\n    // 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS'\n    [method: string]: RestTSRoute;\n  };\n};\n```\n\nHere is an example API that uses `params`, `query`, `body` and `response`.\n```typescript\ninterface User {\n  email: string;\n  name: string;\n}\n\nexport interface SocialAPI {\n  \"/users\": {\n    // Route name (without prefix, if you have one)\n    GET: {\n      // Any valid HTTP method\n      query: {\n        // Query string params (e.g. /users?includeProfilePics=true)\n        includeProfilePics?: \"true\" | \"false\";\n      };\n      response: User[]; // JSON response\n    };\n  };\n\n  \"/user/:id/send-message\": {\n    POST: {\n      params: {\n        // Inline route params\n        id: string;\n      };\n      body: {\n        // JSON request body\n        message: string;\n      };\n      response: {\n        // JSON response\n        success: boolean;\n      };\n    };\n  };\n}\n```\n\n## Limitations\n#### No route definition guards\n`@graywolfai/rest-ts-express` cannot guard against unhandled routes. If you define a get request to `/users` but forget to handle your route as such:\n\n```typescript\napp.get('/users', () =\u003e {\n  ...\n})\n```\n\nNo compile time error will be thrown!\n\n#### Inline parameters can't be type checked on the client\n`@graywolfai/rest-ts-axios` cannot type check inline route parameters. Consider the `SocialAPI` defined above and the following example using `@graywolfai/rest-ts-axios`:\n\n```typescript\n// ERROR: Argument of type '\"/user/12345/send-message\"' is not assignable to parameter of type 'never'.ts(2345)\nclient.post(\"/user/12345/send-message\", { message: \"some message\" });\n```\n\nThe solution, which isn't perfect, is to cast the URL with the inline param as the correct URL.\n\n```typescript\nclient.post(\"/user/12345/send-message\" as \"/user/:id/send-message\", { message: \"some message\" });\n```\n\n## Contributing\n### Prerequisites\nThis repository uses [lerna](https://github.com/lerna/lerna). Make sure to install the dependencies before proceeding.\n```\nnpm install\n```\n\n### Setup\nInstall all of the dependencies and link the packages.\n```\nnpm run bootstrap\n```\n\nThat's it! Now you can enter the packages and make modifications :) This is an interesting library since there isn't actually any code to run.\n\nFinally, ensure the your code is correctly formatted!\n```\nnpm run format:write\n```\n\n### Releasing\nFirst, make sure that there is a `vX.X.X-CHANGELOG.md` in `changelogs/`. Then, bump the version of all of the packages at once!\n```\nnpx lerna version --force-publish\n```\n\u003e It is important that you add `--force-publish` as we want all packages to be updated at once.\n\nThis will push a new tag to `GitHub` which will kick off the publish workflow.\n\nThat's it 🔥\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraywolftech%2Frest-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraywolftech%2Frest-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraywolftech%2Frest-ts/lists"}