Skip to content

Commit

Permalink
add HttpRouter.concatAll + fix HttpRouter.concat mounts (#3439)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej authored Aug 10, 2024
1 parent 17245a4 commit 630d40e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/odd-ways-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

Add `HttpRouter.concatAll`.
5 changes: 5 additions & 0 deletions .changeset/ten-ties-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

Fix `HttpRouter.concat` - add mounts from both routers.
40 changes: 40 additions & 0 deletions packages/platform-node/test/HttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,44 @@ describe("HttpServer", () => {
const res = yield* HttpClientRequest.get("/").pipe(client)
assert.deepStrictEqual(res.status, 500)
}).pipe(Effect.provide(NodeHttpServer.layerTest)))

const routerA = HttpRouter.empty.pipe(
HttpRouter.get("/a", HttpServerResponse.text("a")),
HttpRouter.mountApp("/ma", HttpServerResponse.text("ma"))
)

const routerB = HttpRouter.empty.pipe(
HttpRouter.get("/b", HttpServerResponse.text("b")),
HttpRouter.mountApp("/mb", HttpServerResponse.text("mb"))
)

it.scoped("concat", () =>
Effect.gen(function*() {
yield* HttpRouter.concat(routerA, routerB).pipe(HttpServer.serveEffect())
const [responseA, responseMountA, responseB, responseMountB] = yield* Effect.all([
HttpClientRequest.get("/a"),
HttpClientRequest.get("/ma"),
HttpClientRequest.get("/b"),
HttpClientRequest.get("/mb")
])
expect(yield* responseA.text).toEqual("a")
expect(yield* responseMountA.text).toEqual("ma")
expect(yield* responseB.text).toEqual("b")
expect(yield* responseMountB.text).toEqual("mb")
}).pipe(Effect.provide(NodeHttpServer.layerTest)))

it.scoped("concatAll", () =>
Effect.gen(function*() {
yield* HttpRouter.concatAll(routerA, routerB).pipe(HttpServer.serveEffect())
const [responseA, responseMountA, responseB, responseMountB] = yield* Effect.all([
HttpClientRequest.get("/a"),
HttpClientRequest.get("/ma"),
HttpClientRequest.get("/b"),
HttpClientRequest.get("/mb")
])
expect(yield* responseA.text).toEqual("a")
expect(yield* responseMountA.text).toEqual("ma")
expect(yield* responseB.text).toEqual("b")
expect(yield* responseMountB.text).toEqual("mb")
}).pipe(Effect.provide(NodeHttpServer.layerTest)))
})
8 changes: 8 additions & 0 deletions packages/platform/dtslint/HttpRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as HttpRouter from "@effect/platform/HttpRouter"

declare const router1: HttpRouter.HttpRouter<"E1", "R1">
declare const router2: HttpRouter.HttpRouter<"E2", "R2">
declare const router3: HttpRouter.HttpRouter<"E3", "R3">

// $ExpectType HttpRouter<"E1" | "E2" | "E3", "R1" | "R2" | "R3">
HttpRouter.concatAll(router1, router2, router3)
9 changes: 9 additions & 0 deletions packages/platform/dtslint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.base.json",
"include": ["."],
"compilerOptions": {
"incremental": false,
"composite": false,
"noUnusedLocals": false
}
}
1 change: 1 addition & 0 deletions packages/platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"build-esm": "tsc -b tsconfig.build.json",
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
"build-annotate": "babel build/esm --plugins annotate-pure-calls --out-dir build/esm --source-maps",
"dtslint": "dtslint dtslint",
"check": "tsc -b tsconfig.json",
"test": "vitest",
"coverage": "vitest --coverage"
Expand Down
8 changes: 8 additions & 0 deletions packages/platform/src/HttpRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ export const concat: {
>
} = internal.concat

/**
* @since 1.0.0
* @category combinators
*/
export const concatAll: <Routers extends ReadonlyArray<HttpRouter<unknown, unknown>>>(
...routers: Routers
) => [Routers[number]] extends [HttpRouter<infer E, infer R>] ? HttpRouter<E, R> : never = internal.concatAll

/**
* @since 1.0.0
* @category routing
Expand Down
20 changes: 19 additions & 1 deletion packages/platform/src/internal/httpRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,25 @@ export const concat = dual<
that: Router.HttpRouter<E1, R1>
) => <E, R>(self: Router.HttpRouter<E, R>) => Router.HttpRouter<E | E1, R | R1>,
<E, R, E1, R1>(self: Router.HttpRouter<E, R>, that: Router.HttpRouter<E1, R1>) => Router.HttpRouter<E | E1, R | R1>
>(2, (self, that) => new RouterImpl(Chunk.appendAll(self.routes, that.routes) as any, self.mounts))
>(2, (self, that) => concatAll(self, that))

/** @internal */
export const concatAll = <Routers extends ReadonlyArray<Router.HttpRouter<E, R>>, E, R>(
...routers: Routers
) =>
new RouterImpl(
routers.reduce((cur, acc) => Chunk.appendAll(cur, acc.routes), Chunk.empty<Router.Route<E, R>>()),
routers.reduce(
(cur, acc) => Chunk.appendAll(cur, acc.mounts),
Chunk.empty<
readonly [
prefix: string,
httpApp: App.Default<E, R>,
options?: { readonly includePrefix?: boolean | undefined } | undefined
]
>()
)
) as any

const removeTrailingSlash = (
path: Router.PathInput
Expand Down

0 comments on commit 630d40e

Please sign in to comment.