@rocketmakers/api-swr
Version:
Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.
30 lines (29 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.combineConfigs = void 0;
/**
* Combines a root query config with one or more global fetch configs.
*
* This function merges the `fetchConfig` from `queryConfig` with the provided
* global fetch configurations. The properties in the `queryConfig.fetchConfig`
* take precedence over those in the global configurations. If no configurations
* are provided, it returns `undefined`.
*
* @template TConfig - The type of the fetch configuration object.
* @template TRootConfig - The type of the root configuration object, which includes an optional `fetchConfig` property.
*
* @param {TRootConfig} [queryConfig] - The root configuration object which may contain a `fetchConfig`.
* @param {...Array<TConfig>} globalFetchConfigs - One or more global fetch configurations to be merged.
*
* @returns {TRootConfig | undefined} - The combined configuration object or `undefined` if no valid configurations are provided.
*/
const combineConfigs = (queryConfig, ...globalFetchConfigs) => {
if (!queryConfig && !globalFetchConfigs.length) {
return undefined;
}
if (!(queryConfig === null || queryConfig === void 0 ? void 0 : queryConfig.fetchConfig) && !globalFetchConfigs.length) {
return queryConfig;
}
return Object.assign(Object.assign({}, queryConfig), { fetchConfig: Object.assign(Object.assign({}, globalFetchConfigs.reduce((acc, c) => (Object.assign(Object.assign({}, acc), c)), {})), queryConfig === null || queryConfig === void 0 ? void 0 : queryConfig.fetchConfig) });
};
exports.combineConfigs = combineConfigs;