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.

89 lines (85 loc) 4.47 kB
'use strict'; var createRecursiveProxy = require('./lib/createRecursiveProxy.cjs'); function qraftAPIClient(services, callbacks, options) { const stringTag = 'QraftAPIClient'; const toString = (path)=>{ return `[${stringTag}${path.length ? ' ' + path.join('.') : ''}]`; }; const primitiveMethods = { [Symbol.toPrimitive] (path, hint) { if (hint === 'number') return NaN; if (hint === 'string') return toString(path); return getByPath(services, path); }, valueOf (path) { return getByPath(services, path); }, toJSON (path) { return JSON.stringify(getByPath(services, path)); }, toString (path) { return toString(path); } }; return createRecursiveProxy.createRecursiveProxy(function getCallback(getPath, key) { if (Object.prototype.hasOwnProperty.call(primitiveMethods, key)) { return primitiveMethods[key].bind(undefined, getPath); } else if (key === Symbol.toStringTag) { return stringTag; } if (key !== 'schema') return; if (getPath.length !== 2 && !('schema' in services) && getPath.length !== 1 && !services[getPath[0]]?.schema) return; const serviceOperation = getByPath(services, getPath); if (!isServiceOperation(serviceOperation)) throw new Error(`Service operation not found: ${getPath.join('.')}`); return serviceOperation.schema; }, function applyCallback(applyPath, args) { const { path, callbackName } = extractCallbackDetails(applyPath, services); assertValidCallbackName(callbackName, callbacks); const serviceOperation = getByPath(services, path); if (!isServiceOperation(serviceOperation)) throw new Error(`Service operation not found: ${path.join('.')}`); if (callbackName !== 'operationInvokeFn' && callbackName !== 'getQueryKey' && callbackName !== 'getMutationKey' && callbackName !== 'getInfiniteQueryKey' && callbackName !== 'useInfiniteQuery' && callbackName !== 'useQueries' && callbackName !== 'useQuery' && callbackName !== 'useSuspenseInfiniteQuery' && callbackName !== 'useSuspenseQueries' && callbackName !== 'useSuspenseQuery' && callbackName !== 'useIsFetching' && callbackName !== 'useMutation' && callbackName !== 'useIsMutating' && callbackName !== 'useMutationState') { if (!options || !('queryClient' in options && options.queryClient)) throw new Error(`'qraft.<service>.<operation>.${String(callbackName)}()' requires 'queryClient' in 'createAPIClient(...)' options.`); } // @ts-expect-error - Too complex union type return callbacks[callbackName](options, serviceOperation.schema, args); }, []); } function assertValidCallbackName(callbackName, callbacks) { if (!(callbackName in callbacks)) { if (callbackName === 'operationInvokeFn') throw new Error(`Callback 'operationInvokeFn' is required for executing 'qraft.<service>.<operation>()', but it is not provided in the 'callbacks' object.`); throw new Error(`Callback for 'qraft.<service>.<operation>.${String(callbackName)}()' is not provided in the 'callbacks' object.`); } } /** * Extracts Callback details from the applyPath */ function extractCallbackDetails(applyPath, services) { // client.<service>.<operation>() [OperationInvokeFn] // <service>.<operation>() [OperationInvokeFn] // client() [OperationInvokeFn] // client() if (applyPath.length === 2 && !services[applyPath[0]]?.schema || applyPath.length === 0 && 'schema' in services) { return { path: applyPath, callbackName: 'operationInvokeFn' }; } else { // client.<service>.<operation>.<method>() // <service>.<operation>.<method>() // client.<method>() return { path: applyPath.slice(0, -1), // The last arg is for instance `.useMutation` or `.useQuery()` callbackName: applyPath[applyPath.length - 1] }; } } function isServiceOperation(input) { return input !== null && typeof input === 'object' && 'schema' in input; } function getByPath(obj, path) { return path.reduce((acc, key)=>{ if (acc && typeof acc === 'object' && key in acc) return acc[key]; }, obj); } exports.qraftAPIClient = qraftAPIClient; //# sourceMappingURL=qraftAPIClient.cjs.map