const safeFetch = (
url: string,
init: BunFetchRequestInit | undefined,
) => Effect.gen(function*() {
const method = init?.method ?? 'GET';
const response = yield* Effect.tryPromise({
try: async signal => {
const response = await fetch(url, { ...init, method, signal });
const bodyStr = await response.text();
return { ...response, bodyStr };
},
catch: cause => {
return new NetworkError({ method, url, cause });
},
});
if (!response.ok) {
return yield* new NotOkResponseError({ method, url, response });
}
return response;
}).pipe(
Effect.timeout('1 minutes'),
);
const safeFetch = (
url: string,
init: BunFetchRequestInit | undefined,
) => Effect.gen(function*() {
const method = init?.method ?? 'GET';
const response = yield* Effect.tryPromise({
try: async signal => {
const response = await fetch(url, { ...init, method, signal });
const bodyStr = await response.text();
return { ...response, bodyStr };
},
catch: cause => {
return new NetworkError({ method, url, cause });
},
});
if (!response.ok) {
return yield* new NotOkResponseError({ method, url, response });
}
return response;
}).pipe(
Effect.timeout('1 minutes'),
);