Very useful to create routes, and is necessary to insure good type inference. The parameters are given as an object, with the following keys:
typeDefineRouteParams= {// method and url are required method:"get"|"post"|"put"|"patch"|"delete", url:string// must be a relative ('/something') or absolute url ('https://placeholder.com')// the following are optionnal, and will be considered void if not provided requestBodySchema:z.Schema<RequestBody>, queryParamsSchema:z.Schema<QueryParams>, headersSchema:z.Schema<Headers>, responses: { [statusCode]:z.Schema<Responses>, }}
It is important to note that all the type safety and inference will comme from these definitions. It is important that they are accurate. RequestBody, QueryParams, Headers and Responses will be directly infered from the zod schemas.
Example
The whole documentation will be referencing the following route definitions
import { defineRoute, defineRoutes } from"shared-routes";import { z } from"zod";constbookSchema=z.object({ id:z.string(), title:z.string(), author:z.string(),});typeBook=z.infer<typeof bookSchema>;exportconstbookRoutes=defineRoutes({ getBooks:defineRoute({// you have to define the method and the url : method:"get", url:"/books",// can be absolute or relative// you can define the query params, and the response body : queryParamsSchema:z.object({ titleContains:z.string().optional(), authorContains:z.string().optional(), }), responses: { 200:z.array(bookSchema) }, }), addBook:defineRoute({ method:"post", url:"/books",// you can define specific headers : headersSchema:z.object({ authorization:z.string(), }),// you can define the request body : requestBodySchema:z.object({ title:z.string(), author:z.string(), }), responses: { 201:z.object({ bookId:z.number() }) }, }),// you can also define the path params : getBookById:defineRoute({ method:"get", url:"/books/:bookId", responses: {200: bookSchema,404:z.void(), }, }),});
Use it where needed
You can now take advantage of the routes definitions with many different adapters: