@orpc/vue-query 0.36.1 → 0.38.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,84 @@
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
5
+ return [path, {
6
+ ...withInput,
7
+ ...withType
8
+ }];
9
+ }
10
+
11
+ // src/general-utils.ts
12
+ function createGeneralUtils(path) {
13
+ return {
14
+ key(options) {
15
+ return buildKey(path, options);
11
16
  }
12
- ];
17
+ };
18
+ }
19
+
20
+ // src/procedure-utils.ts
21
+ import { computed } from "vue";
22
+
23
+ // ../shared/src/object.ts
24
+ function isObject(value) {
25
+ if (!value || typeof value !== "object") {
26
+ return false;
27
+ }
28
+ const proto = Object.getPrototypeOf(value);
29
+ return proto === Object.prototype || !proto || !proto.constructor;
13
30
  }
14
31
 
15
32
  // src/utils.ts
16
33
  import { isRef } from "vue";
17
- function deepUnref(value) {
34
+ function unrefDeep(value) {
18
35
  if (isRef(value)) {
19
- return deepUnref(value.value);
36
+ return unrefDeep(value.value);
20
37
  }
21
38
  if (Array.isArray(value)) {
22
- return value.map(deepUnref);
39
+ return value.map(unrefDeep);
23
40
  }
24
- if (value && typeof value === "object") {
41
+ if (isObject(value)) {
25
42
  return Object.keys(value).reduce((acc, key) => {
26
- acc[key] = deepUnref(value[key]);
43
+ acc[key] = unrefDeep(value[key]);
27
44
  return acc;
28
45
  }, {});
29
46
  }
30
47
  return value;
31
48
  }
32
49
 
33
- // src/utils-general.ts
34
- function createGeneralUtils(path) {
35
- return {
36
- key(options) {
37
- return buildKey(path, deepUnref(options));
38
- }
39
- };
40
- }
41
-
42
- // src/utils-procedure.ts
43
- import { computed } from "vue";
50
+ // src/procedure-utils.ts
44
51
  function createProcedureUtils(client, path) {
45
52
  return {
46
- queryOptions(...[options]) {
47
- const input = options?.input;
53
+ call: client,
54
+ queryOptions(...[{ input, context, ...rest } = {}]) {
48
55
  return {
49
- queryKey: computed(() => buildKey(path, { type: "query", input: deepUnref(input) })),
50
- queryFn: ({ signal }) => client(deepUnref(input), { signal, context: deepUnref(options?.context) }),
51
- ...options
56
+ queryKey: computed(() => buildKey(path, { type: "query", input: unrefDeep(input) })),
57
+ queryFn: ({ signal }) => client(unrefDeep(input), { signal, context: unrefDeep(context) }),
58
+ ...rest
52
59
  };
53
60
  },
54
- infiniteOptions(options) {
55
- const input = options.input;
61
+ infiniteOptions({ input, context, ...rest }) {
56
62
  return {
57
- initialPageParam: void 0,
58
- queryKey: computed(() => buildKey(path, { type: "infinite", input: deepUnref(input) })),
63
+ queryKey: computed(() => {
64
+ return buildKey(path, { type: "infinite", input: unrefDeep(input(unrefDeep(rest.initialPageParam))) });
65
+ }),
59
66
  queryFn: ({ pageParam, signal }) => {
60
- return client({ ...deepUnref(input), cursor: pageParam }, { signal, context: deepUnref(options.context) });
67
+ return client(unrefDeep(input(pageParam)), { signal, context: unrefDeep(context) });
61
68
  },
62
- ...options
69
+ ...rest
63
70
  };
64
71
  },
65
- mutationOptions(...[options]) {
72
+ mutationOptions(...[{ context, ...rest } = {}]) {
66
73
  return {
67
74
  mutationKey: buildKey(path, { type: "mutation" }),
68
- mutationFn: (input) => client(input, { context: deepUnref(options?.context) }),
69
- ...options
75
+ mutationFn: (input) => client(input, { context: unrefDeep(context) }),
76
+ ...rest
70
77
  };
71
78
  }
72
79
  };
73
80
  }
74
81
 
75
- // src/utils-router.ts
82
+ // src/router-utils.ts
76
83
  function createRouterUtils(client, path = []) {
77
84
  const generalUtils = createGeneralUtils(path);
78
85
  const procedureUtils = createProcedureUtils(client, path);
@@ -0,0 +1,7 @@
1
+ import type { QueryKey } from '@tanstack/vue-query';
2
+ import type { BuildKeyOptions, KeyType } from './key';
3
+ export interface GeneralUtils<TInput> {
4
+ key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey;
5
+ }
6
+ export declare function createGeneralUtils<TInput>(path: string[]): GeneralUtils<TInput>;
7
+ //# 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,12 @@
1
+ import type { Client, ClientContext } 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 extends ClientContext, TInput, TOutput, TError extends Error> {
6
+ call: Client<TClientContext, TInput, TOutput, TError>;
7
+ queryOptions<U, USelectData = TOutput>(...rest: MaybeOptionalOptions<U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>>): NoInfer<U & QueryOptionsBase<TOutput, TError>>;
8
+ infiniteOptions<U, UPageParam, USelectData = InfiniteData<TOutput>>(options: U & InfiniteOptionsIn<TClientContext, TInput, TOutput, TError, USelectData, UPageParam>): NoInfer<U & InfiniteOptionsBase<TOutput, TError, UPageParam>>;
9
+ mutationOptions<U>(...rest: MaybeOptionalOptions<U & MutationOptionsIn<TClientContext, TInput, TOutput, TError>>): NoInfer<U & MutationOptionsBase<TInput, TOutput, TError>>;
10
+ }
11
+ export declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError extends Error>(client: Client<TClientContext, TInput, TOutput, TError>, path: string[]): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
12
+ //# 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,52 @@
1
+ import type { ClientContext } from '@orpc/contract';
1
2
  import type { AnyFunction, SetOptional } from '@orpc/shared';
2
- import type { MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
3
+ import type { Enabled, MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions, UseInfiniteQueryOptions } from '@tanstack/vue-query';
3
4
  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]>;
5
+ /**
6
+ * Since `@tanstack/vue-query` is not exporting the type `MaybeRefDeep` we need to copy it from the source code.
7
+ * https://github.com/TanStack/query/blob/7ff544e12e79388e513b1cd886aeb946f80f0153/packages/vue-query/src/types.ts#L19C1-L27C2
8
+ */
9
+ export type MaybeRefDeep<T> = MaybeRef<T extends AnyFunction ? T : T extends object ? {
10
+ [Property in keyof T]: MaybeRefDeep<T[Property]>;
6
11
  } : 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>;
12
+ export type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error, TSelectData> = (undefined extends TInput ? {
13
+ input?: MaybeRefDeep<TInput>;
13
14
  } : {
14
- input: MaybeDeepRef<TInput>;
15
- }) & (undefined extends TClientContext ? {
16
- context?: MaybeDeepRef<TClientContext>;
15
+ input: MaybeRefDeep<TInput>;
16
+ }) & (Record<never, never> extends TClientContext ? {
17
+ context?: MaybeRefDeep<TClientContext>;
17
18
  } : {
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;
19
+ context: MaybeRefDeep<TClientContext>;
20
+ }) & {
21
+ [P in keyof Omit<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>, 'queryKey' | 'enabled'>]: MaybeRefDeep<QueryObserverOptions<TOutput, TError, TSelectData, TOutput>[P]>;
22
+ } & {
23
+ enabled?: MaybeRefOrGetter<Enabled<TOutput, TError, TSelectData>>;
24
+ queryKey?: MaybeRefDeep<QueryKey>;
25
+ shallow?: boolean;
24
26
  };
25
27
  export interface QueryOptionsBase<TOutput, TError extends Error> {
26
28
  queryKey: ComputedRef<QueryKey>;
27
29
  queryFn(ctx: QueryFunctionContext): Promise<TOutput>;
28
30
  retry?(failureCount: number, error: TError): boolean;
29
31
  }
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>;
32
+ export type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error, TSelectData, TPageParam> = {
33
+ input: (pageParam: TPageParam) => MaybeRefDeep<TInput>;
34
+ } & (Record<never, never> extends TClientContext ? {
35
+ context?: MaybeRefDeep<TClientContext>;
36
36
  } : {
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> {
37
+ context: MaybeRefDeep<TClientContext>;
38
+ }) & SetOptional<UseInfiniteQueryOptions<TOutput, TError, TSelectData, TOutput, QueryKey, TPageParam>, 'queryKey'>;
39
+ export interface InfiniteOptionsBase<TOutput, TError extends Error, TPageParam> {
40
40
  queryKey: ComputedRef<QueryKey>;
41
- queryFn(ctx: QueryFunctionContext<QueryKey, InferCursor<TInput>>): Promise<TOutput>;
42
- initialPageParam: undefined;
41
+ queryFn(ctx: QueryFunctionContext<QueryKey, TPageParam>): Promise<TOutput>;
43
42
  retry?(failureCount: number, error: TError): boolean;
44
43
  }
45
- export type MutationOptionsExtra<TClientContext, TInput, TOutput, TError extends Error> = (undefined extends TClientContext ? {
46
- context?: MaybeDeepRef<TClientContext>;
44
+ export type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> = (Record<never, never> extends TClientContext ? {
45
+ context?: TClientContext;
47
46
  } : {
48
- context: MaybeDeepRef<TClientContext>;
47
+ context: TClientContext;
49
48
  }) & {
50
- [P in keyof MutationObserverOptions<TOutput, TError, TInput, unknown>]: MaybeDeepRef<MutationObserverOptions<TOutput, TError, TInput, unknown>[P]>;
49
+ [P in keyof MutationObserverOptions<TOutput, TError, TInput>]: MaybeRefDeep<MutationObserverOptions<TOutput, TError, TInput>[P]>;
51
50
  } & {
52
51
  shallow?: MaybeRef<boolean>;
53
52
  };
@@ -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.38.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.38.0",
38
+ "@orpc/client": "0.38.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