From 37ea5acece429e8d41aff5377223d67343abe3ed Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 17 Jan 2025 09:48:36 +0000 Subject: [PATCH] Start to fetch the about-us page using HttpClient Note the request is failing, but the result is ignored. Refs #2186 --- integration/base.ts | 3 ++- src/GhostPage.ts | 14 ++++++++++---- src/ghost.ts | 2 +- src/index.ts | 3 ++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/integration/base.ts b/integration/base.ts index 1d584442e..dc9a2d442 100644 --- a/integration/base.ts +++ b/integration/base.ts @@ -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, @@ -1325,6 +1325,7 @@ const appFixtures: Fixtures, 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() })), diff --git a/src/GhostPage.ts b/src/GhostPage.ts index d2c67435a..d58243a69 100644 --- a/src/GhostPage.ts +++ b/src/GhostPage.ts @@ -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') {} @@ -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( @@ -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)), diff --git a/src/ghost.ts b/src/ghost.ts index 24e9194ce..d849bbe2b 100644 --- a/src/ghost.ts +++ b/src/ghost.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index 3da855e5a..7a092c3b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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* () {