Skip to content

Commit 3fa7def

Browse files
improve makeAstroEndpoint
1 parent b5f8340 commit 3fa7def

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"Pdrmn",
6363
"Pfrns",
6464
"Phchat",
65+
"PKCE",
6566
"pkgs",
6667
"Plbbcp",
6768
"Plbc",

apps/tinyburg.app/api/handler.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@ import { Context, Effect, type ManagedRuntime, type Scope } from "effect";
55
import { AppRuntime } from "./runtime";
66
import { AstroContext } from "./tags";
77

8-
export const makeAstroEndpoint = async <E>(
9-
app: HttpApp.Default<E, AstroContext | Scope.Scope | ManagedRuntime.ManagedRuntime.Context<typeof AppRuntime>>,
8+
export const makeAstroEndpoint = <E>(
9+
app: HttpApp.Default<
10+
E,
11+
| AstroContext
12+
| Scope.Scope
13+
| HttpServerRequest.HttpServerRequest
14+
| ManagedRuntime.ManagedRuntime.Context<typeof AppRuntime>
15+
>,
1016
middleware?: HttpMiddleware.HttpMiddleware | undefined
11-
): Promise<APIRoute> => {
12-
const runtime = await AppRuntime.runtime();
13-
const emptyAstroContext = Context.empty() as Context.Context<AstroContext>;
17+
): APIRoute => {
18+
type R = ManagedRuntime.ManagedRuntime.Context<typeof AppRuntime>;
19+
20+
let cachedHandler:
21+
| ((request: Request, context?: Context.Context<never> | undefined) => Promise<Response>)
22+
| undefined = undefined;
23+
1424
const appWithoutAstroContext = Effect.mapInputContext(
1525
app,
16-
(
17-
requiredContext: Context.Context<
18-
| Scope.Scope
19-
| HttpServerRequest.HttpServerRequest
20-
| ManagedRuntime.ManagedRuntime.Context<typeof AppRuntime>
21-
>
22-
) => Context.merge(requiredContext, emptyAstroContext)
26+
(requiredContext: Context.Context<Scope.Scope | HttpServerRequest.HttpServerRequest | R>) =>
27+
Context.merge(requiredContext, Context.empty() as Context.Context<AstroContext>)
2328
);
24-
const handler = HttpApp.toWebHandlerRuntime(runtime)(appWithoutAstroContext, middleware);
29+
2530
return async (apiContext) => {
31+
const runtime = await AppRuntime.runtime();
32+
cachedHandler ??= HttpApp.toWebHandlerRuntime(runtime)(appWithoutAstroContext, middleware);
2633
const context = Context.make(AstroContext, apiContext);
27-
return await handler(apiContext.request, context);
34+
return await cachedHandler(apiContext.request, context);
2835
};
2936
};

apps/tinyburg.app/src/pages/auth/google/callback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Repository } from "../../../../domain/model";
1515
import { OAuthResponseSchema } from "../_shared";
1616
import { GoogleOAuthConfig, tokenUrl } from "./_shared";
1717

18-
export const GET = await Effect.gen(function* () {
18+
export const GET = Effect.gen(function* () {
1919
const config = yield* Effect.orDie(GoogleOAuthConfig);
2020
const request = yield* HttpServerRequest.HttpServerRequest;
2121

apps/tinyburg.app/src/pages/auth/google/login.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { AstroContext } from "../../../../api/tags";
66
import { randomStateGenerator, Sha256CodeChallenge } from "../_shared";
77
import { authUrl, GoogleOAuthConfig } from "./_shared";
88

9-
export const GET = await Effect.gen(function* () {
9+
export const GET = Effect.gen(function* () {
1010
const Astro = yield* AstroContext;
11+
const config = yield* Effect.orDie(GoogleOAuthConfig);
12+
13+
// Generate state and code verifier for PKCE
1114
const state = randomStateGenerator();
1215
const codeVerifier = randomStateGenerator();
13-
const config = yield* Effect.orDie(GoogleOAuthConfig);
1416

1517
// Build the Google OAuth authorization URL
1618
const maybeGoogleAuthorizationUrl = pipe(

0 commit comments

Comments
 (0)