@zayne-labs/callapi 1.11.24 → 1.11.26
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 +29 -11
- package/dist/defaults-C6WKIXsf.js.map +1 -1
- package/dist/guards-5Ij_loC1.js.map +1 -1
- package/dist/index.d.ts +12 -6
- package/dist/index.js +193 -189
- package/dist/index.js.map +1 -1
- package/dist/utils/external/index.d.ts +1 -1
- package/dist/{common-Bkz3oogl.d.ts → validation-BeCBQ6_6.d.ts} +234 -238
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -977,213 +977,217 @@ const createRetryManager = (ctx) => {
|
|
|
977
977
|
//#endregion
|
|
978
978
|
//#region src/createFetchClient.ts
|
|
979
979
|
const $GlobalRequestInfoCache = /* @__PURE__ */ new Map();
|
|
980
|
-
const
|
|
981
|
-
const $
|
|
982
|
-
|
|
983
|
-
const
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
config,
|
|
1005
|
-
initURL: initURL.toString(),
|
|
1006
|
-
options: mergedExtraOptions,
|
|
1007
|
-
request: mergedRequestOptions
|
|
1008
|
-
});
|
|
1009
|
-
const { fullURL, normalizedInitURL } = getFullAndNormalizedURL({
|
|
1010
|
-
baseURL: resolvedOptions.baseURL,
|
|
1011
|
-
initURL: resolvedInitURL,
|
|
1012
|
-
params: resolvedOptions.params,
|
|
1013
|
-
query: resolvedOptions.query
|
|
1014
|
-
});
|
|
1015
|
-
const options = {
|
|
1016
|
-
...resolvedOptions,
|
|
1017
|
-
...resolvedHooks,
|
|
1018
|
-
...resolvedMiddlewares,
|
|
1019
|
-
fullURL,
|
|
1020
|
-
initURL: resolvedInitURL,
|
|
1021
|
-
initURLNormalized: normalizedInitURL
|
|
1022
|
-
};
|
|
1023
|
-
const newFetchController = new AbortController();
|
|
1024
|
-
const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout), resolvedRequestOptions.signal, newFetchController.signal);
|
|
1025
|
-
const initMethod = getMethod({
|
|
1026
|
-
initURL: resolvedInitURL,
|
|
1027
|
-
method: resolvedRequestOptions.method
|
|
1028
|
-
});
|
|
1029
|
-
const request = {
|
|
1030
|
-
...resolvedRequestOptions,
|
|
1031
|
-
method: initMethod,
|
|
1032
|
-
signal: combinedSignal
|
|
1033
|
-
};
|
|
1034
|
-
const { getAbortErrorMessage, handleRequestCancelStrategy, handleRequestDeferStrategy, removeDedupeKeyFromCache, resolvedDedupeStrategy } = await createDedupeStrategy({
|
|
1035
|
-
$GlobalRequestInfoCache,
|
|
1036
|
-
$LocalRequestInfoCache,
|
|
1037
|
-
baseConfig,
|
|
1038
|
-
config,
|
|
1039
|
-
newFetchController,
|
|
1040
|
-
options,
|
|
1041
|
-
request
|
|
1042
|
-
});
|
|
1043
|
-
try {
|
|
1044
|
-
await handleRequestCancelStrategy();
|
|
1045
|
-
await executeHooks(options.onRequest?.({
|
|
980
|
+
const createFetchClientWithContext = () => {
|
|
981
|
+
const createFetchClient$1 = (initBaseConfig = {}) => {
|
|
982
|
+
const $LocalRequestInfoCache = /* @__PURE__ */ new Map();
|
|
983
|
+
const callApi$1 = async (initURL, initConfig = {}) => {
|
|
984
|
+
const [fetchOptions, extraOptions] = splitConfig(initConfig);
|
|
985
|
+
const baseConfig = isFunction(initBaseConfig) ? initBaseConfig({
|
|
986
|
+
initURL: initURL.toString(),
|
|
987
|
+
options: extraOptions,
|
|
988
|
+
request: fetchOptions
|
|
989
|
+
}) : initBaseConfig;
|
|
990
|
+
const config = initConfig;
|
|
991
|
+
const [baseFetchOptions, baseExtraOptions] = splitBaseConfig(baseConfig);
|
|
992
|
+
const shouldSkipAutoMergeForOptions = baseExtraOptions.skipAutoMergeFor === "all" || baseExtraOptions.skipAutoMergeFor === "options";
|
|
993
|
+
const shouldSkipAutoMergeForRequest = baseExtraOptions.skipAutoMergeFor === "all" || baseExtraOptions.skipAutoMergeFor === "request";
|
|
994
|
+
const mergedExtraOptions = {
|
|
995
|
+
...baseExtraOptions,
|
|
996
|
+
...!shouldSkipAutoMergeForOptions && extraOptions
|
|
997
|
+
};
|
|
998
|
+
const mergedRequestOptions = {
|
|
999
|
+
headers: {},
|
|
1000
|
+
...baseFetchOptions,
|
|
1001
|
+
...!shouldSkipAutoMergeForRequest && fetchOptions
|
|
1002
|
+
};
|
|
1003
|
+
const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequestOptions } = await initializePlugins({
|
|
1046
1004
|
baseConfig,
|
|
1047
1005
|
config,
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig, shouldApplySchemaOutput } = await handleConfigValidation({
|
|
1052
|
-
baseExtraOptions,
|
|
1053
|
-
currentRouteSchemaKey: resolvedCurrentRouteSchemaKey,
|
|
1054
|
-
extraOptions,
|
|
1055
|
-
options,
|
|
1056
|
-
requestOptions: request
|
|
1006
|
+
initURL: initURL.toString(),
|
|
1007
|
+
options: mergedExtraOptions,
|
|
1008
|
+
request: mergedRequestOptions
|
|
1057
1009
|
});
|
|
1058
|
-
|
|
1059
|
-
|
|
1010
|
+
const { fullURL, normalizedInitURL } = getFullAndNormalizedURL({
|
|
1011
|
+
baseURL: resolvedOptions.baseURL,
|
|
1060
1012
|
initURL: resolvedInitURL,
|
|
1061
|
-
|
|
1013
|
+
params: resolvedOptions.params,
|
|
1014
|
+
query: resolvedOptions.query
|
|
1062
1015
|
});
|
|
1063
|
-
const
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
...validMethod && { method: validMethod }
|
|
1016
|
+
const options = {
|
|
1017
|
+
...resolvedOptions,
|
|
1018
|
+
...resolvedHooks,
|
|
1019
|
+
...resolvedMiddlewares,
|
|
1020
|
+
fullURL,
|
|
1021
|
+
initURL: resolvedInitURL,
|
|
1022
|
+
initURLNormalized: normalizedInitURL
|
|
1023
|
+
};
|
|
1024
|
+
const newFetchController = new AbortController();
|
|
1025
|
+
const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout), resolvedRequestOptions.signal, newFetchController.signal);
|
|
1026
|
+
const initMethod = getMethod({
|
|
1027
|
+
initURL: resolvedInitURL,
|
|
1028
|
+
method: resolvedRequestOptions.method
|
|
1077
1029
|
});
|
|
1078
|
-
const
|
|
1030
|
+
const request = {
|
|
1031
|
+
...resolvedRequestOptions,
|
|
1032
|
+
method: initMethod,
|
|
1033
|
+
signal: combinedSignal
|
|
1034
|
+
};
|
|
1035
|
+
const { getAbortErrorMessage, handleRequestCancelStrategy, handleRequestDeferStrategy, removeDedupeKeyFromCache, resolvedDedupeStrategy } = await createDedupeStrategy({
|
|
1036
|
+
$GlobalRequestInfoCache,
|
|
1037
|
+
$LocalRequestInfoCache,
|
|
1079
1038
|
baseConfig,
|
|
1080
1039
|
config,
|
|
1081
|
-
|
|
1082
|
-
request
|
|
1083
|
-
};
|
|
1084
|
-
await executeHooks(options.onRequestReady?.(readyRequestContext));
|
|
1085
|
-
const response = await handleRequestDeferStrategy({
|
|
1086
|
-
fetchApi: getFetchImpl({
|
|
1087
|
-
customFetchImpl: options.customFetchImpl,
|
|
1088
|
-
fetchMiddleware: options.fetchMiddleware,
|
|
1089
|
-
requestContext: readyRequestContext
|
|
1090
|
-
}),
|
|
1040
|
+
newFetchController,
|
|
1091
1041
|
options,
|
|
1092
1042
|
request
|
|
1093
1043
|
});
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1044
|
+
try {
|
|
1045
|
+
await handleRequestCancelStrategy();
|
|
1046
|
+
await executeHooks(options.onRequest?.({
|
|
1047
|
+
baseConfig,
|
|
1048
|
+
config,
|
|
1049
|
+
options,
|
|
1050
|
+
request
|
|
1051
|
+
}));
|
|
1052
|
+
const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig, shouldApplySchemaOutput } = await handleConfigValidation({
|
|
1053
|
+
baseExtraOptions,
|
|
1054
|
+
currentRouteSchemaKey: resolvedCurrentRouteSchemaKey,
|
|
1055
|
+
extraOptions,
|
|
1056
|
+
options,
|
|
1057
|
+
requestOptions: request
|
|
1058
|
+
});
|
|
1059
|
+
if (shouldApplySchemaOutput) Object.assign(options, extraOptionsValidationResult);
|
|
1060
|
+
const validMethod = getMethod({
|
|
1061
|
+
initURL: resolvedInitURL,
|
|
1062
|
+
method: shouldApplySchemaOutput ? requestOptionsValidationResult?.method : request.method
|
|
1063
|
+
});
|
|
1064
|
+
const validBody = getBody({
|
|
1065
|
+
body: shouldApplySchemaOutput ? requestOptionsValidationResult?.body : request.body,
|
|
1066
|
+
bodySerializer: options.bodySerializer
|
|
1067
|
+
});
|
|
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
|
+
Object.assign(request, {
|
|
1075
|
+
...validBody && { body: validBody },
|
|
1076
|
+
...validHeaders && { headers: validHeaders },
|
|
1077
|
+
...validMethod && { method: validMethod }
|
|
1100
1078
|
});
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1079
|
+
const readyRequestContext = {
|
|
1080
|
+
baseConfig,
|
|
1081
|
+
config,
|
|
1082
|
+
options,
|
|
1083
|
+
request
|
|
1084
|
+
};
|
|
1085
|
+
await executeHooks(options.onRequestReady?.(readyRequestContext));
|
|
1086
|
+
const response = await handleRequestDeferStrategy({
|
|
1087
|
+
fetchApi: getFetchImpl({
|
|
1088
|
+
customFetchImpl: options.customFetchImpl,
|
|
1089
|
+
fetchMiddleware: options.fetchMiddleware,
|
|
1090
|
+
requestContext: readyRequestContext
|
|
1091
|
+
}),
|
|
1092
|
+
options,
|
|
1093
|
+
request
|
|
1094
|
+
});
|
|
1095
|
+
const shouldCloneResponse = resolvedDedupeStrategy === "defer" || options.cloneResponse;
|
|
1096
|
+
if (!response.ok) {
|
|
1097
|
+
const validErrorData = await handleSchemaValidation(resolvedSchema, "errorData", {
|
|
1098
|
+
inputValue: await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser),
|
|
1099
|
+
response,
|
|
1100
|
+
schemaConfig: resolvedSchemaConfig
|
|
1101
|
+
});
|
|
1102
|
+
throw new HTTPError({
|
|
1103
|
+
defaultHTTPErrorMessage: options.defaultHTTPErrorMessage,
|
|
1104
|
+
errorData: validErrorData,
|
|
1105
|
+
response
|
|
1106
|
+
}, { cause: validErrorData });
|
|
1107
|
+
}
|
|
1108
|
+
const successContext = {
|
|
1109
|
+
baseConfig,
|
|
1110
|
+
config,
|
|
1111
|
+
data: await handleSchemaValidation(resolvedSchema, "data", {
|
|
1112
|
+
inputValue: await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser),
|
|
1113
|
+
response,
|
|
1114
|
+
schemaConfig: resolvedSchemaConfig
|
|
1115
|
+
}),
|
|
1116
|
+
options,
|
|
1117
|
+
request,
|
|
1104
1118
|
response
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
schemaConfig: resolvedSchemaConfig
|
|
1114
|
-
}),
|
|
1115
|
-
options,
|
|
1116
|
-
request,
|
|
1117
|
-
response
|
|
1118
|
-
};
|
|
1119
|
-
await executeHooks(options.onSuccess?.(successContext), options.onResponse?.({
|
|
1120
|
-
...successContext,
|
|
1121
|
-
error: null
|
|
1122
|
-
}));
|
|
1123
|
-
return resolveSuccessResult(successContext.data, {
|
|
1124
|
-
response: successContext.response,
|
|
1125
|
-
resultMode: options.resultMode
|
|
1126
|
-
});
|
|
1127
|
-
} catch (error) {
|
|
1128
|
-
const errorInfo = {
|
|
1129
|
-
cloneResponse: options.cloneResponse,
|
|
1130
|
-
resultMode: options.resultMode
|
|
1131
|
-
};
|
|
1132
|
-
const { errorDetails, errorResult } = resolveErrorResult(error, errorInfo);
|
|
1133
|
-
const errorContext = {
|
|
1134
|
-
baseConfig,
|
|
1135
|
-
config,
|
|
1136
|
-
error: errorDetails.error,
|
|
1137
|
-
options,
|
|
1138
|
-
request,
|
|
1139
|
-
response: errorDetails.response
|
|
1140
|
-
};
|
|
1141
|
-
const shouldThrowOnError = Boolean(isFunction(options.throwOnError) ? options.throwOnError(errorContext) : options.throwOnError);
|
|
1142
|
-
const hookInfo = {
|
|
1143
|
-
errorInfo,
|
|
1144
|
-
shouldThrowOnError
|
|
1145
|
-
};
|
|
1146
|
-
const { handleRetry, shouldAttemptRetry } = createRetryManager(errorContext);
|
|
1147
|
-
const handleRetryOrGetErrorResult = async () => {
|
|
1148
|
-
if (await shouldAttemptRetry()) return handleRetry({
|
|
1149
|
-
callApi: callApi$1,
|
|
1150
|
-
callApiArgs: {
|
|
1151
|
-
config,
|
|
1152
|
-
initURL
|
|
1153
|
-
},
|
|
1154
|
-
errorContext,
|
|
1155
|
-
hookInfo
|
|
1119
|
+
};
|
|
1120
|
+
await executeHooks(options.onSuccess?.(successContext), options.onResponse?.({
|
|
1121
|
+
...successContext,
|
|
1122
|
+
error: null
|
|
1123
|
+
}));
|
|
1124
|
+
return resolveSuccessResult(successContext.data, {
|
|
1125
|
+
response: successContext.response,
|
|
1126
|
+
resultMode: options.resultMode
|
|
1156
1127
|
});
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1128
|
+
} catch (error) {
|
|
1129
|
+
const errorInfo = {
|
|
1130
|
+
cloneResponse: options.cloneResponse,
|
|
1131
|
+
resultMode: options.resultMode
|
|
1132
|
+
};
|
|
1133
|
+
const { errorDetails, errorResult } = resolveErrorResult(error, errorInfo);
|
|
1134
|
+
const errorContext = {
|
|
1135
|
+
baseConfig,
|
|
1136
|
+
config,
|
|
1137
|
+
error: errorDetails.error,
|
|
1138
|
+
options,
|
|
1139
|
+
request,
|
|
1140
|
+
response: errorDetails.response
|
|
1141
|
+
};
|
|
1142
|
+
const shouldThrowOnError = Boolean(isFunction(options.throwOnError) ? options.throwOnError(errorContext) : options.throwOnError);
|
|
1143
|
+
const hookInfo = {
|
|
1144
|
+
errorInfo,
|
|
1145
|
+
shouldThrowOnError
|
|
1146
|
+
};
|
|
1147
|
+
const { handleRetry, shouldAttemptRetry } = createRetryManager(errorContext);
|
|
1148
|
+
const handleRetryOrGetErrorResult = async () => {
|
|
1149
|
+
if (await shouldAttemptRetry()) return handleRetry({
|
|
1150
|
+
callApi: callApi$1,
|
|
1151
|
+
callApiArgs: {
|
|
1152
|
+
config,
|
|
1153
|
+
initURL
|
|
1154
|
+
},
|
|
1155
|
+
errorContext,
|
|
1156
|
+
hookInfo
|
|
1157
|
+
});
|
|
1158
|
+
if (shouldThrowOnError) throw error;
|
|
1159
|
+
return errorResult;
|
|
1160
|
+
};
|
|
1161
|
+
if (isValidationErrorInstance(error)) return await executeHooksInCatchBlock([options.onValidationError?.(errorContext), options.onError?.(errorContext)], hookInfo) ?? await handleRetryOrGetErrorResult();
|
|
1162
|
+
if (isHTTPErrorInstance(error)) return await executeHooksInCatchBlock([
|
|
1163
|
+
options.onResponseError?.(errorContext),
|
|
1164
|
+
options.onError?.(errorContext),
|
|
1165
|
+
options.onResponse?.({
|
|
1166
|
+
...errorContext,
|
|
1167
|
+
data: null
|
|
1168
|
+
})
|
|
1169
|
+
], hookInfo) ?? await handleRetryOrGetErrorResult();
|
|
1170
|
+
let message = error?.message;
|
|
1171
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
1172
|
+
message = getAbortErrorMessage();
|
|
1173
|
+
!shouldThrowOnError && console.error(`${error.name}:`, message);
|
|
1174
|
+
}
|
|
1175
|
+
if (error instanceof DOMException && error.name === "TimeoutError") {
|
|
1176
|
+
message = `Request timed out after ${options.timeout}ms`;
|
|
1177
|
+
!shouldThrowOnError && console.error(`${error.name}:`, message);
|
|
1178
|
+
}
|
|
1179
|
+
return await executeHooksInCatchBlock([options.onRequestError?.(errorContext), options.onError?.(errorContext)], hookInfo) ?? getCustomizedErrorResult(await handleRetryOrGetErrorResult(), { message });
|
|
1180
|
+
} finally {
|
|
1181
|
+
removeDedupeKeyFromCache();
|
|
1173
1182
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
!shouldThrowOnError && console.error(`${error.name}:`, message);
|
|
1177
|
-
}
|
|
1178
|
-
return await executeHooksInCatchBlock([options.onRequestError?.(errorContext), options.onError?.(errorContext)], hookInfo) ?? getCustomizedErrorResult(await handleRetryOrGetErrorResult(), { message });
|
|
1179
|
-
} finally {
|
|
1180
|
-
removeDedupeKeyFromCache();
|
|
1181
|
-
}
|
|
1183
|
+
};
|
|
1184
|
+
return callApi$1;
|
|
1182
1185
|
};
|
|
1183
|
-
return
|
|
1186
|
+
return createFetchClient$1;
|
|
1184
1187
|
};
|
|
1188
|
+
const createFetchClient = createFetchClientWithContext();
|
|
1185
1189
|
const callApi = createFetchClient();
|
|
1186
1190
|
|
|
1187
1191
|
//#endregion
|
|
1188
|
-
export { callApi, createFetchClient };
|
|
1192
|
+
export { callApi, createFetchClient, createFetchClientWithContext };
|
|
1189
1193
|
//# sourceMappingURL=index.js.map
|