Skip to content

Commit

Permalink
chore: update effect dependencies (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej committed Feb 20, 2024
1 parent cfb91a8 commit 549859b
Show file tree
Hide file tree
Showing 19 changed files with 1,580 additions and 1,528 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-weeks-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect-http-node": patch
"effect-http": patch
---

Update effect dependencies.
68 changes: 36 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ Bootstrap a simple API specification.

```typescript
import { Schema } from "@effect/schema";
import { Effect, pipe } from "effect";
import { Api, Client, RouterBuilder } from "effect-http";
import { NodeServer } from "effect-http-node";
import { Api } from "effect-http";

const User = Schema.struct({
name: Schema.string,
Expand All @@ -84,6 +82,9 @@ const api = Api.api({ title: "Users API" }).pipe(
Create the app implementation.

```typescript
import { Effect, pipe } from "effect";
import { RouterBuilder } from "effect-http";

const app = pipe(
RouterBuilder.make(api),
RouterBuilder.handle("getUser", ({ query }) =>
Expand All @@ -96,21 +97,27 @@ const app = pipe(
Now, we can generate an object providing the HTTP client interface using `Client.make`.

```typescript
import { Client } from "effect-http";

const client = Client.make(api, { baseUrl: "http://localhost:3000" });
```

Spawn the server on port 3000,

```typescript
app.pipe(NodeServer.listen({ port: 3000 }), runMain);
import { NodeRuntime } from "@effect/platform-node"
import { NodeServer } from "effect-http-node";

app.pipe(NodeServer.listen({ port: 3000 }), NodeRuntime.runMain);
```

and call it using the `client`.

```ts
const callServer = pipe(
const response = pipe(
client.getUser({ query: { id: 12 } }),
Effect.flatMap((user) => Effect.log(`Got ${user.name}, nice!`)),
Effect.scoped,
);
```

Expand Down Expand Up @@ -193,7 +200,7 @@ a mapping from header names onto their schemas. The example below shows an API w
a single endpoint `/hello` which expects a header `X-Client-Id` to be present.

```typescript
import { runMain } from "@effect/platform-node/Runtime";
import { NodeRuntime } from "@effect/platform-node";
import { Schema } from "@effect/schema";
import { pipe } from "effect";
import { Api, ExampleServer, RouterBuilder } from "effect-http";
Expand All @@ -212,7 +219,7 @@ pipe(
ExampleServer.make(api),
RouterBuilder.build,
NodeServer.listen({ port: 3000 }),
runMain,
NodeRuntime.runMain,
);
```

Expand Down Expand Up @@ -403,6 +410,7 @@ import { NodeTesting } from 'effect-http-node';
test("test /hello endpoint", async () => {
const response = await NodeTesting.make(app, api).pipe(
Effect.flatMap((client) => client.hello({ query: { input: 12 } })),
Effect.scoped,
Effect.runPromise,
);

Expand Down Expand Up @@ -481,39 +489,35 @@ so we can see the failure behavior.

```typescript
interface UserRepository {
existsByName: (name: string) => Effect.Effect<never, never, boolean>;
store: (user: string) => Effect.Effect<never, never, void>;
userExistsByName: (name: string) => Effect.Effect<boolean>;
storeUser: (user: string) => Effect.Effect<void>;
}

const UserRepository = Context.Tag<UserRepository>();
const UserRepository = Context.GenericTag<UserRepository>("UserRepository");

const mockUserRepository = UserRepository.of({
existsByName: () => Effect.succeed(true),
store: () => Effect.unit,
userExistsByName: () => Effect.succeed(true),
storeUser: () => Effect.unit,
});

const { userExistsByName, storeUser } = Effect.serviceFunctions(UserRepository);
```

And finally, we have the actual `App` implementation.

```typescript

const app = RouterBuilder.make(api).pipe(
RouterBuilder.handle("storeUser", ({ body }) =>
pipe(
Effect.flatMap(UserRepository, (userRepository) =>
userRepository.existsByName(body.name),
),
userExistsByName(body.name),
Effect.filterOrFail(
(alreadyExists) => !alreadyExists,
() => ServerError.conflictError(`User "${body.name}" already exists.`),
),
Effect.flatMap(() =>
Effect.flatMap(UserRepository, (repository) =>
repository.store(body.name),
),
),
Effect.andThen(storeUser(body.name)),
Effect.map(() => `User "${body.name}" stored.`),
),
),
)),
RouterBuilder.build,
);
```
Expand All @@ -525,7 +529,7 @@ the `mockUserRepository` service.
app.pipe(
NodeServer.listen({ port: 3000 }),
Effect.provideService(UserRepository, mockUserRepository),
Effect.runPromise,
NodeRuntime.runMain
);
```

Expand Down Expand Up @@ -570,7 +574,7 @@ This enables separability of concers for big APIs and provides information for
generation of tags for the OpenAPI specification.
```typescript
import { runMain } from "@effect/platform-node/Runtime";
import { NodeRuntime } from "@effect/platform-node";
import { Schema } from "@effect/schema";
import { Api, ExampleServer, RouterBuilder } from "effect-http";
import { NodeServer } from "effect-http-node";
Expand Down Expand Up @@ -604,7 +608,7 @@ const api = Api.api().pipe(
ExampleServer.make(api).pipe(
RouterBuilder.build,
NodeServer.listen({ port: 3000 }),
runMain,
NodeRuntime.runMain,
);
```
Expand Down Expand Up @@ -638,8 +642,8 @@ For an operation-level description, call the API endpoint method (`Api.get`,
desired description.
```ts
import { runMain } from "@effect/platform-node/Runtime";
import * as Schema from "@effect/schema/Schema";
import { NodeRuntime } from "@effect/platform-node";
import { Schema } from "@effect/schema";
import { Effect, pipe } from "effect";
import { Api, RouterBuilder } from "effect-http";
import { NodeServer } from "effect-http-node";
Expand Down Expand Up @@ -676,7 +680,7 @@ const app = RouterBuilder.make(api).pipe(
RouterBuilder.build,
);
app.pipe(NodeServer.listen({ port: 3000 }), runMain);
app.pipe(NodeServer.listen({ port: 3000 }), NodeRuntime.runMain);
```
## Representations
Expand Down Expand Up @@ -716,7 +720,7 @@ be used, and if there is no representation matching the incomming `Accept` media
it will choose the first representation in the list.
```ts
import { runMain } from "@effect/platform-node/Runtime";
import { NodeRuntime } from "@effect/platform-node";
import { Schema } from "@effect/schema";
import { Effect } from "effect";
import { Api, Representation, RouterBuilder } from "effect-http";
Expand Down Expand Up @@ -745,7 +749,7 @@ const program = app.pipe(
Effect.provide(PrettyLogger.layer()),
);
runMain(program);
NodeRuntime.runMain(program);
```
Try running the server above and call the root path with different
Expand Down Expand Up @@ -790,7 +794,7 @@ helpful in the following and probably many more cases.
Use `ExampleServer.make` combinator to generate a `RouterBuilder` from an `Api`.
```typescript
import { runMain } from "@effect/platform-node/Runtime";
import { NodeRuntime } from "@effect/platform-node";
import { Schema } from "@effect/schema";
import { Effect, pipe } from "effect";
import { Api, ExampleServer, RouterBuilder } from "effect-http";
Expand All @@ -807,7 +811,7 @@ pipe(
ExampleServer.make(api),
RouterBuilder.build,
NodeServer.listen({ port: 3000 }),
runMain,
NodeRuntime.runMain,
);
```
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"@effect/dtslint": "^0.0.4",
"@effect/eslint-plugin": "^0.1.2",
"@effect/language-service": "^0.1.0",
"@types/node": "^20.11.18",
"@types/node": "^20.11.19",
"@types/swagger-ui-dist": "^3.30.4",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"@vitest/coverage-v8": "^1.2.2",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@vitest/coverage-v8": "^1.3.0",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"eslint": "^8.56.0",
"eslint-import-resolver-typescript": "^3.6.1",
Expand All @@ -50,7 +50,7 @@
"rimraf": "^5.0.5",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
"vitest": "^1.3.0"
},
"pnpm": {
"patchedDependencies": {
Expand Down
16 changes: 9 additions & 7 deletions packages/effect-http-node/examples/conflict-error-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ const api = pipe(
)

interface UserRepository {
existsByName: (name: string) => Effect.Effect<boolean>
store: (user: string) => Effect.Effect<void>
userExistsByName: (name: string) => Effect.Effect<boolean>
storeUser: (user: string) => Effect.Effect<void>
}

const UserRepository = Context.GenericTag<UserRepository>("@services/UserRepository")
const UserRepository = Context.GenericTag<UserRepository>("UserRepository")

const mockUserRepository = UserRepository.of({
existsByName: () => Effect.succeed(true),
store: () => Effect.unit
userExistsByName: () => Effect.succeed(true),
storeUser: () => Effect.unit
})

const { storeUser, userExistsByName } = Effect.serviceFunctions(UserRepository)

const app = RouterBuilder.make(api).pipe(
RouterBuilder.handle("storeUser", ({ body }) =>
pipe(
Effect.flatMap(UserRepository, (userRepository) => userRepository.existsByName(body.name)),
userExistsByName(body.name),
Effect.filterOrFail(
(alreadyExists) => !alreadyExists,
() => ServerError.conflictError(`User "${body.name}" already exists.`)
),
Effect.flatMap(() => Effect.flatMap(UserRepository, (repository) => repository.store(body.name))),
Effect.andThen(storeUser(body.name)),
Effect.map(() => `User "${body.name}" stored.`)
)),
RouterBuilder.build,
Expand Down
2 changes: 1 addition & 1 deletion packages/effect-http-node/examples/esm-example/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export const CreateItemResponse = Item.pipe(Schema.pick("id", "createdAt"))
export const GetItemsQuery = Item.pipe(
Schema.pick("title", "content"),
Schema.extend(Schema.struct({ id: IntegerFromString })),
Schema.partial
(schema) => Schema.partial(schema)
)
export type GetItemsQuery = Schema.Schema.To<typeof GetItemsQuery>
3 changes: 2 additions & 1 deletion packages/effect-http-node/examples/handle-raw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpServer } from "@effect/platform"
import { NodeRuntime } from "@effect/platform-node"
import { Schema } from "@effect/schema"
import { Effect } from "effect"
import { Api, RouterBuilder } from "effect-http"
Expand Down Expand Up @@ -31,4 +32,4 @@ const program = app.pipe(
Effect.provide(PrettyLogger.layer())
)

Effect.runPromise(program)
NodeRuntime.runMain(program)
2 changes: 1 addition & 1 deletion packages/effect-http-node/examples/headers-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Effect.all(
Effect.catchAll((e) => Effect.logInfo(`Error ${JSON.stringify(e)}`)),
ReadonlyArray.replicate(1000000)
)
).pipe(Effect.runPromise)
).pipe(Effect.scoped, Effect.runPromise)
3 changes: 2 additions & 1 deletion packages/effect-http-node/examples/readme-quickstart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const client = Client.make(api, { baseUrl: "http://localhost:3000" })

const program = pipe(
client.getUser({ query: { id: 12 } }),
Effect.flatMap((user) => Effect.log(`Got ${user.name}, nice!`))
Effect.flatMap((user) => Effect.log(`Got ${user.name}, nice!`)),
Effect.scoped
)

Effect.runPromise(program)
24 changes: 12 additions & 12 deletions packages/effect-http-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
"coverage": "vitest --coverage"
},
"dependencies": {
"swagger-ui-dist": "^5.11.3"
"swagger-ui-dist": "^5.11.7"
},
"peerDependencies": {
"@effect/platform": "^0.44.2",
"@effect/platform-node": "^0.43.2",
"@effect/schema": "^0.62.1",
"effect": "^2.3.1",
"@effect/platform": "^0.45.5",
"@effect/platform-node": "^0.44.6",
"@effect/schema": "^0.62.8",
"effect": "^2.3.7",
"effect-http": "workspace:^"
},
"devDependencies": {
"@effect/platform": "^0.44.2",
"@effect/platform-bun": "^0.31.2",
"@effect/platform-node": "^0.43.2",
"@effect/schema": "^0.62.1",
"@types/node": "^20.11.17",
"effect": "^2.3.1",
"@effect/platform": "^0.45.5",
"@effect/platform-bun": "^0.32.5",
"@effect/platform-node": "^0.44.6",
"@effect/schema": "^0.62.8",
"@types/node": "^20.11.19",
"effect": "^2.3.7",
"effect-http": "workspace:^",
"effect-log": "^0.29.0"
"effect-log": "^0.30.0"
}
}
2 changes: 1 addition & 1 deletion packages/effect-http-node/src/NodeTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const makeRaw: <R, E>(
app: HttpServer.app.Default<R, E>
) => Effect.Effect<
HttpClient.client.Client<
never,
Scope.Scope,
HttpClient.error.HttpClientError,
HttpClient.response.ClientResponse
>,
Expand Down
Loading

0 comments on commit 549859b

Please sign in to comment.