@ts-rest/express
Advanced tools
Comparing version 1.3.0 to 2.0.1
# @ts-rest/express | ||
## 2.0.1 | ||
### Patch Changes | ||
- 119aed6: Bump versions to 2.0.1 | ||
## 2.0.0 | ||
### Major Changes | ||
- 4792b26: Change contract to support multiple responses, for different statuses | ||
- 4792b26: Add error handling support to express | ||
- c88fb99: Rename data to body to be more HTTP spec compliant | ||
## 1.3.0 | ||
@@ -4,0 +18,0 @@ |
{ | ||
"name": "@ts-rest/express", | ||
"version": "1.3.0", | ||
"version": "2.0.1", | ||
"type": "commonjs", | ||
@@ -10,6 +10,6 @@ "main": "./src/index.js", | ||
"express": "4.18.1", | ||
"zod": "^3.17.10", | ||
"@ts-rest/core": "1.3.0", | ||
"zod": "^3.18.0", | ||
"@ts-rest/core": "2.0.1", | ||
"@swc/helpers": "~0.4.3" | ||
} | ||
} |
import { Express } from 'express'; | ||
import { z, ZodTypeAny } from 'zod'; | ||
import { AppRoute, AppRouteMutation, AppRouteQuery, AppRouter, Without, ZodInferOrType } from '@ts-rest/core'; | ||
export declare type ApiRouteResponse<T> = { | ||
[K in keyof T]: { | ||
status: K; | ||
body: ZodInferOrType<T[K]>; | ||
}; | ||
}[keyof T]; | ||
declare type AppRouteQueryImplementation<T extends AppRouteQuery> = (input: Without<{ | ||
params: Parameters<T['path']>[0] extends undefined ? never : Parameters<T['path']>[0]; | ||
query: T['query'] extends ZodTypeAny ? z.infer<T['query']> : null; | ||
}, never>) => Promise<ZodInferOrType<T['response']>>; | ||
}, never>) => Promise<ApiRouteResponse<T['responses']>>; | ||
declare type AppRouteMutationImplementation<T extends AppRouteMutation> = (input: Without<{ | ||
@@ -12,3 +18,3 @@ params: Parameters<T['path']>[0]; | ||
body: T['body'] extends ZodTypeAny ? z.infer<T['body']> : never; | ||
}, never>) => Promise<ZodInferOrType<T['response']>>; | ||
}, never>) => Promise<ApiRouteResponse<T['responses']>>; | ||
declare type AppRouteImplementation<T extends AppRoute> = T extends AppRouteMutation ? AppRouteMutationImplementation<T> : T extends AppRouteQuery ? AppRouteQueryImplementation<T> : never; | ||
@@ -21,3 +27,3 @@ declare type RecursiveRouterObj<T extends AppRouter> = { | ||
}; | ||
export declare const createExpressEndpoints: <T extends RecursiveRouterObj<TRouter>, TRouter extends AppRouter>(schema: AppRouter, router: T, app: Express) => void; | ||
export declare const createExpressEndpoints: <T extends RecursiveRouterObj<TRouter>, TRouter extends AppRouter>(schema: TRouter, router: T, app: Express) => void; | ||
export {}; |
@@ -18,3 +18,5 @@ "use strict"; | ||
}; | ||
const recursivelyApplyExpressRouter = (router, path, routeTransformer)=>{ | ||
const recursivelyApplyExpressRouter = (// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
router, path, // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
routeTransformer)=>{ | ||
if (typeof router === 'object') { | ||
@@ -31,3 +33,4 @@ for(const key in router){ | ||
}; | ||
const transformAppRouteQueryImplementation = (route, schema, app)=>{ | ||
const transformAppRouteQueryImplementation = (// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
route, schema, app)=>{ | ||
const path = (0, _core.getAppRoutePathRoute)(schema); | ||
@@ -42,9 +45,11 @@ console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
} | ||
return res.json(await route({ | ||
const result = await route({ | ||
params: req.params, | ||
query: req.query | ||
})); | ||
}); | ||
return res.status(Number(result.status)).json(result.body); | ||
}); | ||
}; | ||
const transformAppRouteMutationImplementation = (route, schema, app)=>{ | ||
const transformAppRouteMutationImplementation = (// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
route, schema, app)=>{ | ||
const path = (0, _core.getAppRoutePathRoute)(schema); | ||
@@ -72,3 +77,3 @@ console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
}); | ||
return res.json(result); | ||
return res.status(Number(result.status)).json(result.body); | ||
} catch (e) { | ||
@@ -111,7 +116,11 @@ console.error(`[ts-rest] Error on ${method} ${path}`, e); | ||
const routerViaPath = (0, _core.getValue)(schema, path.join('.')); | ||
if (!routerViaPath) { | ||
throw new Error(`[ts-rest] No router found for path ${path.join('.')}`); | ||
} | ||
if ((0, _core.isAppRoute)(routerViaPath)) { | ||
if (routerViaPath.__type === 'AppRouteMutation') { | ||
if (routerViaPath.__tsType === 'AppRouteMutation') { | ||
transformAppRouteMutationImplementation(route, routerViaPath, app); | ||
} else { | ||
transformAppRouteQueryImplementation(route, routerViaPath, app); | ||
transformAppRouteQueryImplementation(// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
route, routerViaPath, app); | ||
} | ||
@@ -118,0 +127,0 @@ } else { |
Sorry, the diff of this file is not supported yet
18967
157