@zayne-labs/callapi 1.11.30 → 1.11.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -57
- package/dist/{index-DvEQIgL-.d.ts → index-DP2YeNBA.d.ts} +29 -19
- package/dist/index.d.ts +2 -2
- package/dist/index.js +66 -52
- package/dist/index.js.map +1 -1
- package/dist/utils/external/index.d.ts +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -65,8 +65,8 @@ Structured errors make robust error handling trivial.
|
|
|
65
65
|
const { data, error } = await callApi("/api/users");
|
|
66
66
|
|
|
67
67
|
if (error) {
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
console.log(error.name); // "HTTPError", "ValidationError"
|
|
69
|
+
console.log(error.errorData); // Actual API response
|
|
70
70
|
}
|
|
71
71
|
```
|
|
72
72
|
|
|
@@ -76,9 +76,9 @@ Supports exponential backoff and custom retry conditions.
|
|
|
76
76
|
|
|
77
77
|
```js
|
|
78
78
|
await callApi("/api/data", {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
retryAttempts: 3,
|
|
80
|
+
retryStrategy: "exponential",
|
|
81
|
+
retryStatusCodes: [429, 500, 502, 503],
|
|
82
82
|
});
|
|
83
83
|
```
|
|
84
84
|
|
|
@@ -92,19 +92,19 @@ import { createFetchClient } from "@zayne-labs/callapi";
|
|
|
92
92
|
import { defineSchema } from "@zayne-labs/callapi/utils";
|
|
93
93
|
|
|
94
94
|
const callMainApi = createFetchClient({
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
schema: defineSchema({
|
|
96
|
+
"/users/:id": {
|
|
97
|
+
data: z.object({
|
|
98
|
+
id: z.number(),
|
|
99
|
+
name: z.string(),
|
|
100
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
// Fully typed + validated
|
|
106
106
|
const user = await callMainApi("/users/:id", {
|
|
107
|
-
|
|
107
|
+
params: { id: 123 },
|
|
108
108
|
});
|
|
109
109
|
```
|
|
110
110
|
|
|
@@ -114,15 +114,15 @@ Hook into CallApi's lifecycle at any point.
|
|
|
114
114
|
|
|
115
115
|
```js
|
|
116
116
|
const api = createFetchClient({
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
117
|
+
onRequest: ({ request }) => {
|
|
118
|
+
request.headers.set("Authorization", `Bearer ${token}`);
|
|
119
|
+
},
|
|
120
|
+
onError: ({ error }) => {
|
|
121
|
+
Sentry.captureException(error);
|
|
122
|
+
},
|
|
123
|
+
onResponseStream: ({ event }) => {
|
|
124
|
+
console.log(`Downloaded ${event.progress}%`);
|
|
125
|
+
},
|
|
126
126
|
});
|
|
127
127
|
```
|
|
128
128
|
|
|
@@ -132,38 +132,38 @@ Extend functionality with setup, hooks, and middleware.
|
|
|
132
132
|
|
|
133
133
|
```js
|
|
134
134
|
const metricsPlugin = definePlugin({
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
135
|
+
id: "metrics",
|
|
136
|
+
name: "Metrics Plugin",
|
|
137
|
+
|
|
138
|
+
setup: ({ options }) => ({
|
|
139
|
+
options: {
|
|
140
|
+
...options,
|
|
141
|
+
meta: { startTime: Date.now() },
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
|
|
145
|
+
hooks: {
|
|
146
|
+
onSuccess: ({ options }) => {
|
|
147
|
+
const duration = Date.now() - options.meta.startTime;
|
|
148
|
+
|
|
149
|
+
console.info(`Request took ${duration}ms`);
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
middlewares: {
|
|
154
|
+
fetchMiddleware: (ctx) => async (input, init) => {
|
|
155
|
+
console.info("→", input);
|
|
156
|
+
|
|
157
|
+
const response = await ctx.fetchImpl(input, init);
|
|
158
|
+
|
|
159
|
+
console.info("←", response.status);
|
|
160
|
+
return response;
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
163
|
});
|
|
164
164
|
|
|
165
165
|
const api = createFetchClient({
|
|
166
|
-
|
|
166
|
+
plugins: [metricsPlugin],
|
|
167
167
|
});
|
|
168
168
|
```
|
|
169
169
|
|
|
@@ -195,10 +195,10 @@ const { data } = await callApi("/api/users");
|
|
|
195
195
|
|
|
196
196
|
// Configured
|
|
197
197
|
const api = createFetchClient({
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
198
|
+
baseURL: "https://api.example.com",
|
|
199
|
+
retryAttempts: 2,
|
|
200
|
+
timeout: 10000,
|
|
201
|
+
onError: ({ error }) => trackError(error),
|
|
202
202
|
});
|
|
203
203
|
```
|
|
204
204
|
|
|
@@ -206,7 +206,7 @@ const api = createFetchClient({
|
|
|
206
206
|
|
|
207
207
|
```html
|
|
208
208
|
<script type="module">
|
|
209
|
-
|
|
209
|
+
import { callApi } from "https://esm.run/@zayne-labs/callapi";
|
|
210
210
|
</script>
|
|
211
211
|
```
|
|
212
212
|
|
|
@@ -349,6 +349,7 @@ type ResultVariant = "infer-input" | "infer-output";
|
|
|
349
349
|
type InferSchemaResult<TSchema, TFallbackResult, TResultVariant extends ResultVariant> = undefined extends TSchema ? TFallbackResult : TSchema extends StandardSchemaV1 ? TResultVariant extends "infer-input" ? StandardSchemaV1.InferInput<TSchema> : StandardSchemaV1.InferOutput<TSchema> : TSchema extends AnyFunction<infer TResult> ? Awaited<TResult> : TFallbackResult;
|
|
350
350
|
type InferSchemaOutput<TSchema, TFallbackResult = unknown> = InferSchemaResult<TSchema, TFallbackResult, "infer-output">;
|
|
351
351
|
type InferSchemaInput<TSchema, TFallbackResult = unknown> = InferSchemaResult<TSchema, TFallbackResult, "infer-input">;
|
|
352
|
+
type BooleanObject = { [Key in keyof CallApiSchema]: boolean };
|
|
352
353
|
interface CallApiSchemaConfig {
|
|
353
354
|
/**
|
|
354
355
|
* The base url of the schema. By default it's the baseURL of the callApi instance.
|
|
@@ -357,14 +358,14 @@ interface CallApiSchemaConfig {
|
|
|
357
358
|
/**
|
|
358
359
|
* Disables runtime validation for the schema.
|
|
359
360
|
*/
|
|
360
|
-
disableRuntimeValidation?: boolean;
|
|
361
|
+
disableRuntimeValidation?: boolean | BooleanObject;
|
|
361
362
|
/**
|
|
362
363
|
* If `true`, the original input value will be used instead of the transformed/validated output.
|
|
363
364
|
*
|
|
364
365
|
* This is useful when you want to validate the input but don't want any transformations
|
|
365
366
|
* applied by the validation schema (e.g., type coercion, default values, etc).
|
|
366
367
|
*/
|
|
367
|
-
disableValidationOutputApplication?: boolean;
|
|
368
|
+
disableValidationOutputApplication?: boolean | BooleanObject;
|
|
368
369
|
/**
|
|
369
370
|
* Optional url prefix that will be substituted for the `baseURL` of the schemaConfig at runtime.
|
|
370
371
|
*
|
|
@@ -1762,24 +1763,17 @@ type CallApiResultErrorVariant<TErrorData> = {
|
|
|
1762
1763
|
error: PossibleJavaScriptOrValidationError;
|
|
1763
1764
|
response: Response | null;
|
|
1764
1765
|
};
|
|
1765
|
-
type
|
|
1766
|
-
type
|
|
1766
|
+
type CallApiResultSuccessOrErrorVariant<TData, TError> = CallApiResultErrorVariant<TError> | CallApiResultSuccessVariant<TData>;
|
|
1767
|
+
type ResultModeMap<TData = DefaultDataType, TErrorData = DefaultDataType, TThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError, TComputedResultWithoutException extends CallApiResultSuccessOrErrorVariant<TData, TErrorData> = CallApiResultSuccessOrErrorVariant<TData, TErrorData>, TComputedResultWithException extends CallApiResultSuccessVariant<TData> = CallApiResultSuccessVariant<TData>, TComputedResult extends (TThrowOnError extends true ? TComputedResultWithException : TComputedResultWithoutException) = (TThrowOnError extends true ? TComputedResultWithException : TComputedResultWithoutException)> = UnmaskType<{
|
|
1767
1768
|
all: TComputedResult;
|
|
1768
1769
|
onlyData: TComputedResult["data"];
|
|
1769
1770
|
onlyResponse: TComputedResult["response"];
|
|
1770
|
-
withoutResponse: DistributiveOmit<TComputedResult, "response"
|
|
1771
|
+
withoutResponse: Prettify<DistributiveOmit<TComputedResult, "response">>;
|
|
1771
1772
|
}>;
|
|
1772
|
-
type ResultModeMapWithException<TData, TComputedResult extends CallApiResultSuccessVariant<TData> = CallApiResultSuccessVariant<TData>> = {
|
|
1773
|
-
all: TComputedResult;
|
|
1774
|
-
onlyData: TComputedResult["data"];
|
|
1775
|
-
onlyResponse: TComputedResult["response"];
|
|
1776
|
-
withoutResponse: DistributiveOmit<TComputedResult, "response">;
|
|
1777
|
-
};
|
|
1778
|
-
type ResultModeMap<TData = DefaultDataType, TErrorData = DefaultDataType, TThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError> = TThrowOnError extends true ? ResultModeMapWithException<TData> : ResultModeMapWithoutException<TData, TErrorData>;
|
|
1779
1773
|
type ResultModePlaceholder = null;
|
|
1780
1774
|
type ResultModeUnion = keyof ResultModeMap;
|
|
1781
1775
|
type ResultModeType = ResultModePlaceholder | ResultModeUnion;
|
|
1782
|
-
type InferCallApiResult<TData, TErrorData, TResultMode extends ResultModeType, TThrowOnError extends ThrowOnErrorUnion, TComputedResultModeMapWithException extends
|
|
1776
|
+
type InferCallApiResult<TData, TErrorData, TResultMode extends ResultModeType, TThrowOnError extends ThrowOnErrorUnion, TComputedResultModeMapWithException extends ResultModeMap<TData, TErrorData, true> = ResultModeMap<TData, TErrorData, true>, TComputedResultModeMapWithoutException extends ResultModeMap<TData, TErrorData, TThrowOnError> = ResultModeMap<TData, TErrorData, TThrowOnError>> = TErrorData extends false ? TComputedResultModeMapWithException["onlyData"] : TErrorData extends false | undefined ? TComputedResultModeMapWithException["onlyData"] : ResultModePlaceholder extends TResultMode ? TComputedResultModeMapWithoutException["all"] : TResultMode extends ResultModeUnion ? TComputedResultModeMapWithoutException[TResultMode] : never;
|
|
1783
1777
|
//#endregion
|
|
1784
1778
|
//#region src/createFetchClient.d.ts
|
|
1785
1779
|
declare const createFetchClientWithContext: <TOuterCallApiContext extends CallApiContext = DefaultCallApiContext>() => <TBaseCallApiContext extends CallApiContext = TOuterCallApiContext, TBaseData = TBaseCallApiContext["Data"], TBaseErrorData = TBaseCallApiContext["ErrorData"], TBaseResultMode extends ResultModeType = (TBaseCallApiContext["ResultMode"] extends ResultModeType ? TBaseCallApiContext["ResultMode"] : ResultModeType), TBaseThrowOnError extends ThrowOnErrorUnion = boolean, TBaseResponseType extends ResponseTypeType = ResponseTypeType, const TBaseSchemaAndConfig extends BaseCallApiSchemaAndConfig = BaseCallApiSchemaAndConfig, const TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TComputedBaseSchemaConfig extends CallApiSchemaConfig = GetBaseSchemaConfig<TBaseSchemaAndConfig>, TComputedBaseSchemaRoutes extends BaseCallApiSchemaRoutes = GetBaseSchemaRoutes<TBaseSchemaAndConfig>>(initBaseConfig?: BaseCallApiConfig<TBaseCallApiContext, TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemaAndConfig, TBasePluginArray>) => <TData = TBaseData, TErrorData = TBaseErrorData, TResultMode extends ResultModeType = TBaseResultMode, TCallApiContext extends CallApiContext = TBaseCallApiContext, TThrowOnError extends ThrowOnErrorUnion = TBaseThrowOnError, TResponseType extends ResponseTypeType = TBaseResponseType, const TSchemaConfig extends CallApiSchemaConfig = TComputedBaseSchemaConfig, TInitURL extends InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig> = InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig>, TCurrentRouteSchemaKey extends GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL> = GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL>, const TSchema extends CallApiSchema = GetCurrentRouteSchema<TComputedBaseSchemaRoutes, TCurrentRouteSchemaKey>, const TPluginArray extends CallApiPlugin[] = TBasePluginArray, TComputedData = InferSchemaOutput<TSchema["data"], GetResponseType<TData, TResponseType>>, TComputedErrorData = InferSchemaOutput<TSchema["errorData"], GetResponseType<TErrorData, TResponseType>>, TComputedResult = CallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError>>(initURL: TInitURL, initConfig?: CallApiConfig<TCallApiContext, TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType, TComputedBaseSchemaRoutes, TSchema, TComputedBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray>) => Promise<TComputedResult>;
|
|
@@ -1787,8 +1781,16 @@ declare const createFetchClient: <TBaseCallApiContext extends CallApiContext = D
|
|
|
1787
1781
|
all: CallApiResultSuccessVariant<TComputedData>;
|
|
1788
1782
|
onlyData: NoInferUnMasked<TComputedData>;
|
|
1789
1783
|
onlyResponse: Response;
|
|
1790
|
-
withoutResponse:
|
|
1791
|
-
|
|
1784
|
+
withoutResponse: {
|
|
1785
|
+
error: null;
|
|
1786
|
+
data: NoInferUnMasked<TComputedData>;
|
|
1787
|
+
};
|
|
1788
|
+
}, {
|
|
1789
|
+
all: TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>;
|
|
1790
|
+
onlyData: (TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>)["data"];
|
|
1791
|
+
onlyResponse: (TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>)["response"];
|
|
1792
|
+
withoutResponse: DistributiveOmit<TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>, "response"> extends infer T ? { [Key in keyof T]: T[Key] } : never;
|
|
1793
|
+
}>>(initURL: TInitURL, initConfig?: CallApiConfig<TCallApiContext, TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType, TComputedBaseSchemaRoutes, TSchema, TComputedBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray>) => Promise<TComputedResult>;
|
|
1792
1794
|
declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode extends ResultModeType = ResultModeType, TCallApiContext extends CallApiContext = DefaultCallApiContext, TThrowOnError extends ThrowOnErrorUnion = boolean, TResponseType extends ResponseTypeType = ResponseTypeType, const TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends ApplyStrictConfig<TSchemaConfig, ApplyURLBasedConfig<TSchemaConfig, AnyString | "@delete/" | "@get/" | "@patch/" | "@post/" | "@put/">> = ApplyStrictConfig<TSchemaConfig, ApplyURLBasedConfig<TSchemaConfig, AnyString | "@delete/" | "@get/" | "@patch/" | "@post/" | "@put/">>, TCurrentRouteSchemaKey extends GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL> = GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL>, const TSchema extends CallApiSchema = GetCurrentRouteSchema<{
|
|
1793
1795
|
[x: AnyString]: CallApiSchema | undefined;
|
|
1794
1796
|
"@default"?: CallApiSchema | undefined;
|
|
@@ -1825,8 +1827,16 @@ declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode exten
|
|
|
1825
1827
|
all: CallApiResultSuccessVariant<TComputedData>;
|
|
1826
1828
|
onlyData: NoInferUnMasked<TComputedData>;
|
|
1827
1829
|
onlyResponse: Response;
|
|
1828
|
-
withoutResponse:
|
|
1829
|
-
|
|
1830
|
+
withoutResponse: {
|
|
1831
|
+
error: null;
|
|
1832
|
+
data: NoInferUnMasked<TComputedData>;
|
|
1833
|
+
};
|
|
1834
|
+
}, {
|
|
1835
|
+
all: TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>;
|
|
1836
|
+
onlyData: (TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>)["data"];
|
|
1837
|
+
onlyResponse: (TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>)["response"];
|
|
1838
|
+
withoutResponse: DistributiveOmit<TThrowOnError extends true ? CallApiResultSuccessVariant<TComputedData> : CallApiResultSuccessOrErrorVariant<TComputedData, TComputedErrorData>, "response"> extends infer T ? { [Key in keyof T]: T[Key] } : never;
|
|
1839
|
+
}>>(initURL: TInitURL, initConfig?: CallApiConfig<TCallApiContext, TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType, {
|
|
1830
1840
|
[x: AnyString]: CallApiSchema | undefined;
|
|
1831
1841
|
"@default"?: CallApiSchema | undefined;
|
|
1832
1842
|
"@delete/"?: CallApiSchema | undefined;
|
|
@@ -1836,5 +1846,5 @@ declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode exten
|
|
|
1836
1846
|
"@put/"?: CallApiSchema | undefined;
|
|
1837
1847
|
}, TSchema, CallApiSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, DefaultPluginArray, TPluginArray>) => Promise<TComputedResult>;
|
|
1838
1848
|
//#endregion
|
|
1839
|
-
export { Writeable as $, ErrorContext as A, CallApiPlugin as B, InferExtendSchemaContext as C, ValidationError as D, HTTPError as E, ResponseContext as F, BaseSchemaRouteKeyPrefixes as G, PluginSetupContext as H, ResponseErrorContext as I, InferSchemaInput as J, CallApiSchema as K, ResponseStreamContext as L, HooksOrHooksArray as M, RequestContext as N, RetryOptions as O, RequestStreamContext as P, Satisfies as Q, SuccessContext as R, GetExtendSchemaConfigContext as S, Register as T, URLOptions as U, PluginHooks as V, BaseCallApiSchemaRoutes as W, InferParamsFromRoute as X, InferSchemaOutput as Y, AnyFunction as Z, CallApiExtraOptionsForHooks as _,
|
|
1840
|
-
//# sourceMappingURL=index-
|
|
1849
|
+
export { Writeable as $, ErrorContext as A, CallApiPlugin as B, InferExtendSchemaContext as C, ValidationError as D, HTTPError as E, ResponseContext as F, BaseSchemaRouteKeyPrefixes as G, PluginSetupContext as H, ResponseErrorContext as I, InferSchemaInput as J, CallApiSchema as K, ResponseStreamContext as L, HooksOrHooksArray as M, RequestContext as N, RetryOptions as O, RequestStreamContext as P, Satisfies as Q, SuccessContext as R, GetExtendSchemaConfigContext as S, Register as T, URLOptions as U, PluginHooks as V, BaseCallApiSchemaRoutes as W, InferParamsFromRoute as X, InferSchemaOutput as Y, AnyFunction as Z, CallApiExtraOptionsForHooks as _, CallApiResultSuccessOrErrorVariant as a, CallApiRequestOptionsForHooks as b, PossibleJavaScriptError as c, ResponseTypeType as d, ResultModeType as f, CallApiExtraOptions as g, CallApiConfig as h, CallApiResultErrorVariant as i, Hooks as j, DedupeOptions as k, PossibleJavaScriptOrValidationError as l, BaseCallApiExtraOptions as m, createFetchClient as n, CallApiResultSuccessVariant as o, BaseCallApiConfig as p, CallApiSchemaConfig as q, createFetchClientWithContext as r, PossibleHTTPError as s, callApi as t, PossibleValidationError as u, CallApiParameters as v, InstanceContext as w, CallApiResultLoose as x, CallApiRequestOptions as y, DefaultCallApiContext as z };
|
|
1850
|
+
//# sourceMappingURL=index-DP2YeNBA.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./validation-Do6HBp6Z.js";
|
|
2
|
-
import { A as ErrorContext, B as CallApiPlugin, C as InferExtendSchemaContext, F as ResponseContext, G as BaseSchemaRouteKeyPrefixes, H as PluginSetupContext, I as ResponseErrorContext, J as InferSchemaInput, K as CallApiSchema, L as ResponseStreamContext, M as HooksOrHooksArray, N as RequestContext, O as RetryOptions, P as RequestStreamContext, R as SuccessContext, S as GetExtendSchemaConfigContext, T as Register, U as URLOptions, V as PluginHooks, W as BaseCallApiSchemaRoutes, X as InferParamsFromRoute, Y as InferSchemaOutput, _ as CallApiExtraOptionsForHooks, a as
|
|
3
|
-
export { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaRoutes, BaseSchemaRouteKeyPrefixes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResultLoose as CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig,
|
|
2
|
+
import { A as ErrorContext, B as CallApiPlugin, C as InferExtendSchemaContext, F as ResponseContext, G as BaseSchemaRouteKeyPrefixes, H as PluginSetupContext, I as ResponseErrorContext, J as InferSchemaInput, K as CallApiSchema, L as ResponseStreamContext, M as HooksOrHooksArray, N as RequestContext, O as RetryOptions, P as RequestStreamContext, R as SuccessContext, S as GetExtendSchemaConfigContext, T as Register, U as URLOptions, V as PluginHooks, W as BaseCallApiSchemaRoutes, X as InferParamsFromRoute, Y as InferSchemaOutput, _ as CallApiExtraOptionsForHooks, a as CallApiResultSuccessOrErrorVariant, b as CallApiRequestOptionsForHooks, c as PossibleJavaScriptError, d as ResponseTypeType, f as ResultModeType, g as CallApiExtraOptions, h as CallApiConfig, i as CallApiResultErrorVariant, j as Hooks, k as DedupeOptions, l as PossibleJavaScriptOrValidationError, m as BaseCallApiExtraOptions, n as createFetchClient, o as CallApiResultSuccessVariant, p as BaseCallApiConfig, q as CallApiSchemaConfig, r as createFetchClientWithContext, s as PossibleHTTPError, t as callApi, u as PossibleValidationError, v as CallApiParameters, w as InstanceContext, x as CallApiResultLoose, y as CallApiRequestOptions, z as DefaultCallApiContext } from "./index-DP2YeNBA.js";
|
|
3
|
+
export { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaRoutes, BaseSchemaRouteKeyPrefixes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResultLoose as CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessOrErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, DedupeOptions, DefaultCallApiContext, ErrorContext, GetExtendSchemaConfigContext, Hooks, HooksOrHooksArray, InferExtendSchemaContext, InferParamsFromRoute, InferSchemaInput, InferSchemaOutput, InstanceContext, PluginHooks, PluginSetupContext, PossibleHTTPError, PossibleJavaScriptError, PossibleJavaScriptOrValidationError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeType, ResultModeType, RetryOptions, SuccessContext, URLOptions, callApi, createFetchClient, createFetchClientWithContext };
|
package/dist/index.js
CHANGED
|
@@ -67,11 +67,15 @@ const routeKeyMethods = defineEnum([
|
|
|
67
67
|
]);
|
|
68
68
|
const handleSchemaValidation = async (fullSchema, schemaName, validationOptions) => {
|
|
69
69
|
const { inputValue, response, schemaConfig } = validationOptions;
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
const disableRuntimeValidationBooleanObject = isObject(schemaConfig?.disableRuntimeValidation) ? schemaConfig.disableRuntimeValidation : {};
|
|
71
|
+
if (schemaConfig?.disableRuntimeValidation === true || disableRuntimeValidationBooleanObject[schemaName] === true) return inputValue;
|
|
72
|
+
const validResult = await standardSchemaParser(fullSchema, schemaName, {
|
|
72
73
|
inputValue,
|
|
73
74
|
response
|
|
74
75
|
});
|
|
76
|
+
const disableResultApplicationBooleanObject = isObject(schemaConfig?.disableValidationOutputApplication) ? schemaConfig.disableValidationOutputApplication : {};
|
|
77
|
+
if (schemaConfig?.disableValidationOutputApplication === true || disableResultApplicationBooleanObject[schemaName] === true) return inputValue;
|
|
78
|
+
return validResult;
|
|
75
79
|
};
|
|
76
80
|
const extraOptionsToBeValidated = [
|
|
77
81
|
"meta",
|
|
@@ -98,11 +102,13 @@ const requestOptionsToBeValidated = [
|
|
|
98
102
|
"method"
|
|
99
103
|
];
|
|
100
104
|
const handleRequestOptionsValidation = async (validationOptions) => {
|
|
101
|
-
const {
|
|
102
|
-
const validationResultArray = await Promise.all(requestOptionsToBeValidated.map((schemaName) =>
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
const { request, schema, schemaConfig } = validationOptions;
|
|
106
|
+
const validationResultArray = await Promise.all(requestOptionsToBeValidated.map((schemaName) => {
|
|
107
|
+
return handleSchemaValidation(schema, schemaName, {
|
|
108
|
+
inputValue: request[schemaName],
|
|
109
|
+
schemaConfig
|
|
110
|
+
});
|
|
111
|
+
}));
|
|
106
112
|
const validatedResultObject = {};
|
|
107
113
|
for (const [index, propertyKey] of requestOptionsToBeValidated.entries()) {
|
|
108
114
|
const validationResult = validationResultArray[index];
|
|
@@ -112,7 +118,7 @@ const handleRequestOptionsValidation = async (validationOptions) => {
|
|
|
112
118
|
return validatedResultObject;
|
|
113
119
|
};
|
|
114
120
|
const handleConfigValidation = async (validationOptions) => {
|
|
115
|
-
const { baseExtraOptions, currentRouteSchemaKey, extraOptions, options,
|
|
121
|
+
const { baseExtraOptions, currentRouteSchemaKey, extraOptions, options, request } = validationOptions;
|
|
116
122
|
const { currentRouteSchema, resolvedSchema } = getResolvedSchema({
|
|
117
123
|
baseExtraOptions,
|
|
118
124
|
currentRouteSchemaKey,
|
|
@@ -127,19 +133,12 @@ const handleConfigValidation = async (validationOptions) => {
|
|
|
127
133
|
issues: [{ message: `Strict Mode - No schema found for route '${currentRouteSchemaKey}' ` }],
|
|
128
134
|
response: null
|
|
129
135
|
});
|
|
130
|
-
if (resolvedSchemaConfig?.disableRuntimeValidation) return {
|
|
131
|
-
extraOptionsValidationResult: null,
|
|
132
|
-
requestOptionsValidationResult: null,
|
|
133
|
-
resolvedSchema,
|
|
134
|
-
resolvedSchemaConfig,
|
|
135
|
-
shouldApplySchemaOutput: false
|
|
136
|
-
};
|
|
137
136
|
const [extraOptionsValidationResult, requestOptionsValidationResult] = await Promise.all([handleExtraOptionsValidation({
|
|
138
137
|
options,
|
|
139
138
|
schema: resolvedSchema,
|
|
140
139
|
schemaConfig: resolvedSchemaConfig
|
|
141
140
|
}), handleRequestOptionsValidation({
|
|
142
|
-
|
|
141
|
+
request,
|
|
143
142
|
schema: resolvedSchema,
|
|
144
143
|
schemaConfig: resolvedSchemaConfig
|
|
145
144
|
})]);
|
|
@@ -147,8 +146,7 @@ const handleConfigValidation = async (validationOptions) => {
|
|
|
147
146
|
extraOptionsValidationResult,
|
|
148
147
|
requestOptionsValidationResult,
|
|
149
148
|
resolvedSchema,
|
|
150
|
-
resolvedSchemaConfig
|
|
151
|
-
shouldApplySchemaOutput: (Boolean(extraOptionsValidationResult) || Boolean(requestOptionsValidationResult)) && !resolvedSchemaConfig?.disableValidationOutputApplication
|
|
149
|
+
resolvedSchemaConfig
|
|
152
150
|
};
|
|
153
151
|
};
|
|
154
152
|
const getResolvedSchema = (context) => {
|
|
@@ -327,12 +325,21 @@ const objectifyHeaders = (headers) => {
|
|
|
327
325
|
if (!headers || isPlainObject(headers)) return headers;
|
|
328
326
|
return Object.fromEntries(headers);
|
|
329
327
|
};
|
|
328
|
+
const getResolvedHeaders = (options) => {
|
|
329
|
+
const { baseHeaders, headers } = options;
|
|
330
|
+
const resolvedHeaders = isFunction(headers) ? headers({ baseHeaders: baseHeaders ?? {} }) : headers ?? baseHeaders;
|
|
331
|
+
if (!resolvedHeaders) return;
|
|
332
|
+
return objectifyHeaders(resolvedHeaders);
|
|
333
|
+
};
|
|
330
334
|
const getHeaders = async (options) => {
|
|
331
|
-
const { auth, body, headers } = options;
|
|
332
|
-
|
|
335
|
+
const { auth, baseHeaders, body, headers } = options;
|
|
336
|
+
const resolvedHeaders = getResolvedHeaders({
|
|
337
|
+
baseHeaders,
|
|
338
|
+
headers
|
|
339
|
+
});
|
|
333
340
|
const headersObject = {
|
|
334
341
|
...await getAuthHeader(auth),
|
|
335
|
-
...objectifyHeaders(
|
|
342
|
+
...objectifyHeaders(resolvedHeaders)
|
|
336
343
|
};
|
|
337
344
|
if (isQueryString(body)) {
|
|
338
345
|
headersObject["Content-Type"] = "application/x-www-form-urlencoded";
|
|
@@ -803,7 +810,7 @@ const initializePlugins = async (context) => {
|
|
|
803
810
|
let resolvedCurrentRouteSchemaKey = currentRouteSchemaKey;
|
|
804
811
|
let resolvedInitURL = mainInitURL;
|
|
805
812
|
let resolvedOptions = options;
|
|
806
|
-
let
|
|
813
|
+
let resolvedRequest = request;
|
|
807
814
|
const executePluginSetupFn = async (pluginSetup) => {
|
|
808
815
|
if (!pluginSetup) return;
|
|
809
816
|
const initResult = await pluginSetup(context);
|
|
@@ -818,10 +825,24 @@ const initializePlugins = async (context) => {
|
|
|
818
825
|
resolvedCurrentRouteSchemaKey = newResult.currentRouteSchemaKey;
|
|
819
826
|
resolvedInitURL = newResult.mainInitURL;
|
|
820
827
|
}
|
|
821
|
-
if (isPlainObject(initResult.request))
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
828
|
+
if (isPlainObject(initResult.request)) {
|
|
829
|
+
const initMethod = getMethod({
|
|
830
|
+
initURL: resolvedInitURL,
|
|
831
|
+
method: initResult.request.method ?? resolvedRequest.method
|
|
832
|
+
});
|
|
833
|
+
const initHeaders = await getHeaders({
|
|
834
|
+
auth: options.auth,
|
|
835
|
+
baseHeaders: baseConfig.headers,
|
|
836
|
+
body: initResult.request.body ?? resolvedRequest.body,
|
|
837
|
+
headers: initResult.request.headers ?? resolvedRequest.headers
|
|
838
|
+
});
|
|
839
|
+
resolvedRequest = {
|
|
840
|
+
...resolvedRequest,
|
|
841
|
+
...initResult.request,
|
|
842
|
+
headers: initHeaders,
|
|
843
|
+
method: initMethod
|
|
844
|
+
};
|
|
845
|
+
}
|
|
825
846
|
if (isPlainObject(initResult.options)) resolvedOptions = {
|
|
826
847
|
...resolvedOptions,
|
|
827
848
|
...initResult.options
|
|
@@ -850,7 +871,7 @@ const initializePlugins = async (context) => {
|
|
|
850
871
|
resolvedInitURL,
|
|
851
872
|
resolvedMiddlewares,
|
|
852
873
|
resolvedOptions,
|
|
853
|
-
|
|
874
|
+
resolvedRequest
|
|
854
875
|
};
|
|
855
876
|
};
|
|
856
877
|
const setupHooksAndMiddlewares = (context) => {
|
|
@@ -1000,7 +1021,7 @@ const createFetchClientWithContext = () => {
|
|
|
1000
1021
|
...baseFetchOptions,
|
|
1001
1022
|
...!shouldSkipAutoMergeForRequest && fetchOptions
|
|
1002
1023
|
};
|
|
1003
|
-
const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions,
|
|
1024
|
+
const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequest } = await initializePlugins({
|
|
1004
1025
|
baseConfig,
|
|
1005
1026
|
config,
|
|
1006
1027
|
initURL: initURL.toString(),
|
|
@@ -1022,14 +1043,9 @@ const createFetchClientWithContext = () => {
|
|
|
1022
1043
|
initURLNormalized: normalizedInitURL
|
|
1023
1044
|
};
|
|
1024
1045
|
const newFetchController = new AbortController();
|
|
1025
|
-
const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout),
|
|
1026
|
-
const initMethod = getMethod({
|
|
1027
|
-
initURL: resolvedInitURL,
|
|
1028
|
-
method: resolvedRequestOptions.method
|
|
1029
|
-
});
|
|
1046
|
+
const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout), resolvedRequest.signal, newFetchController.signal);
|
|
1030
1047
|
const request = {
|
|
1031
|
-
...
|
|
1032
|
-
method: initMethod,
|
|
1048
|
+
...resolvedRequest,
|
|
1033
1049
|
signal: combinedSignal
|
|
1034
1050
|
};
|
|
1035
1051
|
const { getAbortErrorMessage, handleRequestCancelStrategy, handleRequestDeferStrategy, removeDedupeKeyFromCache, resolvedDedupeStrategy } = await createDedupeStrategy({
|
|
@@ -1049,32 +1065,30 @@ const createFetchClientWithContext = () => {
|
|
|
1049
1065
|
options,
|
|
1050
1066
|
request
|
|
1051
1067
|
}));
|
|
1052
|
-
const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig
|
|
1068
|
+
const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig } = await handleConfigValidation({
|
|
1053
1069
|
baseExtraOptions,
|
|
1054
1070
|
currentRouteSchemaKey: resolvedCurrentRouteSchemaKey,
|
|
1055
1071
|
extraOptions,
|
|
1056
1072
|
options,
|
|
1057
|
-
|
|
1058
|
-
});
|
|
1059
|
-
if (shouldApplySchemaOutput) Object.assign(options, extraOptionsValidationResult);
|
|
1060
|
-
const validMethod = getMethod({
|
|
1061
|
-
initURL: resolvedInitURL,
|
|
1062
|
-
method: shouldApplySchemaOutput ? requestOptionsValidationResult?.method : request.method
|
|
1073
|
+
request
|
|
1063
1074
|
});
|
|
1075
|
+
Object.assign(options, extraOptionsValidationResult);
|
|
1064
1076
|
const validBody = getBody({
|
|
1065
|
-
body:
|
|
1077
|
+
body: requestOptionsValidationResult.body,
|
|
1066
1078
|
bodySerializer: options.bodySerializer
|
|
1067
1079
|
});
|
|
1068
|
-
const resolvedHeaders = isFunction(fetchOptions.headers) ? fetchOptions.headers({ baseHeaders: baseFetchOptions.headers ?? {} }) : fetchOptions.headers ?? baseFetchOptions.headers;
|
|
1069
|
-
const validHeaders = await getHeaders({
|
|
1070
|
-
auth: options.auth,
|
|
1071
|
-
body: validBody,
|
|
1072
|
-
headers: shouldApplySchemaOutput ? requestOptionsValidationResult?.headers : resolvedHeaders
|
|
1073
|
-
});
|
|
1074
1080
|
Object.assign(request, {
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1081
|
+
body: validBody,
|
|
1082
|
+
headers: await getHeaders({
|
|
1083
|
+
auth: options.auth,
|
|
1084
|
+
baseHeaders: baseConfig.headers,
|
|
1085
|
+
body: validBody,
|
|
1086
|
+
headers: requestOptionsValidationResult.headers
|
|
1087
|
+
}),
|
|
1088
|
+
method: getMethod({
|
|
1089
|
+
initURL: resolvedInitURL,
|
|
1090
|
+
method: requestOptionsValidationResult.method
|
|
1091
|
+
})
|
|
1078
1092
|
});
|
|
1079
1093
|
const readyRequestContext = {
|
|
1080
1094
|
baseConfig,
|