MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / doltFetch

Function doltFetch

packages/wl-sdk/src/dolthub/api.ts:91–129  ·  view source on GitHub ↗
(opts: DoltFetchOptions)

Source from the content-addressed store, hash-verified

89 * decide whether a non-2xx response is an error.
90 */
91export async function doltFetch(opts: DoltFetchOptions): Promise<DoltFetchResult> {
92 const fetchImpl = opts.fetch ?? globalThis.fetch;
93 const url = buildDoltUrl(opts.path, opts.query);
94
95 const headers: Record<string, string> = {
96 // Bypass Cloudflare's subrequest cache. DoltHub returns stale
97 // reads otherwise, which surfaces as ghost branches / merged PRs
98 // not appearing immediately. The DOM `cache: 'no-store'` init is
99 // rejected by workerd; the cache-control header is what
100 // propagates.
101 'cache-control': 'no-cache',
102 ...buildAuthHeader(opts.auth),
103 };
104
105 const init: RequestInit = { method: opts.method, headers };
106 if (opts.body !== undefined) {
107 headers['content-type'] = 'application/json';
108 init.body = JSON.stringify(opts.body);
109 }
110
111 opts.hooks?.onRequest?.({ method: opts.method, url });
112
113 const res = await fetchImpl(url, init);
114 const text = await res.text();
115 let json: unknown;
116 if (text.length > 0) {
117 try {
118 json = JSON.parse(text);
119 } catch {
120 json = undefined;
121 }
122 }
123
124 if (!res.ok) {
125 opts.hooks?.onError?.({ method: opts.method, url, status: res.status, body: json ?? text });
126 }
127
128 return { status: res.status, headers: res.headers, text, json };
129}
130
131/**
132 * Convenience: throw `WlDoltHubError` if the response is non-2xx.

Callers 15

doltWriteFunction · 0.90
listBranchesFunction · 0.90
deleteBranchFunction · 0.90
createBranchFunction · 0.90
api.test.tsFile · 0.90
doltReadFunction · 0.90
listPullsFunction · 0.90
getPullFunction · 0.90
createPullFunction · 0.90
closePullFunction · 0.90
mergePullFunction · 0.90
commentOnPullFunction · 0.90

Calls 5

buildDoltUrlFunction · 0.85
buildAuthHeaderFunction · 0.85
textMethod · 0.65
fetchImplFunction · 0.50
onErrorMethod · 0.45

Tested by

no test coverage detected