{"version":3,"sources":["../core.ts","../utils.ts","../define-mutation.ts","../define-query.ts","../define-module.ts","../helpers.ts"],"names":["key"],"mappings":";AAEA,IAAM,qBAAN,MAAyB;AAAA,EAAzB,WAAA,GAAA;AACE,IAAA,IAAA,CAAQ,UAAwC,EAAC;AAAA,EAAA;AAAA,EAEjD,QAAA,CACE,YACA,MAAA,EACM;AACN,IAAA,IAAA,CAAK,OAAA,CAAQ,UAAU,CAAA,GAAI,MAAA;AAAA,EAC7B;AAAA,EAEA,GAAA,CACE,UAAA,EACA,GAAA,EACA,OAAA,EACK;AACL,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,OAAA,CAAQ,UAAU,CAAA;AACtC,IAAA,IAAI,CAAC,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,QAAA,EAAW,UAAU,CAAA,WAAA,CAAa,CAAA;AAAA,IACpD;AAEA,IAAA,MAAM,EAAA,GAAK,OAAO,GAAG,CAAA;AAErB,IAAA,IAAI,CAAC,EAAA,EAAI;AACP,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,KAAA,EAAQ,GAAG,CAAA,uBAAA,EAA0B,UAAU,CAAA,CAAA,CAAG,CAAA;AAAA,IACpE;AAEA,IAAA,OAAO,GAAG,OAAO,CAAA;AAAA,EACnB;AAAA,EAEA,cAAA,GAAiB;AACf,IAAA,OAAO,MAAA,CAAO,IAAA,CAAK,IAAA,CAAK,OAAO,CAAA;AAAA,EACjC;AAAA,EAEA,QACE,UAAA,EACA;AACA,IAAA,OAAO,IAAA,CAAK,QAAQ,UAAU,CAAA;AAAA,EAChC;AACF,CAAA;AAEO,IAAM,kBAAA,GAAqB,IAAI,kBAAA;;;AC1C/B,SAAS,YAAY,GAAA,EAAqC;AAC/D,EAAA,OAAQ,GAAA,CAAkB,MAAA;AAAA,IACxB,CAACA,IAAAA,KAAQA,IAAAA,KAAQ,MAAA,IAAaA,IAAAA,KAAQ,QAAQA,IAAAA,KAAQ;AAAA,GACx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,IACF,CAAA;AAAA,IAEA;AAAA,GACF;AACF;AAgBO,SAAS,kBAAA,CASd,UAAA,EACA,GAAA,EACA,OAAA,EAKQ;AACR,EAAA,OAAO,kBAAA,CAAmB,GAAA,CAAI,UAAA,EAAY,GAAA,EAAK,OAAO,CAAA;AACxD;AAMO,SAAS,wBAAA,GAA2B;AACzC,EAAA,OAAO,mBAAmB,cAAA,EAAe;AAC3C;AAQO,SAAS,cAEd,UAAA,EAAyB;AACzB,EAAA,OAAO,kBAAA,CAAmB,QAAQ,UAAU,CAAA;AAC9C","file":"index.mjs","sourcesContent":["import { ModuleConfig, TanstackQueryCraftRegistry } from \"./types\";\n\nclass TanstackQueryCraft {\n  private modules: Record<string, ModuleConfig> = {};\n\n  register<TModuleName extends string, TModule extends ModuleConfig>(\n    moduleName: TModuleName,\n    module: TModule\n  ): void {\n    this.modules[moduleName] = module;\n  }\n\n  get<TModuleName extends string, TKey extends string, TOptions extends any>(\n    moduleName: TModuleName,\n    key: TKey,\n    options?: TOptions\n  ): any {\n    const module = this.modules[moduleName];\n    if (!module) {\n      throw new Error(`Module \"${moduleName}\" not found`);\n    }\n\n    const fn = module[key];\n\n    if (!fn) {\n      throw new Error(`Key \"${key}\" not found in module \"${moduleName}\"`);\n    }\n\n    return fn(options);\n  }\n\n  getModuleNames() {\n    return Object.keys(this.modules);\n  }\n\n  getKeys<TModuleName extends keyof TanstackQueryCraftRegistry>(\n    moduleName: TModuleName\n  ) {\n    return this.modules[moduleName];\n  }\n}\n\nexport const tanstackQueryCraft = new TanstackQueryCraft();\n","export function sanitizeKey(key: unknown[] | readonly unknown[]) {\n  return (key as unknown[]).filter(\n    (key) => key !== undefined && key !== null && key !== \"\"\n  ) as readonly unknown[];\n}\n","import { MutationKey } from \"@tanstack/react-query\";\nimport { MutationConfig } from \"./types\";\nimport { sanitizeKey } from \"./utils\";\n\n/**\n * Creates a reusable mutation definition for TanStack Query.\n *\n * This function allows you to define a mutation with a key and mutation function,\n * which can be called later with optional variables. It supports both static\n * and dynamic mutation keys based on the provided variables. Mutations are used\n * for operations that modify data (POST, PUT, DELETE, etc.).\n *\n * @template TVariables - The type of variables passed to the mutation (defaults to void)\n * @template TData - The type of data returned by the mutation\n *\n * @param config - Configuration object for the mutation\n * @param config.mutationKey - Static mutation key or a function that generates a key from variables\n * @param config.mutationFn - Async function that performs the mutation using the provided variables\n *\n * @returns A function that accepts optional variables and returns a MutationConfig object\n *\n * @example\n * const createUserMutation = defineMutation({\n *   mutationKey: ['createUser'],\n *   mutationFn: (vars) => createUser(vars.name, vars.email)\n * });\n */\nexport function defineMutation<TVariables = void, TData = unknown>(config: {\n  mutationKey: MutationKey | ((variables: TVariables) => MutationKey);\n  mutationFn: (variables: TVariables) => Promise<TData>;\n}) {\n  return (options?: TVariables) => {\n    const mutationKey =\n      typeof config.mutationKey === \"function\"\n        ? config.mutationKey(options as TVariables)\n        : config.mutationKey;\n\n    return {\n      type: \"mutation\" as const,\n      mutationKey: sanitizeKey(mutationKey),\n      mutationFn: config.mutationFn,\n    } satisfies MutationConfig<TVariables, TData>;\n  };\n}\n","import { QueryKey } from \"@tanstack/react-query\";\nimport { QueryConfig } from \"./types\";\nimport { sanitizeKey } from \"./utils\";\n\n/**\n * Creates a reusable query definition for TanStack Query.\n *\n * This function allows you to define a query with a key and fetch function,\n * which can be called later with optional variables. It supports both static\n * and dynamic query keys based on the provided variables.\n *\n * @template TVariables - The type of variables passed to the query (defaults to void)\n * @template TData - The type of data returned by the query\n *\n * @param config - Configuration object for the query\n * @param config.queryKey - Static query key or a function that generates a key from variables\n * @param config.queryFn - Async function that fetches the data using the provided variables\n *\n * @returns A function that accepts optional variables and returns a QueryConfig object\n *\n * @example\n * const getUserQuery = defineQuery({\n *   queryKey: (vars) => ['user', vars.id],\n *   queryFn: (vars) => fetchUser(vars.id)\n * });\n */\nexport function defineQuery<TVariables = void, TData = unknown>(config: {\n  queryKey: QueryKey | ((variables: TVariables) => QueryKey);\n  queryFn: (variables: TVariables) => Promise<TData>;\n}) {\n  return (options?: { variables?: TVariables }) => {\n    const queryKey =\n      typeof config.queryKey === \"function\"\n        ? config.queryKey(options?.variables as TVariables)\n        : config.queryKey;\n\n    return {\n      type: \"query\" as const,\n      queryKey: sanitizeKey(queryKey),\n      queryFn: () => config.queryFn(options?.variables as TVariables),\n    } satisfies QueryConfig<TData>;\n  };\n}\n","import { tanstackQueryCraft } from \"./core\";\nimport { ModuleConfig } from \"./types\";\n\n/**\n * Register a module configuration under the given `name` in the global\n * `tanstackQueryCraft` registry and return the module unchanged.\n *\n * This is a convenience wrapper around the registry's `register` method\n * that preserves typing for the module being defined.\n *\n * @param name - The unique module name to register.\n * @param module - The module configuration object (queries/mutations).\n * @returns The same `module` object that was registered.\n */\nexport function defineModule<\n  TModuleName extends string,\n  TModule extends ModuleConfig\n>(name: TModuleName, module: TModule): TModule {\n  tanstackQueryCraft.register(name, module);\n\n  return module as TModule;\n}\n","import { QueryClient, QueryKey } from \"@tanstack/react-query\";\nimport {\n  MutationConfig,\n  QueryConfig,\n  TanstackQueryCraftRegistry,\n} from \"./types\";\nimport { tanstackQueryCraft } from \"./core\";\n\n/**\n * Returns a set of helpers to invalidate queries using a provided\n * `QueryClient` instance. The returned object includes methods to:\n * - `invalidate`: invalidate a single registered query (optionally as the\n *   parent by using `asParent`).\n * - `invalidateByPrefix`: invalidate queries that match a given prefix\n *   `QueryKey`.\n * - `invalidateAll`: invalidate all queries in the client.\n * - `invalidateBulk`: invalidate multiple registered queries in parallel.\n *\n * This helper centralizes invalidation logic so callers can reference\n * registered module/query definitions instead of constructing query keys\n * manually.\n *\n * @param queryClient - The TanStack `QueryClient` used to perform\n *   invalidation operations.\n */\nexport function invalidateQueries(queryClient: QueryClient) {\n  return {\n    invalidate: <\n      TModuleName extends keyof TanstackQueryCraftRegistry & string,\n      TKey extends keyof TanstackQueryCraftRegistry[TModuleName] & string,\n      TOptions = TanstackQueryCraftRegistry[TModuleName][TKey] extends (\n        options?: infer O\n      ) => any\n        ? O\n        : never\n    >(\n      moduleName: TModuleName,\n      key: TKey,\n      options?: TOptions & {\n        asParent?: boolean;\n      }\n    ) => {\n      const mergedOptions = {\n        ...options,\n        asParent: options?.asParent ?? false,\n      };\n\n      const config = getRegisteredQuery(moduleName, key, mergedOptions) as\n        | QueryConfig\n        | MutationConfig;\n\n      if (config.type === \"query\") {\n        return queryClient.invalidateQueries({\n          queryKey: mergedOptions.asParent\n            ? config.queryKey.slice(0, config.queryKey.length - 1)\n            : config.queryKey,\n        });\n      }\n\n      return Promise.resolve();\n    },\n\n    invalidateByPrefix: (prefix: QueryKey) => {\n      return queryClient.invalidateQueries({ queryKey: prefix });\n    },\n\n    invalidateAll: () => {\n      return queryClient.invalidateQueries();\n    },\n\n    invalidateBulk: <\n      TModuleName extends keyof TanstackQueryCraftRegistry & string,\n      TKey extends keyof TanstackQueryCraftRegistry[TModuleName] & string,\n      TOptions = TanstackQueryCraftRegistry[TModuleName][TKey] extends (\n        options?: infer O\n      ) => any\n        ? O\n        : never\n    >(\n      queries: Array<{\n        moduleName: TModuleName;\n        key: TKey;\n        options?: TOptions & {\n          asParent?: boolean;\n        };\n      }>\n    ) => {\n      return Promise.all(\n        queries.map(({ moduleName, key, options }) => {\n          const mergedOptions = {\n            ...options,\n            asParent: options?.asParent ?? false,\n          };\n          const config = getRegisteredQuery(moduleName, key, mergedOptions) as\n            | QueryConfig\n            | MutationConfig;\n\n          if (config.type === \"query\") {\n            return queryClient.invalidateQueries({\n              queryKey: mergedOptions.asParent\n                ? config.queryKey.slice(0, config.queryKey.length - 1)\n                : config.queryKey,\n            });\n          }\n\n          return Promise.resolve();\n        })\n      );\n    },\n\n    queryClient,\n  };\n}\n\n/**\n * Retrieve a registered query or mutation configuration from the global\n * `tanstackQueryCraft` registry.\n *\n * This function forwards the `moduleName`, `key` and optional `options`\n * to the registry and returns the resolved configuration or factory\n * result. Type generics ensure the return type matches the registered\n * definition.\n *\n * @param moduleName - The registered module name.\n * @param key - The key of the query or mutation within the module.\n * @param options - Optional options that may be passed to the registered\n *   factory function when resolving the configuration.\n */\nexport function getRegisteredQuery<\n  TModuleName extends keyof TanstackQueryCraftRegistry & string,\n  TKey extends keyof TanstackQueryCraftRegistry[TModuleName] & string,\n  TOptions = TanstackQueryCraftRegistry[TModuleName][TKey] extends (\n    options?: infer O\n  ) => any\n    ? O\n    : never\n>(\n  moduleName: TModuleName,\n  key: TKey,\n  options?: TOptions\n): TanstackQueryCraftRegistry[TModuleName][TKey] extends (\n  options?: any\n) => infer R\n  ? R\n  : never {\n  return tanstackQueryCraft.get(moduleName, key, options);\n}\n\n/**\n * Return the list of module names that have been registered in the\n * `tanstackQueryCraft` registry.\n */\nexport function getRegisteredModuleNames() {\n  return tanstackQueryCraft.getModuleNames();\n}\n\n/**\n * Return the keys (query and mutation identifiers) available for the\n * specified registered module.\n *\n * @param moduleName - The name of the registered module to inspect.\n */\nexport function getModuleKeys<\n  TModuleName extends keyof TanstackQueryCraftRegistry\n>(moduleName: TModuleName) {\n  return tanstackQueryCraft.getKeys(moduleName);\n}\n"]}