@orpc/swr 2.0.0-beta.48 → 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/README.md +4 -4
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/key.d.ts +12 -0
- package/dist/key.js +14 -0
- package/dist/procedure-utils.d.ts +45 -0
- package/dist/procedure-utils.js +165 -0
- package/dist/router-utils.d.ts +24 -0
- package/dist/router-utils.js +39 -0
- package/dist/shared-utils.d.ts +14 -0
- package/dist/shared-utils.js +30 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.js +9 -0
- package/dist/utils.js +6 -0
- package/package.json +27 -29
- package/dist/index.d.mts +0 -9
- package/dist/index.mjs +0 -6
- package/dist/key.d.mts +0 -18
- package/dist/key.mjs +0 -8
- package/dist/procedure-utils.d.mts +0 -50
- package/dist/procedure-utils.mjs +0 -158
- package/dist/router-utils.d.mts +0 -17
- package/dist/router-utils.mjs +0 -36
- package/dist/shared-utils.d.mts +0 -14
- package/dist/shared-utils.mjs +0 -28
- package/dist/types.d.mts +0 -83
- package/dist/types.mjs +0 -2
- package/dist/utils.d.mts +0 -1
- package/dist/utils.mjs +0 -4
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/swr">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fswr?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PartialDeep } from "@orpc/shared";
|
|
2
|
+
export interface OperationKeyPrefixOptions {
|
|
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
|
+
}
|
|
9
|
+
export type OperationKeyOptions<TInput> = OperationKeyPrefixOptions & {
|
|
10
|
+
input?: PartialDeep<TInput> | undefined;
|
|
11
|
+
};
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/key.ts
|
|
2
|
+
/**
|
|
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
|
+
];
|
|
13
|
+
}
|
|
14
|
+
export { generateOperationKey };
|
|
@@ -0,0 +1,45 @@
|
|
|
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 {}
|
|
7
|
+
export declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
|
|
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
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
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 };
|
|
@@ -0,0 +1,24 @@
|
|
|
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>>;
|
|
7
|
+
export interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Base path for all keys.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
path?: string[] | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
17
|
+
* helpers for every procedure in the router.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
23
|
+
*/
|
|
24
|
+
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
|
6
|
+
/**
|
|
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
|
+
} });
|
|
38
|
+
}
|
|
39
|
+
export { createRouterUtils };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { OperationKeyPrefixOptions } from "./key.js";
|
|
2
|
+
import { Matcher, MatcherOptions, MatcherStrategy } from "./types.js";
|
|
3
|
+
import { MaybeOptionalOptions } from "@orpc/shared";
|
|
4
|
+
export declare class SharedUtils<TInput> {
|
|
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
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
export type InferSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
6
|
+
export type InferLiveSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
7
|
+
export type OperationType = "fetcher" | "mutator" | "subscriber" | "liveSubscriber";
|
|
8
|
+
/**
|
|
9
|
+
* The symbol under which SWR utils attach operation details
|
|
10
|
+
* (key and operation type) to the client context.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
13
|
+
*/
|
|
14
|
+
export declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
15
|
+
/**
|
|
16
|
+
* A client context automatically populated by SWR utils,
|
|
17
|
+
* exposing the operation key and type of the calling operation.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
20
|
+
*/
|
|
21
|
+
export interface OperationContext {
|
|
22
|
+
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
23
|
+
key: unknown;
|
|
24
|
+
type: OperationType;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export type KeyOptions<TInput> = undefined extends TInput ? {
|
|
28
|
+
input?: TInput;
|
|
29
|
+
} : {
|
|
30
|
+
input: TInput;
|
|
31
|
+
};
|
|
32
|
+
export type MatcherStrategy = "exact" | "partial";
|
|
33
|
+
export type MatcherOptions<TStrategy extends MatcherStrategy, TInput> = ("partial" extends TStrategy ? {
|
|
34
|
+
input?: PartialDeep<TInput> | undefined;
|
|
35
|
+
} : undefined extends TInput ? {
|
|
36
|
+
input?: TInput;
|
|
37
|
+
} : {
|
|
38
|
+
input: TInput;
|
|
39
|
+
}) & {
|
|
40
|
+
strategy?: TStrategy | undefined;
|
|
41
|
+
};
|
|
42
|
+
export type Matcher = (key?: unknown) => boolean;
|
|
43
|
+
export type FetcherOptions<TClientContext extends ClientContext> = object extends TClientContext ? {
|
|
44
|
+
context?: TClientContext | undefined;
|
|
45
|
+
} : {
|
|
46
|
+
context: TClientContext;
|
|
47
|
+
};
|
|
48
|
+
export type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
|
|
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
|
+
};
|
|
65
|
+
export type MutatorOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext>;
|
|
66
|
+
export type Fetcher<TInput, TOutput> = (key: OperationKey<TInput>) => Promise<TOutput>;
|
|
67
|
+
export type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
68
|
+
arg: TInput;
|
|
69
|
+
}>) => Promise<TOutput>;
|
|
70
|
+
export type Subscriber<TInput, TData, TError> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => () => void;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
/**
|
|
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
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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;
|
|
5
|
+
}
|
|
6
|
+
export { isSubsetOf };
|
package/package.json
CHANGED
|
@@ -1,53 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/swr",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.48",
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
5
4
|
"description": "SWR integration for oRPC: typesafe React data fetching hooks",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"data-fetching",
|
|
7
|
+
"hooks",
|
|
8
|
+
"orpc",
|
|
9
|
+
"query",
|
|
10
|
+
"react",
|
|
11
|
+
"swr",
|
|
12
|
+
"typesafe",
|
|
13
|
+
"typescript"
|
|
10
14
|
],
|
|
11
15
|
"homepage": "https://orpc.dev",
|
|
16
|
+
"license": "MIT",
|
|
12
17
|
"repository": {
|
|
13
18
|
"type": "git",
|
|
14
19
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
20
|
"directory": "packages/swr"
|
|
16
21
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"react",
|
|
21
|
-
"hooks",
|
|
22
|
-
"data-fetching",
|
|
23
|
-
"query",
|
|
24
|
-
"typescript",
|
|
25
|
-
"typesafe"
|
|
22
|
+
"funding": [
|
|
23
|
+
"https://github.com/sponsors/dinwwwh",
|
|
24
|
+
"https://opencollective.com/middleapi"
|
|
26
25
|
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
27
30
|
"sideEffects": false,
|
|
28
31
|
"exports": {
|
|
29
32
|
"./package.json": "./package.json",
|
|
30
33
|
".": {
|
|
31
|
-
"types": "./dist/index.d.
|
|
32
|
-
"import": "./dist/index.
|
|
33
|
-
"default": "./dist/index.
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"default": "./dist/index.js"
|
|
34
37
|
}
|
|
35
38
|
},
|
|
36
|
-
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
],
|
|
39
|
-
"peerDependencies": {
|
|
40
|
-
"swr": ">=2.4.2"
|
|
41
|
-
},
|
|
42
39
|
"dependencies": {
|
|
43
|
-
"@orpc/client": "2.0.0-beta.
|
|
44
|
-
"@orpc/shared": "2.0.0-beta.
|
|
40
|
+
"@orpc/client": "2.0.0-beta.50",
|
|
41
|
+
"@orpc/shared": "2.0.0-beta.50"
|
|
45
42
|
},
|
|
46
43
|
"devDependencies": {
|
|
47
44
|
"swr": "^2.5.0",
|
|
48
45
|
"zod": "^4.6.7"
|
|
49
46
|
},
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
}
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"swr": ">=2.4.2"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {}
|
|
53
51
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export type { OperationKey, OperationKeyOptions, OperationKeyPrefixOptions } from "./key.mjs";
|
|
2
|
-
export { ProcedureUtils } from "./procedure-utils.mjs";
|
|
3
|
-
export type { ProcedureUtilsOptions } from "./procedure-utils.mjs";
|
|
4
|
-
export * from "./router-utils.mjs";
|
|
5
|
-
export { createRouterUtils as createSWRUtils } from "./router-utils.mjs";
|
|
6
|
-
export { SharedUtils } from "./shared-utils.mjs";
|
|
7
|
-
export * from "./types.mjs";
|
|
8
|
-
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, type OperationContext as SWROperationContext } from "./types.mjs";
|
|
9
|
-
export type { Public } from "@orpc/shared";
|
package/dist/index.mjs
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { ProcedureUtils } from "./procedure-utils.mjs";
|
|
2
|
-
export * from "./router-utils.mjs";
|
|
3
|
-
export { createRouterUtils as createSWRUtils } from "./router-utils.mjs";
|
|
4
|
-
export { SharedUtils } from "./shared-utils.mjs";
|
|
5
|
-
export * from "./types.mjs";
|
|
6
|
-
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL } from "./types.mjs";
|
package/dist/key.d.mts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { PartialDeep } from "@orpc/shared";
|
|
2
|
-
export interface OperationKeyPrefixOptions {
|
|
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
|
-
}
|
|
9
|
-
export type OperationKeyOptions<TInput> = OperationKeyPrefixOptions & {
|
|
10
|
-
input?: PartialDeep<TInput> | undefined;
|
|
11
|
-
};
|
|
12
|
-
export type OperationKey<TInput> = [path: string[], options: Omit<OperationKeyOptions<TInput>, "prefix">] | [prefix: string, path: string[], options: Omit<OperationKeyOptions<TInput>, "prefix">];
|
|
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>;
|
package/dist/key.mjs
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { Client, ClientContext } from "@orpc/client";
|
|
2
|
-
import type { MaybeOptionalOptions } from "@orpc/shared";
|
|
3
|
-
import type { OperationKey, OperationKeyPrefixOptions } from "./key.mjs";
|
|
4
|
-
import type { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Mutator, MutatorOptions, Subscriber, SubscriberOptions } from "./types.mjs";
|
|
5
|
-
import { SharedUtils } from "./shared-utils.mjs";
|
|
6
|
-
export interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {}
|
|
7
|
-
export declare class ProcedureUtils<
|
|
8
|
-
TClientContext extends ClientContext,
|
|
9
|
-
TInput,
|
|
10
|
-
TOutput,
|
|
11
|
-
TError
|
|
12
|
-
> extends SharedUtils<TInput> {
|
|
13
|
-
/**
|
|
14
|
-
* Calling corresponding procedure client
|
|
15
|
-
*
|
|
16
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
17
|
-
*/
|
|
18
|
-
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
19
|
-
constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions);
|
|
20
|
-
/**
|
|
21
|
-
* Generate a **full matching** key for SWR operations.
|
|
22
|
-
*
|
|
23
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
24
|
-
*/
|
|
25
|
-
key(...rest: MaybeOptionalOptions<KeyOptions<TInput>>): OperationKey<TInput>;
|
|
26
|
-
/**
|
|
27
|
-
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
30
|
-
*/
|
|
31
|
-
fetcher(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
32
|
-
/**
|
|
33
|
-
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
34
|
-
*
|
|
35
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
36
|
-
*/
|
|
37
|
-
subscriber(...rest: MaybeOptionalOptions<SubscriberOptions<TClientContext>>): Subscriber<TInput, InferSubscriberOutput<TOutput>, TError>;
|
|
38
|
-
/**
|
|
39
|
-
* 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.
|
|
40
|
-
*
|
|
41
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
42
|
-
*/
|
|
43
|
-
liveSubscriber(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Subscriber<TInput, InferLiveSubscriberOutput<TOutput>, TError>;
|
|
44
|
-
/**
|
|
45
|
-
* Generate a mutator function for use with useSWRMutation, etc.
|
|
46
|
-
*
|
|
47
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
48
|
-
*/
|
|
49
|
-
mutator(...rest: MaybeOptionalOptions<MutatorOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
50
|
-
}
|
package/dist/procedure-utils.mjs
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { isAsyncIteratorObject, resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
2
|
-
import { generateOperationKey } from "./key.mjs";
|
|
3
|
-
import { SharedUtils } from "./shared-utils.mjs";
|
|
4
|
-
import { OPERATION_CONTEXT_SYMBOL } from "./types.mjs";
|
|
5
|
-
export class ProcedureUtils extends SharedUtils {
|
|
6
|
-
|
|
7
|
-
call;
|
|
8
|
-
constructor(path, client, options = {}) {
|
|
9
|
-
super(path, options);
|
|
10
|
-
this.call = client;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
key(...rest) {
|
|
14
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
15
|
-
return generateOperationKey(this.path, {
|
|
16
|
-
...options,
|
|
17
|
-
prefix: this.options.prefix
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
fetcher(...rest) {
|
|
22
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
23
|
-
return async (key) => {
|
|
24
|
-
|
|
25
|
-
const { input } = key[key.length - 1];
|
|
26
|
-
return this.call(input, { context: {
|
|
27
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
28
|
-
key,
|
|
29
|
-
type: "fetcher"
|
|
30
|
-
},
|
|
31
|
-
...options.context
|
|
32
|
-
} });
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
subscriber(...rest) {
|
|
37
|
-
const { maxChunks, refetchMode = "reset", ...options } = resolveMaybeOptionalOptions(rest);
|
|
38
|
-
return (key, { next }) => {
|
|
39
|
-
const controller = new AbortController();
|
|
40
|
-
void (async () => {
|
|
41
|
-
try {
|
|
42
|
-
|
|
43
|
-
const { input } = key[key.length - 1];
|
|
44
|
-
const iterator = await this.call(input, {
|
|
45
|
-
context: {
|
|
46
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
47
|
-
key,
|
|
48
|
-
type: "subscriber"
|
|
49
|
-
},
|
|
50
|
-
...options.context
|
|
51
|
-
},
|
|
52
|
-
signal: controller.signal
|
|
53
|
-
});
|
|
54
|
-
if (!isAsyncIteratorObject(iterator)) {
|
|
55
|
-
throw new TypeError(".subscriber requires an AsyncIteratorObject output");
|
|
56
|
-
}
|
|
57
|
-
if (controller.signal.aborted) {
|
|
58
|
-
await iterator.return?.();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
let hasPreviousData = false;
|
|
62
|
-
if (refetchMode === "reset") {
|
|
63
|
-
next(undefined, undefined);
|
|
64
|
-
} else if (refetchMode === "replace") {
|
|
65
|
-
|
|
66
|
-
next(undefined, (old) => {
|
|
67
|
-
hasPreviousData = old !== undefined;
|
|
68
|
-
return old;
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
const shouldUpdateDataDuringStream = refetchMode !== "replace" || !hasPreviousData;
|
|
72
|
-
let buffer = [];
|
|
73
|
-
for await (const event of iterator) {
|
|
74
|
-
if (controller.signal.aborted) {
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
if (shouldUpdateDataDuringStream) {
|
|
78
|
-
next(undefined, (old) => {
|
|
79
|
-
const newData = Array.isArray(old) ? [...old, event] : [event];
|
|
80
|
-
if (typeof maxChunks === "number" && newData.length > maxChunks) {
|
|
81
|
-
return newData.slice(newData.length - maxChunks);
|
|
82
|
-
}
|
|
83
|
-
return newData;
|
|
84
|
-
});
|
|
85
|
-
} else {
|
|
86
|
-
buffer = [...buffer, event];
|
|
87
|
-
if (typeof maxChunks === "number" && buffer.length > maxChunks) {
|
|
88
|
-
buffer = buffer.slice(buffer.length - maxChunks);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (!shouldUpdateDataDuringStream && !controller.signal.aborted) {
|
|
93
|
-
next(undefined, buffer);
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
|
|
97
|
-
if (!controller.signal.aborted) {
|
|
98
|
-
next(error);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
})();
|
|
102
|
-
return () => {
|
|
103
|
-
controller.abort();
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
liveSubscriber(...rest) {
|
|
109
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
110
|
-
return (key, { next }) => {
|
|
111
|
-
const controller = new AbortController();
|
|
112
|
-
void (async () => {
|
|
113
|
-
try {
|
|
114
|
-
|
|
115
|
-
const { input } = key[key.length - 1];
|
|
116
|
-
const iterator = await this.call(input, {
|
|
117
|
-
context: {
|
|
118
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
119
|
-
key,
|
|
120
|
-
type: "liveSubscriber"
|
|
121
|
-
},
|
|
122
|
-
...options.context
|
|
123
|
-
},
|
|
124
|
-
signal: controller.signal
|
|
125
|
-
});
|
|
126
|
-
if (!isAsyncIteratorObject(iterator)) {
|
|
127
|
-
throw new TypeError(".liveSubscriber requires an AsyncIteratorObject output");
|
|
128
|
-
}
|
|
129
|
-
for await (const event of iterator) {
|
|
130
|
-
if (controller.signal.aborted) {
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
next(undefined, event);
|
|
134
|
-
}
|
|
135
|
-
} catch (error) {
|
|
136
|
-
|
|
137
|
-
if (!controller.signal.aborted) {
|
|
138
|
-
next(error);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
})();
|
|
142
|
-
return () => {
|
|
143
|
-
controller.abort();
|
|
144
|
-
};
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
mutator(...rest) {
|
|
149
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
150
|
-
return (key, { arg }) => this.call(arg, { context: {
|
|
151
|
-
[OPERATION_CONTEXT_SYMBOL]: {
|
|
152
|
-
key,
|
|
153
|
-
type: "mutator"
|
|
154
|
-
},
|
|
155
|
-
...options.context
|
|
156
|
-
} });
|
|
157
|
-
}
|
|
158
|
-
}
|
package/dist/router-utils.d.mts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { AnyNestedClient, Client } from "@orpc/client";
|
|
2
|
-
import type { Public } from "@orpc/shared";
|
|
3
|
-
import type { OperationKeyPrefixOptions } from "./key.mjs";
|
|
4
|
-
import { ProcedureUtils } from "./procedure-utils.mjs";
|
|
5
|
-
import { SharedUtils } from "./shared-utils.mjs";
|
|
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>>;
|
|
7
|
-
export interface RouterUtilsOptions extends OperationKeyPrefixOptions {}
|
|
8
|
-
/**
|
|
9
|
-
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
10
|
-
* helpers for every procedure in the router.
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* **Note**: Both client-side and server-side clients are supported.
|
|
14
|
-
*
|
|
15
|
-
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
16
|
-
*/
|
|
17
|
-
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
package/dist/router-utils.mjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from "@orpc/client";
|
|
2
|
-
import { bindMethods, isTypescriptObject, toArray } from "@orpc/shared";
|
|
3
|
-
import { ProcedureUtils } from "./procedure-utils.mjs";
|
|
4
|
-
import { SharedUtils } from "./shared-utils.mjs";
|
|
5
|
-
|
|
6
|
-
export function createRouterUtils(client, options = {}) {
|
|
7
|
-
const path = toArray(options.path);
|
|
8
|
-
const utils = typeof client === "function" ? bindMethods(new ProcedureUtils(path, client, { prefix: options.prefix }), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, { prefix: options.prefix }));
|
|
9
|
-
const cache = new Map();
|
|
10
|
-
const recursive = new Proxy(utils, { get(target, prop) {
|
|
11
|
-
const value = Reflect.get(target, prop);
|
|
12
|
-
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
13
|
-
return value ?? Reflect.get(Object.prototype, prop);
|
|
14
|
-
}
|
|
15
|
-
const nextClient = Reflect.get(client, prop);
|
|
16
|
-
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
17
|
-
return value;
|
|
18
|
-
}
|
|
19
|
-
let result = cache.get(prop);
|
|
20
|
-
if (result === undefined) {
|
|
21
|
-
const nextUtils = createRouterUtils(nextClient, {
|
|
22
|
-
...options,
|
|
23
|
-
path: [...path, prop]
|
|
24
|
-
});
|
|
25
|
-
result = typeof value !== "function" ? nextUtils : new Proxy(value, { get(target, prop) {
|
|
26
|
-
if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
27
|
-
return Reflect.get(target, prop);
|
|
28
|
-
}
|
|
29
|
-
return Reflect.get(nextUtils, prop);
|
|
30
|
-
} });
|
|
31
|
-
cache.set(prop, result);
|
|
32
|
-
}
|
|
33
|
-
return result;
|
|
34
|
-
} });
|
|
35
|
-
return recursive;
|
|
36
|
-
}
|
package/dist/shared-utils.d.mts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { MaybeOptionalOptions } from "@orpc/shared";
|
|
2
|
-
import type { OperationKeyPrefixOptions } from "./key.mjs";
|
|
3
|
-
import type { Matcher, MatcherOptions, MatcherStrategy } from "./types.mjs";
|
|
4
|
-
export declare class SharedUtils<TInput> {
|
|
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.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
2
|
-
import { generateOperationKey } from "./key.mjs";
|
|
3
|
-
import { isSubsetOf } from "./utils.mjs";
|
|
4
|
-
export class SharedUtils {
|
|
5
|
-
path;
|
|
6
|
-
options;
|
|
7
|
-
constructor(path, options) {
|
|
8
|
-
this.path = path;
|
|
9
|
-
this.options = options;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
matcher(...rest) {
|
|
13
|
-
const { input, strategy = "partial" } = resolveMaybeOptionalOptions(rest);
|
|
14
|
-
const expectedKey = generateOperationKey(this.path, {
|
|
15
|
-
prefix: this.options.prefix,
|
|
16
|
-
input
|
|
17
|
-
});
|
|
18
|
-
return (key) => {
|
|
19
|
-
if (!isSubsetOf(expectedKey, key)) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
if (strategy === "exact" && !isSubsetOf(key, expectedKey)) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
package/dist/types.d.mts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import type { ClientContext } from "@orpc/client";
|
|
2
|
-
import type { PartialDeep } from "@orpc/shared";
|
|
3
|
-
import type { SWRSubscriptionOptions } from "swr/subscription";
|
|
4
|
-
import type { OperationKey } from "./key.mjs";
|
|
5
|
-
export type InferSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
6
|
-
export type InferLiveSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
7
|
-
export type OperationType = "fetcher" | "mutator" | "subscriber" | "liveSubscriber";
|
|
8
|
-
/**
|
|
9
|
-
* The symbol under which SWR utils attach operation details
|
|
10
|
-
* (key and operation type) to the client context.
|
|
11
|
-
*
|
|
12
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
13
|
-
*/
|
|
14
|
-
export declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
15
|
-
/**
|
|
16
|
-
* A client context automatically populated by SWR utils,
|
|
17
|
-
* exposing the operation key and type of the calling operation.
|
|
18
|
-
*
|
|
19
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
20
|
-
*/
|
|
21
|
-
export interface OperationContext {
|
|
22
|
-
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
23
|
-
key: unknown;
|
|
24
|
-
type: OperationType;
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
export type KeyOptions<TInput> = undefined extends TInput ? {
|
|
28
|
-
input?: TInput;
|
|
29
|
-
} : {
|
|
30
|
-
input: TInput;
|
|
31
|
-
};
|
|
32
|
-
export type MatcherStrategy = "exact" | "partial";
|
|
33
|
-
export type MatcherOptions<
|
|
34
|
-
TStrategy extends MatcherStrategy,
|
|
35
|
-
TInput
|
|
36
|
-
> = ("partial" extends TStrategy ? {
|
|
37
|
-
input?: PartialDeep<TInput> | undefined;
|
|
38
|
-
} : undefined extends TInput ? {
|
|
39
|
-
input?: TInput;
|
|
40
|
-
} : {
|
|
41
|
-
input: TInput;
|
|
42
|
-
}) & {
|
|
43
|
-
strategy?: TStrategy | undefined;
|
|
44
|
-
};
|
|
45
|
-
export type Matcher = (key?: unknown) => boolean;
|
|
46
|
-
export type FetcherOptions<TClientContext extends ClientContext> = object extends TClientContext ? {
|
|
47
|
-
context?: TClientContext | undefined;
|
|
48
|
-
} : {
|
|
49
|
-
context: TClientContext;
|
|
50
|
-
};
|
|
51
|
-
export type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
|
|
52
|
-
/**
|
|
53
|
-
* Determines how data is handled when the subscription is subscribed again
|
|
54
|
-
*
|
|
55
|
-
* - `'reset'`: Clears existing data before streaming new data.
|
|
56
|
-
* - `'append'`: Adds new streamed chunks to the existing data.
|
|
57
|
-
* - `'replace'`: Buffers streamed data and replaces existing data after the stream completes.
|
|
58
|
-
*
|
|
59
|
-
* @default 'reset'
|
|
60
|
-
*/
|
|
61
|
-
refetchMode?: "append" | "reset" | "replace" | undefined;
|
|
62
|
-
/**
|
|
63
|
-
* Maximum number of chunks to store.
|
|
64
|
-
* When exceeded, the oldest chunks will be removed.
|
|
65
|
-
*/
|
|
66
|
-
maxChunks?: number | undefined;
|
|
67
|
-
};
|
|
68
|
-
export type MutatorOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext>;
|
|
69
|
-
export type Fetcher<
|
|
70
|
-
TInput,
|
|
71
|
-
TOutput
|
|
72
|
-
> = (key: OperationKey<TInput>) => Promise<TOutput>;
|
|
73
|
-
export type Mutator<
|
|
74
|
-
TInput,
|
|
75
|
-
TOutput
|
|
76
|
-
> = (key: unknown, options: Readonly<{
|
|
77
|
-
arg: TInput;
|
|
78
|
-
}>) => Promise<TOutput>;
|
|
79
|
-
export type Subscriber<
|
|
80
|
-
TInput,
|
|
81
|
-
TData,
|
|
82
|
-
TError
|
|
83
|
-
> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => () => void;
|
package/dist/types.mjs
DELETED
package/dist/utils.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean;
|
package/dist/utils.mjs
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { isPlainObject } from "@orpc/shared";
|
|
2
|
-
export function isSubsetOf(subsetKey, fullKey) {
|
|
3
|
-
return subsetKey === fullKey ? true : typeof subsetKey !== typeof fullKey ? false : isPlainObject(subsetKey) && isPlainObject(fullKey) ? Object.keys(subsetKey).every((key) => subsetKey[key] === undefined || 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;
|
|
4
|
-
}
|