@orpc/experimental-react-swr 0.0.0-next.ff7ad2e
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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/index.d.mts +133 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.mjs +171 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 oRPC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1></h1>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/experimental-react-swr">
|
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fexperimental-react-swr?logo=npm" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
|
+
</a>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
|
+
|
|
24
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Highlights
|
|
29
|
+
|
|
30
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
31
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
32
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
33
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
34
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
35
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
36
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
37
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
38
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
39
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
40
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
41
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
|
46
|
+
|
|
47
|
+
## Packages
|
|
48
|
+
|
|
49
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
50
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
51
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
53
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
54
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
55
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
57
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
58
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
60
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
61
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
62
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
63
|
+
|
|
64
|
+
## `@orpc/experimental-react-swr`
|
|
65
|
+
|
|
66
|
+
[SWR](https://swr.vercel.app/) integration for oRPC, providing a simple way to fetch data using oRPC endpoints with SWR's caching and revalidation features.
|
|
67
|
+
|
|
68
|
+
## Sponsors
|
|
69
|
+
|
|
70
|
+
<p align="center">
|
|
71
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
72
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
73
|
+
</a>
|
|
74
|
+
</p>
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { PartialDeep, MaybeOptionalOptions, InferAsyncIterableYield } from '@orpc/shared';
|
|
2
|
+
import { ClientContext, Client, NestedClient } from '@orpc/client';
|
|
3
|
+
import { SWRSubscriptionOptions } from 'swr/subscription';
|
|
4
|
+
|
|
5
|
+
declare const SWR_OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
6
|
+
interface SWROperationContext {
|
|
7
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: {
|
|
8
|
+
type: 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscriber';
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
type CreateKeyOptions<TInput> = undefined extends TInput ? {
|
|
12
|
+
input?: TInput;
|
|
13
|
+
} : {
|
|
14
|
+
input: TInput;
|
|
15
|
+
};
|
|
16
|
+
declare function resolveCreateKeyOptions<TInput>(options: CreateKeyOptions<TInput>): CreateKeyOptions<TInput> & {
|
|
17
|
+
input: TInput;
|
|
18
|
+
};
|
|
19
|
+
type KeyMeta<TInput> = {
|
|
20
|
+
input: TInput;
|
|
21
|
+
};
|
|
22
|
+
type Key<TInput> = [path: readonly string[], options: KeyMeta<TInput>];
|
|
23
|
+
type MatcherStrategy = 'exact' | 'partial';
|
|
24
|
+
type CreateMatcherOptions<TStrategy extends MatcherStrategy, TInput> = ('partial' extends TStrategy ? {
|
|
25
|
+
input?: PartialDeep<TInput>;
|
|
26
|
+
} : undefined extends TInput ? {
|
|
27
|
+
input?: TInput;
|
|
28
|
+
} : {
|
|
29
|
+
input: TInput;
|
|
30
|
+
}) & {
|
|
31
|
+
strategy?: TStrategy;
|
|
32
|
+
};
|
|
33
|
+
type Matcher = (key?: unknown) => boolean;
|
|
34
|
+
type CreateFetcherOptions<TClientContext extends ClientContext> = (Record<never, never> extends TClientContext ? {
|
|
35
|
+
context?: TClientContext;
|
|
36
|
+
} : {
|
|
37
|
+
context: TClientContext;
|
|
38
|
+
});
|
|
39
|
+
declare function resolveCreateFetcherOptions<T extends CreateFetcherOptions<any>>(options: T): T & {
|
|
40
|
+
context: Exclude<T['context'], undefined>;
|
|
41
|
+
};
|
|
42
|
+
type Fetcher<TInput, TOutput> = (key: Key<TInput>) => Promise<TOutput>;
|
|
43
|
+
type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
44
|
+
arg: TInput;
|
|
45
|
+
}>) => Promise<TOutput>;
|
|
46
|
+
type CreateSubscriberOptions<TClientContext extends ClientContext> = CreateFetcherOptions<TClientContext> & {
|
|
47
|
+
/**
|
|
48
|
+
* Determines how data is handled when the subscription refetches.
|
|
49
|
+
* - `reset`: Replace existing data with new data
|
|
50
|
+
* - `append`: Add new data to existing data
|
|
51
|
+
*
|
|
52
|
+
* @default 'reset'
|
|
53
|
+
*/
|
|
54
|
+
refetchMode?: 'reset' | 'append';
|
|
55
|
+
/**
|
|
56
|
+
* Maximum number of chunks to store.
|
|
57
|
+
* When exceeded, the oldest chunks will be removed.
|
|
58
|
+
*/
|
|
59
|
+
maxChunks?: number;
|
|
60
|
+
};
|
|
61
|
+
type Subscriber<TInput, TOutput, TError> = (key: Key<TInput>, options: SWRSubscriptionOptions<TOutput, TError>) => (() => void);
|
|
62
|
+
|
|
63
|
+
interface GeneralUtils<TInput> {
|
|
64
|
+
/**
|
|
65
|
+
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#manual-revalidation React SWR Manual Revalidation Docs}
|
|
68
|
+
*/
|
|
69
|
+
matcher<TStrategy extends MatcherStrategy>(...rest: MaybeOptionalOptions<CreateMatcherOptions<TStrategy, TInput>>): Matcher;
|
|
70
|
+
}
|
|
71
|
+
declare function createGeneralUtils<TInput>(path: readonly string[]): GeneralUtils<TInput>;
|
|
72
|
+
|
|
73
|
+
interface ProcedureUtilsOptions {
|
|
74
|
+
path: readonly string[];
|
|
75
|
+
}
|
|
76
|
+
interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
77
|
+
/**
|
|
78
|
+
* Calling corresponding procedure client
|
|
79
|
+
*
|
|
80
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#calling-clients React SWR Calling Procedure Client Docs}
|
|
81
|
+
*/
|
|
82
|
+
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
83
|
+
/**
|
|
84
|
+
* Generate a **full matching** key for SWR operations.
|
|
85
|
+
*
|
|
86
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#data-fetching React SWR Key Docs}
|
|
87
|
+
*/
|
|
88
|
+
key(...rest: MaybeOptionalOptions<CreateKeyOptions<TInput>>): Key<TInput>;
|
|
89
|
+
/**
|
|
90
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#data-fetching React SWR Data Fetching Docs}
|
|
93
|
+
*/
|
|
94
|
+
fetcher(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
95
|
+
/**
|
|
96
|
+
* Generate a subscriber function that subscribes to an [Event Iterator](https://orpc.unnoq.com/docs/event-iterator) for use with useSWRSubscription, etc.
|
|
97
|
+
*
|
|
98
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#subscriptions React SWR Subscriptions Docs}
|
|
99
|
+
*/
|
|
100
|
+
subscriber(...rest: MaybeOptionalOptions<CreateSubscriberOptions<TClientContext>>): Subscriber<TInput, InferAsyncIterableYield<TOutput>[], TError>;
|
|
101
|
+
/**
|
|
102
|
+
* Generate a live subscriber that subscribes to the latest events from an [Event Iterator](https://orpc.unnoq.com/docs/event-iterator) for use with useSWRSubscription, etc.
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#subscriptions React SWR Subscriptions Docs}
|
|
105
|
+
*/
|
|
106
|
+
liveSubscriber(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Subscriber<TInput, InferAsyncIterableYield<TOutput>, TError>;
|
|
107
|
+
/**
|
|
108
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
109
|
+
*
|
|
110
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#mutations React SWR Mutations Docs}
|
|
111
|
+
*/
|
|
112
|
+
mutator(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
113
|
+
}
|
|
114
|
+
declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: ProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
|
|
115
|
+
|
|
116
|
+
type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
|
|
117
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
|
|
118
|
+
} & GeneralUtils<unknown>;
|
|
119
|
+
interface CreateRouterUtilsOptions {
|
|
120
|
+
path?: readonly string[];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a swr router utils from a client.
|
|
124
|
+
*
|
|
125
|
+
* @info Both client-side and server-side clients are supported.
|
|
126
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr React SWR Integration}
|
|
127
|
+
*/
|
|
128
|
+
declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
|
|
129
|
+
|
|
130
|
+
declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean;
|
|
131
|
+
|
|
132
|
+
export { SWR_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createSWRUtils, isSubsetOf, resolveCreateFetcherOptions, resolveCreateKeyOptions };
|
|
133
|
+
export type { CreateFetcherOptions, CreateKeyOptions, CreateMatcherOptions, CreateRouterUtilsOptions, CreateSubscriberOptions, Fetcher, GeneralUtils, Key, KeyMeta, Matcher, MatcherStrategy, Mutator, ProcedureUtils, ProcedureUtilsOptions, RouterUtils, SWROperationContext, Subscriber };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { PartialDeep, MaybeOptionalOptions, InferAsyncIterableYield } from '@orpc/shared';
|
|
2
|
+
import { ClientContext, Client, NestedClient } from '@orpc/client';
|
|
3
|
+
import { SWRSubscriptionOptions } from 'swr/subscription';
|
|
4
|
+
|
|
5
|
+
declare const SWR_OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
6
|
+
interface SWROperationContext {
|
|
7
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: {
|
|
8
|
+
type: 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscriber';
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
type CreateKeyOptions<TInput> = undefined extends TInput ? {
|
|
12
|
+
input?: TInput;
|
|
13
|
+
} : {
|
|
14
|
+
input: TInput;
|
|
15
|
+
};
|
|
16
|
+
declare function resolveCreateKeyOptions<TInput>(options: CreateKeyOptions<TInput>): CreateKeyOptions<TInput> & {
|
|
17
|
+
input: TInput;
|
|
18
|
+
};
|
|
19
|
+
type KeyMeta<TInput> = {
|
|
20
|
+
input: TInput;
|
|
21
|
+
};
|
|
22
|
+
type Key<TInput> = [path: readonly string[], options: KeyMeta<TInput>];
|
|
23
|
+
type MatcherStrategy = 'exact' | 'partial';
|
|
24
|
+
type CreateMatcherOptions<TStrategy extends MatcherStrategy, TInput> = ('partial' extends TStrategy ? {
|
|
25
|
+
input?: PartialDeep<TInput>;
|
|
26
|
+
} : undefined extends TInput ? {
|
|
27
|
+
input?: TInput;
|
|
28
|
+
} : {
|
|
29
|
+
input: TInput;
|
|
30
|
+
}) & {
|
|
31
|
+
strategy?: TStrategy;
|
|
32
|
+
};
|
|
33
|
+
type Matcher = (key?: unknown) => boolean;
|
|
34
|
+
type CreateFetcherOptions<TClientContext extends ClientContext> = (Record<never, never> extends TClientContext ? {
|
|
35
|
+
context?: TClientContext;
|
|
36
|
+
} : {
|
|
37
|
+
context: TClientContext;
|
|
38
|
+
});
|
|
39
|
+
declare function resolveCreateFetcherOptions<T extends CreateFetcherOptions<any>>(options: T): T & {
|
|
40
|
+
context: Exclude<T['context'], undefined>;
|
|
41
|
+
};
|
|
42
|
+
type Fetcher<TInput, TOutput> = (key: Key<TInput>) => Promise<TOutput>;
|
|
43
|
+
type Mutator<TInput, TOutput> = (key: unknown, options: Readonly<{
|
|
44
|
+
arg: TInput;
|
|
45
|
+
}>) => Promise<TOutput>;
|
|
46
|
+
type CreateSubscriberOptions<TClientContext extends ClientContext> = CreateFetcherOptions<TClientContext> & {
|
|
47
|
+
/**
|
|
48
|
+
* Determines how data is handled when the subscription refetches.
|
|
49
|
+
* - `reset`: Replace existing data with new data
|
|
50
|
+
* - `append`: Add new data to existing data
|
|
51
|
+
*
|
|
52
|
+
* @default 'reset'
|
|
53
|
+
*/
|
|
54
|
+
refetchMode?: 'reset' | 'append';
|
|
55
|
+
/**
|
|
56
|
+
* Maximum number of chunks to store.
|
|
57
|
+
* When exceeded, the oldest chunks will be removed.
|
|
58
|
+
*/
|
|
59
|
+
maxChunks?: number;
|
|
60
|
+
};
|
|
61
|
+
type Subscriber<TInput, TOutput, TError> = (key: Key<TInput>, options: SWRSubscriptionOptions<TOutput, TError>) => (() => void);
|
|
62
|
+
|
|
63
|
+
interface GeneralUtils<TInput> {
|
|
64
|
+
/**
|
|
65
|
+
* Generate a matcher function that returns `true` if the key matches the specified conditions.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#manual-revalidation React SWR Manual Revalidation Docs}
|
|
68
|
+
*/
|
|
69
|
+
matcher<TStrategy extends MatcherStrategy>(...rest: MaybeOptionalOptions<CreateMatcherOptions<TStrategy, TInput>>): Matcher;
|
|
70
|
+
}
|
|
71
|
+
declare function createGeneralUtils<TInput>(path: readonly string[]): GeneralUtils<TInput>;
|
|
72
|
+
|
|
73
|
+
interface ProcedureUtilsOptions {
|
|
74
|
+
path: readonly string[];
|
|
75
|
+
}
|
|
76
|
+
interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
77
|
+
/**
|
|
78
|
+
* Calling corresponding procedure client
|
|
79
|
+
*
|
|
80
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#calling-clients React SWR Calling Procedure Client Docs}
|
|
81
|
+
*/
|
|
82
|
+
call: Client<TClientContext, TInput, TOutput, TError>;
|
|
83
|
+
/**
|
|
84
|
+
* Generate a **full matching** key for SWR operations.
|
|
85
|
+
*
|
|
86
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#data-fetching React SWR Key Docs}
|
|
87
|
+
*/
|
|
88
|
+
key(...rest: MaybeOptionalOptions<CreateKeyOptions<TInput>>): Key<TInput>;
|
|
89
|
+
/**
|
|
90
|
+
* Generate a fetcher function for use with useSWR, useSWRInfinite, and other SWR hooks.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#data-fetching React SWR Data Fetching Docs}
|
|
93
|
+
*/
|
|
94
|
+
fetcher(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Fetcher<TInput, TOutput>;
|
|
95
|
+
/**
|
|
96
|
+
* Generate a subscriber function that subscribes to an [Event Iterator](https://orpc.unnoq.com/docs/event-iterator) for use with useSWRSubscription, etc.
|
|
97
|
+
*
|
|
98
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#subscriptions React SWR Subscriptions Docs}
|
|
99
|
+
*/
|
|
100
|
+
subscriber(...rest: MaybeOptionalOptions<CreateSubscriberOptions<TClientContext>>): Subscriber<TInput, InferAsyncIterableYield<TOutput>[], TError>;
|
|
101
|
+
/**
|
|
102
|
+
* Generate a live subscriber that subscribes to the latest events from an [Event Iterator](https://orpc.unnoq.com/docs/event-iterator) for use with useSWRSubscription, etc.
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#subscriptions React SWR Subscriptions Docs}
|
|
105
|
+
*/
|
|
106
|
+
liveSubscriber(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Subscriber<TInput, InferAsyncIterableYield<TOutput>, TError>;
|
|
107
|
+
/**
|
|
108
|
+
* Generate a mutator function for use with useSWRMutation, etc.
|
|
109
|
+
*
|
|
110
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr#mutations React SWR Mutations Docs}
|
|
111
|
+
*/
|
|
112
|
+
mutator(...rest: MaybeOptionalOptions<CreateFetcherOptions<TClientContext>>): Mutator<TInput, TOutput>;
|
|
113
|
+
}
|
|
114
|
+
declare function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError>(client: Client<TClientContext, TInput, TOutput, TError>, options: ProcedureUtilsOptions): ProcedureUtils<TClientContext, TInput, TOutput, TError>;
|
|
115
|
+
|
|
116
|
+
type RouterUtils<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ProcedureUtils<UClientContext, UInput, UOutput, UError> & GeneralUtils<UInput> : {
|
|
117
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? RouterUtils<T[K]> : never;
|
|
118
|
+
} & GeneralUtils<unknown>;
|
|
119
|
+
interface CreateRouterUtilsOptions {
|
|
120
|
+
path?: readonly string[];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a swr router utils from a client.
|
|
124
|
+
*
|
|
125
|
+
* @info Both client-side and server-side clients are supported.
|
|
126
|
+
* @see {@link https://orpc.unnoq.com/docs/integrations/react-swr React SWR Integration}
|
|
127
|
+
*/
|
|
128
|
+
declare function createRouterUtils<T extends NestedClient<any>>(client: T, options?: CreateRouterUtilsOptions): RouterUtils<T>;
|
|
129
|
+
|
|
130
|
+
declare function isSubsetOf(subsetKey: unknown, fullKey: unknown): boolean;
|
|
131
|
+
|
|
132
|
+
export { SWR_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createSWRUtils, isSubsetOf, resolveCreateFetcherOptions, resolveCreateKeyOptions };
|
|
133
|
+
export type { CreateFetcherOptions, CreateKeyOptions, CreateMatcherOptions, CreateRouterUtilsOptions, CreateSubscriberOptions, Fetcher, GeneralUtils, Key, KeyMeta, Matcher, MatcherStrategy, Mutator, ProcedureUtils, ProcedureUtilsOptions, RouterUtils, SWROperationContext, Subscriber };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { isObject, resolveMaybeOptionalOptions, isAsyncIteratorObject, toArray } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
function isSubsetOf(subsetKey, fullKey) {
|
|
4
|
+
return subsetKey === fullKey ? true : typeof subsetKey !== typeof fullKey ? false : isObject(subsetKey) && isObject(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])) : false;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function createGeneralUtils(path) {
|
|
8
|
+
return {
|
|
9
|
+
matcher(...rest) {
|
|
10
|
+
const { input, strategy = "partial" } = resolveMaybeOptionalOptions(rest);
|
|
11
|
+
return (key) => {
|
|
12
|
+
const expectedKey = [path, { input }];
|
|
13
|
+
if (!isSubsetOf(expectedKey, key)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (strategy === "exact" && !isSubsetOf(key, expectedKey)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const SWR_OPERATION_CONTEXT_SYMBOL = Symbol("ORPC_SWR_OPERATION_CONTEXT");
|
|
26
|
+
function resolveCreateKeyOptions(options) {
|
|
27
|
+
return {
|
|
28
|
+
...options,
|
|
29
|
+
/**
|
|
30
|
+
* Input is optional when it can be undefined,
|
|
31
|
+
* so we can safely cast it to TInput with an undefined fallback.
|
|
32
|
+
*/
|
|
33
|
+
input: options.input ?? void 0
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function resolveCreateFetcherOptions(options) {
|
|
37
|
+
return {
|
|
38
|
+
...options,
|
|
39
|
+
/**
|
|
40
|
+
* Context is optional when all fields in TClientContext are optional,
|
|
41
|
+
* so we can safely cast it to TClientContext with an empty object fallback.
|
|
42
|
+
*/
|
|
43
|
+
context: options.context ?? {}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function createProcedureUtils(client, options) {
|
|
48
|
+
return {
|
|
49
|
+
call: client,
|
|
50
|
+
key(...rest) {
|
|
51
|
+
const { input } = resolveCreateKeyOptions(resolveMaybeOptionalOptions(rest));
|
|
52
|
+
return [options.path, { input }];
|
|
53
|
+
},
|
|
54
|
+
fetcher(...rest) {
|
|
55
|
+
const { context } = resolveCreateFetcherOptions(resolveMaybeOptionalOptions(rest));
|
|
56
|
+
return async ([, { input }]) => client(input, {
|
|
57
|
+
context: {
|
|
58
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: { type: "fetcher" },
|
|
59
|
+
...context
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
subscriber(...rest) {
|
|
64
|
+
const { context, maxChunks, refetchMode = "reset" } = resolveCreateFetcherOptions(resolveMaybeOptionalOptions(rest));
|
|
65
|
+
return ([, { input }], { next }) => {
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
void (async () => {
|
|
68
|
+
try {
|
|
69
|
+
const iterator = await client(input, {
|
|
70
|
+
context: {
|
|
71
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: { type: "subscriber" },
|
|
72
|
+
...context
|
|
73
|
+
},
|
|
74
|
+
signal: controller.signal
|
|
75
|
+
});
|
|
76
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
77
|
+
throw new Error(".subscriber requires an event iterator output");
|
|
78
|
+
}
|
|
79
|
+
if (refetchMode === "reset") {
|
|
80
|
+
next(void 0, void 0);
|
|
81
|
+
}
|
|
82
|
+
for await (const event of iterator) {
|
|
83
|
+
next(void 0, (old) => {
|
|
84
|
+
const newData = Array.isArray(old) ? [...old, event] : [event];
|
|
85
|
+
if (typeof maxChunks === "number" && newData.length > maxChunks) {
|
|
86
|
+
return newData.slice(newData.length - maxChunks);
|
|
87
|
+
}
|
|
88
|
+
return newData;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (!controller.signal.aborted) {
|
|
93
|
+
next(error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return () => {
|
|
98
|
+
controller.abort();
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
liveSubscriber(...rest) {
|
|
103
|
+
const { context } = resolveCreateFetcherOptions(resolveMaybeOptionalOptions(rest));
|
|
104
|
+
return ([, { input }], { next }) => {
|
|
105
|
+
const controller = new AbortController();
|
|
106
|
+
void (async () => {
|
|
107
|
+
try {
|
|
108
|
+
const iterator = await client(input, {
|
|
109
|
+
context: {
|
|
110
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: { type: "liveSubscriber" },
|
|
111
|
+
...context
|
|
112
|
+
},
|
|
113
|
+
signal: controller.signal
|
|
114
|
+
});
|
|
115
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
116
|
+
throw new Error(".liveSubscriber requires an event iterator output");
|
|
117
|
+
}
|
|
118
|
+
for await (const event of iterator) {
|
|
119
|
+
next(void 0, event);
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (!controller.signal.aborted) {
|
|
123
|
+
next(error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
})();
|
|
127
|
+
return () => {
|
|
128
|
+
controller.abort();
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
mutator(...rest) {
|
|
133
|
+
const { context } = resolveCreateFetcherOptions(resolveMaybeOptionalOptions(rest));
|
|
134
|
+
return (_key, { arg }) => client(arg, {
|
|
135
|
+
context: {
|
|
136
|
+
[SWR_OPERATION_CONTEXT_SYMBOL]: { type: "mutator" },
|
|
137
|
+
...context
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function createRouterUtils(client, options = {}) {
|
|
145
|
+
const path = toArray(options.path);
|
|
146
|
+
const generalUtils = createGeneralUtils(path);
|
|
147
|
+
const procedureUtils = createProcedureUtils(client, { path });
|
|
148
|
+
const recursive = new Proxy({
|
|
149
|
+
...generalUtils,
|
|
150
|
+
...procedureUtils
|
|
151
|
+
}, {
|
|
152
|
+
get(target, prop) {
|
|
153
|
+
const value = Reflect.get(target, prop);
|
|
154
|
+
if (typeof prop !== "string") {
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
const nextUtils = createRouterUtils(client[prop], { ...options, path: [...path, prop] });
|
|
158
|
+
if (typeof value !== "function") {
|
|
159
|
+
return nextUtils;
|
|
160
|
+
}
|
|
161
|
+
return new Proxy(value, {
|
|
162
|
+
get(_, prop2) {
|
|
163
|
+
return Reflect.get(nextUtils, prop2);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
return recursive;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { SWR_OPERATION_CONTEXT_SYMBOL, createGeneralUtils, createProcedureUtils, createRouterUtils, createRouterUtils as createSWRUtils, isSubsetOf, resolveCreateFetcherOptions, resolveCreateKeyOptions };
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orpc/experimental-react-swr",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0-next.ff7ad2e",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://orpc.unnoq.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/unnoq/orpc.git",
|
|
10
|
+
"directory": "packages/react-swr"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"unnoq",
|
|
14
|
+
"orpc"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"swr": ">=2.3.6",
|
|
28
|
+
"@orpc/client": "0.0.0-next.ff7ad2e"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@orpc/shared": "0.0.0-next.ff7ad2e"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"swr": "^2.3.6"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "unbuild",
|
|
38
|
+
"build:watch": "pnpm run build --watch",
|
|
39
|
+
"type:check": "tsc -b"
|
|
40
|
+
}
|
|
41
|
+
}
|