Skip to content

Commit

Permalink
remove Scope from HttpClient requirements (#4573)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Mar 10, 2025
1 parent 4d4781e commit 88fe129
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 213 deletions.
33 changes: 33 additions & 0 deletions .changeset/lucky-shrimps-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
"@effect/platform-browser": minor
"@effect/platform-node": minor
"@effect/platform": minor
---

remove Scope from HttpClient requirements

Before:

```ts
import { HttpClient } from "@effect/platform"
import { Effect } from "effect"

Effect.gen(function* () {
const client = yield* HttpClient.HttpClient
const response = yield* client.get("https://api.github.com/users/octocat")
return yield* response.json
}).pipe(Effect.scoped)
```

After:

```ts
import { HttpClient } from "@effect/platform"
import { Effect } from "effect"

Effect.gen(function* () {
const client = yield* HttpClient.HttpClient
const response = yield* client.get("https://api.github.com/users/octocat")
return yield* response.json
}) // no need to add Effect.scoped
```
7 changes: 2 additions & 5 deletions packages/platform-browser/test/BrowserHttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ describe("BrowserHttpClient", () => {
it.effect("json", () =>
Effect.gen(function*() {
const body = yield* HttpClient.get("http://localhost:8080/my/url").pipe(
Effect.flatMap((_) => _.json),
Effect.scoped
Effect.flatMap((_) => _.json)
)
assert.deepStrictEqual(body, { message: "Success!" })
}).pipe(Effect.provide(layer({
Expand Down Expand Up @@ -50,8 +49,7 @@ describe("BrowserHttpClient", () => {
it.effect("cookies", () =>
Effect.gen(function*() {
const cookies = yield* HttpClient.get("http://localhost:8080/my/url").pipe(
Effect.map((res) => res.cookies),
Effect.scoped
Effect.map((res) => res.cookies)
)
assert.deepStrictEqual(Cookies.toRecord(cookies), {
foo: "bar"
Expand All @@ -69,7 +67,6 @@ describe("BrowserHttpClient", () => {
Effect.gen(function*() {
const body = yield* HttpClient.get("http://localhost:8080/my/url").pipe(
Effect.flatMap((_) => _.arrayBuffer),
Effect.scoped,
BrowserHttpClient.withXHRArrayBuffer
)
assert.strictEqual(new TextDecoder().decode(body), "{ \"message\": \"Success!\" }")
Expand Down
23 changes: 7 additions & 16 deletions packages/platform-node/test/HttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const makeJsonPlaceholder = Effect.gen(function*(_) {
HttpClientRequest.post("/todos").pipe(
HttpClientRequest.schemaBodyJson(TodoWithoutId)(todo),
Effect.flatMap(client.execute),
Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo)),
Effect.scoped
Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo))
)
return {
client,
Expand All @@ -53,8 +52,7 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
Effect.gen(function*(_) {
const response = yield* _(
HttpClient.get("https://www.google.com/"),
Effect.flatMap((_) => _.text),
Effect.scoped
Effect.flatMap((_) => _.text)
)
expect(response).toContain("Google")
}).pipe(Effect.provide(layer)))
Expand All @@ -65,8 +63,7 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
HttpClient.followRedirects()
)
const response = yield* client.get("http://google.com/").pipe(
Effect.flatMap((_) => _.text),
Effect.scoped
Effect.flatMap((_) => _.text)
)
expect(response).toContain("Google")
}).pipe(Effect.provide(layer)))
Expand All @@ -86,8 +83,7 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
Effect.gen(function*() {
const jp = yield* JsonPlaceholder
const response = yield* jp.client.get("/todos/1").pipe(
Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo)),
Effect.scoped
Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo))
)
expect(response.id).toBe(1)
}).pipe(Effect.provide(JsonPlaceholderLive.pipe(
Expand All @@ -113,8 +109,7 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
const response = yield* client.head("https://jsonplaceholder.typicode.com/todos").pipe(
Effect.flatMap(
HttpClientResponse.schemaJson(Schema.Struct({ status: Schema.Literal(200) }))
),
Effect.scoped
)
)
expect(response).toEqual({ status: 200 })
}).pipe(Effect.provide(layer)))
Expand All @@ -124,7 +119,6 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
const client = yield* HttpClient.HttpClient
const response = yield* client.get("https://www.google.com/").pipe(
Effect.flatMap((_) => _.text),
Effect.scoped,
Effect.timeout(1),
Effect.asSome,
Effect.catchTag("TimeoutException", () => Effect.succeedNone)
Expand All @@ -133,11 +127,8 @@ const JsonPlaceholderLive = Layer.effect(JsonPlaceholder, makeJsonPlaceholder)
}).pipe(Effect.provide(layer)))

it.effect("close early", () =>
Effect.gen(function*(_) {
const response = yield* _(
HttpClient.get("https://www.google.com/"),
Effect.scoped
)
Effect.gen(function*() {
const response = yield* HttpClient.get("https://www.google.com/")
expect(response.status).toBe(200)
}).pipe(Effect.provide(layer)))
})
Expand Down
Loading

0 comments on commit 88fe129

Please sign in to comment.