@orpc/swr 2.0.0-beta.47 → 2.0.0-beta.49
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 +6 -7
- package/dist/index.d.ts +9 -162
- package/dist/index.js +6 -0
- package/dist/key.d.ts +18 -0
- package/dist/key.js +12 -0
- package/dist/procedure-utils.d.ts +46 -0
- package/dist/procedure-utils.js +174 -0
- package/dist/router-utils.d.ts +26 -0
- package/dist/router-utils.js +55 -0
- package/dist/shared-utils.d.ts +14 -0
- package/dist/shared-utils.js +32 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.js +7 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +14 -0
- package/package.json +27 -29
- package/dist/index.d.mts +0 -162
- package/dist/index.mjs +0 -240
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)
|
|
@@ -191,15 +192,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
192
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
192
193
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
193
194
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
194
|
-
<td align="center"><a href="https://
|
|
195
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
195
196
|
</tr>
|
|
196
197
|
<tr>
|
|
197
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
198
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
199
|
</tr>
|
|
200
200
|
</table>
|
|
201
201
|
|
|
202
|
-
With thanks to [
|
|
202
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
203
|
|
|
204
204
|
## Open Source Programs
|
|
205
205
|
|
|
@@ -209,7 +209,6 @@ With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
209
209
|
<tr>
|
|
210
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>
|
|
211
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>
|
|
212
|
-
<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>
|
|
213
212
|
</tr>
|
|
214
213
|
</table>
|
|
215
214
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,162 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
prefix?: string | undefined;
|
|
12
|
-
}
|
|
13
|
-
type OperationKeyOptions<TInput> = OperationKeyPrefixOptions & {
|
|
14
|
-
input?: PartialDeep<TInput> | undefined;
|
|
15
|
-
};
|
|
16
|
-
type OperationKey<TInput> = [path: string[], options: Omit<OperationKeyOptions<TInput>, 'prefix'>] | [prefix: string, path: string[], options: Omit<OperationKeyOptions<TInput>, 'prefix'>];
|
|
17
|
-
|
|
18
|
-
type InferSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
19
|
-
type InferLiveSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
20
|
-
type OperationType = 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscriber';
|
|
21
|
-
/**
|
|
22
|
-
* The symbol under which SWR utils attach operation details
|
|
23
|
-
* (key and operation type) to the client context.
|
|
24
|
-
*
|
|
25
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
26
|
-
*/
|
|
27
|
-
declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
28
|
-
/**
|
|
29
|
-
* A client context automatically populated by SWR utils,
|
|
30
|
-
* exposing the operation key and type of the calling operation.
|
|
31
|
-
*
|
|
32
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
33
|
-
*/
|
|
34
|
-
interface OperationContext {
|
|
35
|
-
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
36
|
-
key: unknown;
|
|
37
|
-
type: OperationType;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
type KeyOptions<TInput> = undefined extends TInput ? {
|
|
41
|
-
input?: TInput;
|
|
42
|
-
} : {
|
|
43
|
-
input: TInput;
|
|
44
|
-
};
|
|
45
|
-
type MatcherStrategy = 'exact' | 'partial';
|
|
46
|
-
type MatcherOptions<TStrategy extends MatcherStrategy, TInput> = ('partial' extends TStrategy ? {
|
|
47
|
-
input?: PartialDeep<TInput> | undefined;
|
|
48
|
-
} : undefined extends TInput ? {
|
|
49
|
-
input?: TInput;
|
|
50
|
-
} : {
|
|
51
|
-
input: TInput;
|
|
52
|
-
}) & {
|
|
53
|
-
strategy?: TStrategy | undefined;
|
|
54
|
-
};
|
|
55
|
-
type Matcher = (key?: unknown) => boolean;
|
|
56
|
-
type FetcherOptions<TClientContext extends ClientContext> = object extends TClientContext ? {
|
|
57
|
-
context?: TClientContext | undefined;
|
|
58
|
-
} : {
|
|
59
|
-
context: TClientContext;
|
|
60
|
-
};
|
|
61
|
-
type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
|
|
62
|
-
/**
|
|
63
|
-
* Determines how data is handled when the subscription is subscribed again
|
|
64
|
-
*
|
|
65
|
-
* - `'reset'`: Clears existing data before streaming new data.
|
|
66
|
-
* - `'append'`: Adds new streamed chunks to the existing data.
|
|
67
|
-
* - `'replace'`: Buffers streamed data and replaces existing data after the stream completes.
|
|
68
|
-
*
|
|
69
|
-
* @default 'reset'
|
|
70
|
-
*/
|
|
71
|
-
refetchMode?: 'append' | 'reset' | 'replace' | undefined;
|
|
72
|
-
/**
|
|
73
|
-
* Maximum number of chunks to store.
|
|
74
|
-
* When exceeded, the oldest chunks will be removed.
|
|
75
|
-
*/
|
|
76
|
-
maxChunks?: number | undefined;
|
|
77
|
-
};
|
|
78
|
-
type MutatorOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext>;
|
|
79
|
-
type Fetcher<TInput, TOutput> = (key: OperationKey<TInput>) => Promise<TOutput>;
|
|
80
|
-
type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
81
|
-
arg: TInput;
|
|
82
|
-
}>) => Promise<TOutput>;
|
|
83
|
-
type Subscriber<TInput, TData, TError> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => (() => void);
|
|
84
|
-
|
|
85
|
-
declare class SharedUtils<TInput> {
|
|
86
|
-
protected readonly path: string[];
|
|
87
|
-
protected readonly options: OperationKeyPrefixOptions;
|
|
88
|
-
constructor(path: string[], options: OperationKeyPrefixOptions);
|
|
89
|
-
/**
|
|
90
|
-
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
91
|
-
*
|
|
92
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
93
|
-
*/
|
|
94
|
-
matcher<TStrategy extends MatcherStrategy>(...rest: MaybeOptionalOptions<MatcherOptions<TStrategy, TInput>>): Matcher;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {
|
|
98
|
-
}
|
|
99
|
-
declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
|
|
100
|
-
/**
|
|
101
|
-
* Calling corresponding procedure client
|
|
102
|
-
*
|
|
103
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
104
|
-
*/
|
|
105
|
-
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
106
|
-
constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions);
|
|
107
|
-
/**
|
|
108
|
-
* Generate a **full matching** key for SWR operations.
|
|
109
|
-
*
|
|
110
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
111
|
-
*/
|
|
112
|
-
key(...rest: MaybeOptionalOptions<KeyOptions<TInput>>): OperationKey<TInput>;
|
|
113
|
-
/**
|
|
114
|
-
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
115
|
-
*
|
|
116
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
117
|
-
*/
|
|
118
|
-
fetcher(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
119
|
-
/**
|
|
120
|
-
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
121
|
-
*
|
|
122
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
123
|
-
*/
|
|
124
|
-
subscriber(...rest: MaybeOptionalOptions<SubscriberOptions<TClientContext>>): Subscriber<TInput, InferSubscriberOutput<TOutput>, TError>;
|
|
125
|
-
/**
|
|
126
|
-
* 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.
|
|
127
|
-
*
|
|
128
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
129
|
-
*/
|
|
130
|
-
liveSubscriber(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Subscriber<TInput, InferLiveSubscriberOutput<TOutput>, TError>;
|
|
131
|
-
/**
|
|
132
|
-
* Generate a mutator function for use with useSWRMutation, etc.
|
|
133
|
-
*
|
|
134
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
135
|
-
*/
|
|
136
|
-
mutator(...rest: MaybeOptionalOptions<MutatorOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : {
|
|
140
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never;
|
|
141
|
-
} & Public<SharedUtils<unknown>>;
|
|
142
|
-
interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
143
|
-
/**
|
|
144
|
-
* Base path for all keys.
|
|
145
|
-
*
|
|
146
|
-
* @internal
|
|
147
|
-
*/
|
|
148
|
-
path?: string[] | undefined;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
152
|
-
* helpers for every procedure in the router.
|
|
153
|
-
*
|
|
154
|
-
* @remarks
|
|
155
|
-
* **Note**: Both client-side and server-side clients are supported.
|
|
156
|
-
*
|
|
157
|
-
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
158
|
-
*/
|
|
159
|
-
declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
|
160
|
-
|
|
161
|
-
export { OPERATION_CONTEXT_SYMBOL, ProcedureUtils, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, SharedUtils, createRouterUtils, createRouterUtils as createSWRUtils };
|
|
162
|
-
export type { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Matcher, MatcherOptions, MatcherStrategy, Mutator, MutatorOptions, OperationContext, OperationKey, OperationKeyOptions, OperationKeyPrefixOptions, OperationType, ProcedureUtilsOptions, RouterUtils, RouterUtilsOptions, OperationContext as SWROperationContext, Subscriber, SubscriberOptions };
|
|
1
|
+
export type { OperationKey, OperationKeyOptions, OperationKeyPrefixOptions } from './key.ts';
|
|
2
|
+
export { ProcedureUtils } from './procedure-utils.ts';
|
|
3
|
+
export type { ProcedureUtilsOptions } from './procedure-utils.ts';
|
|
4
|
+
export * from './router-utils.ts';
|
|
5
|
+
export { createRouterUtils as createSWRUtils } from './router-utils.ts';
|
|
6
|
+
export { SharedUtils } from './shared-utils.ts';
|
|
7
|
+
export * from './types.ts';
|
|
8
|
+
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, type OperationContext as SWROperationContext, } from './types.ts';
|
|
9
|
+
export type { Public } from '@orpc/shared';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ProcedureUtils } from './procedure-utils.js';
|
|
2
|
+
export * from './router-utils.js';
|
|
3
|
+
export { createRouterUtils as createSWRUtils } from './router-utils.js';
|
|
4
|
+
export { SharedUtils } from './shared-utils.js';
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export { OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, } from './types.js';
|
package/dist/key.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a **full matching** key for SWR operations.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
5
|
+
*/
|
|
6
|
+
export function generateOperationKey(path, options = {}) {
|
|
7
|
+
return [
|
|
8
|
+
...(options.prefix !== undefined ? [options.prefix] : []),
|
|
9
|
+
path,
|
|
10
|
+
options.input !== undefined ? { input: options.input } : {},
|
|
11
|
+
];
|
|
12
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Client, ClientContext } from '@orpc/client';
|
|
2
|
+
import type { MaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
+
import type { OperationKey, OperationKeyPrefixOptions } from './key.ts';
|
|
4
|
+
import { SharedUtils } from './shared-utils.ts';
|
|
5
|
+
import type { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Mutator, MutatorOptions, Subscriber, SubscriberOptions } from './types.ts';
|
|
6
|
+
export interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {
|
|
7
|
+
}
|
|
8
|
+
export declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
|
|
9
|
+
/**
|
|
10
|
+
* Calling corresponding procedure client
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
13
|
+
*/
|
|
14
|
+
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
15
|
+
constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions);
|
|
16
|
+
/**
|
|
17
|
+
* Generate a **full matching** key for SWR operations.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
20
|
+
*/
|
|
21
|
+
key(...rest: MaybeOptionalOptions<KeyOptions<TInput>>): OperationKey<TInput>;
|
|
22
|
+
/**
|
|
23
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
26
|
+
*/
|
|
27
|
+
fetcher(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
28
|
+
/**
|
|
29
|
+
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
32
|
+
*/
|
|
33
|
+
subscriber(...rest: MaybeOptionalOptions<SubscriberOptions<TClientContext>>): Subscriber<TInput, InferSubscriberOutput<TOutput>, TError>;
|
|
34
|
+
/**
|
|
35
|
+
* 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.
|
|
36
|
+
*
|
|
37
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
38
|
+
*/
|
|
39
|
+
liveSubscriber(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Subscriber<TInput, InferLiveSubscriberOutput<TOutput>, TError>;
|
|
40
|
+
/**
|
|
41
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
44
|
+
*/
|
|
45
|
+
mutator(...rest: MaybeOptionalOptions<MutatorOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { isAsyncIteratorObject, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
+
import { generateOperationKey } from './key.js';
|
|
3
|
+
import { SharedUtils } from './shared-utils.js';
|
|
4
|
+
import { OPERATION_CONTEXT_SYMBOL } from './types.js';
|
|
5
|
+
export class ProcedureUtils extends SharedUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Calling corresponding procedure client
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
10
|
+
*/
|
|
11
|
+
call;
|
|
12
|
+
constructor(path, client, options = {}) {
|
|
13
|
+
super(path, options);
|
|
14
|
+
this.call = client;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate a **full matching** key for SWR operations.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
20
|
+
*/
|
|
21
|
+
key(...rest) {
|
|
22
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
23
|
+
return generateOperationKey(this.path, {
|
|
24
|
+
...options,
|
|
25
|
+
prefix: this.options.prefix,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
32
|
+
*/
|
|
33
|
+
fetcher(...rest) {
|
|
34
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
35
|
+
return async (key) => {
|
|
36
|
+
/**
|
|
37
|
+
* Input can be omitted from the key when it is undefined,
|
|
38
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
39
|
+
*/
|
|
40
|
+
const { input } = key[key.length - 1];
|
|
41
|
+
return this.call(input, {
|
|
42
|
+
context: {
|
|
43
|
+
[OPERATION_CONTEXT_SYMBOL]: { key, type: 'fetcher' },
|
|
44
|
+
...options.context,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
51
|
+
*
|
|
52
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
53
|
+
*/
|
|
54
|
+
subscriber(...rest) {
|
|
55
|
+
const { maxChunks, refetchMode = 'reset', ...options } = resolveMaybeOptionalOptions(rest);
|
|
56
|
+
return (key, { next }) => {
|
|
57
|
+
const controller = new AbortController();
|
|
58
|
+
void (async () => {
|
|
59
|
+
try {
|
|
60
|
+
/**
|
|
61
|
+
* Input can be omitted from the key when it is undefined,
|
|
62
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
63
|
+
*/
|
|
64
|
+
const { input } = key[key.length - 1];
|
|
65
|
+
const iterator = await this.call(input, {
|
|
66
|
+
context: {
|
|
67
|
+
[OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' },
|
|
68
|
+
...options.context,
|
|
69
|
+
},
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
});
|
|
72
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
73
|
+
throw new TypeError('.subscriber requires an AsyncIteratorObject output');
|
|
74
|
+
}
|
|
75
|
+
if (controller.signal.aborted) {
|
|
76
|
+
await iterator.return?.();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (refetchMode === 'reset') {
|
|
80
|
+
next(undefined, []);
|
|
81
|
+
}
|
|
82
|
+
for await (const event of iterator) {
|
|
83
|
+
if (controller.signal.aborted) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
next(undefined, (old) => {
|
|
87
|
+
const newData = Array.isArray(old) ? [...old, event] : [event];
|
|
88
|
+
if (typeof maxChunks === 'number' && newData.length > maxChunks) {
|
|
89
|
+
return newData.slice(newData.length - maxChunks);
|
|
90
|
+
}
|
|
91
|
+
return newData;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
/**
|
|
97
|
+
* Only report the error if the controller is not aborted.
|
|
98
|
+
*/
|
|
99
|
+
if (!controller.signal.aborted) {
|
|
100
|
+
next(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})();
|
|
104
|
+
return () => {
|
|
105
|
+
controller.abort();
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 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.
|
|
111
|
+
*
|
|
112
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
113
|
+
*/
|
|
114
|
+
liveSubscriber(...rest) {
|
|
115
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
116
|
+
return (key, { next }) => {
|
|
117
|
+
const controller = new AbortController();
|
|
118
|
+
void (async () => {
|
|
119
|
+
try {
|
|
120
|
+
/**
|
|
121
|
+
* Input can be omitted from the key when it is undefined,
|
|
122
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
123
|
+
*/
|
|
124
|
+
const { input } = key[key.length - 1];
|
|
125
|
+
const iterator = await this.call(input, {
|
|
126
|
+
context: {
|
|
127
|
+
[OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' },
|
|
128
|
+
...options.context,
|
|
129
|
+
},
|
|
130
|
+
signal: controller.signal,
|
|
131
|
+
});
|
|
132
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
133
|
+
throw new TypeError('.liveSubscriber requires an AsyncIteratorObject output');
|
|
134
|
+
}
|
|
135
|
+
if (controller.signal.aborted) {
|
|
136
|
+
await iterator.return?.();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
for await (const event of iterator) {
|
|
140
|
+
if (controller.signal.aborted) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
next(undefined, event);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
/**
|
|
148
|
+
* Only report the error if the controller is not aborted.
|
|
149
|
+
*/
|
|
150
|
+
if (!controller.signal.aborted) {
|
|
151
|
+
next(error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})();
|
|
155
|
+
return () => {
|
|
156
|
+
controller.abort();
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
162
|
+
*
|
|
163
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
164
|
+
*/
|
|
165
|
+
mutator(...rest) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AnyNestedClient, Client } from '@orpc/client';
|
|
2
|
+
import type { Public } from '@orpc/shared';
|
|
3
|
+
import type { OperationKeyPrefixOptions } from './key.ts';
|
|
4
|
+
import { ProcedureUtils } from './procedure-utils.ts';
|
|
5
|
+
import { SharedUtils } from './shared-utils.ts';
|
|
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>>;
|
|
9
|
+
export interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Base path for all keys.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
path?: string[] | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
19
|
+
* helpers for every procedure in the router.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
25
|
+
*/
|
|
26
|
+
export declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
2
|
+
import { bindMethods, isNonPrimitive, toArray } from '@orpc/shared';
|
|
3
|
+
import { ProcedureUtils } from './procedure-utils.js';
|
|
4
|
+
import { SharedUtils } from './shared-utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
7
|
+
* helpers for every procedure in the router.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* **Note**: Both client-side and server-side clients are supported.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
13
|
+
*/
|
|
14
|
+
export function createRouterUtils(client, options = {}) {
|
|
15
|
+
const path = toArray(options.path);
|
|
16
|
+
const utils = typeof client === 'function'
|
|
17
|
+
? bindMethods(new ProcedureUtils(path, client, { prefix: options.prefix }), {
|
|
18
|
+
unbound: ['call'],
|
|
19
|
+
})
|
|
20
|
+
: bindMethods(new SharedUtils(path, { prefix: options.prefix }));
|
|
21
|
+
const cache = new Map();
|
|
22
|
+
const recursive = new Proxy(utils, {
|
|
23
|
+
get(target, prop) {
|
|
24
|
+
const value = Reflect.get(target, prop);
|
|
25
|
+
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
26
|
+
return value ?? Reflect.get(Object.prototype, prop);
|
|
27
|
+
}
|
|
28
|
+
const nextClient = Reflect.get(client, prop);
|
|
29
|
+
if (typeof prop !== 'string' || !isNonPrimitive(nextClient)) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
let result = cache.get(prop);
|
|
33
|
+
if (result === undefined) {
|
|
34
|
+
const nextUtils = createRouterUtils(nextClient, {
|
|
35
|
+
...options,
|
|
36
|
+
path: [...path, prop],
|
|
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;
|
|
55
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
+
import type { OperationKeyPrefixOptions } from './key.ts';
|
|
3
|
+
import type { Matcher, MatcherOptions, MatcherStrategy } from './types.ts';
|
|
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,32 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
+
import { generateOperationKey } from './key.js';
|
|
3
|
+
import { isSubsetOf } from './utils.js';
|
|
4
|
+
export class SharedUtils {
|
|
5
|
+
path;
|
|
6
|
+
options;
|
|
7
|
+
constructor(path, options) {
|
|
8
|
+
this.path = path;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
15
|
+
*/
|
|
16
|
+
matcher(...rest) {
|
|
17
|
+
const { input, strategy = 'partial' } = resolveMaybeOptionalOptions(rest);
|
|
18
|
+
const expectedKey = generateOperationKey(this.path, {
|
|
19
|
+
prefix: this.options.prefix,
|
|
20
|
+
input: input,
|
|
21
|
+
});
|
|
22
|
+
return (key) => {
|
|
23
|
+
if (!isSubsetOf(expectedKey, key)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (strategy === 'exact' && !isSubsetOf(key, expectedKey)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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.ts';
|
|
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,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The symbol under which SWR utils attach operation details
|
|
3
|
+
* (key and operation type) to the client context.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
6
|
+
*/
|
|
7
|
+
export const OPERATION_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_SWR_OPERATION_CONTEXT');
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isPlainObject } from '@orpc/shared';
|
|
2
|
+
export function isSubsetOf(subsetKey, fullKey) {
|
|
3
|
+
return subsetKey === fullKey
|
|
4
|
+
? true
|
|
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;
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,53 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/swr",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
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.49",
|
|
41
|
+
"@orpc/shared": "2.0.0-beta.49"
|
|
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,162 +0,0 @@
|
|
|
1
|
-
import { PartialDeep, MaybeOptionalOptions, Public } from '@orpc/shared';
|
|
2
|
-
export { Public } from '@orpc/shared';
|
|
3
|
-
import { ClientContext, Client, AnyNestedClient } from '@orpc/client';
|
|
4
|
-
import { SWRSubscriptionOptions } from 'swr/subscription';
|
|
5
|
-
|
|
6
|
-
interface OperationKeyPrefixOptions {
|
|
7
|
-
/**
|
|
8
|
-
* Prepended as the first element of the key when present.
|
|
9
|
-
* Use this to avoid key conflicts when multiple router utils share the same client.
|
|
10
|
-
*/
|
|
11
|
-
prefix?: string | undefined;
|
|
12
|
-
}
|
|
13
|
-
type OperationKeyOptions<TInput> = OperationKeyPrefixOptions & {
|
|
14
|
-
input?: PartialDeep<TInput> | undefined;
|
|
15
|
-
};
|
|
16
|
-
type OperationKey<TInput> = [path: string[], options: Omit<OperationKeyOptions<TInput>, 'prefix'>] | [prefix: string, path: string[], options: Omit<OperationKeyOptions<TInput>, 'prefix'>];
|
|
17
|
-
|
|
18
|
-
type InferSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
19
|
-
type InferLiveSubscriberOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
20
|
-
type OperationType = 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscriber';
|
|
21
|
-
/**
|
|
22
|
-
* The symbol under which SWR utils attach operation details
|
|
23
|
-
* (key and operation type) to the client context.
|
|
24
|
-
*
|
|
25
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
26
|
-
*/
|
|
27
|
-
declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
28
|
-
/**
|
|
29
|
-
* A client context automatically populated by SWR utils,
|
|
30
|
-
* exposing the operation key and type of the calling operation.
|
|
31
|
-
*
|
|
32
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context}
|
|
33
|
-
*/
|
|
34
|
-
interface OperationContext {
|
|
35
|
-
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
36
|
-
key: unknown;
|
|
37
|
-
type: OperationType;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
type KeyOptions<TInput> = undefined extends TInput ? {
|
|
41
|
-
input?: TInput;
|
|
42
|
-
} : {
|
|
43
|
-
input: TInput;
|
|
44
|
-
};
|
|
45
|
-
type MatcherStrategy = 'exact' | 'partial';
|
|
46
|
-
type MatcherOptions<TStrategy extends MatcherStrategy, TInput> = ('partial' extends TStrategy ? {
|
|
47
|
-
input?: PartialDeep<TInput> | undefined;
|
|
48
|
-
} : undefined extends TInput ? {
|
|
49
|
-
input?: TInput;
|
|
50
|
-
} : {
|
|
51
|
-
input: TInput;
|
|
52
|
-
}) & {
|
|
53
|
-
strategy?: TStrategy | undefined;
|
|
54
|
-
};
|
|
55
|
-
type Matcher = (key?: unknown) => boolean;
|
|
56
|
-
type FetcherOptions<TClientContext extends ClientContext> = object extends TClientContext ? {
|
|
57
|
-
context?: TClientContext | undefined;
|
|
58
|
-
} : {
|
|
59
|
-
context: TClientContext;
|
|
60
|
-
};
|
|
61
|
-
type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
|
|
62
|
-
/**
|
|
63
|
-
* Determines how data is handled when the subscription is subscribed again
|
|
64
|
-
*
|
|
65
|
-
* - `'reset'`: Clears existing data before streaming new data.
|
|
66
|
-
* - `'append'`: Adds new streamed chunks to the existing data.
|
|
67
|
-
* - `'replace'`: Buffers streamed data and replaces existing data after the stream completes.
|
|
68
|
-
*
|
|
69
|
-
* @default 'reset'
|
|
70
|
-
*/
|
|
71
|
-
refetchMode?: 'append' | 'reset' | 'replace' | undefined;
|
|
72
|
-
/**
|
|
73
|
-
* Maximum number of chunks to store.
|
|
74
|
-
* When exceeded, the oldest chunks will be removed.
|
|
75
|
-
*/
|
|
76
|
-
maxChunks?: number | undefined;
|
|
77
|
-
};
|
|
78
|
-
type MutatorOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext>;
|
|
79
|
-
type Fetcher<TInput, TOutput> = (key: OperationKey<TInput>) => Promise<TOutput>;
|
|
80
|
-
type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
81
|
-
arg: TInput;
|
|
82
|
-
}>) => Promise<TOutput>;
|
|
83
|
-
type Subscriber<TInput, TData, TError> = (key: OperationKey<TInput>, options: SWRSubscriptionOptions<TData, TError>) => (() => void);
|
|
84
|
-
|
|
85
|
-
declare class SharedUtils<TInput> {
|
|
86
|
-
protected readonly path: string[];
|
|
87
|
-
protected readonly options: OperationKeyPrefixOptions;
|
|
88
|
-
constructor(path: string[], options: OperationKeyPrefixOptions);
|
|
89
|
-
/**
|
|
90
|
-
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
91
|
-
*
|
|
92
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
93
|
-
*/
|
|
94
|
-
matcher<TStrategy extends MatcherStrategy>(...rest: MaybeOptionalOptions<MatcherOptions<TStrategy, TInput>>): Matcher;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {
|
|
98
|
-
}
|
|
99
|
-
declare class ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> extends SharedUtils<TInput> {
|
|
100
|
-
/**
|
|
101
|
-
* Calling corresponding procedure client
|
|
102
|
-
*
|
|
103
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
104
|
-
*/
|
|
105
|
-
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
106
|
-
constructor(path: string[], client: Client<TClientContext, TInput, TOutput, TError>, options?: ProcedureUtilsOptions);
|
|
107
|
-
/**
|
|
108
|
-
* Generate a **full matching** key for SWR operations.
|
|
109
|
-
*
|
|
110
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
111
|
-
*/
|
|
112
|
-
key(...rest: MaybeOptionalOptions<KeyOptions<TInput>>): OperationKey<TInput>;
|
|
113
|
-
/**
|
|
114
|
-
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
115
|
-
*
|
|
116
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
117
|
-
*/
|
|
118
|
-
fetcher(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
119
|
-
/**
|
|
120
|
-
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
121
|
-
*
|
|
122
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
123
|
-
*/
|
|
124
|
-
subscriber(...rest: MaybeOptionalOptions<SubscriberOptions<TClientContext>>): Subscriber<TInput, InferSubscriberOutput<TOutput>, TError>;
|
|
125
|
-
/**
|
|
126
|
-
* 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.
|
|
127
|
-
*
|
|
128
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
129
|
-
*/
|
|
130
|
-
liveSubscriber(...rest: MaybeOptionalOptions<FetcherOptions<TClientContext>>): Subscriber<TInput, InferLiveSubscriberOutput<TOutput>, TError>;
|
|
131
|
-
/**
|
|
132
|
-
* Generate a mutator function for use with useSWRMutation, etc.
|
|
133
|
-
*
|
|
134
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
135
|
-
*/
|
|
136
|
-
mutator(...rest: MaybeOptionalOptions<MutatorOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
type RouterUtils<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Public<ProcedureUtils<UClientContext, UInput, UOutput, UError>> : {
|
|
140
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? RouterUtils<T[K]> : never;
|
|
141
|
-
} & Public<SharedUtils<unknown>>;
|
|
142
|
-
interface RouterUtilsOptions extends OperationKeyPrefixOptions {
|
|
143
|
-
/**
|
|
144
|
-
* Base path for all keys.
|
|
145
|
-
*
|
|
146
|
-
* @internal
|
|
147
|
-
*/
|
|
148
|
-
path?: string[] | undefined;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Creates SWR utils from a client, exposing fetcher/mutator/subscriber
|
|
152
|
-
* helpers for every procedure in the router.
|
|
153
|
-
*
|
|
154
|
-
* @remarks
|
|
155
|
-
* **Note**: Both client-side and server-side clients are supported.
|
|
156
|
-
*
|
|
157
|
-
* @see {@link https://orpc.dev/docs/integrations/swr | SWR Integration}
|
|
158
|
-
*/
|
|
159
|
-
declare function createRouterUtils<T extends AnyNestedClient>(client: T, options?: RouterUtilsOptions): RouterUtils<T>;
|
|
160
|
-
|
|
161
|
-
export { OPERATION_CONTEXT_SYMBOL, ProcedureUtils, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, SharedUtils, createRouterUtils, createRouterUtils as createSWRUtils };
|
|
162
|
-
export type { Fetcher, FetcherOptions, InferLiveSubscriberOutput, InferSubscriberOutput, KeyOptions, Matcher, MatcherOptions, MatcherStrategy, Mutator, MutatorOptions, OperationContext, OperationKey, OperationKeyOptions, OperationKeyPrefixOptions, OperationType, ProcedureUtilsOptions, RouterUtils, RouterUtilsOptions, OperationContext as SWROperationContext, Subscriber, SubscriberOptions };
|
package/dist/index.mjs
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { isPlainObject, resolveMaybeOptionalOptions, isAsyncIteratorObject, toArray, bindMethods, isTypescriptObject } from '@orpc/shared';
|
|
2
|
-
import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
3
|
-
|
|
4
|
-
function generateOperationKey(path, options = {}) {
|
|
5
|
-
return [
|
|
6
|
-
...options.prefix !== void 0 ? [options.prefix] : [],
|
|
7
|
-
path,
|
|
8
|
-
{
|
|
9
|
-
...options.input !== void 0 ? { input: options.input } : {}
|
|
10
|
-
}
|
|
11
|
-
];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function isSubsetOf(subsetKey, fullKey) {
|
|
15
|
-
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;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class SharedUtils {
|
|
19
|
-
constructor(path, options) {
|
|
20
|
-
this.path = path;
|
|
21
|
-
this.options = options;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
25
|
-
*
|
|
26
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#manual-revalidation | SWR Integration - Manual Revalidation}
|
|
27
|
-
*/
|
|
28
|
-
matcher(...rest) {
|
|
29
|
-
const { input, strategy = "partial" } = resolveMaybeOptionalOptions(rest);
|
|
30
|
-
const expectedKey = generateOperationKey(this.path, { prefix: this.options.prefix, input });
|
|
31
|
-
return (key) => {
|
|
32
|
-
if (!isSubsetOf(expectedKey, key)) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
if (strategy === "exact" && !isSubsetOf(key, expectedKey)) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
return true;
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const OPERATION_CONTEXT_SYMBOL = Symbol.for("ORPC_SWR_OPERATION_CONTEXT");
|
|
44
|
-
|
|
45
|
-
class ProcedureUtils extends SharedUtils {
|
|
46
|
-
/**
|
|
47
|
-
* Calling corresponding procedure client
|
|
48
|
-
*
|
|
49
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#calling-clients | SWR Integration - Calling Clients}
|
|
50
|
-
*/
|
|
51
|
-
call;
|
|
52
|
-
constructor(path, client, options = {}) {
|
|
53
|
-
super(path, options);
|
|
54
|
-
this.call = client;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Generate a **full matching** key for SWR operations.
|
|
58
|
-
*
|
|
59
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
60
|
-
*/
|
|
61
|
-
key(...rest) {
|
|
62
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
63
|
-
return generateOperationKey(this.path, { ...options, prefix: this.options.prefix });
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
67
|
-
*
|
|
68
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#data-fetching | SWR Integration - Data Fetching}
|
|
69
|
-
*/
|
|
70
|
-
fetcher(...rest) {
|
|
71
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
72
|
-
return async (key) => {
|
|
73
|
-
const { input } = key[key.length - 1];
|
|
74
|
-
return this.call(input, {
|
|
75
|
-
context: {
|
|
76
|
-
[OPERATION_CONTEXT_SYMBOL]: { key, type: "fetcher" },
|
|
77
|
-
...options.context
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Generate a subscriber function that subscribes to an [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) for use with useSWRSubscription, etc.
|
|
84
|
-
*
|
|
85
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
86
|
-
*/
|
|
87
|
-
subscriber(...rest) {
|
|
88
|
-
const { maxChunks, refetchMode = "reset", ...options } = resolveMaybeOptionalOptions(rest);
|
|
89
|
-
return (key, { next }) => {
|
|
90
|
-
const controller = new AbortController();
|
|
91
|
-
void (async () => {
|
|
92
|
-
try {
|
|
93
|
-
const { input } = key[key.length - 1];
|
|
94
|
-
const iterator = await this.call(input, {
|
|
95
|
-
context: {
|
|
96
|
-
[OPERATION_CONTEXT_SYMBOL]: { key, type: "subscriber" },
|
|
97
|
-
...options.context
|
|
98
|
-
},
|
|
99
|
-
signal: controller.signal
|
|
100
|
-
});
|
|
101
|
-
if (!isAsyncIteratorObject(iterator)) {
|
|
102
|
-
throw new TypeError(".subscriber requires an AsyncIteratorObject output");
|
|
103
|
-
}
|
|
104
|
-
if (controller.signal.aborted) {
|
|
105
|
-
await iterator.return?.();
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
let hasPreviousData = false;
|
|
109
|
-
if (refetchMode === "reset") {
|
|
110
|
-
next(void 0, void 0);
|
|
111
|
-
} else if (refetchMode === "replace") {
|
|
112
|
-
next(void 0, (old) => {
|
|
113
|
-
hasPreviousData = old !== void 0;
|
|
114
|
-
return old;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
const shouldUpdateDataDuringStream = refetchMode !== "replace" || !hasPreviousData;
|
|
118
|
-
let buffer = [];
|
|
119
|
-
for await (const event of iterator) {
|
|
120
|
-
if (controller.signal.aborted) {
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
if (shouldUpdateDataDuringStream) {
|
|
124
|
-
next(void 0, (old) => {
|
|
125
|
-
const newData = Array.isArray(old) ? [...old, event] : [event];
|
|
126
|
-
if (typeof maxChunks === "number" && newData.length > maxChunks) {
|
|
127
|
-
return newData.slice(newData.length - maxChunks);
|
|
128
|
-
}
|
|
129
|
-
return newData;
|
|
130
|
-
});
|
|
131
|
-
} else {
|
|
132
|
-
buffer = [...buffer, event];
|
|
133
|
-
if (typeof maxChunks === "number" && buffer.length > maxChunks) {
|
|
134
|
-
buffer = buffer.slice(buffer.length - maxChunks);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (!shouldUpdateDataDuringStream && !controller.signal.aborted) {
|
|
139
|
-
next(void 0, buffer);
|
|
140
|
-
}
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (!controller.signal.aborted) {
|
|
143
|
-
next(error);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
})();
|
|
147
|
-
return () => {
|
|
148
|
-
controller.abort();
|
|
149
|
-
};
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* 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.
|
|
154
|
-
*
|
|
155
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#subscriptions | SWR Integration - Subscriptions}
|
|
156
|
-
*/
|
|
157
|
-
liveSubscriber(...rest) {
|
|
158
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
159
|
-
return (key, { next }) => {
|
|
160
|
-
const controller = new AbortController();
|
|
161
|
-
void (async () => {
|
|
162
|
-
try {
|
|
163
|
-
const { input } = key[key.length - 1];
|
|
164
|
-
const iterator = await this.call(input, {
|
|
165
|
-
context: {
|
|
166
|
-
[OPERATION_CONTEXT_SYMBOL]: { key, type: "liveSubscriber" },
|
|
167
|
-
...options.context
|
|
168
|
-
},
|
|
169
|
-
signal: controller.signal
|
|
170
|
-
});
|
|
171
|
-
if (!isAsyncIteratorObject(iterator)) {
|
|
172
|
-
throw new TypeError(".liveSubscriber requires an AsyncIteratorObject output");
|
|
173
|
-
}
|
|
174
|
-
for await (const event of iterator) {
|
|
175
|
-
if (controller.signal.aborted) {
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
next(void 0, event);
|
|
179
|
-
}
|
|
180
|
-
} catch (error) {
|
|
181
|
-
if (!controller.signal.aborted) {
|
|
182
|
-
next(error);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
})();
|
|
186
|
-
return () => {
|
|
187
|
-
controller.abort();
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Generate a mutator function for use with useSWRMutation, etc.
|
|
193
|
-
*
|
|
194
|
-
* @see {@link https://orpc.dev/docs/integrations/swr#mutations | SWR Integration - Mutations}
|
|
195
|
-
*/
|
|
196
|
-
mutator(...rest) {
|
|
197
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
198
|
-
return (key, { arg }) => this.call(arg, {
|
|
199
|
-
context: {
|
|
200
|
-
[OPERATION_CONTEXT_SYMBOL]: { key, type: "mutator" },
|
|
201
|
-
...options.context
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function createRouterUtils(client, options = {}) {
|
|
208
|
-
const path = toArray(options.path);
|
|
209
|
-
const utils = typeof client === "function" ? bindMethods(new ProcedureUtils(path, client, { prefix: options.prefix }), { unbound: ["call"] }) : bindMethods(new SharedUtils(path, { prefix: options.prefix }));
|
|
210
|
-
const cache = /* @__PURE__ */ new Map();
|
|
211
|
-
const recursive = new Proxy(utils, {
|
|
212
|
-
get(target, prop) {
|
|
213
|
-
const value = Reflect.get(target, prop);
|
|
214
|
-
if (RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
215
|
-
return value ?? Reflect.get(Object.prototype, prop);
|
|
216
|
-
}
|
|
217
|
-
const nextClient = Reflect.get(client, prop);
|
|
218
|
-
if (typeof prop !== "string" || !isTypescriptObject(nextClient)) {
|
|
219
|
-
return value;
|
|
220
|
-
}
|
|
221
|
-
let result = cache.get(prop);
|
|
222
|
-
if (result === void 0) {
|
|
223
|
-
const nextUtils = createRouterUtils(nextClient, { ...options, path: [...path, prop] });
|
|
224
|
-
result = typeof value !== "function" ? nextUtils : new Proxy(value, {
|
|
225
|
-
get(target2, prop2) {
|
|
226
|
-
if (typeof prop2 !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop2)) {
|
|
227
|
-
return Reflect.get(target2, prop2);
|
|
228
|
-
}
|
|
229
|
-
return Reflect.get(nextUtils, prop2);
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
cache.set(prop, result);
|
|
233
|
-
}
|
|
234
|
-
return result;
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
return recursive;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export { OPERATION_CONTEXT_SYMBOL, ProcedureUtils, OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, SharedUtils, createRouterUtils, createRouterUtils as createSWRUtils };
|