From 756e956f27d081dac56a21cc46f0505a9335612e Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Dec 2024 13:34:23 +0000 Subject: [PATCH] Use dedicated constructor Refs #2141 --- src/LegacyRouter.ts | 7 ++----- src/Router.ts | 14 +++----------- src/WebApp.ts | 3 +-- test/LegacyRouter.test.ts | 9 ++------- 4 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/LegacyRouter.ts b/src/LegacyRouter.ts index cfb0b3735..e51c53d5e 100644 --- a/src/LegacyRouter.ts +++ b/src/LegacyRouter.ts @@ -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' @@ -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 }) } diff --git a/src/Router.ts b/src/Router.ts index 469f6d3e1..b852ad57d 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -1,6 +1,5 @@ import { Cookies, - Headers, type HttpMethod, HttpMiddleware, HttpRouter, @@ -205,16 +204,12 @@ function toHttpServerResponse( ): Effect.Effect { 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: '/' }), ]), @@ -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 diff --git a/src/WebApp.ts b/src/WebApp.ts index 920516c21..2226925d0 100644 --- a/src/WebApp.ts +++ b/src/WebApp.ts @@ -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, }) }), diff --git a/test/LegacyRouter.test.ts b/test/LegacyRouter.test.ts index d07a6923f..c1c9aeccc 100644 --- a/test/LegacyRouter.test.ts +++ b/test/LegacyRouter.test.ts @@ -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' @@ -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), ) })