()
| 33 | |
| 34 | class JStack { |
| 35 | init<E extends Env = any>() { |
| 36 | return { |
| 37 | /** |
| 38 | * Type-safe router factory function that creates a new router instance. |
| 39 | * |
| 40 | * @template T - Record of operation types (get/post/websockets) |
| 41 | * @template E - Environment type for the router |
| 42 | * @returns {Router<T, E>} A new router instance with type-safe procedure definitions |
| 43 | * |
| 44 | * @example |
| 45 | * const userRouter = router({ |
| 46 | * getUser: publicProcedure |
| 47 | * .input(z.object({ id: z.string() })) |
| 48 | * .get(async ({ input }) => { |
| 49 | * return { id: input.id, name: "John Doe" } |
| 50 | * }), |
| 51 | * |
| 52 | * createUser: publicProcedure |
| 53 | * .input(z.object({ name: z.string() })) |
| 54 | * .post(async ({ input }) => { |
| 55 | * return { id: "123", name: input.name } |
| 56 | * }) |
| 57 | * }) |
| 58 | */ |
| 59 | router, |
| 60 | mergeRouters, |
| 61 | middleware: <T = {}, R = void>( |
| 62 | middleware: MiddlewareFunction<T, R, E> |
| 63 | ): MiddlewareFunction<T, R, E> => middleware, |
| 64 | fromHono, |
| 65 | procedure: new Procedure<E>(), |
| 66 | defaults: { |
| 67 | /** |
| 68 | * CORS middleware configuration with default settings for API endpoints. |
| 69 | * |
| 70 | * @default |
| 71 | * - Allows 'x-is-superjson' and 'Content-Type' in headers |
| 72 | * - Exposes 'x-is-superjson' in headers |
| 73 | * - Accepts all origins |
| 74 | * - Enables credentials |
| 75 | */ |
| 76 | cors: cors({ |
| 77 | allowHeaders: ["x-is-superjson", "Content-Type"], |
| 78 | exposeHeaders: ["x-is-superjson"], |
| 79 | origin: (origin) => origin, |
| 80 | credentials: true, |
| 81 | }), |
| 82 | /** |
| 83 | * Global error handler for API endpoints. |
| 84 | * |
| 85 | * @example |
| 86 | * // Client-side error handling |
| 87 | * const { mutate } = useMutation({ |
| 88 | * onError: (err: HTTPException) => { |
| 89 | * if (err.status === 401) { |
| 90 | * console.log(err.message) // Handle unauthorized |
| 91 | * } |
| 92 | * } |
no outgoing calls
no test coverage detected