Skip to content

Commit

Permalink
Pass the locale through to Express pages
Browse files Browse the repository at this point in the history
This change avoids duplicating reading the cookie on the legacy pages by passing through the already-read value. This requires a change to our Express usage to allow for request-specific arguments. As we're not using Express to listen for requests, we've favoured this type-safe way (as opposed to Express's standard of modifying the request object).

Refs #2044
  • Loading branch information
thewilkybarkid committed Oct 25, 2024
1 parent 42be6ee commit 736373f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DeprecatedLoggerEnv extends Context.Tag('DeprecatedLoggerEnv')<Depr

export class DeprecatedSleepEnv extends Context.Tag('DeprecatedSleepEnv')<DeprecatedSleepEnv, SleepEnv>() {}

export class Express extends Context.Tag('Express')<Express, ExpressServer>() {}
export class Express extends Context.Tag('Express')<Express, (locale: SupportedLocale) => ExpressServer>() {}

export class ExpressConfig extends Context.Tag('ExpressConfig')<
ExpressConfig,
Expand Down
12 changes: 6 additions & 6 deletions src/ExpressHttpApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { type HttpApp, HttpMiddleware, HttpServerRequest, HttpServerResponse } f
import { NodeHttpServerRequest } from '@effect/platform-node'
import { Effect } from 'effect'
import express, { type ErrorRequestHandler } from 'express'
import { Express } from './Context.js'
import { Express, Locale } from './Context.js'

export const ExpressHttpApp: HttpApp.Default<never, Express | HttpServerRequest.HttpServerRequest> = Effect.gen(
function* () {
export const ExpressHttpApp: HttpApp.Default<never, Express | HttpServerRequest.HttpServerRequest | Locale> =
Effect.gen(function* () {
const expressApp = yield* Express
const request = yield* HttpServerRequest.HttpServerRequest

const nodeRequest = NodeHttpServerRequest.toIncomingMessage(request)
const nodeResponse = NodeHttpServerRequest.toServerResponse(request)
const locale = yield* Locale

return yield* Effect.async<HttpServerResponse.HttpServerResponse>(resume => {
nodeResponse.once('close', () => resume(Effect.succeed(HttpServerResponse.empty())))

express()
.use(expressApp)
.use(expressApp(locale))
.use(((error, req, res, next) => {
if (error instanceof Error && 'code' in error && error.code === 'ERR_HTTP_HEADERS_SENT') {
return next()
Expand All @@ -25,5 +26,4 @@ export const ExpressHttpApp: HttpApp.Default<never, Express | HttpServerRequest.
next(error)
}) satisfies ErrorRequestHandler)(nodeRequest, nodeResponse)
}).pipe(HttpMiddleware.withLoggerDisabled)
},
)
})
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { pageNotFound } from './http-error.js'
import { getUserOnboarding } from './keyv.js'
import { getPreprintIdFromLegacyPreviewUuid, getProfileIdFromLegacyPreviewUuid } from './legacy-prereview.js'
import { type LegacyEnv, legacyRoutes } from './legacy-routes/index.js'
import { DefaultLocale } from './locales/index.js'
import type { SupportedLocale } from './locales/index.js'
import { type MailjetApiEnv, sendEmailWithMailjet } from './mailjet.js'
import { type NodemailerEnv, sendEmailWithNodemailer } from './nodemailer.js'
import { page } from './page.js'
Expand Down Expand Up @@ -85,7 +85,7 @@ const withEnv =
(...a: A) =>
f(...a)(env)

export const app = (config: ConfigEnv) =>
export const app = (config: ConfigEnv) => (locale: SupportedLocale) =>
express()
.disable('x-powered-by')
.use((req, res, next) => {
Expand Down Expand Up @@ -232,7 +232,7 @@ export const app = (config: ConfigEnv) =>
getUserOnboarding: withEnv(getUserOnboarding, env),
getPreprint: withEnv(getPreprint, env),
getPreprintTitle: withEnv(getPreprintTitle, env),
locale: DefaultLocale,
locale,
templatePage: withEnv(page, env),
getPreprintIdFromUuid: withEnv(getPreprintIdFromLegacyPreviewUuid, env),
getProfileIdFromUuid: withEnv(getProfileIdFromLegacyPreviewUuid, env),
Expand Down

0 comments on commit 736373f

Please sign in to comment.