@tiesen/effect-tanstack-query 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +2 -3
- package/dist/index.mjs +1 -1
- package/dist/react.d.mts +2 -4
- package/dist/react.mjs +1 -1
- package/dist/types-A-WfeH_j.d.mts +110 -0
- package/package.json +1 -6
- package/dist/types-MoWlEtTb.d.mts +0 -59
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { r as TanstackQueryOptionsProxy } from "./types-
|
|
1
|
+
import { r as TanstackQueryOptionsProxy } from "./types-A-WfeH_j.mjs";
|
|
2
2
|
import { Service } from "effect/Context";
|
|
3
3
|
import { ManagedRuntime } from "effect/ManagedRuntime";
|
|
4
|
-
import { NodeInspectSymbol } from "effect/Inspectable";
|
|
5
4
|
//#region src/create-proxy.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* Creates a type-safe proxy that bridges Effect `HttpApiClient` endpoints
|
|
@@ -44,4 +43,4 @@ import { NodeInspectSymbol } from "effect/Inspectable";
|
|
|
44
43
|
*/
|
|
45
44
|
declare function createTanstackQueryOptionsProxy<TServiceTag, TService>(tag: Service<TServiceTag, TService>, runtime: ManagedRuntime<TServiceTag, never>): TanstackQueryOptionsProxy<TService>;
|
|
46
45
|
//#endregion
|
|
47
|
-
export { type
|
|
46
|
+
export { type TanstackQueryOptionsProxy, createTanstackQueryOptionsProxy };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"effect/Effect";import*as t from"effect/Fiber";import*as n from"effect/Schedule";import*as r from"effect/Stream";function i(i,a){let o=new Map,s=c=>{let l=c.join(`.`);if(o.has(l))return o.get(l);let u=new Proxy(()=>{},{get(e,t){if(typeof t!=`symbol`&&t!==`then`)return s([...c,String(t)])},apply(o,s,l){let u=c.at(-1),d=c.slice(0,-1),[f,p]=l,m=e.fn(function*(e){let t=yield*i;for(let e of d)t=t[e];return yield*t(e)}),h=(e,t)=>a.runPromise(m(e),{signal:t}),g=(e,t)=>[{type:e},...d,...t?[t]:[]];if(u===`queryOptions`)return{...p,queryKey:g(`query`,f),queryFn:({signal:e})=>h(f,e)};if(u===`getQueryKey`)return g(`query`,f);if(u===`mutationOptions`)return{...p,mutationKey:g(`mutation`,f),mutationFn:e=>h({payload:e})};if(u===`subscriptionOptions`)
|
|
1
|
+
import*as e from"effect/Effect";import*as t from"effect/Fiber";import*as n from"effect/Schedule";import*as r from"effect/Stream";function i(i,a){let o=new Map,s=c=>{let l=c.join(`.`);if(o.has(l))return o.get(l);let u=new Proxy(()=>{},{get(e,t){if(typeof t!=`symbol`&&t!==`then`)return s([...c,String(t)])},apply(o,s,l){let u=c.at(-1),d=c.slice(0,-1),[f,p]=l,m=e.fn(function*(e){let t=yield*i;for(let e of d)t=t[e];return yield*t(e)}),h=(e,t)=>a.runPromise(m(e),{signal:t}),g=(e,t)=>[{type:e},...d,...t?[t]:[]];if(u===`queryOptions`)return{...p,queryKey:g(`query`,f),queryFn:({signal:e})=>h(f,e)};if(u===`getQueryKey`)return g(`query`,f);if(u===`mutationOptions`)return{...p,mutationKey:g(`mutation`,f),mutationFn:e=>h({payload:e})};if(u===`subscriptionOptions`)return{...p,enabled:p?.enabled??!0,subcriptionKey:g(`subscription`,f),subscriptionFn:({autoReconnect:i,keepAlive:o,...s})=>{let{onStarted:c,onData:l,onError:u,onConnectionChange:d}=s,p=n.max([n.exponential(i??0),n.spaced(`30 seconds`),n.recurs(3)]).pipe(n.jittered),h=e.gen(function*(){d?.({status:`connecting`}),c?.();let t=yield*m({...f,responseMode:`response-only`});d?.({status:`pending`});let n=yield*t.stream.pipe(r.decodeText,r.splitLines,r.filter(e=>e.length>0),r.share({capacity:`unbounded`})),a=n.pipe(r.filter(e=>e.startsWith(o?.message??`:keep-alive`)),r.timeout(o?.timeout??`10 seconds`)),s=yield*n.pipe(r.filter(e=>e.startsWith(`data:`)),r.map(e=>e.slice(5).trim()),r.filter(e=>e.length>0),r.tap(t=>e.sync(()=>l?.(t))),r.share({capacity:`unbounded`})),u=r.merge(a,s);yield*r.runDrain(u),i&&(yield*e.fail(`restart`))}).pipe(e.tapError(t=>t===`restart`||typeof t==`object`&&t._tag===`HttpClientError`&&t.reason?._tag===`DecodeError`?e.void:e.sync(()=>u?.(t)))),g=(i?h.pipe(e.retry(p)):h).pipe(e.ensuring(e.sync(()=>d?.({status:`idle`}))),e.scoped),_=a.runFork(g);return()=>a.runPromise(t.interrupt(_))}}}});return o.set(l,u),u};return s([])}export{i as createTanstackQueryOptionsProxy};
|
package/dist/react.d.mts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { n as SubscriptionReturns, t as SubscriptionOptions } from "./types-
|
|
1
|
+
import { n as SubscriptionReturns, t as SubscriptionOptions } from "./types-A-WfeH_j.mjs";
|
|
2
2
|
//#region src/react.d.ts
|
|
3
|
-
declare function useSubscription<TData, TError>(
|
|
4
|
-
enabled?: boolean;
|
|
5
|
-
}): SubscriptionReturns;
|
|
3
|
+
declare function useSubscription<TData, TError>(opts: SubscriptionOptions<TData, TError>): SubscriptionReturns<TData, TError>;
|
|
6
4
|
//#endregion
|
|
7
5
|
export { type SubscriptionOptions, type SubscriptionReturns, useSubscription };
|
package/dist/react.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{hashKey as e}from"@tanstack/query-core";import*as t from"react";function n(n){let i=t.useRef(n);i.current=n;let a=t.useRef(new Set),o=t.useCallback(e=>{a.current.add(e)},[]),s=t.useRef(()=>{}),c=t.useCallback(()=>{if(s.current(),p(l),!n.enabled)return;let e=n.subscriptionFn({autoReconnect:i.current.autoReconnect,keepAlive:i.current.keepAlive,onStarted:()=>{i.current.onStarted?.(),p(e=>({...e,status:`pending`,error:null}))},onData:e=>{i.current.onData?.(e),p(t=>({...t,status:`pending`,data:e,error:null}))},onError:e=>{i.current.onError?.(e),p(t=>({...t,status:`error`,error:e}))},onConnectionChange:e=>{i.current.onConnectionChange?.(e),p(t=>{switch(e.status){case`connecting`:return{...t,status:`connecting`,error:e.error??null};case`pending`:return t;case`idle`:return{...t,status:`idle`,data:null,error:null};default:return t}})}});s.current=()=>e()},[e(n.subcriptionKey),n.enabled]),l=t.useCallback(()=>n.enabled?{data:null,error:null,status:`connecting`,reset:c}:{data:null,error:null,status:`idle`,reset:c},[n.enabled,c]),u=t.useRef(l()),[d,f]=t.useState(r(u,o));d.reset=c;let p=t.useCallback(e=>{let t=u.current,n=u.current=e(t),i=!1;for(let e of a.current)if(t[e]!==n[e]){i=!0;break}i&&f(r(u,o))},[o]);return t.useEffect(()=>{if(n.enabled)return c(),()=>s.current()},[e(n.subcriptionKey),n.enabled]),d}function r(e,t){return new Proxy(e.current,{get(n,r){return t(r),e.current[r]}})}export{n as useSubscription};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { DefaultError, MutationOptions, QueryKey, QueryObserverOptions } from "@tanstack/query-core";
|
|
2
|
+
import { Input } from "effect/Duration";
|
|
3
|
+
import { Schema } from "effect/Schema";
|
|
4
|
+
import { Client } from "effect/unstable/httpapi/HttpApiClient";
|
|
5
|
+
import { HttpApiEndpoint } from "effect/unstable/httpapi/HttpApiEndpoint";
|
|
6
|
+
import { SseEventFromData, StreamSse } from "effect/unstable/httpapi/HttpApiSchema";
|
|
7
|
+
//#region src/types.d.ts
|
|
8
|
+
type TanstackQueryOptionsProxy<T> = T extends Client.Method<HttpApiEndpoint<infer _Identifier, infer Method, infer _Path, infer Params, infer Query, infer Payload, infer Headers, infer Success, infer Error, infer _Middleware, infer _MiddlewareService>, infer _Error, infer _Requires> ? {
|
|
9
|
+
queryOptions: Method extends 'GET' ? <TQuery = QueryOptions<UnwrapCodec<Success>, UnwrapCodec<Error>>>(input: MakeOptionalInput<([Params] extends [never] ? unknown : {
|
|
10
|
+
params: UnwrapCodec<Params>;
|
|
11
|
+
}) & ([Query] extends [never] ? unknown : {
|
|
12
|
+
query: UnwrapCodec<Query>;
|
|
13
|
+
}) & ([Headers] extends [never] ? unknown : {
|
|
14
|
+
headers: UnwrapCodec<Headers>;
|
|
15
|
+
})>, options?: Omit<TQuery, 'queryKey' | 'queryFn'>) => TQuery : never;
|
|
16
|
+
subscriptionOptions: Success extends StreamSse<SseEventFromData<infer StreamSuccess>, infer StreamError, infer _StreamValue> ? (input: MakeOptionalInput<([Params] extends [never] ? unknown : {
|
|
17
|
+
params: UnwrapCodec<Params>;
|
|
18
|
+
}) & ([Query] extends [never] ? unknown : {
|
|
19
|
+
query: UnwrapCodec<Query>;
|
|
20
|
+
}) & ([Headers] extends [never] ? unknown : {
|
|
21
|
+
headers: UnwrapCodec<Headers>;
|
|
22
|
+
})>, options?: Omit<SubscriptionOptions<UnwrapCodec<StreamSuccess>, UnwrapCodec<StreamError>>, 'subcriptionKey' | 'subscriptionFn'>) => SubscriptionOptions<UnwrapCodec<StreamSuccess>, UnwrapCodec<StreamError>> : never;
|
|
23
|
+
mutationOptions: Method extends 'GET' ? never : <TMutation = MutationOptions<UnwrapCodec<Success>, UnwrapCodec<Error>, UnwrapCodec<Payload>>>(input: MakeOptionalInput<([Params] extends [never] ? unknown : {
|
|
24
|
+
params: UnwrapCodec<Params>;
|
|
25
|
+
}) & ([Headers] extends [never] ? unknown : {
|
|
26
|
+
headers: UnwrapCodec<Headers>;
|
|
27
|
+
})>, options?: Omit<TMutation, 'mutationKey' | 'mutationFn'>) => TMutation;
|
|
28
|
+
getQueryKey: Method extends 'GET' ? (input?: Partial<MakeOptionalInput<([Params] extends [never] ? unknown : {
|
|
29
|
+
params: UnwrapCodec<Params>;
|
|
30
|
+
}) & ([Query] extends [never] ? unknown : {
|
|
31
|
+
query: UnwrapCodec<Query>;
|
|
32
|
+
}) & ([Headers] extends [never] ? unknown : {
|
|
33
|
+
headers: UnwrapCodec<Headers>;
|
|
34
|
+
})>>) => QueryKey : never;
|
|
35
|
+
} : T extends object ? { readonly [K in keyof T as K extends symbol ? never : K]: TanstackQueryOptionsProxy<T[K]>; } : T;
|
|
36
|
+
interface SubscriptionOptions<TData, TError> {
|
|
37
|
+
/**
|
|
38
|
+
* Determines whether the subscription is active. If set to `false`, the subscription will not be initiated.
|
|
39
|
+
* Default is `true`.
|
|
40
|
+
*/
|
|
41
|
+
enabled?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for the heartbeat keep-alive mechanism to prevent connection timeouts.
|
|
44
|
+
*
|
|
45
|
+
* @property{string} message The string payload sent by the server to maintain the connection. Default is `":keep-alive"`.
|
|
46
|
+
* @property{Input} timeout The maximum duration to wait for a heartbeat or data payload before timing out and reconnecting. Default is `10 seconds`.
|
|
47
|
+
*/
|
|
48
|
+
keepAlive?: {
|
|
49
|
+
message?: string;
|
|
50
|
+
timeout?: Input;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Defines the retry schedule or delay duration before attempting to reconnect when the stream disconnects or fails.
|
|
54
|
+
* Accepts an `Effect.Duration.Input` (e.g., `"3 seconds"`, `3000`, or `Duration.seconds(3)`).
|
|
55
|
+
*/
|
|
56
|
+
autoReconnect?: Input;
|
|
57
|
+
/**
|
|
58
|
+
* A unique key that identifies the subscription. This key is used for caching and managing the subscription state.
|
|
59
|
+
*/
|
|
60
|
+
subcriptionKey: readonly unknown[];
|
|
61
|
+
/**
|
|
62
|
+
* Initiates the subscription.
|
|
63
|
+
*
|
|
64
|
+
* @param options.signal An `AbortSignal` used to cancel the active subscription.
|
|
65
|
+
* @param options.keepAliveTimeout Maximum duration to wait for data or server `:keep-alive` events before timing out. Default is 10 seconds.
|
|
66
|
+
*/
|
|
67
|
+
subscriptionFn: (options: Omit<SubscriptionOptions<TData, TError>, 'enabled' | 'subcriptionKey' | 'subscriptionFn'>) => () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Callback invoked when the subscription is started.
|
|
70
|
+
*/
|
|
71
|
+
onStarted?: () => void;
|
|
72
|
+
/**
|
|
73
|
+
* Callback invoked when new data is received from the subscription.
|
|
74
|
+
*/
|
|
75
|
+
onData?: (data: TData) => void;
|
|
76
|
+
/**
|
|
77
|
+
* Callback invoked when an error occurs during the subscription.
|
|
78
|
+
*/
|
|
79
|
+
onError?: (error: TError) => void;
|
|
80
|
+
/**
|
|
81
|
+
* Callback invoked when the connection's status changes
|
|
82
|
+
*/
|
|
83
|
+
onConnectionChange?: (result: Partial<SubscriptionReturns<TData, TError>>) => void;
|
|
84
|
+
}
|
|
85
|
+
type QueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> & {
|
|
86
|
+
subscribed?: boolean;
|
|
87
|
+
};
|
|
88
|
+
type SubscriptionReturns<TData, TError> = {
|
|
89
|
+
reset: () => void;
|
|
90
|
+
} & ({
|
|
91
|
+
status: 'idle';
|
|
92
|
+
data: null;
|
|
93
|
+
error: null;
|
|
94
|
+
} | {
|
|
95
|
+
status: 'connecting';
|
|
96
|
+
data: TData | null;
|
|
97
|
+
error: TError | null;
|
|
98
|
+
} | {
|
|
99
|
+
status: 'pending';
|
|
100
|
+
data: TData | null;
|
|
101
|
+
error: null;
|
|
102
|
+
} | {
|
|
103
|
+
status: 'error';
|
|
104
|
+
data: TData | null;
|
|
105
|
+
error: TError;
|
|
106
|
+
});
|
|
107
|
+
type UnwrapCodec<T> = T extends Schema<unknown> ? Schema.Type<T> : T extends object ? { [K in keyof T]: UnwrapCodec<T[K]>; } : T;
|
|
108
|
+
type MakeOptionalInput<T> = keyof T extends never ? void | undefined : T;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { SubscriptionReturns as n, TanstackQueryOptionsProxy as r, SubscriptionOptions as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiesen/effect-tanstack-query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Effortlessly bridge Effect HTTP API clients with TanStack Query. Enjoy end-to-end type safety, zero-boilerplate options, and built-in streaming support out of the box.",
|
|
6
6
|
"keywords": [
|
|
@@ -67,11 +67,6 @@
|
|
|
67
67
|
"effect": ">=4.0.0-beta.100 || >=4.0.0",
|
|
68
68
|
"react": ">=19.2.8"
|
|
69
69
|
},
|
|
70
|
-
"peerDependenciesMeta": {
|
|
71
|
-
"react": {
|
|
72
|
-
"optional": true
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
70
|
"packageManager": "bun@1.3.0",
|
|
76
71
|
"patchedDependencies": {
|
|
77
72
|
"tegami@1.3.1": "patches/tegami@1.3.1.patch"
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { DefaultError, MutationOptions, QueryKey, QueryObserverOptions } from "@tanstack/query-core";
|
|
2
|
-
import { Input } from "effect/Duration";
|
|
3
|
-
import { Schema } from "effect/Schema";
|
|
4
|
-
import { Client } from "effect/unstable/httpapi/HttpApiClient";
|
|
5
|
-
import { HttpApiEndpoint } from "effect/unstable/httpapi/HttpApiEndpoint";
|
|
6
|
-
import { SseEventFromData, StreamSse } from "effect/unstable/httpapi/HttpApiSchema";
|
|
7
|
-
//#region src/types.d.ts
|
|
8
|
-
type TanstackQueryOptionsProxy<T> = T extends Client.Method<HttpApiEndpoint<infer _Key, infer Method, infer _Path, infer Params, infer Query, infer Payload, infer Headers, infer Success, infer Error, infer _Middleware>, infer _Error, infer _Requires> ? {
|
|
9
|
-
queryOptions: Method extends 'GET' ? <TQuery = QueryOptions<UnwrapCodec<Success>, UnwrapCodec<Error>>>(input: MakeOptionalInput<([Params] extends [never] ? {} : {
|
|
10
|
-
params: UnwrapCodec<Params>;
|
|
11
|
-
}) & ([Query] extends [never] ? {} : {
|
|
12
|
-
query: UnwrapCodec<Query>;
|
|
13
|
-
}) & ([Headers] extends [never] ? {} : {
|
|
14
|
-
headers: UnwrapCodec<Headers>;
|
|
15
|
-
})>, options?: Omit<TQuery, 'queryKey' | 'queryFn'>) => TQuery : never;
|
|
16
|
-
subscriptionOptions: Success extends StreamSse<SseEventFromData<infer StreamSuccess>, infer StreamError, infer _StreamValue> ? (input: MakeOptionalInput<([Params] extends [never] ? {} : {
|
|
17
|
-
params: UnwrapCodec<Params>;
|
|
18
|
-
}) & ([Query] extends [never] ? {} : {
|
|
19
|
-
query: UnwrapCodec<Query>;
|
|
20
|
-
}) & ([Headers] extends [never] ? {} : {
|
|
21
|
-
headers: UnwrapCodec<Headers>;
|
|
22
|
-
})>, options?: {
|
|
23
|
-
onData: (data: UnwrapCodec<StreamSuccess>) => void;
|
|
24
|
-
onError?: (error: UnwrapCodec<StreamError>) => void;
|
|
25
|
-
}) => SubscriptionOptions<UnwrapCodec<StreamSuccess>, UnwrapCodec<StreamError>> : never;
|
|
26
|
-
mutationOptions: Method extends 'GET' ? never : <TMutation = MutationOptions<UnwrapCodec<Success>, UnwrapCodec<Error>, UnwrapCodec<Payload>>>(input: MakeOptionalInput<([Params] extends [never] ? {} : {
|
|
27
|
-
params: UnwrapCodec<Params>;
|
|
28
|
-
}) & ([Headers] extends [never] ? {} : {
|
|
29
|
-
headers: UnwrapCodec<Headers>;
|
|
30
|
-
})>, options?: Omit<TMutation, 'mutationKey' | 'mutationFn'>) => TMutation;
|
|
31
|
-
getQueryKey: Method extends 'GET' ? (input?: Partial<MakeOptionalInput<([Params] extends [never] ? {} : {
|
|
32
|
-
params: UnwrapCodec<Params>;
|
|
33
|
-
}) & ([Query] extends [never] ? {} : {
|
|
34
|
-
query: UnwrapCodec<Query>;
|
|
35
|
-
}) & ([Headers] extends [never] ? {} : {
|
|
36
|
-
headers: UnwrapCodec<Headers>;
|
|
37
|
-
})>>) => readonly unknown[] : never;
|
|
38
|
-
} : T extends object ? { readonly [K in keyof T]: TanstackQueryOptionsProxy<T[K]>; } : T;
|
|
39
|
-
type UnsubscribeFn = () => void;
|
|
40
|
-
interface SubscriptionOptions<TData, TError> {
|
|
41
|
-
subcriptionKey: readonly unknown[];
|
|
42
|
-
subscriptionFn: (options: {
|
|
43
|
-
signal?: AbortSignal;
|
|
44
|
-
}) => UnsubscribeFn;
|
|
45
|
-
onData: (data: TData) => void;
|
|
46
|
-
onError?: (error: TError) => void;
|
|
47
|
-
keepAliveTimeout?: Input;
|
|
48
|
-
}
|
|
49
|
-
type QueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> & {
|
|
50
|
-
subscribed?: boolean;
|
|
51
|
-
};
|
|
52
|
-
interface SubscriptionReturns {
|
|
53
|
-
status: 'connecting' | 'connected' | 'disconnected' | 'error';
|
|
54
|
-
reconnect: () => void;
|
|
55
|
-
}
|
|
56
|
-
type UnwrapCodec<T> = T extends Schema<unknown> ? Schema.Type<T> : T extends object ? { [K in keyof T]: UnwrapCodec<T[K]>; } : T;
|
|
57
|
-
type MakeOptionalInput<T> = keyof T extends never ? void | undefined : T;
|
|
58
|
-
//#endregion
|
|
59
|
-
export { SubscriptionReturns as n, TanstackQueryOptionsProxy as r, SubscriptionOptions as t };
|