UNPKG

@trpc/client

Version:

The tRPC client library

110 lines (109 loc) • 3.75 kB
import { t as _objectSpread2 } from "./objectSpread2-weooBxVk.mjs"; import { t as TRPCClientError } from "./TRPCClientError-Byo7P4QG.mjs"; import { a as jsonHttpRequester, i as httpRequest, o as resolveHTTPLinkOptions, r as getUrl, u as raceAbortSignalsWithCleanup } from "./httpUtils-5Hmhcy-z.mjs"; import { observable } from "@trpc/server/observable"; import { transformResult } from "@trpc/server/unstable-core-do-not-import"; //#region src/links/internals/contentTypes.ts /** Whether an input is binary: a `Uint8Array`, `Blob` or `File`. */ function isOctetType(input) { return input instanceof Uint8Array || input instanceof Blob; } /** Whether an input is `FormData`. */ function isFormData(input) { return input instanceof FormData; } /** * Whether an input can't be sent as JSON: binary data or `FormData`. Use * it in a `splitLink` to send such inputs with `httpLink`. * @see https://trpc.io/docs/server/non-json-content-types#client-setup */ function isNonJsonSerializable(input) { return isOctetType(input) || isFormData(input); } //#endregion //#region src/links/httpLink.ts const universalRequester = (opts) => { if ("input" in opts) { const { input } = opts; if (isFormData(input)) { if (opts.type !== "mutation" && opts.methodOverride !== "POST") throw new Error("FormData is only supported for mutations"); return httpRequest(_objectSpread2(_objectSpread2({}, opts), {}, { contentTypeHeader: void 0, getUrl, getBody: () => input })); } if (isOctetType(input)) { if (opts.type !== "mutation" && opts.methodOverride !== "POST") throw new Error("Octet type input is only supported for mutations"); return httpRequest(_objectSpread2(_objectSpread2({}, opts), {}, { contentTypeHeader: "application/octet-stream", getUrl, getBody: () => input })); } } return jsonHttpRequester(opts); }; /** * A terminating link that sends each call in its own HTTP request. It can * send `FormData` and binary inputs. * @see https://trpc.io/docs/client/links/httpLink */ function httpLink(opts) { const resolvedOpts = resolveHTTPLinkOptions(opts); return () => { return (operationOpts) => { const { op } = operationOpts; return observable((observer) => { const { path, input, type } = op; /* istanbul ignore if -- @preserve */ if (type === "subscription") throw new Error("Subscriptions are unsupported by `httpLink` - use `httpSubscriptionLink` or `wsLink`"); const ac = new AbortController(); const { signal, cleanup } = raceAbortSignalsWithCleanup(op.signal, ac.signal); let request; try { request = universalRequester(_objectSpread2(_objectSpread2({}, resolvedOpts), {}, { type, path, input, signal, headers() { if (!opts.headers) return {}; if (typeof opts.headers === "function") return opts.headers({ op }); return opts.headers; } })); } catch (cause) { cleanup(); throw cause; } let isDone = false; let meta = void 0; request.then((res) => { isDone = true; meta = res.meta; const transformed = transformResult(res.json, resolvedOpts.transformer.output); if (!transformed.ok) { observer.error(TRPCClientError.from(transformed.error, { meta })); return; } observer.next({ context: res.meta, result: transformed.result }); observer.complete(); }).catch((cause) => { isDone = true; observer.error(TRPCClientError.from(cause, { meta })); }); return () => { if (!isDone) ac.abort(); cleanup(); }; }); }; }; } //#endregion export { isOctetType as i, isFormData as n, isNonJsonSerializable as r, httpLink as t }; //# sourceMappingURL=httpLink-CWd1U86f.mjs.map