@tiesen/effect-tanstack-query 0.0.8 → 0.0.10

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 CHANGED
@@ -98,7 +98,7 @@ class ApiClient extends Context.Service<
98
98
  }
99
99
  ```
100
100
 
101
- ### 3. Use `effect-tanstack-query` to Generate Query and Mutation Options
101
+ ### 3. Create a TanStack Query Options Proxy
102
102
 
103
103
  ```ts
104
104
  import { ManagedRuntime } from 'effect'
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { r as TanstackQueryOptionsProxy } from "./types-BGHsAv5y.mjs";
1
+ import { r as TanstackQueryOptionsProxy } from "./types-CcB8FTUv.mjs";
2
2
  import { Service } from "effect/Context";
3
3
  import { ManagedRuntime } from "effect/ManagedRuntime";
4
4
  //#region src/index.d.ts
@@ -21,10 +21,10 @@ import { ManagedRuntime } from "effect/ManagedRuntime";
21
21
  * class Api extends HttpApi.make('Api').add(ApiGroup) {}
22
22
  *
23
23
  * class ApiClient extends Context.Service<ApiClient, HttpApiClient.ForApi<typeof Api>>()('ApiClient') {
24
- * public static live = Layer.effect(this, HttpApiClient.make(Api)).pipe(Layer.provide(FetchHttpClient.layer))
24
+ * public static layer = Layer.effect(this, HttpApiClient.make(Api)).pipe(Layer.provide(FetchHttpClient.layer))
25
25
  * }
26
26
  *
27
- * const runtime = ManagedRuntime.make(ApiClient.live)
27
+ * const runtime = ManagedRuntime.make(ApiClient.layer)
28
28
  * const api = createTanstackQueryOptionsProxy(ApiClient, runtime);
29
29
  *
30
30
  * // Example usage in a React component with TanStack Query
@@ -37,7 +37,7 @@ import { ManagedRuntime } from "effect/ManagedRuntime";
37
37
  *
38
38
  * useSubscription(api.stream.subscriptionOptions({
39
39
  * onData: (data) => console.log(data),
40
- * ^? const data: string = "data: keep-alive\n\n"
40
+ * ^? const data: string = "Never gonna give you up, never gonna let you down..."
41
41
  * }))
42
42
  * ```
43
43
  */
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import*as e from"effect/Effect";import*as t from"effect/Stream";function n(n,r){let i=new Map,a=o=>{let s=o.join(`.`);if(i.has(s))return i.get(s);let c=new Proxy(()=>{},{get(e,t){if(typeof t!=`symbol`&&t!==`then`)return a([...o,String(t)])},apply(i,a,s){let c=o.at(-1),l=o.slice(0,-1),[u,d]=s,f=e.fn(function*(e){let t=yield*n;for(let e of l)t=t[e];return yield*t(e)}),p=(e,t)=>r.runPromise(f(e),{signal:t}),m=(e,t)=>[{type:e},...l,...t?[t]:[]];if(c===`queryOptions`)return{...d,queryKey:m(`query`,u),queryFn:({signal:e})=>p(u,e)};if(c===`getQueryKey`)return m(`query`,u);if(c===`subscriptionOptions`){let n=t.unwrap(f({...u,responseMode:`response-only`}).pipe(e.map(e=>e.stream.pipe(t.decodeText())))),i=d;return{...d,subcriptionKey:m(`subscription`,u),subscriptionFn:({signal:a}={})=>{let o=r.runFork(n.pipe(t.tap(t=>e.sync(()=>i.onData(t))),t.runDrain)),s=()=>o.interruptUnsafe(o.id);return a&&(a.aborted?s():a.addEventListener(`abort`,s,{once:!0})),o.addObserver(e=>{e._tag===`Failure`&&i.onError?.(e.cause)}),s}}}if(c===`mutationOptions`)return{...d,mutationKey:m(`mutation`,u),mutationFn:e=>p({payload:e})}}});return i.set(s,c),c};return a([])}export{n as createTanstackQueryOptionsProxy};
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`){let{onData:i,onError:o,keepAliveTimeout:s=`10 seconds`}=p;return{...p,subcriptionKey:g(`subscription`,f),subscriptionFn:({signal:c}={})=>{let l=e.gen(function*(){let t=yield*(yield*m({...f,responseMode:`response-only`})).stream.pipe(r.decodeText,r.splitLines,r.filter(e=>e.length>0),r.share({capacity:`unbounded`})),n=t.pipe(r.filter(e=>e.startsWith(`:keep-alive`)),r.timeout(s),r.tapError(()=>e.logError(`Keep-alive timeout, reconnecting...`))),a=yield*t.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(()=>i(t))),r.share({capacity:`unbounded`})),o=r.merge(n,a);yield*r.runDrain(o),yield*e.fail(`restart`)}).pipe(e.catchTag(`HttpClientError`,t=>t.reason._tag===`DecodeError`?e.void:(o&&o(t),e.fail(t))),e.scoped,e.retry(n.exponential(`3 seconds`))),u=a.runFork(l),d=()=>t.interrupt(u);return c&&(c.aborted?d():c.addEventListener(`abort`,d,{once:!0})),d}}}}});return o.set(l,u),u};return s([])}export{i as createTanstackQueryOptionsProxy};
package/dist/react.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as SubscriptionReturns, t as SubscriptionOptions } from "./types-BGHsAv5y.mjs";
1
+ import { n as SubscriptionReturns, t as SubscriptionOptions } from "./types-CcB8FTUv.mjs";
2
2
  //#region src/react.d.ts
3
3
  declare function useSubscription<TData, TError>({ enabled, ...subscriptionOptions }: SubscriptionOptions<TData, TError> & {
4
4
  enabled?: boolean;
package/dist/react.mjs CHANGED
@@ -1 +1 @@
1
- import*as e from"react";function t({enabled:t=!0,...n}){let[r,i]=e.useState(`disconnected`),[a,o]=e.useState(0),s=e.useCallback(()=>{o(e=>e+1)},[]);return e.useEffect(()=>{if(!t){i(`disconnected`);return}i(`connecting`);let e=new AbortController,r=n.subscriptionFn({signal:e.signal});return i(`connected`),()=>{e.abort(),r(),i(`disconnected`)}},[t,n.subscriptionFn,a]),e.useMemo(()=>({status:r,reconnect:s}),[r,s])}export{t as useSubscription};
1
+ import*as e from"react";function t({enabled:t=!0,...n}){let[r,i]=e.useState(`disconnected`),[a,o]=e.useState(0),s=e.useCallback(()=>{o(e=>e+1)},[]),c=e.useMemo(()=>JSON.stringify(n.subcriptionKey),[n.subcriptionKey]);return e.useEffect(()=>{if(!t)return i(`disconnected`);i(`connecting`);let e=new AbortController,r=n.subscriptionFn({signal:e.signal});return i(`connected`),()=>{e.abort(),r(),i(`disconnected`)}},[t,c,a]),e.useMemo(()=>({status:r,reconnect:s}),[r,s])}export{t as useSubscription};
@@ -1,4 +1,5 @@
1
1
  import { DefinedInitialDataOptions, MutationOptions } from "@tanstack/react-query";
2
+ import { Input } from "effect/Duration";
2
3
  import { Schema } from "effect/Schema";
3
4
  import { Client } from "effect/unstable/httpapi/HttpApiClient";
4
5
  import { HttpApiEndpoint } from "effect/unstable/httpapi/HttpApiEndpoint";
@@ -19,9 +20,9 @@ type TanstackQueryOptionsProxy<T> = T extends Client.Method<HttpApiEndpoint<infe
19
20
  }) & ([Headers] extends [never] ? {} : {
20
21
  headers: UnwrapCodec<Headers>;
21
22
  })>, options?: {
22
- onData: (data: StreamSuccess) => void;
23
- onError?: (error: StreamError) => void;
24
- }) => SubscriptionOptions<StreamSuccess, StreamError> : never;
23
+ onData: (data: UnwrapCodec<StreamSuccess>) => void;
24
+ onError?: (error: UnwrapCodec<StreamError>) => void;
25
+ }) => SubscriptionOptions<UnwrapCodec<StreamSuccess>, UnwrapCodec<StreamError>> : never;
25
26
  mutationOptions: Method extends 'GET' ? never : <TMutation = MutationOptions<UnwrapCodec<Success>, UnwrapCodec<Error>, UnwrapCodec<Payload>>>(input: MakeOptionalInput<([Params] extends [never] ? {} : {
26
27
  params: UnwrapCodec<Params>;
27
28
  }) & ([Headers] extends [never] ? {} : {
@@ -43,6 +44,7 @@ interface SubscriptionOptions<TData, TError> {
43
44
  }) => UnsubscribeFn;
44
45
  onData: (data: TData) => void;
45
46
  onError?: (error: TError) => void;
47
+ keepAliveTimeout?: Input;
46
48
  }
47
49
  interface SubscriptionReturns {
48
50
  status: 'connecting' | 'connected' | 'disconnected' | 'error';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiesen/effect-tanstack-query",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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,6 +67,11 @@
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
+ },
70
75
  "packageManager": "bun@1.3.0",
71
76
  "patchedDependencies": {
72
77
  "tegami@1.2.7": "patches/tegami@1.2.7.patch"