@zayne-labs/callapi 1.11.30 → 1.11.32

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/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
- if (schemaConfig?.disableRuntimeValidation) return inputValue;
71
- return await standardSchemaParser(fullSchema, schemaName, {
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 { requestOptions, schema, schemaConfig } = validationOptions;
102
- const validationResultArray = await Promise.all(requestOptionsToBeValidated.map((schemaName) => handleSchemaValidation(schema, schemaName, {
103
- inputValue: requestOptions[schemaName],
104
- schemaConfig
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, requestOptions } = validationOptions;
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
- requestOptions,
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) => {
@@ -324,15 +322,19 @@ const pickKeys = (initialObject, keysToPick) => {
324
322
  const splitBaseConfig = (baseConfig) => [pickKeys(baseConfig, fetchSpecificKeys), omitKeys(baseConfig, fetchSpecificKeys)];
325
323
  const splitConfig = (config) => [pickKeys(config, fetchSpecificKeys), omitKeys(config, fetchSpecificKeys)];
326
324
  const objectifyHeaders = (headers) => {
327
- if (!headers || isPlainObject(headers)) return headers;
325
+ if (!headers) return {};
326
+ if (isPlainObject(headers)) return headers;
328
327
  return Object.fromEntries(headers);
329
328
  };
329
+ const getResolvedHeaders = (options) => {
330
+ const { baseHeaders, headers } = options;
331
+ return objectifyHeaders(isFunction(headers) ? headers({ baseHeaders: objectifyHeaders(baseHeaders) }) : headers ?? baseHeaders);
332
+ };
330
333
  const getHeaders = async (options) => {
331
- const { auth, body, headers } = options;
332
- if (!(Boolean(headers) || Boolean(body) || Boolean(auth))) return;
334
+ const { auth, body, resolvedHeaders } = options;
333
335
  const headersObject = {
334
336
  ...await getAuthHeader(auth),
335
- ...objectifyHeaders(headers)
337
+ ...objectifyHeaders(resolvedHeaders)
336
338
  };
337
339
  if (isQueryString(body)) {
338
340
  headersObject["Content-Type"] = "application/x-www-form-urlencoded";
@@ -788,8 +790,8 @@ const getResolvedPlugins = (context) => {
788
790
  const { baseConfig, options } = context;
789
791
  return isFunction(options.plugins) ? options.plugins({ basePlugins: baseConfig.plugins ?? [] }) : options.plugins ?? [];
790
792
  };
791
- const initializePlugins = async (context) => {
792
- const { baseConfig, config, initURL, options, request } = context;
793
+ const initializePlugins = async (setupContext) => {
794
+ const { baseConfig, config, initURL, options, request } = setupContext;
793
795
  const { addMainHooks, addMainMiddlewares, addPluginHooks, addPluginMiddlewares, getResolvedHooks, getResolvedMiddlewares } = setupHooksAndMiddlewares({
794
796
  baseConfig,
795
797
  config,
@@ -802,12 +804,21 @@ const initializePlugins = async (context) => {
802
804
  });
803
805
  let resolvedCurrentRouteSchemaKey = currentRouteSchemaKey;
804
806
  let resolvedInitURL = mainInitURL;
805
- let resolvedOptions = options;
806
- let resolvedRequestOptions = request;
807
+ const resolvedOptions = options;
808
+ const resolvedRequest = Object.assign(request, {
809
+ headers: getResolvedHeaders({
810
+ baseHeaders: baseConfig.headers,
811
+ headers: request.headers
812
+ }),
813
+ method: getMethod({
814
+ initURL: resolvedInitURL,
815
+ method: request.method
816
+ })
817
+ });
807
818
  const executePluginSetupFn = async (pluginSetup) => {
808
819
  if (!pluginSetup) return;
809
- const initResult = await pluginSetup(context);
810
- if (!isPlainObject(initResult)) return;
820
+ const initResult = await pluginSetup(setupContext);
821
+ if (!initResult) return;
811
822
  const urlString = initResult.initURL?.toString();
812
823
  if (isString(urlString)) {
813
824
  const newResult = getCurrentRouteSchemaKeyAndMainInitURL({
@@ -818,14 +829,8 @@ const initializePlugins = async (context) => {
818
829
  resolvedCurrentRouteSchemaKey = newResult.currentRouteSchemaKey;
819
830
  resolvedInitURL = newResult.mainInitURL;
820
831
  }
821
- if (isPlainObject(initResult.request)) resolvedRequestOptions = {
822
- ...resolvedRequestOptions,
823
- ...initResult.request
824
- };
825
- if (isPlainObject(initResult.options)) resolvedOptions = {
826
- ...resolvedOptions,
827
- ...initResult.options
828
- };
832
+ if (initResult.request) Object.assign(resolvedRequest, initResult.request);
833
+ if (initResult.options) Object.assign(resolvedOptions, initResult.options);
829
834
  };
830
835
  const resolvedPlugins = getResolvedPlugins({
831
836
  baseConfig,
@@ -834,8 +839,8 @@ const initializePlugins = async (context) => {
834
839
  for (const plugin of resolvedPlugins) {
835
840
  const [, pluginHooks, pluginMiddlewares] = await Promise.all([
836
841
  executePluginSetupFn(plugin.setup),
837
- isFunction(plugin.hooks) ? plugin.hooks(context) : plugin.hooks,
838
- isFunction(plugin.middlewares) ? plugin.middlewares(context) : plugin.middlewares
842
+ isFunction(plugin.hooks) ? plugin.hooks(setupContext) : plugin.hooks,
843
+ isFunction(plugin.middlewares) ? plugin.middlewares(setupContext) : plugin.middlewares
839
844
  ]);
840
845
  pluginHooks && addPluginHooks(pluginHooks);
841
846
  pluginMiddlewares && addPluginMiddlewares(pluginMiddlewares);
@@ -850,7 +855,7 @@ const initializePlugins = async (context) => {
850
855
  resolvedInitURL,
851
856
  resolvedMiddlewares,
852
857
  resolvedOptions,
853
- resolvedRequestOptions
858
+ resolvedRequest
854
859
  };
855
860
  };
856
861
  const setupHooksAndMiddlewares = (context) => {
@@ -996,11 +1001,10 @@ const createFetchClientWithContext = () => {
996
1001
  ...!shouldSkipAutoMergeForOptions && extraOptions
997
1002
  };
998
1003
  const mergedRequestOptions = {
999
- headers: {},
1000
1004
  ...baseFetchOptions,
1001
1005
  ...!shouldSkipAutoMergeForRequest && fetchOptions
1002
1006
  };
1003
- const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequestOptions } = await initializePlugins({
1007
+ const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequest } = await initializePlugins({
1004
1008
  baseConfig,
1005
1009
  config,
1006
1010
  initURL: initURL.toString(),
@@ -1022,14 +1026,9 @@ const createFetchClientWithContext = () => {
1022
1026
  initURLNormalized: normalizedInitURL
1023
1027
  };
1024
1028
  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
1029
- });
1029
+ const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout), resolvedRequest.signal, newFetchController.signal);
1030
1030
  const request = {
1031
- ...resolvedRequestOptions,
1032
- method: initMethod,
1031
+ ...resolvedRequest,
1033
1032
  signal: combinedSignal
1034
1033
  };
1035
1034
  const { getAbortErrorMessage, handleRequestCancelStrategy, handleRequestDeferStrategy, removeDedupeKeyFromCache, resolvedDedupeStrategy } = await createDedupeStrategy({
@@ -1049,32 +1048,29 @@ const createFetchClientWithContext = () => {
1049
1048
  options,
1050
1049
  request
1051
1050
  }));
1052
- const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig, shouldApplySchemaOutput } = await handleConfigValidation({
1051
+ const { extraOptionsValidationResult, requestOptionsValidationResult, resolvedSchema, resolvedSchemaConfig } = await handleConfigValidation({
1053
1052
  baseExtraOptions,
1054
1053
  currentRouteSchemaKey: resolvedCurrentRouteSchemaKey,
1055
1054
  extraOptions,
1056
1055
  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
1056
+ request
1063
1057
  });
1058
+ Object.assign(options, extraOptionsValidationResult);
1064
1059
  const validBody = getBody({
1065
- body: shouldApplySchemaOutput ? requestOptionsValidationResult?.body : request.body,
1060
+ body: requestOptionsValidationResult.body,
1066
1061
  bodySerializer: options.bodySerializer
1067
1062
  });
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
1063
  Object.assign(request, {
1075
- ...validBody && { body: validBody },
1076
- ...validHeaders && { headers: validHeaders },
1077
- ...validMethod && { method: validMethod }
1064
+ body: validBody,
1065
+ headers: await getHeaders({
1066
+ auth: options.auth,
1067
+ body: validBody,
1068
+ resolvedHeaders: requestOptionsValidationResult.headers
1069
+ }),
1070
+ method: getMethod({
1071
+ initURL: resolvedInitURL,
1072
+ method: requestOptionsValidationResult.method
1073
+ })
1078
1074
  });
1079
1075
  const readyRequestContext = {
1080
1076
  baseConfig,