Skip to content

Commit

Permalink
Start to fetch the about-us page using HttpClient
Browse files Browse the repository at this point in the history
Note the request is failing, but the result is ignored.

Refs #2186
  • Loading branch information
thewilkybarkid committed Jan 17, 2025
1 parent 235102d commit 37ea5ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion integration/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FetchHttpClient } from '@effect/platform'
import { NodeHttpServer } from '@effect/platform-node'
import { NodeHttpClient, NodeHttpServer } from '@effect/platform-node'
import { LibsqlClient } from '@effect/sql-libsql'
import {
test as baseTest,
Expand Down Expand Up @@ -1325,6 +1325,7 @@ const appFixtures: Fixtures<AppFixtures, Record<never, never>, PlaywrightTestArg
Effect.provide(Nodemailer.layer(nodemailer)),
Effect.provideService(PublicUrl, new URL(`http://localhost:${port}`)),
Effect.provideService(SessionSecret, Redacted.make('')),
Effect.provide(NodeHttpClient.layer),
Effect.provideService(FetchHttpClient.Fetch, fetch as unknown as typeof globalThis.fetch),
Effect.provide(LibsqlClient.layer({ url: `file:${testInfo.outputPath('database.db')}` })),
Effect.provide(TemplatePage.optionsLayer({ fathomId: Option.none(), environmentLabel: Option.none() })),
Expand Down
14 changes: 10 additions & 4 deletions src/GhostPage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FetchHttpClient } from '@effect/platform'
import { FetchHttpClient, HttpClient } from '@effect/platform'
import { Context, Data, Effect, flow, Layer, Match, pipe } from 'effect'
import * as R from 'fp-ts/lib/Reader.js'
import type * as TE from 'fp-ts/lib/TaskEither.js'
import * as FptsToEffect from './FptsToEffect.js'
import { getPage, GhostApi } from './ghost.js'
import { getPage, getPageWithEffect, GhostApi } from './ghost.js'
import type { Html } from './html.js'

export class PageIsNotFound extends Data.TaggedError('PageIsNotFound') {}
Expand All @@ -22,7 +22,7 @@ export interface GetPageFromGhostEnv {
export const getPageFromGhost = (id: string) =>
R.asks(({ getPageFromGhost }: GetPageFromGhostEnv) => getPageFromGhost(id))

const loadWithCachingClient = () => Effect.log('Loading about-us page')
const loadWithCachingClient = (id: string) => pipe(getPageWithEffect(id), Effect.ignoreLogged)

const legacyFetch = (ghostApi: typeof GhostApi.Service, fetch: typeof FetchHttpClient.Fetch.Service) => (id: string) =>
pipe(
Expand All @@ -43,12 +43,18 @@ const legacyFetch = (ghostApi: typeof GhostApi.Service, fetch: typeof FetchHttpC
export const layer = Layer.effect(
GetPageFromGhost,
Effect.gen(function* () {
const httpClient = yield* HttpClient.HttpClient
const fetch = yield* FetchHttpClient.Fetch
const ghostApi = yield* GhostApi
return id =>
pipe(
Effect.if(id === '6154aa157741400e8722bb14', {
onTrue: loadWithCachingClient,
onTrue: () =>
pipe(
loadWithCachingClient(id),
Effect.provideService(GhostApi, ghostApi),
Effect.provideService(HttpClient.HttpClient, httpClient),
),
onFalse: () => Effect.void,
}),
Effect.andThen(legacyFetch(ghostApi, fetch)(id)),
Expand Down
2 changes: 1 addition & 1 deletion src/ghost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GhostPageNotFound extends Data.TaggedError('GhostPageNotFound') {}

class GhostPageUnavailable extends Data.TaggedError('GhostPageUnavailable') {}

const getPageWithEffect = (id: string) =>
export const getPageWithEffect = (id: string) =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient
const ghostApi = yield* GhostApi
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FetchHttpClient } from '@effect/platform'
import { NodeHttpServer, NodeRuntime } from '@effect/platform-node'
import { NodeHttpClient, NodeHttpServer, NodeRuntime } from '@effect/platform-node'
import { LibsqlClient } from '@effect/sql-libsql'
import { Config, Effect, Function, Layer, Logger, LogLevel, Schema } from 'effect'
import { pipe } from 'fp-ts/lib/function.js'
Expand All @@ -26,6 +26,7 @@ pipe(
Layer.mergeAll(
NodeHttpServer.layerConfig(() => createServer(), { port: Config.succeed(3000) }),
Layer.effect(ExpressConfig, ExpressConfigLive),
NodeHttpClient.layer,
Layer.effect(
FetchHttpClient.Fetch,
Effect.gen(function* () {
Expand Down

0 comments on commit 37ea5ac

Please sign in to comment.