@ts-rest/core
Advanced tools
Comparing version 3.15.0 to 3.16.0
68
index.js
@@ -96,13 +96,16 @@ const isAppRoute = (obj) => { | ||
const result = await fetch(path, { method, headers, body, credentials }); | ||
try { | ||
return { | ||
status: result.status, | ||
body: await result.json(), | ||
}; | ||
const contentType = result.headers.get('content-type'); | ||
if (!contentType) { | ||
throw TypeError('Resource Response missing content-type header'); | ||
} | ||
catch { | ||
return { | ||
status: result.status, | ||
body: await result.text(), | ||
}; | ||
switch (contentType) { | ||
case 'application/json': { | ||
return { status: result.status, body: await result.json() }; | ||
} | ||
case 'text/plain': { | ||
return { status: result.status, body: await result.text() }; | ||
} | ||
default: { | ||
return { status: result.status, body: await result.blob() }; | ||
} | ||
} | ||
@@ -170,5 +173,2 @@ }; | ||
}; | ||
function getRouteResponses(router) { | ||
return {}; | ||
} | ||
@@ -191,6 +191,3 @@ const isZodObject = (body) => { | ||
success: false, | ||
error: { | ||
name: result.error.name, | ||
issues: result.error.issues, | ||
}, | ||
error: result.error, | ||
}; | ||
@@ -203,3 +200,38 @@ } | ||
}; | ||
const zodErrorResponse = (error) => { | ||
return { | ||
name: error.name, | ||
issues: error.issues, | ||
}; | ||
}; | ||
export { checkZodSchema, convertQueryParamsToUrlString, defaultApi, encodeQueryParams, encodeQueryParamsJson, fetchApi, getCompleteUrl, getRouteQuery, getRouteResponses, initClient, initContract, initTsRest, insertParamsIntoPath, isAppRoute, isZodObject, parseJsonQueryObject }; | ||
class ResponseValidationError extends Error { | ||
constructor(cause) { | ||
super('Response validation failed'); | ||
this.cause = cause; | ||
} | ||
} | ||
const isAppRouteResponse = (value) => { | ||
return (value != null && | ||
typeof value === 'object' && | ||
'status' in value && | ||
typeof value.status === 'number'); | ||
}; | ||
const validateResponse = ({ responseType, response, }) => { | ||
if (isAppRouteResponse(response)) { | ||
const { body } = response; | ||
const responseValidation = checkZodSchema(body, responseType); | ||
if (!responseValidation.success) { | ||
const { error } = responseValidation; | ||
throw new ResponseValidationError(error); | ||
} | ||
return { | ||
status: response.status, | ||
body: responseValidation.data, | ||
}; | ||
} | ||
return response; | ||
}; | ||
export { ResponseValidationError, checkZodSchema, convertQueryParamsToUrlString, defaultApi, encodeQueryParams, encodeQueryParamsJson, fetchApi, getCompleteUrl, getRouteQuery, initClient, initContract, initTsRest, insertParamsIntoPath, isAppRoute, isAppRouteResponse, isZodObject, parseJsonQueryObject, validateResponse, zodErrorResponse }; |
{ | ||
"name": "@ts-rest/core", | ||
"version": "3.15.0", | ||
"version": "3.16.0", | ||
"description": "RPC-like experience over a regular REST API, with type safe server implementations 🪄", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -8,1 +8,3 @@ export * from './lib/client'; | ||
export * from './lib/zod-utils'; | ||
export * from './lib/server'; | ||
export * from './lib/response-validation-error'; |
@@ -1,10 +0,9 @@ | ||
import { z, ZodTypeAny } from 'zod'; | ||
import { AppRoute, AppRouteMutation, AppRouter } from './dsl'; | ||
import { ParamsFromUrl } from './paths'; | ||
import { HTTPStatusCode } from './status-codes'; | ||
import { AreAllPropertiesOptional, Merge, OptionalIfAllOptional, Without, ZodInferOrType } from './type-utils'; | ||
import { AreAllPropertiesOptional, Merge, OptionalIfAllOptional, Without, ZodInferOrType, ZodInputOrType } from './type-utils'; | ||
type RecursiveProxyObj<T extends AppRouter> = { | ||
[TKey in keyof T]: T[TKey] extends AppRoute ? AppRouteFunction<T[TKey]> : T[TKey] extends AppRouter ? RecursiveProxyObj<T[TKey]> : never; | ||
}; | ||
type AppRouteMutationType<T> = T extends ZodTypeAny ? z.input<T> : T; | ||
type AppRouteMutationType<T> = ZodInputOrType<T>; | ||
/** | ||
@@ -34,3 +33,2 @@ * Extract the path params from the path in the contract | ||
}; | ||
export type ApiResponseForRoute<T extends AppRoute> = ApiRouteResponse<T['responses']>; | ||
/** | ||
@@ -69,3 +67,2 @@ * Returned from a mutation or query call | ||
export declare const initClient: <T extends AppRouter>(router: T, args: ClientArgs) => RecursiveProxyObj<T>; | ||
export declare function getRouteResponses<T extends AppRouter>(router: T): { [K in keyof T]: T[K] extends AppRoute ? ApiResponseForRoute<T[K]> : "not a route"; }; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { z, ZodTypeAny } from 'zod'; | ||
import { z } from 'zod'; | ||
type GetIndexedField<T, K> = K extends keyof T ? T[K] : K extends `${number}` ? '0' extends keyof T ? undefined : number extends keyof T ? T[number] : undefined : undefined; | ||
@@ -14,4 +14,11 @@ type FieldWithPossiblyUndefined<T, Key> = GetFieldType<Exclude<T, undefined>, Key> | Extract<T, undefined>; | ||
export type With<T, V> = Pick<T, ExcludeKeysWithoutTypeOf<T, V>>; | ||
export type ZodInferOrType<T> = T extends ZodTypeAny ? z.infer<T> : T; | ||
export type ZodInferOrType<T> = T extends z.ZodTypeAny ? z.infer<T> : T; | ||
export type ZodInputOrType<T> = T extends z.ZodTypeAny ? z.input<T> : T; | ||
export type Merge<T, U> = Omit<T, keyof U> & U; | ||
type Try<A, B, C> = A extends B ? A : C; | ||
type NarrowRaw<T> = (T extends Function ? T : never) | (T extends string | number | bigint | boolean ? T : never) | (T extends [] ? [] : never) | { | ||
[K in keyof T]: K extends 'description' ? T[K] : NarrowNotZod<T[K]>; | ||
}; | ||
type NarrowNotZod<T> = Try<T, z.ZodType, NarrowRaw<T>>; | ||
export type Narrow<T> = Try<T, [], NarrowNotZod<T>>; | ||
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
@@ -18,0 +25,0 @@ type OptionalKeys<T> = T extends unknown ? { |
import { z } from 'zod'; | ||
export declare const isZodObject: (body: unknown) => body is z.ZodObject<any, any, any, any, { | ||
[x: string]: any; | ||
}>; | ||
export declare const isZodObject: (body: unknown) => body is z.AnyZodObject; | ||
export declare const checkZodSchema: (data: unknown, schema: unknown, { passThroughExtraKeys }?: { | ||
@@ -12,3 +10,4 @@ passThroughExtraKeys?: boolean | undefined; | ||
success: false; | ||
error: unknown; | ||
error: z.ZodError; | ||
}; | ||
export declare const zodErrorResponse: (error: z.ZodError) => Pick<z.ZodError, 'name' | 'issues'>; |
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
36006
15
779