Skip to content

Commit

Permalink
Surface the context required by the application
Browse files Browse the repository at this point in the history
We've decided to put all the context tags in one module, which different parts of the application can depend on. This keeps the tags separate from the implementations.

Refs #1834
  • Loading branch information
thewilkybarkid committed Sep 4, 2024
1 parent 0db3be5 commit d0adcdd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/Context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Context } from 'effect'
import type { Express as ExpressServer } from 'express'
import type { Redis as IoRedis } from 'ioredis'
import type { LoggerEnv } from 'logger-fp-ts'
import type { EnvVars } from './env.js'

export class DeprecatedEnvVars extends Context.Tag('DeprecatedEnvVars')<DeprecatedEnvVars, EnvVars>() {}

export class DeprecatedLoggerEnv extends Context.Tag('DeprecatedLoggerEnv')<DeprecatedLoggerEnv, LoggerEnv>() {}

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

export class Redis extends Context.Tag('Redis')<Redis, IoRedis>() {}
6 changes: 2 additions & 4 deletions src/ExpressServer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import KeyvRedis from '@keyv/redis'
import { SystemClock } from 'clock-ts'
import { Context, Effect } from 'effect'
import { Effect } from 'effect'
import * as C from 'fp-ts/lib/Console.js'
import { pipe } from 'fp-ts/lib/function.js'
import type { Redis as IoRedis } from 'ioredis'
import Keyv from 'keyv'
import * as L from 'logger-fp-ts'
import fetch from 'make-fetch-happen'
import nodemailer from 'nodemailer'
import { P, match } from 'ts-pattern'
import { app } from './app.js'
import { Redis } from './Context.js'
import { decodeEnv } from './env.js'

export class Redis extends Context.Tag('Redis')<Redis, IoRedis>() {}

export const expressServer = Effect.gen(function* () {
const redis = yield* Redis

Expand Down
6 changes: 2 additions & 4 deletions src/Redis.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Context, Effect } from 'effect'
import { Effect } from 'effect'
import { toError } from 'fp-ts/lib/Either.js'
import { Redis as IoRedis } from 'ioredis'
import * as L from 'logger-fp-ts'
import { DeprecatedEnvVars } from './env.js'

export class DeprecatedLoggerEnv extends Context.Tag('DeprecatedLoggerEnv')<DeprecatedLoggerEnv, L.LoggerEnv>() {}
import { DeprecatedEnvVars, DeprecatedLoggerEnv } from './Context.js'

const makeRedis = Effect.gen(function* () {
const env = yield* DeprecatedEnvVars
Expand Down
3 changes: 1 addition & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Context } from 'effect'
import * as C from 'fp-ts/lib/Console.js'
import * as E from 'fp-ts/lib/Either.js'
import * as IOE from 'fp-ts/lib/IOEither.js'
Expand All @@ -9,7 +8,7 @@ import { isOrcid } from 'orcid-id-ts'
import { v4 } from 'uuid-ts'
import { type NonEmptyString, NonEmptyStringC } from './types/string.js'

export class DeprecatedEnvVars extends Context.Tag('DeprecatedEnvVars')<DeprecatedEnvVars, D.TypeOf<typeof EnvD>>() {}
export type EnvVars = D.TypeOf<typeof EnvD>

export function decodeEnv(process: NodeJS.Process) {
return pipe(
Expand Down
12 changes: 5 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { NodeRuntime } from '@effect/platform-node'
import cacache from 'cacache'
import { SystemClock } from 'clock-ts'
import * as dns from 'dns'
import { Context, Effect, Layer } from 'effect'
import { Effect, Layer } from 'effect'
import * as C from 'fp-ts/lib/Console.js'
import * as E from 'fp-ts/lib/Either.js'
import { pipe } from 'fp-ts/lib/function.js'
import type { JsonRecord } from 'fp-ts/lib/Json.js'
import * as L from 'logger-fp-ts'
import type { app } from './app.js'
import { decodeEnv, DeprecatedEnvVars } from './env.js'
import { expressServer, Redis } from './ExpressServer.js'
import { DeprecatedLoggerEnv, redisLifecycle } from './Redis.js'
import { DeprecatedEnvVars, DeprecatedLoggerEnv, Express, Redis } from './Context.js'
import { decodeEnv } from './env.js'
import { expressServer } from './ExpressServer.js'
import { redisLifecycle } from './Redis.js'

const env = decodeEnv(process)()

Expand All @@ -32,8 +32,6 @@ if (env.VERIFY_CACHE) {
.catch((error: unknown) => L.errorP('Failed to verify cache')({ error: E.toError(error).message })(loggerEnv)())
}

class Express extends Context.Tag('Express')<Express, ReturnType<typeof app>>() {}

const expressServerLifecycle = Effect.acquireRelease(
Effect.gen(function* () {
const app = yield* Express
Expand Down

0 comments on commit d0adcdd

Please sign in to comment.