UNPKG

@orpc/client

Version:

<div align="center"> <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" /> </div>

47 lines (42 loc) 1.7 kB
import { toArray, intercept } from '@orpc/shared'; import { toFetchRequest, toStandardLazyResponse } from '@orpc/standard-server-fetch'; import { C as CompositeStandardLinkPlugin, c as StandardRPCLink } from '../../shared/client.CRfRve1v.mjs'; import '@orpc/standard-server'; import '../../shared/client.BwSYEMrK.mjs'; import '../../shared/client.BLtwTQUg.mjs'; class CompositeLinkFetchPlugin extends CompositeStandardLinkPlugin { initRuntimeAdapter(options) { for (const plugin of this.plugins) { plugin.initRuntimeAdapter?.(options); } } } class LinkFetchClient { fetch; toFetchRequestOptions; adapterInterceptors; constructor(options) { const plugin = new CompositeLinkFetchPlugin(options.plugins); plugin.initRuntimeAdapter(options); this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis); this.toFetchRequestOptions = options; this.adapterInterceptors = toArray(options.adapterInterceptors); } async call(standardRequest, options, path, input) { const request = toFetchRequest(standardRequest, this.toFetchRequestOptions); const fetchResponse = await intercept( this.adapterInterceptors, { ...options, request, path, input, init: { redirect: "manual" } }, ({ request: request2, path: path2, input: input2, init, ...options2 }) => this.fetch(request2, init, options2, path2, input2) ); const lazyResponse = toStandardLazyResponse(fetchResponse, { signal: request.signal }); return lazyResponse; } } class RPCLink extends StandardRPCLink { constructor(options) { const linkClient = new LinkFetchClient(options); super(linkClient, options); } } export { LinkFetchClient, RPCLink };