MCPcopy
hub / github.com/blitz-js/blitz / httpClient

Function httpClient

packages/blitz-rpc/src/client/rpc.ts:53–156  ·  view source on GitHub ↗
(params, opts = {}, signal = undefined)

Source from the content-addressed store, hash-verified

51 const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
52 const routePathURL = new URL(fullRoutePath, window.location.origin)
53 const httpClient: RpcClientBase = async (params, opts = {}, signal = undefined) => {
54 const debug = (await import("debug")).default("blitz:rpc")
55 if (!opts.fromQueryHook && !opts.fromInvoke) {
56 console.warn(
57 "[Deprecation] Directly calling queries/mutations is deprecated in favor of invoke(queryFn, params)",
58 )
59 }
60
61 if (isServer) {
62 return Promise.resolve() as unknown
63 }
64 debug("Starting request for", fullRoutePath, "with", params, "and", opts)
65
66 let serialized: SuperJSONResult
67 if (opts.alreadySerialized) {
68 // params is already serialized with superjson when it gets here
69 // We have to serialize the params before passing to react-query in the query key
70 // because otherwise react-query will use JSON.parse(JSON.stringify)
71 // so by the time the arguments come here the real JS objects are lost
72 serialized = params as unknown as SuperJSONResult
73 } else {
74 serialized = serialize(params)
75 }
76
77 if (httpMethod === "GET" && serialized.json) {
78 routePathURL.searchParams.set("params", stringify(serialized.json))
79 routePathURL.searchParams.set("meta", stringify(serialized.meta))
80 }
81
82 const {beforeHttpRequest, beforeHttpResponse} = globalThis.__BLITZ_MIDDLEWARE_HOOKS
83
84 const promise = window
85 .fetch(
86 routePathURL,
87 beforeHttpRequest({
88 method: httpMethod,
89 headers: {
90 "Content-Type": "application/json",
91 },
92 credentials: "include",
93 redirect: "follow",
94 body:
95 httpMethod === "POST"
96 ? JSON.stringify({
97 params: serialized.json,
98 meta: {
99 params: serialized.meta,
100 },
101 })
102 : undefined,
103 signal,
104 }),
105 )
106 .then(async (response) => {
107 debug("Received request for", routePath)
108 response = beforeHttpResponse(response)
109 if (!response.ok) {
110 const error = new Error(response.statusText)

Callers

nothing calls this directly

Calls 4

beforeHttpRequestFunction · 0.85
beforeHttpResponseFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected