@ts-rest/core
Advanced tools
Comparing version 0.1.10 to 0.2.0
{ | ||
"name": "@ts-rest/core", | ||
"version": "0.1.10", | ||
"version": "0.2.0", | ||
"description": "RPC-like experience over a regular REST API, with type safe server implementations 🪄", | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
# ts-rest | ||
<p align="center"> | ||
<img src="https://avatars.githubusercontent.com/u/109956939?s=400&u=8bf67b1281da46d64eab85f48255cd1892bf0885&v=4" height=150 /> | ||
</p> | ||
## Motivation | ||
@@ -55,5 +59,5 @@ | ||
```typescript | ||
import { initTsCont } from 'ts-rest-core'; | ||
import { initts-rest } from 'ts-rest-core'; | ||
const c = initTsCont(); | ||
const c = initts-rest(); | ||
@@ -60,0 +64,0 @@ export type Post = { |
export * from './lib/dsl'; | ||
export * from './lib/client'; | ||
export * from './lib/server-express'; | ||
export * from './lib/server-nest'; | ||
export * from './lib/type-utils'; | ||
export * from './lib/server'; |
@@ -7,3 +7,4 @@ "use strict"; | ||
tslib_1.__exportStar(require("./lib/server-express"), exports); | ||
tslib_1.__exportStar(require("./lib/server-nest"), exports); | ||
tslib_1.__exportStar(require("./lib/type-utils"), exports); | ||
tslib_1.__exportStar(require("./lib/server"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -15,3 +15,8 @@ import { z } from 'zod'; | ||
data: TRoute['response']; | ||
error: null; | ||
status: number; | ||
} | { | ||
data: null; | ||
error: string; | ||
status: number; | ||
}>; | ||
@@ -31,2 +36,7 @@ declare type ClientArgs = { | ||
data: unknown; | ||
error: null; | ||
} | { | ||
status: number; | ||
data: null; | ||
error: string; | ||
}>; | ||
@@ -33,0 +43,0 @@ export declare type InitClientReturn<T extends AppRouter> = RecursiveProxyObj<T>; |
@@ -7,4 +7,8 @@ "use strict"; | ||
const defaultApi = ({ path, method, headers, body }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield fetch(path, { method, headers, body }).then((res) => res.json()); | ||
return { status: 200, data: result }; | ||
const result = yield fetch(path, { method, headers, body }); | ||
if (result.ok) { | ||
const json = yield result.json(); | ||
return { status: result.status, data: json, error: null }; | ||
} | ||
return { status: result.status, data: null, error: result.statusText }; | ||
}); | ||
@@ -36,3 +40,3 @@ const getRouteQuery = (route, clientArgs) => { | ||
}); | ||
return { data: result.data, status: result.status }; | ||
return result; | ||
}); | ||
@@ -39,0 +43,0 @@ }; |
@@ -22,3 +22,3 @@ export declare type AppRouteQuery = { | ||
declare type PathFunction = (arg: any) => string; | ||
declare type TsCont = { | ||
declare type tsRest = { | ||
router: <T extends { | ||
@@ -46,3 +46,3 @@ [key: string]: AppRoute | AppRouter; | ||
}; | ||
export declare const initTsCont: () => TsCont; | ||
export declare const initTsRest: () => tsRest; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.initTsCont = exports.isAppRoute = void 0; | ||
exports.initTsRest = exports.isAppRoute = void 0; | ||
const isAppRoute = (obj) => { | ||
@@ -8,3 +8,3 @@ return obj.method !== undefined; | ||
exports.isAppRoute = isAppRoute; | ||
const initTsCont = () => { | ||
const initTsRest = () => { | ||
return { | ||
@@ -18,3 +18,3 @@ router: (args) => args, | ||
}; | ||
exports.initTsCont = initTsCont; | ||
exports.initTsRest = initTsRest; | ||
//# sourceMappingURL=dsl.js.map |
import { Express } from 'express'; | ||
import { z } from 'zod'; | ||
import { AppRoute, AppRouteMutation, AppRouteQuery, AppRouter } from './dsl'; | ||
declare type AppRouteQueryImplementation<T extends AppRouteQuery> = (input: { | ||
import { Without } from './type-utils'; | ||
declare type AppRouteQueryImplementation<T extends AppRouteQuery> = (input: Without<{ | ||
params: Parameters<T['path']>[0] extends undefined ? never : Parameters<T['path']>[0]; | ||
query: T['query'] extends z.AnyZodObject ? z.infer<T['query']> : null; | ||
}, never>) => Promise<T['response']>; | ||
declare type AppRouteMutationImplementation<T extends AppRouteMutation> = (input: Without<{ | ||
params: Parameters<T['path']>[0]; | ||
}) => Promise<T['response']>; | ||
declare type AppRouteMutationImplementation<T extends AppRouteMutation> = (input: { | ||
params: Parameters<T['path']>[0]; | ||
body: T['body'] extends z.AnyZodObject ? z.infer<T['body']> : null; | ||
}) => Promise<T['response']>; | ||
query: T['query'] extends z.AnyZodObject ? z.infer<T['query']> : never; | ||
body: T['body'] extends z.AnyZodObject ? z.infer<T['body']> : never; | ||
}, never>) => Promise<T['response']>; | ||
declare type AppRouteImplementation<T extends AppRoute> = T extends AppRouteMutation ? AppRouteMutationImplementation<T> : T extends AppRouteQuery ? AppRouteQueryImplementation<T> : never; | ||
@@ -12,0 +15,0 @@ declare type RecursiveRouterObj<T extends AppRouter> = { |
@@ -26,5 +26,11 @@ "use strict"; | ||
const path = (0, server_1.getAppRoutePathRoute)(schema); | ||
console.log(`[tscont] Initialized ${schema.method} ${path}`); | ||
console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
app.get(path, (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
return res.json(yield route({ params: req.params })); | ||
const zodQueryIssues = returnZodErrorsIfZodSchema(schema.query, req.query); | ||
if (zodQueryIssues.length > 0) { | ||
return res.status(400).json({ | ||
errors: zodQueryIssues, | ||
}); | ||
} | ||
return res.json(yield route({ params: req.params, query: req.query })); | ||
})); | ||
@@ -34,11 +40,27 @@ }; | ||
const path = (0, server_1.getAppRoutePathRoute)(schema); | ||
console.log(`[tscont] Initialized ${schema.method} ${path}`); | ||
console.log(`[ts-rest] Initialized ${schema.method} ${path}`); | ||
const method = schema.method; | ||
const callback = (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
const result = yield route({ params: req.params, body: req.body }); | ||
const zodBodyIssues = returnZodErrorsIfZodSchema(schema.body, req.body); | ||
if (zodBodyIssues.length > 0) { | ||
return res.status(400).json({ | ||
errors: zodBodyIssues, | ||
}); | ||
} | ||
const zodQueryIssues = returnZodErrorsIfZodSchema(schema.query, req.query); | ||
if (zodQueryIssues.length > 0) { | ||
return res.status(400).json({ | ||
errors: zodQueryIssues, | ||
}); | ||
} | ||
const result = yield route({ | ||
params: req.params, | ||
body: req.body, | ||
query: req.query, | ||
}); | ||
return res.json(result); | ||
} | ||
catch (e) { | ||
console.error(`[tscont] Error on ${method} ${path}`, e); | ||
console.error(`[ts-rest] Error on ${method} ${path}`, e); | ||
return res.status(500).send('Internal Server Error'); | ||
@@ -65,2 +87,15 @@ } | ||
}; | ||
const returnZodErrorsIfZodSchema = (schema, body) => { | ||
const bodySchema = schema; | ||
if (bodySchema && | ||
bodySchema._def && | ||
bodySchema._def.typeName === 'ZodObject') { | ||
// Check body schema | ||
const parsed = bodySchema.safeParse(body); | ||
if (parsed.success === false) { | ||
return parsed.error.issues; | ||
} | ||
} | ||
return []; | ||
}; | ||
const createExpressEndpoints = (schema, router, app) => { | ||
@@ -67,0 +102,0 @@ recursivelyApplyExpressRouter(router, [], (route, path) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30240
367
160
20