@ts-rest/core
Advanced tools
Comparing version 3.21.2 to 3.22.0
35
index.js
@@ -179,7 +179,21 @@ 'use strict'; | ||
class UnknownStatusError extends Error { | ||
constructor(response, knownResponseStatuses) { | ||
const expectedStatuses = knownResponseStatuses.join(','); | ||
super(`Server returned unexpected response. Expected one of: ${expectedStatuses} got: ${response.status}`); | ||
this.response = response; | ||
} | ||
} | ||
function getRouteResponses(router) { | ||
return {}; | ||
} | ||
const tsRestFetchApi = async ({ path, method, headers, body, credentials, }) => { | ||
const result = await fetch(path, { method, headers, body, credentials }); | ||
const tsRestFetchApi = async ({ path, method, headers, body, credentials, signal, }) => { | ||
const result = await fetch(path, { | ||
method, | ||
headers, | ||
body, | ||
credentials, | ||
signal, | ||
}); | ||
const contentType = result.headers.get('content-type'); | ||
@@ -209,3 +223,3 @@ if (contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json')) { | ||
}; | ||
const fetchApi = ({ path, clientArgs, route, body, query, extraInputArgs, headers, }) => { | ||
const fetchApi = ({ path, clientArgs, route, body, query, extraInputArgs, headers, signal, }) => { | ||
const apiFetcher = clientArgs.api || tsRestFetchApi; | ||
@@ -223,2 +237,3 @@ const combinedHeaders = { | ||
return apiFetcher({ | ||
route, | ||
path, | ||
@@ -232,2 +247,3 @@ method: route.method, | ||
contentType: 'multipart/form-data', | ||
signal, | ||
...extraInputArgs, | ||
@@ -237,2 +253,3 @@ }); | ||
return apiFetcher({ | ||
route, | ||
path, | ||
@@ -249,2 +266,3 @@ method: route.method, | ||
contentType: route.method !== 'GET' ? 'application/json' : undefined, | ||
signal, | ||
...extraInputArgs, | ||
@@ -262,6 +280,7 @@ }); | ||
const getRouteQuery = (route, clientArgs) => { | ||
const knownResponseStatuses = Object.keys(route.responses); | ||
return async (inputArgs) => { | ||
const { query, params, body, headers, extraHeaders, ...extraInputArgs } = inputArgs || {}; | ||
const completeUrl = getCompleteUrl(query, clientArgs.baseUrl, params, route, !!clientArgs.jsonQuery); | ||
return await fetchApi({ | ||
const response = await fetchApi({ | ||
path: completeUrl, | ||
@@ -278,2 +297,9 @@ clientArgs, | ||
}); | ||
if (!clientArgs.throwOnUnknownStatus) { | ||
return response; | ||
} | ||
if (knownResponseStatuses.includes(response.status.toString())) { | ||
return response; | ||
} | ||
throw new UnknownStatusError(response, knownResponseStatuses); | ||
}; | ||
@@ -322,2 +348,3 @@ }; | ||
exports.ResponseValidationError = ResponseValidationError; | ||
exports.UnknownStatusError = UnknownStatusError; | ||
exports.checkZodSchema = checkZodSchema; | ||
@@ -324,0 +351,0 @@ exports.convertQueryParamsToUrlString = convertQueryParamsToUrlString; |
{ | ||
"name": "@ts-rest/core", | ||
"version": "3.21.2", | ||
"version": "3.22.0", | ||
"description": "RPC-like experience over a regular REST API, with type safe server implementations 🪄", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,1 +11,2 @@ export * from './lib/client'; | ||
export * from './lib/infer-types'; | ||
export * from './lib/unknown-status-error'; |
@@ -32,3 +32,3 @@ import { AppRoute, AppRouteMutation, AppRouter } from './dsl'; | ||
type DataReturnArgs<TRoute extends AppRoute, TClientArgs extends ClientArgs> = OptionalIfAllOptional<Without<DataReturnArgsBase<TRoute, TClientArgs>, never>>; | ||
export type ApiRouteResponse<T> = { | ||
export type ApiRouteResponseNoUnknownStatus<T> = { | ||
[K in keyof T]: { | ||
@@ -38,3 +38,4 @@ status: K; | ||
}; | ||
}[keyof T] | { | ||
}[keyof T]; | ||
export type ApiRouteResponse<T> = ApiRouteResponseNoUnknownStatus<T> | { | ||
status: Exclude<HTTPStatusCode, keyof T>; | ||
@@ -51,6 +52,9 @@ body: unknown; | ||
export declare function getRouteResponses<T extends AppRouter>(router: T): { [K in keyof T]: T[K] extends AppRoute ? ApiResponseForRoute<T[K]> : "not a route"; }; | ||
type AppRouteFunctionReturn<TRoute extends AppRoute, TClientArgs extends ClientArgs> = TClientArgs extends { | ||
throwOnUnknownStatus: true; | ||
} ? ApiRouteResponseNoUnknownStatus<TRoute['responses']> : ApiRouteResponse<TRoute['responses']>; | ||
/** | ||
* Returned from a mutation or query call | ||
*/ | ||
export type AppRouteFunction<TRoute extends AppRoute, TClientArgs extends ClientArgs> = AreAllPropertiesOptional<DataReturnArgs<TRoute, TClientArgs>> extends true ? (args?: Prettify<DataReturnArgs<TRoute, TClientArgs>>) => Promise<Prettify<ApiRouteResponse<TRoute['responses']>>> : (args: Prettify<DataReturnArgs<TRoute, TClientArgs>>) => Promise<Prettify<ApiRouteResponse<TRoute['responses']>>>; | ||
export type AppRouteFunction<TRoute extends AppRoute, TClientArgs extends ClientArgs> = AreAllPropertiesOptional<DataReturnArgs<TRoute, TClientArgs>> extends true ? (args?: Prettify<DataReturnArgs<TRoute, TClientArgs>>) => Promise<Prettify<AppRouteFunctionReturn<TRoute, TClientArgs>>> : (args: Prettify<DataReturnArgs<TRoute, TClientArgs>>) => Promise<Prettify<AppRouteFunctionReturn<TRoute, TClientArgs>>>; | ||
export interface ClientArgs { | ||
@@ -64,2 +68,3 @@ baseUrl: string; | ||
export type ApiFetcherArgs = { | ||
route: AppRoute; | ||
path: string; | ||
@@ -73,2 +78,3 @@ method: string; | ||
credentials?: RequestCredentials; | ||
signal?: AbortSignal; | ||
}; | ||
@@ -87,3 +93,3 @@ export type ApiFetcher = (args: ApiFetcherArgs) => Promise<{ | ||
export declare const tsRestFetchApi: ApiFetcher; | ||
export declare const fetchApi: ({ path, clientArgs, route, body, query, extraInputArgs, headers, }: { | ||
export declare const fetchApi: ({ path, clientArgs, route, body, query, extraInputArgs, headers, signal, }: { | ||
path: string; | ||
@@ -96,2 +102,3 @@ clientArgs: ClientArgs; | ||
headers: Record<string, string | undefined>; | ||
signal?: AbortSignal | undefined; | ||
}) => Promise<{ | ||
@@ -105,3 +112,3 @@ status: number; | ||
export declare const getCompleteUrl: (query: unknown, baseUrl: string, params: unknown, route: AppRoute, jsonQuery: boolean) => string; | ||
export declare const getRouteQuery: <TAppRoute extends AppRoute>(route: TAppRoute, clientArgs: ClientArgs) => (inputArgs?: DataReturnArgsBase<any, ClientArgs>) => Promise<{ | ||
export declare const getRouteQuery: <TAppRoute extends AppRoute>(route: TAppRoute, clientArgs: InitClientArgs) => (inputArgs?: DataReturnArgsBase<any, ClientArgs>) => Promise<{ | ||
status: number; | ||
@@ -111,3 +118,10 @@ body: unknown; | ||
export type InitClientReturn<T extends AppRouter, TClientArgs extends ClientArgs> = RecursiveProxyObj<T, TClientArgs>; | ||
export declare const initClient: <T extends AppRouter, TClientArgs extends ClientArgs>(router: T, args: TClientArgs) => RecursiveProxyObj<T, TClientArgs>; | ||
export type InitClientArgs = ClientArgs & { | ||
/** | ||
* Ensures that the responses from the server match those defined in the | ||
* contract. | ||
*/ | ||
throwOnUnknownStatus?: boolean; | ||
}; | ||
export declare const initClient: <T extends AppRouter, TClientArgs extends InitClientArgs>(router: T, args: TClientArgs) => RecursiveProxyObj<T, TClientArgs>; | ||
export {}; |
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
53679
18
1141