MCPcopy Create free account
hub / github.com/vercel/examples / fetchAPI

Function fetchAPI

edge-middleware/ip-blocking/lib/fetch-api.ts:1–25  ·  view source on GitHub ↗
(
  url: string,
  options: RequestInit & { data?: any } = {}
)

Source from the content-addressed store, hash-verified

1export default async function fetchAPI(
2 url: string,
3 options: RequestInit & { data?: any } = {}
4) {
5 try {
6 const res = await fetch(`/api${url}`, {
7 ...options,
8 headers: {
9 ...options.headers,
10 Accept: 'application/json',
11 'Content-Type': 'application/json; charset=utf-8',
12 },
13 ...(options.data ? { body: JSON.stringify(options.data) } : {}),
14 })
15 const data = await res.json()
16 return data
17 } catch (e: any) {
18 if (e.message === 'cancelled') {
19 // Cancelled by browser
20 console.log('Request Cancelled by the Browser', e)
21 } else {
22 console.error('Network Error, CORS or timeout.')
23 }
24 }
25}

Callers 1

IndexFunction · 0.50

Calls 1

fetchFunction · 0.85

Tested by

no test coverage detected