one
Version:
One is a new React Framework that makes Vite serve both native and web.
31 lines (26 loc) • 785 B
text/typescript
import type { OneRouter } from './interfaces/router'
type MaybePromise<T> = T | Promise<T>
type APIRouteHandler<Params extends Record<string, string> = Record<string, string>> = (
request: Request,
context: { params: Params }
) => MaybePromise<Response>
/**
* Type helper for API route handlers with typed params.
*
* @example
* ```ts
* // app/api/users/[id]+api.ts
* import { createAPIRoute } from 'one'
*
* export const GET = createAPIRoute<'/api/users/[id]'>((request, { params }) => {
* // params.id is typed as string
* return Response.json({ id: params.id })
* })
* ```
*/
export function createAPIRoute<Path extends string = string>(
handler: APIRouteHandler<OneRouter.RouteType<Path>['Params']>
) {
return handler
}
export type { APIRouteHandler }