( input: string | URL, init?: RequestInit, )
| 18 | // Only for use in the browser. |
| 19 | // On the server, use `@effect/platform.HttpClient` instead. |
| 20 | export const fetchEff = ( |
| 21 | input: string | URL, |
| 22 | init?: RequestInit, |
| 23 | ): Micro.Micro<ResponseWithURL, FetchError, FetchContext> => |
| 24 | Micro.flatMap(Micro.service(FetchContext), (fetch) => { |
| 25 | const headers = new Headers(init?.headers ?? []); |
| 26 | |
| 27 | const reqInfo = { |
| 28 | url: input.toString(), |
| 29 | method: init?.method, |
| 30 | body: init?.body, |
| 31 | headers: Object.fromEntries(headers), |
| 32 | }; |
| 33 | |
| 34 | return Micro.tryPromise({ |
| 35 | try: (signal) => fetch(input, { ...init, headers, signal }), |
| 36 | catch: (error) => |
| 37 | new FetchError({ |
| 38 | error: |
| 39 | error instanceof Error |
| 40 | ? { |
| 41 | ...error, |
| 42 | name: error.name, |
| 43 | message: error.message, |
| 44 | stack: error.stack, |
| 45 | } |
| 46 | : error, |
| 47 | input: reqInfo, |
| 48 | }), |
| 49 | }).pipe( |
| 50 | // eslint-disable-next-line no-console |
| 51 | Micro.tapError((e) => Micro.sync(() => console.error(e.input))), |
| 52 | Micro.map((res) => Object.assign(res, { requestUrl: reqInfo.url })), |
| 53 | Micro.withTrace("fetch"), |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | export const parseResponseJson = ( |
| 58 | res: ResponseWithURL, |
no test coverage detected