@eleven-am/xquery
Version:
XQuery is a sophisticated TypeScript wrapper for TanStack React Query, designed to enhance developer productivity with OpenAPI-generated clients. It enables developers to build and execute queries using fully type-safe factories instead of managing query
98 lines • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildQueryFn = buildQueryFn;
exports.buildMutationFn = buildMutationFn;
exports.buildQueryKey = buildQueryKey;
exports.buildMutationOptions = buildMutationOptions;
exports.buildActionOptions = buildActionOptions;
exports.buildInfiniteOptions = buildInfiniteOptions;
function buildQueryFn(toastError, clientGetter, errorMapper, queryFn) {
return ({ signal }) => queryFn(clientGetter(signal)).then(errorMapper(toastError));
}
function buildMutationFn(toastError, clientGetter, errorMapper, mutationFn) {
return (variables) => mutationFn(clientGetter(), variables).then(errorMapper(toastError));
}
function buildQueryKey(namespace, key, mapQueryKey, clientGetter, errorMapper, options) {
const toastError = options.toastError || false;
const oldQueryKey = options.queryKey || [];
const queryKey = mapQueryKey([namespace, key, ...oldQueryKey]);
const queryFn = buildQueryFn(toastError, clientGetter, errorMapper, options.queryFn);
return {
...options,
queryKey,
queryFn,
};
}
function buildMutationOptions(queryClient, clientGetter, errorMapper, options) {
const toastError = options.toastError ?? true;
const invalidateKeys = options.invalidateKeys ?? [];
const mutationFn = buildMutationFn(toastError, clientGetter, errorMapper, options.mutationFn);
const onSuccess = async (data, variables, context) => {
const promises = invalidateKeys.map((queryKey) => queryClient().invalidateQueries({
queryKey,
}));
await Promise.all(promises);
await options.onSuccess?.(data, variables, context);
};
return {
...options,
mutationFn,
onSuccess,
};
}
function buildActionOptions(namespace, key, mapQueryKey, queryClient, clientGetter, errorMapper, options) {
const toastError = options.toastError ?? true;
const oldQueryKey = options.queryKey || [];
const queryKey = mapQueryKey([namespace, key, ...oldQueryKey]);
const keysToInvalidate = [queryKey, ...(options.invalidateKeys || [])];
const mutationFn = buildMutationFn(toastError, clientGetter, errorMapper, options.mutationFn);
const latestVariablesKey = [...queryKey, 'latestVariables'];
const queryFn = ({ signal }) => {
const latestVariables = queryClient().getQueryData(latestVariablesKey);
return options.queryFn(clientGetter(signal), latestVariables).then(errorMapper(toastError));
};
const onSuccess = async (data, variables, context) => {
const promises = keysToInvalidate.map((queryKey) => queryClient().invalidateQueries({
queryKey,
}));
await queryClient().setQueryData(latestVariablesKey, data);
await options.onSuccess?.(data, variables, context);
await Promise.all(promises);
};
return {
queryClientGetter: queryClient,
queryOptions: {
...options,
queryKey,
queryFn,
},
mutateOptions: {
...options,
mutationFn,
onSuccess,
},
};
}
function buildInfiniteOptions(namespace, propertyNamespace, mapQueryKey, queryClient, clientGetter, mapResponse, property) {
const oldKey = property.queryKey || [];
const newKey = mapQueryKey([namespace, propertyNamespace, ...oldKey]);
const queryFn = ({ pageParam }) => property.queryFn(clientGetter(), pageParam || 1).then(mapResponse(property.toastError || false));
const getNextPageParam = (data) => data.page === data.totalPages ? null : data.page + 1;
let initialData;
if (property.initialData) {
initialData = {
pages: [property.initialData],
pageParams: [1],
};
}
return {
...property,
initialData,
queryKey: newKey,
queryFn,
getNextPageParam,
initialPageParam: 1,
queryClientGetter: queryClient,
};
}
//# sourceMappingURL=queryBuilders.js.map