UNPKG

@openapi-qraft/react

Version:

OpenAPI client for React, providing type-safe requests and dynamic TanStack Query React Hooks via a modular, Proxy-based architecture.

47 lines (39 loc) 1.21 kB
import type { OperationSchema } from './requestFn.js'; import { composeMutationKey } from './composeMutationKey.js'; /** * Replaces the `parameters` field in the filters with a `mutationKey` field based on the schema. * If no filters are provided, a `mutationKey` will be composed schema's base query key. * @param schema * @param filters */ export function composeMutationFilters<Filters extends object>( schema: OperationSchema, filters: Filters | undefined ) { if (!filters) { return { exact: false, mutationKey: composeMutationKey(schema, undefined), }; } if (filters && 'mutationKey' in filters && 'parameters' in filters) { throw new Error( `'composeMutationFilters': 'mutationKey' and 'parameters' cannot be used together` ); } if ('mutationKey' in filters) { return filters; } if ('parameters' in filters) { const { parameters, ...filtersWithoutParameters } = filters; Object.assign(filtersWithoutParameters, { mutationKey: composeMutationKey(schema, parameters), }); return filtersWithoutParameters; } return { exact: false, mutationKey: composeMutationKey(schema, undefined), ...filters, }; }