Skip to content

Commit

Permalink
Use dedicated constructor
Browse files Browse the repository at this point in the history
Refs #2141
  • Loading branch information
thewilkybarkid committed Dec 20, 2024
1 parent ad70b73 commit 756e956
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
7 changes: 2 additions & 5 deletions src/LegacyRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers, HttpRouter, HttpServerResponse } from '@effect/platform'
import { HttpRouter, HttpServerResponse } from '@effect/platform'
import type { Doi } from 'doi-ts'
import { Effect, pipe, Schema } from 'effect'
import { format } from 'fp-ts-routing'
Expand Down Expand Up @@ -91,8 +91,5 @@ export const LegacyRouter = HttpRouter.fromIterable([
])

function movedPermanently(location: string) {
return HttpServerResponse.empty({
status: StatusCodes.MOVED_PERMANENTLY,
headers: Headers.fromInput({ location }),
})
return HttpServerResponse.redirect(location, { status: StatusCodes.MOVED_PERMANENTLY })
}
14 changes: 3 additions & 11 deletions src/Router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Cookies,
Headers,
type HttpMethod,
HttpMiddleware,
HttpRouter,
Expand Down Expand Up @@ -205,16 +204,12 @@ function toHttpServerResponse(
): Effect.Effect<HttpServerResponse.HttpServerResponse, never, Locale | TemplatePage | ExpressConfig | PublicUrl> {
return Effect.gen(function* () {
if (response._tag === 'RedirectResponse') {
return yield* HttpServerResponse.empty({
status: response.status,
headers: Headers.fromInput({ Location: response.location.toString() }),
})
return yield* HttpServerResponse.redirect(response.location, { status: response.status })
}

if (response._tag === 'FlashMessageResponse') {
return yield* HttpServerResponse.empty({
return yield* HttpServerResponse.redirect(response.location, {
status: StatusCodes.SEE_OTHER,
headers: Headers.fromInput({ Location: response.location.toString() }),
cookies: Cookies.fromIterable([
Cookies.unsafeMakeCookie('flash-message', response.message, { httpOnly: true, path: '/' }),
]),
Expand All @@ -229,10 +224,7 @@ function toHttpServerResponse(
state: new URL(`${publicUrl.origin}${response.location}`).href,
})

return yield* HttpServerResponse.empty({
status: StatusCodes.MOVED_TEMPORARILY,
headers: Headers.fromInput({ Location: location.href }),
})
return yield* HttpServerResponse.redirect(location, { status: StatusCodes.MOVED_TEMPORARILY })
}

const locale = yield* Locale
Expand Down
3 changes: 1 addition & 2 deletions src/WebApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const removeTrailingSlashes = HttpMiddleware.make(app =>
return yield* app
}

return yield* HttpServerResponse.empty({
headers: Headers.fromInput({ Location: request.url.slice(0, request.url.length - 1) }),
return yield* HttpServerResponse.redirect(request.url.slice(0, request.url.length - 1), {
status: StatusCodes.MOVED_PERMANENTLY,
})
}),
Expand Down
9 changes: 2 additions & 7 deletions test/LegacyRouter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers, HttpServerRequest, HttpServerResponse } from '@effect/platform'
import { HttpServerRequest, HttpServerResponse } from '@effect/platform'
import { test } from '@fast-check/jest'
import { describe, expect } from '@jest/globals'
import { Effect, TestContext } from 'effect'
Expand Down Expand Up @@ -67,12 +67,7 @@ describe('LegacyRouter', () => {

const response = yield* Effect.provideService(_.LegacyRouter, HttpServerRequest.HttpServerRequest, request)

expect(response).toStrictEqual(
HttpServerResponse.empty({
status: StatusCodes.MOVED_PERMANENTLY,
headers: Headers.fromInput({ location: expected }),
}),
)
expect(response).toStrictEqual(HttpServerResponse.redirect(expected, { status: StatusCodes.MOVED_PERMANENTLY }))
}).pipe(Effect.provide(TestContext.TestContext), Effect.runSync),
)
})

0 comments on commit 756e956

Please sign in to comment.