functype-react 1.25.0 → 1.26.0
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
|
@@ -139,7 +139,7 @@ Errors accumulate applicatively — every failing rule is surfaced in one pass,
|
|
|
139
139
|
|
|
140
140
|
`Http.get(...)` returns `IO<never, HttpError, HttpResponse<T>>`, and neither `IO` terminal fits TanStack Query: `.run()` never throws (so React Query never sees a failure), and `.runOrThrow()` throws the raw tagged object — which is **not** an `Error`, so the ubiquitous `error instanceof Error ? error.message : fallback` handler silently degrades.
|
|
141
141
|
|
|
142
|
-
`functype-react/query` owns that bridge. Failures arrive boxed as `IOQueryError<E>`: a real `Error` with a populated `.message`, whose `.error` is still the fully discriminable functype error.
|
|
142
|
+
`functype-react/query` owns that bridge. Failures arrive boxed as `IOQueryError<E>`: a real `Error` with a populated `.message`, whose `.error` is still the fully discriminable functype error. Its `.name` is the cause's `_tag`, or a `TypedError`'s code (`NOT_FOUND`), so an error tracker groups by cause and can drop expected failures by name. A defect or an untagged cause keeps the name `IOQueryError`. Use `instanceof IOQueryError` or `isIOQueryError`, not `.name`, to recognize the box.
|
|
143
143
|
|
|
144
144
|
```ts
|
|
145
145
|
import { useIOQuery } from "functype-react/query"
|
|
@@ -17,6 +17,7 @@ export declare const formatIOError: (error: unknown) => string;
|
|
|
17
17
|
*
|
|
18
18
|
* error instanceof Error // true
|
|
19
19
|
* error.message // "HTTP 403 Forbidden — /api/limits"
|
|
20
|
+
* error.name // "HttpStatusError"
|
|
20
21
|
*
|
|
21
22
|
* HttpErrors.match(error.error, {
|
|
22
23
|
* NetworkError: (e) => retryBanner(e),
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{Try as e}from"functype";const t=e=>typeof e==`object`&&!!e&&`_tag`in e&&typeof e._tag==`string`,n=n=>{if(n instanceof Error)return n.message;if(t(n)){let e=typeof n.url==`string`?` — ${n.url}`:``;if(typeof n.status==`number`){let t=typeof n.statusText==`string`&&n.statusText.length>0?` ${n.statusText}`:``;return`HTTP ${n.status}${t}${e}`}return typeof n.message==`string`&&n.message.length>0?n.message:`${n._tag}${e}`}return typeof n==`object`&&n?e(()=>JSON.stringify(n)).orElse(String(n)):String(n)};var
|
|
1
|
+
import{Try as e}from"functype";const t=e=>typeof e==`object`&&!!e&&`_tag`in e&&typeof e._tag==`string`,n=n=>{if(n instanceof Error)return n.message;if(t(n)){let e=typeof n.url==`string`?` — ${n.url}`:``;if(typeof n.status==`number`){let t=typeof n.statusText==`string`&&n.statusText.length>0?` ${n.statusText}`:``;return`HTTP ${n.status}${t}${e}`}return typeof n.message==`string`&&n.message.length>0?n.message:`${n._tag}${e}`}return typeof n==`object`&&n?e(()=>JSON.stringify(n)).orElse(String(n)):String(n)},r=(e,n)=>n||!t(e)||e._tag.length===0?`IOQueryError`:e._tag===`Throwable`&&typeof e.name==`string`&&e.name.length>0?e.name:e._tag;var i=class extends Error{constructor(e,t,i=!1){super(t??n(e),{cause:e}),this.name=r(e,i),this.error=e,this.defect=i}};const a=e=>e instanceof i;export{i as IOQueryError,n as formatIOError,a as isIOQueryError};
|
|
2
2
|
//# sourceMappingURL=IOQueryError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IOQueryError.js","names":["isTaggedError","formatIOError","error","where","statusText","Try","
|
|
1
|
+
{"version":3,"file":"IOQueryError.js","names":["isTaggedError","formatIOError","error","where","statusText","Try","nameOf","defect","IOQueryError","message","isIOQueryError"],"sources":["../../src/query/IOQueryError.ts"],"sourcesContent":["/**\n * Error boxing for the React Query bridge.\n *\n * `IO`'s own terminals don't fit React Query's contract: `.run()` returns\n * `Promise<Either<E, A>>` and never throws (so RQ never sees a failure), while\n * `.runOrThrow()` throws the *raw* tagged error object — `{ _tag: \"NetworkError\", ... }`\n * is not an `Error` instance, so the ubiquitous\n * `error instanceof Error ? error.message : fallback` handler silently degrades.\n *\n * `IOQueryError` boxes the typed error so both worlds work: `instanceof Error` and\n * `.message` behave as any React Query consumer expects, while `.error` preserves the\n * fully discriminable functype `E`.\n */\nimport { Try } from \"functype\"\n\n/** Structural view of a tagged functype error (e.g. any `HttpError` variant). */\ntype TaggedError = {\n readonly _tag: string\n readonly name?: unknown\n readonly url?: unknown\n readonly status?: unknown\n readonly statusText?: unknown\n readonly message?: unknown\n}\n\nconst isTaggedError = (e: unknown): e is TaggedError =>\n typeof e === \"object\" && e !== null && \"_tag\" in e && typeof (e as { _tag: unknown })._tag === \"string\"\n\n/**\n * Derives a human-readable message from an arbitrary error channel value.\n *\n * Deliberately *structural* — this module does not import `functype/fetch`, so the\n * query bridge stays generic over any `IO<never, E, A>` rather than being HTTP-specific.\n * An `HttpStatusError` is recognized by its shape, not its type.\n *\n * Pass `formatError` to any of the query entry points to override this entirely.\n */\nexport const formatIOError = (error: unknown): string => {\n if (error instanceof Error) {\n return error.message\n }\n\n if (isTaggedError(error)) {\n const where = typeof error.url === \"string\" ? ` — ${error.url}` : \"\"\n\n if (typeof error.status === \"number\") {\n const statusText =\n typeof error.statusText === \"string\" && error.statusText.length > 0 ? ` ${error.statusText}` : \"\"\n return `HTTP ${error.status}${statusText}${where}`\n }\n\n // A tagged error carrying its own `message` is the normal shape for a domain error\n // in the `IO` channel, and that message is what the author wrote for a human to\n // read. Falling back to the bare tag was the one case where information already\n // present got discarded — a raw `Error` gets `.message`, an untagged object gets\n // JSON, but a tagged one got neither. None of functype's own `HttpError` variants\n // carry a `message`, so the HTTP renderings above are unaffected.\n if (typeof error.message === \"string\" && error.message.length > 0) {\n return error.message\n }\n\n return `${error._tag}${where}`\n }\n\n // `String({})` is \"[object Object]\", which tells a reader nothing. Prefer a JSON\n // rendering for plain objects, falling back to String() for cycles and symbols.\n if (typeof error === \"object\" && error !== null) {\n return Try(() => JSON.stringify(error)).orElse(String(error))\n }\n\n return String(error)\n}\n\n/**\n * What `name` reads for a boxed failure. Error trackers title and group by `name`, so a typed failure\n * takes its cause's tag (`OAuthFlowError`, `HttpStatusError`) and a closed popup doesn't land in the same\n * group as a 500. A defect's cause isn't an `E`, so it stays `IOQueryError`, whatever tag it carries.\n *\n * Every functype `Throwable` shares the tag `Throwable`, so one's specific name is in `.name`: a\n * `TypedError`'s code (`NOT_FOUND`), or a `Throwable`'s `taskInfo.name`.\n */\nconst nameOf = (error: unknown, defect: boolean): string => {\n if (defect || !isTaggedError(error) || error._tag.length === 0) return \"IOQueryError\"\n return error._tag === \"Throwable\" && typeof error.name === \"string\" && error.name.length > 0 ? error.name : error._tag\n}\n\n/**\n * An `Error` subclass carrying the typed functype error channel.\n *\n * ```ts\n * const { error } = useIOQuery([\"limits\", userId], ({ signal }) => Http.get(url, { signal }))\n *\n * error instanceof Error // true\n * error.message // \"HTTP 403 Forbidden — /api/limits\"\n * error.name // \"HttpStatusError\"\n *\n * HttpErrors.match(error.error, {\n * NetworkError: (e) => retryBanner(e),\n * HttpStatusError: (e) => (e.status === 403 ? upgradePrompt() : genericFailure(e)),\n * DecodeError: (e) => reportSchemaDrift(e),\n * })\n * ```\n */\nexport class IOQueryError<E> extends Error {\n /**\n * The raw functype error channel value — discriminable via its own `_tag`.\n *\n * When {@link defect} is `true` this is **not** an `E`. Check `defect` before\n * matching on `_tag`.\n */\n readonly error: E\n\n /**\n * `true` when {@link error} is *not* a value from the effect's typed error channel.\n * Three things set it:\n *\n * - the effect factory threw before an `IO` was ever produced\n * - the effect produced a defect (`Exit.Die`) — a throwing `IO.sync` thunk, a\n * throwing `map` / `flatMap` / `mapError` callback, or `IO.die`\n * - the effect was interrupted, so `error` is an `InterruptedError`\n *\n * Kept as an explicit flag rather than widening `E` to `unknown`, which would force\n * every consumer to handle a case that should never occur.\n *\n * `defect: false` genuinely means `error` is an `E`. Before 1.8.0 it did not: a\n * defect reached the adapter as an ordinary `Left` and was indistinguishable from a\n * typed failure, so the documented guard didn't guard anything (#259).\n */\n readonly defect: boolean\n\n constructor(error: E, message?: string, defect = false) {\n super(message ?? formatIOError(error), { cause: error })\n this.name = nameOf(error, defect)\n this.error = error\n this.defect = defect\n }\n}\n\n/** Runtime guard for {@link IOQueryError}. */\nexport const isIOQueryError = <E = unknown>(error: unknown): error is IOQueryError<E> => error instanceof IOQueryError\n"],"mappings":"+BAyBA,MAAMA,EAAiB,GACrB,OAAO,GAAM,YAAY,GAAc,SAAU,GAAK,OAAQ,EAAwB,MAAS,SAWpFC,EAAiB,GAA2B,CACvD,GAAIC,aAAiB,MACnB,OAAOA,EAAM,QAGf,GAAIF,EAAcE,CAAK,EAAG,CACxB,IAAMC,EAAQ,OAAOD,EAAM,KAAQ,SAAW,MAAMA,EAAM,MAAQ,GAElE,GAAI,OAAOA,EAAM,QAAW,SAAU,CACpC,IAAME,EACJ,OAAOF,EAAM,YAAe,UAAYA,EAAM,WAAW,OAAS,EAAI,IAAIA,EAAM,aAAe,GACjG,MAAO,QAAQA,EAAM,SAASE,IAAaD,GAC7C,CAYA,OAJI,OAAOD,EAAM,SAAY,UAAYA,EAAM,QAAQ,OAAS,EACvDA,EAAM,QAGR,GAAGA,EAAM,OAAOC,GACzB,CAQA,OAJI,OAAOD,GAAU,UAAYA,EACxBG,MAAU,KAAK,UAAUH,CAAK,CAAC,CAAC,CAAC,OAAO,OAAOA,CAAK,CAAC,EAGvD,OAAOA,CAAK,CACrB,EAUMI,GAAU,EAAgB,IAC1BC,GAAU,CAACP,EAAcE,CAAK,GAAKA,EAAM,KAAK,SAAW,EAAU,eAChEA,EAAM,OAAS,aAAe,OAAOA,EAAM,MAAS,UAAYA,EAAM,KAAK,OAAS,EAAIA,EAAM,KAAOA,EAAM,KAoBpH,IAAaM,EAAb,cAAqC,KAAM,CA2BzC,YAAY,EAAU,EAAkB,EAAS,GAAO,CACtD,MAAMC,GAAWR,EAAcC,CAAK,EAAG,CAAE,MAAOA,CAAM,CAAC,EACvD,KAAK,KAAOI,EAAOJ,EAAOK,CAAM,EAChC,KAAK,MAAQL,EACb,KAAK,OAASK,CAChB,CACF,EAGA,MAAaG,EAA+B,GAA6CR,aAAiBM"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functype-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.26.0",
|
|
4
4
|
"description": "React bindings for functype — ADT-aware hooks and exhaustive pattern matching components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"functype",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@types/react": "^19.3.0",
|
|
49
49
|
"@types/react-dom": "^19.3.0",
|
|
50
50
|
"fast-check": "^4.10.2",
|
|
51
|
-
"functype": "^1.
|
|
51
|
+
"functype": "^1.26.0",
|
|
52
52
|
"jsdom": "^30.1.1",
|
|
53
53
|
"react": "^19.3.0",
|
|
54
54
|
"react-dom": "^19.3.0",
|