@orpc/vue-query 0.36.1 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,77 +2,81 @@
2
2
  function buildKey(path, options) {
3
3
  const withInput = options?.input !== void 0 ? { input: options?.input } : {};
4
4
  const withType = options?.type !== void 0 ? { type: options?.type } : {};
5
- return [
6
- "__ORPC__",
7
- path,
8
- {
9
- ...withInput,
10
- ...withType
11
- }
12
- ];
5
+ return [path, {
6
+ ...withInput,
7
+ ...withType
8
+ }];
9
+ }
10
+
11
+ // ../shared/src/object.ts
12
+ function isObject(value) {
13
+ if (!value || typeof value !== "object") {
14
+ return false;
15
+ }
16
+ const proto = Object.getPrototypeOf(value);
17
+ return proto === Object.prototype || proto === null;
13
18
  }
14
19
 
15
20
  // src/utils.ts
16
21
  import { isRef } from "vue";
17
- function deepUnref(value) {
22
+ function unrefDeep(value) {
18
23
  if (isRef(value)) {
19
- return deepUnref(value.value);
24
+ return unrefDeep(value.value);
20
25
  }
21
26
  if (Array.isArray(value)) {
22
- return value.map(deepUnref);
27
+ return value.map(unrefDeep);
23
28
  }
24
- if (value && typeof value === "object") {
29
+ if (isObject(value)) {
25
30
  return Object.keys(value).reduce((acc, key) => {
26
- acc[key] = deepUnref(value[key]);
31
+ acc[key] = unrefDeep(value[key]);
27
32
  return acc;
28
33
  }, {});
29
34
  }
30
35
  return value;
31
36
  }
32
37
 
33
- // src/utils-general.ts
38
+ // src/general-utils.ts
34
39
  function createGeneralUtils(path) {
35
40
  return {
36
41
  key(options) {
37
- return buildKey(path, deepUnref(options));
42
+ return buildKey(path, unrefDeep(options));
38
43
  }
39
44
  };
40
45
  }
41
46
 
42
- // src/utils-procedure.ts
47
+ // src/procedure-utils.ts
43
48
  import { computed } from "vue";
44
49
  function createProcedureUtils(client, path) {
45
50
  return {
46
- queryOptions(...[options]) {
47
- const input = options?.input;
51
+ queryOptions(...[{ input, context, ...rest } = {}]) {
48
52
  return {
49
- queryKey: computed(() => buildKey(path, { type: "query", input: deepUnref(input) })),
50
- queryFn: ({ signal }) => client(deepUnref(input), { signal, context: deepUnref(options?.context) }),
51
- ...options
53
+ queryKey: computed(() => buildKey(path, { type: "query", input: unrefDeep(input) })),
54
+ queryFn: ({ signal }) => client(unrefDeep(input), { signal, context: unrefDeep(context) }),
55
+ ...rest
52
56
  };
53
57
  },
54
- infiniteOptions(options) {
55
- const input = options.input;
58
+ infiniteOptions({ input, context, ...rest }) {
56
59
  return {
57
- initialPageParam: void 0,
58
- queryKey: computed(() => buildKey(path, { type: "infinite", input: deepUnref(input) })),
60
+ queryKey: computed(() => {
61
+ return buildKey(path, { type: "infinite", input: unrefDeep(input(unrefDeep(rest.initialPageParam))) });
62
+ }),
59
63
  queryFn: ({ pageParam, signal }) => {
60
- return client({ ...deepUnref(input), cursor: pageParam }, { signal, context: deepUnref(options.context) });
64
+ return client(unrefDeep(input(pageParam)), { signal, context: unrefDeep(context) });
61
65
  },
62
- ...options
66
+ ...rest
63
67
  };
64
68
  },
65
- mutationOptions(...[options]) {
69
+ mutationOptions(...[{ context, ...rest } = {}]) {
66
70
  return {
67
71
  mutationKey: buildKey(path, { type: "mutation" }),
68
- mutationFn: (input) => client(input, { context: deepUnref(options?.context) }),
69
- ...options
72
+ mutationFn: (input) => client(input, { context: unrefDeep(context) }),
73
+ ...rest
70
74
  };
71
75
  }
72
76
  };
73
77
  }
74
78
 
75
- // src/utils-router.ts
79
+ // src/router-utils.ts
76
80
  function createRouterUtils(client, path = []) {
77
81
  const generalUtils = createGeneralUtils(path);
78
82
  const procedureUtils = createProcedureUtils(client, path);
@@ -0,0 +1,8 @@
1
+ import type { QueryKey } from '@tanstack/vue-query';
2
+ import type { BuildKeyOptions, KeyType } from './key';
3
+ import type { MaybeRefDeep } from './types';
4
+ export interface GeneralUtils<TInput> {
5
+ key<UType extends KeyType = undefined>(options?: MaybeRefDeep<BuildKeyOptions<UType, TInput>>): QueryKey;
6
+ }
7
+ export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
8
+ //# sourceMappingURL=general-utils.d.ts.map
@@ -1,8 +1,8 @@
1
- import { createRouterUtils } from './utils-router';
1
+ import { createRouterUtils } from './router-utils';
2
+ export * from './general-utils';
2
3
  export * from './key';
4
+ export * from './procedure-utils';
5
+ export * from './router-utils';
3
6
  export * from './types';
4
- export * from './utils-general';
5
- export * from './utils-procedure';
6
- export * from './utils-router';
7
7
  export declare const createORPCVueQueryUtils: typeof createRouterUtils;
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,11 @@
1
+ import type { Client } from '@orpc/contract';
2
+ import type { MaybeOptionalOptions } from '@orpc/shared';
3
+ import type { InfiniteData } from '@tanstack/vue-query';
4
+ import type { InfiniteOptionsBase, InfiniteOptionsIn, MutationOptionsBase, MutationOptionsIn, QueryOptionsBase, QueryOptionsIn } from './types';
5
+ export interface ProcedureUtils<TClientContext, TInput, TOutput, TError extends Error> {
6
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & QueryOptionsBase<TOutput, TError>>;
7
+ infiniteOptions<U, UPageParam, USelectData = InfiniteData<TOutput>>(options: U & InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam>): NoInfer<U & InfiniteOptionsBase<TOutput, TError, UPageParam>>;
8
+ mutationOptions<U>(...rest: MaybeOptionalOptions<U & MutationOptionsIn<TClientContext, TInput, TOutput, TError>>): NoInfer<U & MutationOptionsBase<TInput, TOutput, TError>>;
9
+ }
10
+ export declare function createProcedureUtils<TClientContext, TInput, TOutput, TError extends Error>(client: Client<TClientContext, TInput, TOutput, TError>, path: string[]): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
11
+ //# sourceMappingURL=procedure-utils.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { Client, NestedClient } from '@orpc/contract';
2
+ import { type GeneralUtils } from './general-utils';
3
+ import { type ProcedureUtils } from './procedure-utils';
4
+ export type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
5
+ [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
6
+ } & GeneralUtils<unknown>;
7
+ /**
8
+ * @param client - Any kind of oRPC clients: `createRouterClient`, `createORPCClient`, ...
9
+ * @param path - The base path for query key, when it it will be prefix to all keys
10
+ */
11
+ export declare function createRouterUtils<T extends NestedClient<any>>(client: T, path?: string[]): RouterUtils<T>;
12
+ //# sourceMappingURL=router-utils.d.ts.map
@@ -1,53 +1,51 @@
1
1
  import type { AnyFunction, SetOptional } from '@orpc/shared';
2
- import type { MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
2
+ import type { Enabled, MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
3
3
  import type { ComputedRef, MaybeRef, MaybeRefOrGetter } from 'vue';
4
- export type MaybeDeepRef<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
5
- [K in keyof T]: MaybeDeepRef<T[K]>;
4
+ /**
5
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
6
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
7
+ */
8
+ export type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
9
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
6
10
  } : T>;
7
- export type NonUndefinedGuard<T> = T extends undefined ? never : T;
8
- export type InferCursor<T> = T extends {
9
- cursor?: any;
10
- } ? T['cursor'] : never;
11
- export type QueryOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
12
- input?: MaybeDeepRef<TInput>;
11
+ export type QueryOptionsIn<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
12
+ input?: MaybeRefDeep<TInput>;
13
13
  } : {
14
- input: MaybeDeepRef<TInput>;
14
+ input: MaybeRefDeep<TInput>;
15
15
  }) & (undefined extends TClientContext ? {
16
- context?: MaybeDeepRef<TClientContext>;
16
+ context?: MaybeRefDeep<TClientContext>;
17
17
  } : {
18
- context: MaybeDeepRef<TClientContext>;
19
- }) & SetOptional<{
20
- [P in keyof QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>]: P extends 'enabled' ? MaybeRefOrGetter<QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>[P]> : MaybeDeepRef<QueryObserverOptions<TOutput, TError, TSelectData, TOutput, QueryKey>[P]>;
21
- }, 'queryKey'> & {
22
- shallow?: MaybeRef<boolean>;
23
- initialData?: NonUndefinedGuard<TOutput> | (() => NonUndefinedGuard<TOutput>) | undefined;
18
+ context: MaybeRefDeep<TClientContext>;
19
+ }) & {
20
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
21
+ } & {
22
+ enabled?: MaybeRefOrGetter<Enabled<TOutput, TError, TSelectData>>;
23
+ queryKey?: MaybeRefDeep<QueryKey>;
24
+ shallow?: boolean;
24
25
  };
25
26
  export interface QueryOptionsBase<TOutput, TError extends Error> {
26
27
  queryKey: ComputedRef<QueryKey>;
27
28
  queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
28
29
  retry?(failureCount: number, error: TError): boolean;
29
30
  }
30
- export type InfiniteOptionsExtra<TClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
31
- input?: MaybeDeepRef<Omit<TInput, 'cursor'>>;
32
- } : {
33
- input: MaybeDeepRef<Omit<TInput, 'cursor'>>;
34
- }) & (undefined extends TClientContext ? {
35
- context?: MaybeDeepRef<TClientContext>;
31
+ export type InfiniteOptionsIn<TClientContext, TInput, TOutput, TError extends Error, TSelectData, TPageParam> = {
32
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
33
+ } & (undefined extends TClientContext ? {
34
+ context?: MaybeRefDeep<TClientContext>;
36
35
  } : {
37
- context: MaybeDeepRef<TClientContext>;
38
- }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, InferCursor<TInput>>, 'queryKey' | (undefined extends InferCursor<TInput> ? 'initialPageParam' : never)>;
39
- export interface InfiniteOptionsBase<TInput, TOutput, TError extends Error> {
36
+ context: MaybeRefDeep<TClientContext>;
37
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
38
+ export interface InfiniteOptionsBase<TOutput, TError extends Error, TPageParam> {
40
39
  queryKey: ComputedRef<QueryKey>;
41
- queryFn(ctx: QueryFunctionContext<QueryKey, InferCursor<TInput>>): Promise<TOutput>;
42
- initialPageParam: undefined;
40
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
43
41
  retry?(failureCount: number, error: TError): boolean;
44
42
  }
45
- export type MutationOptionsExtra<TClientContext, TInput, TOutput, TError extends Error> = (undefined extends TClientContext ? {
46
- context?: MaybeDeepRef<TClientContext>;
43
+ export type MutationOptionsIn<TClientContext, TInput, TOutput, TError extends Error> = (undefined extends TClientContext ? {
44
+ context?: TClientContext;
47
45
  } : {
48
- context: MaybeDeepRef<TClientContext>;
46
+ context: TClientContext;
49
47
  }) & {
50
- [P in keyof MutationObserverOptions<TOutput, TError, TInput, unknown>]: MaybeDeepRef<MutationObserverOptions<TOutput, TError, TInput, unknown>[P]>;
48
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput>[P]>;
51
49
  } & {
52
50
  shallow?: MaybeRef<boolean>;
53
51
  };
@@ -1,7 +1,7 @@
1
- import type { AnyFunction } from '@orpc/shared';
2
1
  import type { Ref } from 'vue';
3
- export type DeepUnref<T> = T extends Ref<infer U> ? DeepUnref<U> : T extends AnyFunction ? T : T extends object ? {
4
- [K in keyof T]: DeepUnref<T[K]>;
2
+ import { type AnyFunction } from '@orpc/shared';
3
+ export type UnrefDeep<T> = T extends Ref<infer U> ? UnrefDeep<U> : T extends AnyFunction ? T : T extends object ? {
4
+ [K in keyof T]: UnrefDeep<T[K]>;
5
5
  } : T;
6
- export declare function deepUnref<T>(value: T): DeepUnref<T>;
6
+ export declare function unrefDeep<T>(value: T): UnrefDeep<T>;
7
7
  //# sourceMappingURL=utils.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/vue-query",
3
3
  "type": "module",
4
- "version": "0.36.1",
4
+ "version": "0.37.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -34,8 +34,8 @@
34
34
  "peerDependencies": {
35
35
  "@tanstack/vue-query": ">=5.50.0",
36
36
  "vue": ">=3.3.0",
37
- "@orpc/client": "0.36.1",
38
- "@orpc/contract": "0.36.1"
37
+ "@orpc/contract": "0.37.0",
38
+ "@orpc/client": "0.37.0"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
@@ -1,11 +0,0 @@
1
- import type { QueryKey } from '@tanstack/vue-query';
2
- import type { BuildKeyOptions, KeyType } from './key';
3
- import type { MaybeDeepRef } from './types';
4
- /**
5
- * Utils at any level (procedure or router)
6
- */
7
- export interface GeneralUtils<TInput> {
8
- key<UType extends KeyType = undefined>(options?: MaybeDeepRef<BuildKeyOptions<UType, TInput>>): QueryKey;
9
- }
10
- export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
11
- //# sourceMappingURL=utils-general.d.ts.map
@@ -1,13 +0,0 @@
1
- import type { Client } from '@orpc/contract';
2
- import type { IsEqual } from '@orpc/shared';
3
- import type { InfiniteOptionsBase, InfiniteOptionsExtra, MutationOptionsBase, MutationOptionsExtra, QueryOptionsBase, QueryOptionsExtra } from './types';
4
- /**
5
- * Utils at procedure level
6
- */
7
- export interface ProcedureUtils<TClientContext, TInput, TOutput, TError extends Error> {
8
- queryOptions<U extends QueryOptionsExtra<TClientContext, TInput, TOutput, TError, any>>(...opt: [options: U] | (undefined extends TInput & TClientContext ? [] : never)): IsEqual<U, QueryOptionsExtra<TClientContext, TInput, TOutput, TError, any>> extends true ? QueryOptionsBase<TOutput, TError> : Omit<QueryOptionsBase<TOutput, TError>, keyof U> & U;
9
- infiniteOptions<U extends InfiniteOptionsExtra<TClientContext, TInput, TOutput, TError, any>>(options: U): Omit<InfiniteOptionsBase<TInput, TOutput, TError>, keyof U> & U;
10
- mutationOptions<U extends MutationOptionsExtra<TClientContext, TInput, TOutput, TError>>(...opt: [options: U] | (undefined extends TClientContext ? [] : never)): IsEqual<U, MutationOptionsExtra<TClientContext, TInput, TOutput, TError>> extends true ? MutationOptionsBase<TInput, TOutput, TError> : Omit<MutationOptionsBase<TInput, TOutput, TError>, keyof U> & U;
11
- }
12
- export declare function createProcedureUtils<TClientContext, TInput, TOutput, TError extends Error>(client: Client<TClientContext, TInput, TOutput, TError>, path: string[]): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
13
- //# sourceMappingURL=utils-procedure.d.ts.map
@@ -1,12 +0,0 @@
1
- import type { Client, NestedClient } from '@orpc/contract';
2
- import { type GeneralUtils } from './utils-general';
3
- import { type ProcedureUtils } from './utils-procedure';
4
- export type RouterUtils<T extends NestedClient<any>> = T extends Client<infer TClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<TClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
5
- [K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
6
- } & GeneralUtils<unknown>;
7
- /**
8
- * @param client - The client create form `@orpc/client`
9
- * @param path - The base path for query key
10
- */
11
- export declare function createRouterUtils<T extends NestedClient<any>>(client: T, path?: string[]): RouterUtils<T>;
12
- //# sourceMappingURL=utils-router.d.ts.map