(opts: DoltFetchOptions)
| 89 | * decide whether a non-2xx response is an error. |
| 90 | */ |
| 91 | export 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. |
no test coverage detected