@@ -9,3 +9,2 @@ import { pipe } from "@effect/data/Function"; | ||
| const interpreter = yield* s(Interpreter); | ||
| // let currentInterceptor: Interceptor | null = null; | ||
| if (interceptors) { | ||
@@ -15,6 +14,5 @@ for (const interceptor of interceptors) { | ||
| continue; | ||
| // currentInterceptor = interceptor; | ||
| const { name = "(anonymous)" } = interceptor; | ||
| const run = interceptor.request.bind(interceptor); | ||
| const result = yield* s(E.logDebug("Executing interceptor"), E.flatMap(() => run(mutable_request)), E.tap(() => E.logDebug("Exiting interceptor")), E.annotateLogs("interceptor", name), E.annotateLogs("type", "Request"), E.logSpan("ms")); | ||
| const result = yield* s(E.logDebug("Executing interceptor"), E.flatMap(() => run(mutable_request)), E.tap(() => E.logDebug("Exiting interceptor")), E.annotateLogs("interceptor", name), E.annotateLogs("type", "Request"), E.withLogSpan("ms")); | ||
| if (interpreter.isResponse(result)) { | ||
@@ -32,3 +30,3 @@ request_response = result; | ||
| ? request_response | ||
| : yield* s(E.logDebug("Executing request"), E.flatMap(() => interpreter.execute(mutable_request)), E.tap(() => E.logDebug("Request done")), E.logSpan("ms")); | ||
| : yield* s(E.logDebug("Executing request"), E.flatMap(() => interpreter.execute(mutable_request)), E.tap(() => E.logDebug("Request done")), E.withLogSpan("ms")); | ||
| if (interceptors) { | ||
@@ -40,3 +38,3 @@ for (const interceptor of interceptors) { | ||
| const run = interceptor.response.bind(interceptor); | ||
| mutable_response = yield* s(E.logDebug("Executing interceptor"), E.flatMap(() => run(mutable_response, mutable_request)), E.tap(() => E.logDebug("Exiting interceptor")), E.annotateLogs("interceptor", name), E.annotateLogs("type", "Response"), E.logSpan("ms")); | ||
| mutable_response = yield* s(E.logDebug("Executing interceptor"), E.flatMap(() => run(mutable_response, mutable_request)), E.tap(() => E.logDebug("Exiting interceptor")), E.annotateLogs("interceptor", name), E.annotateLogs("type", "Response"), E.withLogSpan("ms")); | ||
| } | ||
@@ -43,0 +41,0 @@ } |
| import * as Effect from "@effect/io/Effect"; | ||
| import { HttpError } from "../exception.js"; | ||
| const fetch_ = (req) => { | ||
| return Effect.tryCatchPromiseInterrupt((signal) => fetch(req.url, Object.assign(Object.assign({}, req.init), { signal })), (error) => new HttpError("Fetch error", error)); | ||
| return Effect.tryPromiseInterrupt({ | ||
| try: (signal) => fetch(req.url, Object.assign(Object.assign({}, req.init), { signal })), | ||
| catch: (error) => new HttpError("Fetch error", error), | ||
| }); | ||
| }; | ||
@@ -6,0 +9,0 @@ const newHeaders = function (headers) { |
+4
-1
@@ -6,3 +6,6 @@ import * as Effect from "@effect/io/Effect"; | ||
| export function url(url, base) { | ||
| return Effect.tryCatch(() => new URL(url, base), () => new Error("Invalid URL")); | ||
| return Effect.try({ | ||
| try: () => new URL(url, base), | ||
| catch: () => new Error("Invalid URL"), | ||
| }); | ||
| } |
@@ -0,1 +1,2 @@ | ||
| import * as Predicate from "@effect/data/Predicate"; | ||
| import * as Effect from "@effect/io/Effect"; | ||
@@ -16,4 +17,4 @@ import { Err } from "./exception.js"; | ||
| } | ||
| export declare function toJson<T>(): <A extends Response, R, E>(effect: Effect.Effect<R, E, A>) => Effect.Effect<R, JsonParseError | E, T>; | ||
| export declare function filterStatus<A extends Res>(func: (status: number) => boolean): <R, E>(effect: Effect.Effect<R, E, A>) => Effect.Effect<R, StatusError | E, A>; | ||
| export declare function filterStatusOk(): <R, E>(effect: Effect.Effect<R, E, Response>) => Effect.Effect<R, StatusError | E, Response>; | ||
| export declare function toJson<T>(): <R, E, A extends Response>(fx: Effect.Effect<R, E, A>) => Effect.Effect<R, JsonParseError | E, T>; | ||
| export declare function filterStatus<A extends Res>(func: Predicate.Predicate<number>): <R, E>(fx: Effect.Effect<R, E, A>) => Effect.Effect<R, StatusError | E, A>; | ||
| export declare function filterStatusOk(): <R, E>(fx: Effect.Effect<R, E, Response>) => Effect.Effect<R, StatusError | E, Response>; |
+9
-6
@@ -21,11 +21,14 @@ import { pipe } from "@effect/data/Function"; | ||
| export function toJson() { | ||
| return (effect) => { | ||
| return pipe(effect, Effect.flatMap((res) => Effect.tryCatchPromise(() => res.json(), () => new JsonParseError(res, "Unable to parse JSON")))); | ||
| return (fx) => { | ||
| return pipe(fx, Effect.flatMap((res) => { | ||
| return Effect.tryPromise({ | ||
| try: () => res.json(), | ||
| catch: () => new JsonParseError(res, "Unable to parse JSON"), | ||
| }); | ||
| })); | ||
| }; | ||
| } | ||
| export function filterStatus(func) { | ||
| return (effect) => { | ||
| return pipe(effect, Effect.filterOrElseWith((res) => func(res.status), (res) => { | ||
| return Effect.fail(new StatusError(res, `Received invalid status code: ${res.status}`)); | ||
| })); | ||
| return (fx) => { | ||
| return pipe(fx, Effect.filterOrElse((res) => func(res.status), (res) => Effect.fail(new StatusError(res, `Received invalid status code: ${res.status}`)))); | ||
| }; | ||
@@ -32,0 +35,0 @@ } |
+5
-5
| { | ||
| "name": "http-kit", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "type": "module", | ||
@@ -61,4 +61,4 @@ "description": "Platform agnostic fetch kit for the Effect package", | ||
| "devDependencies": { | ||
| "@effect/data": "^0.12.10", | ||
| "@effect/io": "^0.27.2", | ||
| "@effect/data": "^0.17.0", | ||
| "@effect/io": "^0.36.1", | ||
| "rimraf": "^5.0.1", | ||
@@ -68,4 +68,4 @@ "typescript": "^5.1.6" | ||
| "peerDependencies": { | ||
| "@effect/data": "^0.12.10", | ||
| "@effect/io": "^0.27.2" | ||
| "@effect/data": "^0.17.0", | ||
| "@effect/io": "^0.36.1" | ||
| }, | ||
@@ -72,0 +72,0 @@ "scripts": { |
18240
0.2%382
2.14%