@orpc/swr 2.0.0-beta.49 → 2.0.0-beta.50
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.d.ts +7 -9
- package/dist/index.js +4 -6
- package/dist/key.d.ts +8 -14
- package/dist/key.js +12 -10
- package/dist/procedure-utils.d.ts +44 -45
- package/dist/procedure-utils.js +165 -174
- package/dist/router-utils.d.ts +13 -15
- package/dist/router-utils.js +37 -53
- package/dist/shared-utils.d.ts +13 -13
- package/dist/shared-utils.js +30 -32
- package/dist/types.d.ts +36 -36
- package/dist/types.js +8 -6
- package/dist/utils.js +5 -13
- package/package.json +3 -3
- package/dist/utils.d.ts +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export
|
|
8
|
-
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, type OperationContext as SWROperationContext, } from './types.ts';
|
|
9
|
-
export type { Public } from '@orpc/shared';
|
|
1
|
+
import { OperationKey, OperationKeyOptions, OperationKeyPrefixOptions } from "./key.js";
|
|
2
|
+
import { ProcedureUtils, ProcedureUtilsOptions } from "./procedure-utils.js";
|
|
3
|
+
import { RouterUtils, RouterUtilsOptions, createRouterUtils } from "./router-utils.js";
|
|
4
|
+
import { SharedUtils } from "./shared-utils.js";
|
|
5
|
+
import { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Matcher, MatcherOptions, MatcherStrategy, Mutator, MutatorOptions, OPERATION_CONTEXT_SYMBOL, OperationContext, OperationType, Subscriber, SubscriberOptions } from "./types.js";
|
|
6
|
+
import { Public } from "@orpc/shared";
|
|
7
|
+
export { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Matcher, MatcherOptions, MatcherStrategy, Mutator, MutatorOptions, OPERATION_CONTEXT_SYMBOL, OperationContext, type OperationKey, type OperationKeyOptions, type OperationKeyPrefixOptions, OperationType, ProcedureUtils, type ProcedureUtilsOptions, type Public, RouterUtils, RouterUtilsOptions, type OperationContext as SWROperationContext, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, SharedUtils, Subscriber, SubscriberOptions, createRouterUtils, createRouterUtils as createSWRUtils };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export { ProcedureUtils } from
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export * from './types.js';
|
|
6
|
-
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, } from './types.js';
|
|
1
|
+
export { ProcedureUtils } from "./procedure-utils.js";
|
|
2
|
+
export { createRouterUtils, createRouterUtils as createSWRUtils } from "./router-utils.js";
|
|
3
|
+
export { SharedUtils } from "./shared-utils.js";
|
|
4
|
+
export { OPERATION_CONTEXT_SYMBOL, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL } from "./types.js";
|
package/dist/key.d.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PartialDeep } from "@orpc/shared";
|
|
2
2
|
export interface OperationKeyPrefixOptions {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Prepended as the first element of the key when present.
|
|
5
|
+
* Use this to avoid key conflicts when multiple router utils share the same client.
|
|
6
|
+
*/
|
|
7
|
+
prefix?: string | undefined;
|
|
8
8
|
}
|
|
9
9
|
export type OperationKeyOptions<TInput> = OperationKeyPrefixOptions & {
|
|
10
|
-
|
|
10
|
+
input?: PartialDeep<TInput> | undefined;
|
|
11
11
|
};
|
|
12
|
-
export type OperationKey<TInput> = [path: string[], options: Omit<OperationKeyOptions<TInput>,
|
|
13
|
-
/**
|
|
14
|
-
* Generate a **full matching** key for SWR operations.
|
|
15
|
-
*
|
|
16
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
17
|
-
*/
|
|
18
|
-
export declare function generateOperationKey<TInput>(path: string[], options?: OperationKeyOptions<TInput>): OperationKey<TInput>;
|
|
12
|
+
export type OperationKey<TInput> = [path: string[], options: Omit<OperationKeyOptions<TInput>, "prefix">] | [prefix: string, path: string[], options: Omit<OperationKeyOptions<TInput>, "prefix">];
|
package/dist/key.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
// src/key.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
* Generate a **full matching** key for SWR operations.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
6
|
+
*/
|
|
7
|
+
function generateOperationKey(path, options = {}) {
|
|
8
|
+
return [
|
|
9
|
+
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
10
|
+
path,
|
|
11
|
+
options.input !== void 0 ? { input: options.input } : {}
|
|
12
|
+
];
|
|
12
13
|
}
|
|
14
|
+
export { generateOperationKey };
|
|
@@ -1,46 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
export interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {
|
|
7
|
-
}
|
|
1
|
+
import { OperationKey, OperationKeyPrefixOptions } from "./key.js";
|
|
2
|
+
import { SharedUtils } from "./shared-utils.js";
|
|
3
|
+
import { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Mutator, MutatorOptions, Subscriber, SubscriberOptions } from "./types.js";
|
|
4
|
+
import { MaybeOptionalOptions } from "@orpc/shared";
|
|
5
|
+
import { Client, ClientContext } from "@orpc/client";
|
|
6
|
+
export interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {}
|
|
8
7
|
export declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
8
|
+
/**
|
|
9
|
+
* Calling corresponding procedure client
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
12
|
+
*/
|
|
13
|
+
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
14
|
+
constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions);
|
|
15
|
+
/**
|
|
16
|
+
* Generate a **full matching** key for SWR operations.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
19
|
+
*/
|
|
20
|
+
key(...rest: MaybeOptionalOptions<KeyOptions<TInput>>): OperationKey<TInput>;
|
|
21
|
+
/**
|
|
22
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
25
|
+
*/
|
|
26
|
+
fetcher(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
27
|
+
/**
|
|
28
|
+
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
31
|
+
*/
|
|
32
|
+
subscriber(...rest: MaybeOptionalOptions<SubscriberOptions<TClientContext>>): Subscriber<TInput, InferSubscriberOutput<TOutput>, TError>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate a live subscriber that subscribes to the latest events from an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
35
|
+
*
|
|
36
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
37
|
+
*/
|
|
38
|
+
liveSubscriber(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Subscriber<TInput, InferLiveSubscriberOutput<TOutput>, TError>;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
43
|
+
*/
|
|
44
|
+
mutator(...rest: MaybeOptionalOptions<MutatorOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
45
|
+
}
|
package/dist/procedure-utils.js
CHANGED
|
@@ -1,174 +1,165 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
167
|
-
return (key, { arg }) => this.call(arg, {
|
|
168
|
-
context: {
|
|
169
|
-
[OPERATION_CONTEXT_SYMBOL]: { key, type: 'mutator' },
|
|
170
|
-
...options.context,
|
|
171
|
-
},
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
}
|
|
1
|
+
import { generateOperationKey } from "./key.js";
|
|
2
|
+
import { SharedUtils } from "./shared-utils.js";
|
|
3
|
+
import { OPERATION_CONTEXT_SYMBOL } from "./types.js";
|
|
4
|
+
import { isAsyncIteratorObject, resolveMaybeOptionalOptions, takeLast } from "@orpc/shared";
|
|
5
|
+
// src/procedure-utils.ts
|
|
6
|
+
var ProcedureUtils = class extends SharedUtils {
|
|
7
|
+
/**
|
|
8
|
+
* Calling corresponding procedure client
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
11
|
+
*/
|
|
12
|
+
call;
|
|
13
|
+
constructor(path, client, options = {}) {
|
|
14
|
+
super(path, options);
|
|
15
|
+
this.call = client;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Generate a **full matching** key for SWR operations.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
21
|
+
*/
|
|
22
|
+
key(...rest) {
|
|
23
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
24
|
+
return generateOperationKey(this.path, {
|
|
25
|
+
...options,
|
|
26
|
+
prefix: this.options.prefix
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
31
|
+
*
|
|
32
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
33
|
+
*/
|
|
34
|
+
fetcher(...rest) {
|
|
35
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
36
|
+
return async (key) => {
|
|
37
|
+
/**
|
|
38
|
+
* Input can be omitted from the key when it is undefined,
|
|
39
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
40
|
+
*/
|
|
41
|
+
const { input } = key[key.length - 1];
|
|
42
|
+
return this.call(input, { context: {
|
|
43
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
44
|
+
key,
|
|
45
|
+
type: "fetcher"
|
|
46
|
+
},
|
|
47
|
+
...options.context
|
|
48
|
+
} });
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
55
|
+
*/
|
|
56
|
+
subscriber(...rest) {
|
|
57
|
+
const { maxChunks, refetchMode = "reset", ...options } = resolveMaybeOptionalOptions(rest);
|
|
58
|
+
return (key, { next }) => {
|
|
59
|
+
const controller = new AbortController();
|
|
60
|
+
(async () => {
|
|
61
|
+
try {
|
|
62
|
+
/**
|
|
63
|
+
* Input can be omitted from the key when it is undefined,
|
|
64
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
65
|
+
*/
|
|
66
|
+
const { input } = key[key.length - 1];
|
|
67
|
+
const iterator = await this.call(input, {
|
|
68
|
+
context: {
|
|
69
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
70
|
+
key,
|
|
71
|
+
type: "subscriber"
|
|
72
|
+
},
|
|
73
|
+
...options.context
|
|
74
|
+
},
|
|
75
|
+
signal: controller.signal
|
|
76
|
+
});
|
|
77
|
+
if (!isAsyncIteratorObject(iterator)) throw new TypeError(".subscriber requires an AsyncIteratorObject output");
|
|
78
|
+
if (controller.signal.aborted) {
|
|
79
|
+
await iterator.return?.();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (refetchMode === "reset") next(void 0, []);
|
|
83
|
+
for await (const event of iterator) {
|
|
84
|
+
if (controller.signal.aborted) break;
|
|
85
|
+
next(void 0, (old) => {
|
|
86
|
+
const newData = Array.isArray(old) ? [...old, event] : [event];
|
|
87
|
+
return typeof maxChunks === "number" ? takeLast(newData, maxChunks) : newData;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
/**
|
|
92
|
+
* Only report the error if the controller is not aborted.
|
|
93
|
+
*/
|
|
94
|
+
if (!controller.signal.aborted) next(error);
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return () => {
|
|
98
|
+
controller.abort();
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generate a live subscriber that subscribes to the latest events from an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
104
|
+
*
|
|
105
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
106
|
+
*/
|
|
107
|
+
liveSubscriber(...rest) {
|
|
108
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
109
|
+
return (key, { next }) => {
|
|
110
|
+
const controller = new AbortController();
|
|
111
|
+
(async () => {
|
|
112
|
+
try {
|
|
113
|
+
/**
|
|
114
|
+
* Input can be omitted from the key when it is undefined,
|
|
115
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
116
|
+
*/
|
|
117
|
+
const { input } = key[key.length - 1];
|
|
118
|
+
const iterator = await this.call(input, {
|
|
119
|
+
context: {
|
|
120
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
121
|
+
key,
|
|
122
|
+
type: "liveSubscriber"
|
|
123
|
+
},
|
|
124
|
+
...options.context
|
|
125
|
+
},
|
|
126
|
+
signal: controller.signal
|
|
127
|
+
});
|
|
128
|
+
if (!isAsyncIteratorObject(iterator)) throw new TypeError(".liveSubscriber requires an AsyncIteratorObject output");
|
|
129
|
+
if (controller.signal.aborted) {
|
|
130
|
+
await iterator.return?.();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
for await (const event of iterator) {
|
|
134
|
+
if (controller.signal.aborted) break;
|
|
135
|
+
next(void 0, event);
|
|
136
|
+
}
|
|
137
|
+
} catch (error) {
|
|
138
|
+
/**
|
|
139
|
+
* Only report the error if the controller is not aborted.
|
|
140
|
+
*/
|
|
141
|
+
if (!controller.signal.aborted) next(error);
|
|
142
|
+
}
|
|
143
|
+
})();
|
|
144
|
+
return () => {
|
|
145
|
+
controller.abort();
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
151
|
+
*
|
|
152
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
153
|
+
*/
|
|
154
|
+
mutator(...rest) {
|
|
155
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
156
|
+
return (key, { arg }) => this.call(arg, { context: {
|
|
157
|
+
[OPERATION_CONTEXT_SYMBOL]: {
|
|
158
|
+
key,
|
|
159
|
+
type: "mutator"
|
|
160
|
+
},
|
|
161
|
+
...options.context
|
|
162
|
+
} });
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
export { ProcedureUtils };
|
package/dist/router-utils.d.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : {
|
|
7
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never;
|
|
8
|
-
} & Public<SharedUtils<unknown>>;
|
|
1
|
+
import { OperationKeyPrefixOptions } from "./key.js";
|
|
2
|
+
import { ProcedureUtils } from "./procedure-utils.js";
|
|
3
|
+
import { SharedUtils } from "./shared-utils.js";
|
|
4
|
+
import { Public } from "@orpc/shared";
|
|
5
|
+
import { AnyNestedClient, Client } from "@orpc/client";
|
|
6
|
+
export type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : { [K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never; } & Public<SharedUtils<unknown>>;
|
|
9
7
|
export interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Base path for all keys.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
path?: string[] | undefined;
|
|
16
14
|
}
|
|
17
15
|
/**
|
|
18
16
|
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
@@ -23,4 +21,4 @@ export interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
|
23
21
|
*
|
|
24
22
|
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
25
23
|
*/
|
|
26
|
-
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
|
24
|
+
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
package/dist/router-utils.js
CHANGED
|
@@ -1,55 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { ProcedureUtils } from "./procedure-utils.js";
|
|
2
|
+
import { SharedUtils } from "./shared-utils.js";
|
|
3
|
+
import { bindMethods, isNonPrimitive, toArray } from "@orpc/shared";
|
|
4
|
+
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from "@orpc/client";
|
|
5
|
+
// src/router-utils.ts
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
});
|
|
38
|
-
result =
|
|
39
|
-
typeof value !== 'function'
|
|
40
|
-
? nextUtils
|
|
41
|
-
: new Proxy(value, {
|
|
42
|
-
get(target, prop) {
|
|
43
|
-
if (typeof prop !== 'string' || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
44
|
-
return Reflect.get(target, prop);
|
|
45
|
-
}
|
|
46
|
-
return Reflect.get(nextUtils, prop);
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
cache.set(prop, result);
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
return recursive;
|
|
7
|
+
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
8
|
+
* helpers for every procedure in the router.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
14
|
+
*/
|
|
15
|
+
function createRouterUtils(client, options = {}) {
|
|
16
|
+
const path = toArray(options.path);
|
|
17
|
+
const utils = typeof client === "function" ? bindMethods(new ProcedureUtils(path, client, { prefix: options.prefix }), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, { prefix: options.prefix }));
|
|
18
|
+
const cache = /* @__PURE__ */ new Map();
|
|
19
|
+
return new Proxy(utils, { get(target, prop) {
|
|
20
|
+
const value = Reflect.get(target, prop);
|
|
21
|
+
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) return value ?? Reflect.get(Object.prototype, prop);
|
|
22
|
+
const nextClient = Reflect.get(client, prop);
|
|
23
|
+
if (typeof prop !== "string" || !isNonPrimitive(nextClient)) return value;
|
|
24
|
+
let result = cache.get(prop);
|
|
25
|
+
if (result === void 0) {
|
|
26
|
+
const nextUtils = createRouterUtils(nextClient, {
|
|
27
|
+
...options,
|
|
28
|
+
path: [...path, prop]
|
|
29
|
+
});
|
|
30
|
+
result = typeof value !== "function" ? nextUtils : new Proxy(value, { get(target, prop) {
|
|
31
|
+
if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) return Reflect.get(target, prop);
|
|
32
|
+
return Reflect.get(nextUtils, prop);
|
|
33
|
+
} });
|
|
34
|
+
cache.set(prop, result);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
} });
|
|
55
38
|
}
|
|
39
|
+
export { createRouterUtils };
|
package/dist/shared-utils.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { OperationKeyPrefixOptions } from "./key.js";
|
|
2
|
+
import { Matcher, MatcherOptions, MatcherStrategy } from "./types.js";
|
|
3
|
+
import { MaybeOptionalOptions } from "@orpc/shared";
|
|
4
4
|
export declare class SharedUtils<TInput> {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
5
|
+
protected readonly path: string[];
|
|
6
|
+
protected readonly options: OperationKeyPrefixOptions;
|
|
7
|
+
constructor(path: string[], options: OperationKeyPrefixOptions);
|
|
8
|
+
/**
|
|
9
|
+
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
12
|
+
*/
|
|
13
|
+
matcher<TStrategy extends MatcherStrategy>(...rest: MaybeOptionalOptions<MatcherOptions<TStrategy, TInput>>): Matcher;
|
|
14
|
+
}
|
package/dist/shared-utils.js
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
}
|
|
1
|
+
import { generateOperationKey } from "./key.js";
|
|
2
|
+
import { isSubsetOf } from "./utils.js";
|
|
3
|
+
import { resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
4
|
+
// src/shared-utils.ts
|
|
5
|
+
var SharedUtils = class {
|
|
6
|
+
path;
|
|
7
|
+
options;
|
|
8
|
+
constructor(path, options) {
|
|
9
|
+
this.path = path;
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
16
|
+
*/
|
|
17
|
+
matcher(...rest) {
|
|
18
|
+
const { input, strategy = "partial" } = resolveMaybeOptionalOptions(rest);
|
|
19
|
+
const expectedKey = generateOperationKey(this.path, {
|
|
20
|
+
prefix: this.options.prefix,
|
|
21
|
+
input
|
|
22
|
+
});
|
|
23
|
+
return (key) => {
|
|
24
|
+
if (!isSubsetOf(expectedKey, key)) return false;
|
|
25
|
+
if (strategy === "exact" && !isSubsetOf(key, expectedKey)) return false;
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export { SharedUtils };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { OperationKey } from "./key.js";
|
|
2
|
+
import { PartialDeep } from "@orpc/shared";
|
|
3
|
+
import { ClientContext } from "@orpc/client";
|
|
4
|
+
import { SWRSubscriptionOptions } from "swr/subscription";
|
|
5
5
|
export type InferSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
6
6
|
export type InferLiveSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
7
|
-
export type OperationType =
|
|
7
|
+
export type OperationType = "fetcher" | "mutator" | "subscriber" | "liveSubscriber";
|
|
8
8
|
/**
|
|
9
9
|
* The symbol under which SWR utils attach operation details
|
|
10
10
|
* (key and operation type) to the client context.
|
|
@@ -19,52 +19,52 @@ export declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
|
19
19
|
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
20
20
|
*/
|
|
21
21
|
export interface OperationContext {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
23
|
+
key: unknown;
|
|
24
|
+
type: OperationType;
|
|
25
|
+
};
|
|
26
26
|
}
|
|
27
27
|
export type KeyOptions<TInput> = undefined extends TInput ? {
|
|
28
|
-
|
|
28
|
+
input?: TInput;
|
|
29
29
|
} : {
|
|
30
|
-
|
|
30
|
+
input: TInput;
|
|
31
31
|
};
|
|
32
|
-
export type MatcherStrategy =
|
|
33
|
-
export type MatcherOptions<TStrategy extends MatcherStrategy, TInput> = (
|
|
34
|
-
|
|
32
|
+
export type MatcherStrategy = "exact" | "partial";
|
|
33
|
+
export type MatcherOptions<TStrategy extends MatcherStrategy, TInput> = ("partial" extends TStrategy ? {
|
|
34
|
+
input?: PartialDeep<TInput> | undefined;
|
|
35
35
|
} : undefined extends TInput ? {
|
|
36
|
-
|
|
36
|
+
input?: TInput;
|
|
37
37
|
} : {
|
|
38
|
-
|
|
38
|
+
input: TInput;
|
|
39
39
|
}) & {
|
|
40
|
-
|
|
40
|
+
strategy?: TStrategy | undefined;
|
|
41
41
|
};
|
|
42
42
|
export type Matcher = (key?: unknown) => boolean;
|
|
43
43
|
export type FetcherOptions<TClientContext extends ClientContext> = object extends TClientContext ? {
|
|
44
|
-
|
|
44
|
+
context?: TClientContext | undefined;
|
|
45
45
|
} : {
|
|
46
|
-
|
|
46
|
+
context: TClientContext;
|
|
47
47
|
};
|
|
48
48
|
export type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Determines how data is handled when the subscription is subscribed again.
|
|
51
|
+
* Existing data stays until the new stream connects.
|
|
52
|
+
*
|
|
53
|
+
* - `'reset'`: The data becomes `[]` when the stream connects.
|
|
54
|
+
* - `'append'`: New chunks are added to the existing data.
|
|
55
|
+
*
|
|
56
|
+
* @default 'reset'
|
|
57
|
+
*/
|
|
58
|
+
refetchMode?: "append" | "reset" | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Maximum number of chunks to store.
|
|
61
|
+
* When exceeded, the oldest chunks will be removed.
|
|
62
|
+
*/
|
|
63
|
+
maxChunks?: number | undefined;
|
|
64
64
|
};
|
|
65
65
|
export type MutatorOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext>;
|
|
66
66
|
export type Fetcher<TInput, TOutput> = (key: OperationKey<TInput>) => Promise<TOutput>;
|
|
67
67
|
export type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
68
|
-
|
|
68
|
+
arg: TInput;
|
|
69
69
|
}>) => Promise<TOutput>;
|
|
70
|
-
export type Subscriber<TInput, TData, TError> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => () => void;
|
|
70
|
+
export type Subscriber<TInput, TData, TError> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => () => void;
|
package/dist/types.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
// src/types.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
* The symbol under which SWR utils attach operation details
|
|
4
|
+
* (key and operation type) to the client context.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
7
|
+
*/
|
|
8
|
+
const OPERATION_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_SWR_OPERATION_CONTEXT");
|
|
9
|
+
export { OPERATION_CONTEXT_SYMBOL };
|
package/dist/utils.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { isPlainObject } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
: typeof subsetKey !== typeof fullKey
|
|
6
|
-
? false
|
|
7
|
-
: isPlainObject(subsetKey) && isPlainObject(fullKey)
|
|
8
|
-
? Object.keys(subsetKey).every((key) => subsetKey[key] === undefined || isSubsetOf(subsetKey[key], fullKey[key]))
|
|
9
|
-
: Array.isArray(subsetKey) && Array.isArray(fullKey)
|
|
10
|
-
? subsetKey.every((value, index) => isSubsetOf(value, fullKey[index]))
|
|
11
|
-
: subsetKey instanceof Date && fullKey instanceof Date
|
|
12
|
-
? Object.is(subsetKey.getTime(), fullKey.getTime())
|
|
13
|
-
: false;
|
|
1
|
+
import { isPlainObject } from "@orpc/shared";
|
|
2
|
+
// src/utils.ts
|
|
3
|
+
function isSubsetOf(subsetKey, fullKey) {
|
|
4
|
+
return subsetKey === fullKey ? true : typeof subsetKey !== typeof fullKey ? false : isPlainObject(subsetKey) && isPlainObject(fullKey) ? Object.keys(subsetKey).every((key) => subsetKey[key] === void 0 || isSubsetOf(subsetKey[key], fullKey[key])) : Array.isArray(subsetKey) && Array.isArray(fullKey) ? subsetKey.every((value, index) => isSubsetOf(value, fullKey[index])) : subsetKey instanceof Date && fullKey instanceof Date ? Object.is(subsetKey.getTime(), fullKey.getTime()) : false;
|
|
14
5
|
}
|
|
6
|
+
export { isSubsetOf };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/swr",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
4
4
|
"description": "SWR integration for oRPC: typesafe React data fetching hooks",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-fetching",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@orpc/client": "2.0.0-beta.
|
|
41
|
-
"@orpc/shared": "2.0.0-beta.
|
|
40
|
+
"@orpc/client": "2.0.0-beta.50",
|
|
41
|
+
"@orpc/shared": "2.0.0-beta.50"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"swr": "^2.5.0",
|
package/dist/utils.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean;
|