MCPcopy Create free account
hub / github.com/Bytez-com/docs / request

Method request

sdk/javascript/src/client.ts:41–85  ·  view source on GitHub ↗
(
    path: string,
    method?: Method,
    body?: RequestBody,
    providerKey?: string
  )

Source from the content-addressed store, hash-verified

39 host = "";
40 headers = {};
41 async request(
42 path: string,
43 method?: Method,
44 body?: RequestBody,
45 providerKey?: string
46 ) {
47 try {
48 const res = await fetch(this.host + path, {
49 method,
50 headers:
51 providerKey === undefined
52 ? this.headers
53 : { ...this.headers, ["provider-key"]: providerKey },
54 // @ts-expect-error dispatcher is undici-only
55 dispatcher: this.#dispatcher,
56 signal: AbortSignal.timeout(this.#timeout),
57 body: body ? JSON.stringify(body) : undefined
58 });
59
60 if (
61 method === "POST" &&
62 !res.ok &&
63 res.headers.get("content-type") !== "application/json"
64 ) {
65 const { error } = await res.json().catch(console.error);
66
67 throw new Error(error || res.statusText);
68 }
69
70 if (
71 res.body !== null &&
72 (body?.stream === true || body?.json === false)
73 ) {
74 // in browsers, return readable text stream
75 // in node, return node readable stream
76 return this.#isBrowser
77 ? res.body.pipeThrough(new TextDecoderStream())
78 : this.#Readable.fromWeb(res.body as any, { encoding: "utf8" });
79 } else {
80 return await res.json();
81 }
82 } catch (error) {
83 return { error: error.message, output: null } as Response;
84 }
85 }
86}

Callers 2

BytezClass · 0.45
runMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected