{"version":3,"file":"utils-DghvpG0v.mjs","names":[],"sources":["../src/internals/utils.ts"],"sourcesContent":["import { skipToken, type QueryClient } from '@tanstack/react-query';\nimport {\n  isFunction,\n  isObject,\n  run,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type {\n  AnyTRPCMutationKey,\n  AnyTRPCQueryKey,\n  FeatureFlags,\n  QueryType,\n  TRPCMutationKeyWithoutPrefix,\n  TRPCQueryKey,\n  TRPCQueryKeyWithoutPrefix,\n  TRPCQueryKeyWithPrefix,\n  TRPCQueryOptionsResult,\n} from './types';\n\n/**\n * @internal\n */\nexport function createTRPCOptionsResult(value: {\n  path: string[];\n}): TRPCQueryOptionsResult['trpc'] {\n  const path = value.path.join('.');\n\n  return {\n    path,\n  };\n}\n\nexport function isPrefixedQueryKey(\n  queryKey: TRPCQueryKey<any>,\n): queryKey is TRPCQueryKeyWithPrefix {\n  return queryKey.length >= 3;\n}\n\nexport function readQueryKey(queryKey: AnyTRPCQueryKey) {\n  if (isPrefixedQueryKey(queryKey)) {\n    return {\n      type: 'prefixed' as const,\n      prefix: queryKey[0],\n      path: queryKey[1],\n      args: queryKey[2],\n    };\n  } else {\n    return {\n      type: 'unprefixed' as const,\n      prefix: undefined,\n      path: queryKey[0],\n      args: queryKey[1],\n    };\n  }\n}\n\n/**\n * @internal\n */\nexport function getClientArgs<TOptions, TFeatureFlags extends FeatureFlags>(\n  queryKey: TRPCQueryKey<TFeatureFlags['keyPrefix']>,\n  opts: TOptions,\n  infiniteParams?: {\n    pageParam: any;\n    direction: 'forward' | 'backward';\n  },\n) {\n  const queryKeyData = readQueryKey(queryKey);\n\n  let input = queryKeyData.args?.input;\n  if (infiniteParams) {\n    input = {\n      ...(queryKeyData.args?.input ?? {}),\n      ...(infiniteParams.pageParam !== undefined\n        ? { cursor: infiniteParams.pageParam }\n        : {}),\n      direction: infiniteParams.direction,\n    };\n  }\n  return [queryKeyData.path.join('.'), input, (opts as any)?.trpc] as const;\n}\n\n/**\n * @internal\n */\nexport async function buildQueryFromAsyncIterable<\n  TQueryKey extends TRPCQueryKey<any>,\n>(\n  asyncIterable: AsyncIterable<unknown>,\n  queryClient: QueryClient,\n  queryKey: TQueryKey,\n) {\n  const queryCache = queryClient.getQueryCache();\n\n  const query = queryCache.build(queryClient, {\n    queryKey,\n  });\n\n  query.setState({\n    data: [],\n    status: 'success',\n  });\n\n  const aggregate: unknown[] = [];\n  for await (const value of asyncIterable) {\n    aggregate.push(value);\n\n    query.setState({\n      data: [...aggregate],\n    });\n  }\n  return aggregate;\n}\n\n/**\n * To allow easy interactions with groups of related queries, such as\n * invalidating all queries of a router, we use an array as the path when\n * storing in tanstack query.\n *\n * @internal\n */\nexport function getQueryKeyInternal(opts: {\n  path: string[];\n  input?: unknown;\n  type: QueryType;\n  prefix: string | undefined;\n}): AnyTRPCQueryKey {\n  const key = run((): TRPCQueryKeyWithoutPrefix => {\n    const { input, type } = opts;\n\n    // Construct a query key that is easy to destructure and flexible for\n    // partial selecting etc.\n    // https://github.com/trpc/trpc/issues/3128\n\n    // some parts of the path may be dot-separated, split them up\n    const splitPath = opts.path.flatMap((part) => part.split('.'));\n\n    if (typeof input === 'undefined' && type === 'any') {\n      // this matches also all mutations (see `getMutationKeyInternal`)\n\n      // for `utils.invalidate()` to match all queries (including vanilla react-query)\n      // we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`\n      return splitPath.length ? [splitPath] : ([] as unknown as TRPCQueryKey);\n    }\n\n    if (\n      type === 'infinite' &&\n      isObject(input) &&\n      ('direction' in input || 'cursor' in input)\n    ) {\n      const {\n        cursor: _,\n        direction: __,\n        ...inputWithoutCursorAndDirection\n      } = input;\n      return [\n        splitPath,\n        {\n          input: inputWithoutCursorAndDirection,\n          type: 'infinite',\n        },\n      ];\n    }\n\n    return [\n      splitPath,\n      {\n        ...(typeof input !== 'undefined' &&\n          input !== skipToken && { input: input }),\n        ...(type && type !== 'any' && { type: type }),\n      },\n    ];\n  });\n\n  if (opts.prefix) {\n    key.unshift([opts.prefix]);\n  }\n  return key;\n}\n\n/**\n * @internal\n */\nexport function getMutationKeyInternal(opts: {\n  prefix: string | undefined;\n  path: string[];\n}): AnyTRPCMutationKey {\n  // some parts of the path may be dot-separated, split them up\n  const key: TRPCMutationKeyWithoutPrefix = [\n    opts.path.flatMap((part) => part.split('.')),\n  ];\n\n  if (opts.prefix) {\n    key.unshift([opts.prefix]);\n  }\n  return key;\n}\n\n/**\n * @internal\n */\nexport function unwrapLazyArg<T>(valueOrLazy: T | (() => T)): T {\n  return (isFunction(valueOrLazy) ? valueOrLazy() : valueOrLazy) as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsJQ,MAAA,YAAA,CAAA,UACA,WAAA;;;;AAlIR,SAAgB,wBAAwB,OAEL;CAGjC,OAAO,EACL,MAHW,MAAM,KAAK,KAAK,GAGxB,EACL;AACF;AAEA,SAAgB,mBACd,UACoC;CACpC,OAAO,SAAS,UAAU;AAC5B;AAEA,SAAgB,aAAa,UAA2B;CACtD,IAAI,mBAAmB,QAAQ,GAC7B,OAAO;EACL,MAAM;EACN,QAAQ,SAAS;EACjB,MAAM,SAAS;EACf,MAAM,SAAS;CACjB;MAEA,OAAO;EACL,MAAM;EACN,QAAQ,KAAA;EACR,MAAM,SAAS;EACf,MAAM,SAAS;CACjB;AAEJ;;;;AAKA,SAAgB,cACd,UACA,MACA,gBAIA;;CACA,MAAM,eAAe,aAAa,QAAQ;CAE1C,IAAI,SAAA,qBAAQ,aAAa,UAAA,QAAA,uBAAA,KAAA,IAAA,KAAA,IAAA,mBAAM;CAC/B,IAAI,gBAAgB;;EAClB,QAAA,eAAA,eAAA,eAAA,CAAA,IAAA,yBAAA,sBACM,aAAa,UAAA,QAAA,wBAAA,KAAA,IAAA,KAAA,IAAA,oBA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