@ts-rest/express
Advanced tools
Comparing version 2.1.0 to 3.0.0
# @ts-rest/express | ||
## 3.0.0 | ||
### Major Changes | ||
- 895112a: Migrate paths to a string rather than function | ||
## 2.1.0 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "@ts-rest/express", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"type": "commonjs", | ||
@@ -11,5 +11,5 @@ "main": "./src/index.js", | ||
"zod": "^3.18.0", | ||
"@ts-rest/core": "2.1.0", | ||
"@ts-rest/core": "3.0.0", | ||
"@swc/helpers": "~0.4.11" | ||
} | ||
} |
import { Express } from 'express'; | ||
import { z, ZodTypeAny } from 'zod'; | ||
import { AppRoute, AppRouteMutation, AppRouteQuery, AppRouter, Without, ZodInferOrType } from '@ts-rest/core'; | ||
import { AppRoute, AppRouteMutation, AppRouteQuery, AppRouter, Without, ZodInferOrType, PathParams } from '@ts-rest/core'; | ||
export declare type ApiRouteResponse<T> = { | ||
@@ -11,7 +11,7 @@ [K in keyof T]: { | ||
declare type AppRouteQueryImplementation<T extends AppRouteQuery> = (input: Without<{ | ||
params: Parameters<T['path']>[0] extends undefined ? never : Parameters<T['path']>[0]; | ||
params: PathParams<T>; | ||
query: T['query'] extends ZodTypeAny ? z.infer<T['query']> : null; | ||
}, never>) => Promise<ApiRouteResponse<T['responses']>>; | ||
declare type AppRouteMutationImplementation<T extends AppRouteMutation> = (input: Without<{ | ||
params: Parameters<T['path']>[0]; | ||
params: PathParams<T>; | ||
query: T['query'] extends ZodTypeAny ? z.infer<T['query']> : never; | ||
@@ -18,0 +18,0 @@ body: T['body'] extends ZodTypeAny ? z.infer<T['body']> : never; |
@@ -34,5 +34,4 @@ "use strict"; | ||
route, schema, app)=>{ | ||
const path = (0, _core.getAppRoutePathRoute)(schema); | ||
console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
app.get(path, async (req, res)=>{ | ||
console.log(`[ts-rest] Initialized ${schema.method} ${schema.path}`); | ||
app.get(schema.path, async (req, res)=>{ | ||
const zodQueryIssues = (0, _core.returnZodErrorsIfZodSchema)(schema.query, req.query); | ||
@@ -45,2 +44,3 @@ if (zodQueryIssues.length > 0) { | ||
const result = await route({ | ||
// @ts-expect-error because the decorator shape is any | ||
params: req.params, | ||
@@ -54,4 +54,3 @@ query: req.query | ||
route, schema, app)=>{ | ||
const path = (0, _core.getAppRoutePathRoute)(schema); | ||
console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
console.log(`[ts-rest] Initialized ${schema.method} ${schema.path}`); | ||
const method = schema.method; | ||
@@ -73,2 +72,3 @@ const callback = async (req, res)=>{ | ||
const result = await route({ | ||
// @ts-expect-error because the decorator shape is any | ||
params: req.params, | ||
@@ -80,3 +80,3 @@ body: req.body, | ||
} catch (e) { | ||
console.error(`[ts-rest] Error on ${method} ${path}`, e); | ||
console.error(`[ts-rest] Error on ${method} ${schema.path}`, e); | ||
return res.status(500).send('Internal Server Error'); | ||
@@ -87,12 +87,12 @@ } | ||
case 'DELETE': | ||
app.delete(path, callback); | ||
app.delete(schema.path, callback); | ||
break; | ||
case 'POST': | ||
app.post(path, callback); | ||
app.post(schema.path, callback); | ||
break; | ||
case 'PUT': | ||
app.put(path, callback); | ||
app.put(schema.path, callback); | ||
break; | ||
case 'PATCH': | ||
app.patch(path, callback); | ||
app.patch(schema.path, callback); | ||
break; | ||
@@ -111,3 +111,3 @@ default: | ||
if ((0, _core.isAppRoute)(routerViaPath)) { | ||
if (routerViaPath.__tsType === 'AppRouteMutation') { | ||
if (routerViaPath.method !== 'GET') { | ||
transformAppRouteMutationImplementation(route, routerViaPath, app); | ||
@@ -114,0 +114,0 @@ } else { |
Sorry, the diff of this file is not supported yet
18015