Skip to content

Commit

Permalink
feat(@astro/cloudflare): improve DX for runtime typing (#8560)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr authored Sep 18, 2023
1 parent c9bbd30 commit 3da5d84
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-pigs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

add the option to type environment variables using a generic
12 changes: 10 additions & 2 deletions packages/integrations/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ If you're using the `advanced` runtime, you can type the `runtime` object as fol
/// <reference types="astro/client" />
import type { AdvancedRuntime } from '@astrojs/cloudflare';

type ENV = {
SERVER_URL: string;
}

declare namespace App {
interface Locals extends AdvancedRuntime {
interface Locals extends AdvancedRuntime<ENV> {
user: {
name: string;
surname: string;
Expand All @@ -132,8 +136,12 @@ If you're using the `directory` runtime, you can type the `runtime` object as fo
/// <reference types="astro/client" />
import type { DirectoryRuntime } from '@astrojs/cloudflare';

type ENV = {
SERVER_URL: string;
}

declare namespace App {
interface Locals extends DirectoryRuntime {
interface Locals extends DirectoryRuntime<ENV> {
user: {
name: string;
surname: string;
Expand Down
5 changes: 2 additions & 3 deletions packages/integrations/cloudflare/src/server.advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ if (!isNode) {

type Env = {
ASSETS: { fetch: (req: Request) => Promise<Response> };
name: string;
};

export interface AdvancedRuntime {
export interface AdvancedRuntime<T extends object = object> {
runtime: {
waitUntil: (promise: Promise<any>) => void;
env: Env;
env: Env & T;
cf: CFRequest['cf'];
caches: typeof caches;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/cloudflare/src/server.directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ if (!isNode) {
process.env = getProcessEnvProxy();
}

export interface DirectoryRuntime {
export interface DirectoryRuntime<T extends object = object> {
runtime: {
waitUntil: (promise: Promise<any>) => void;
env: EventContext<unknown, string, unknown>['env'];
env: EventContext<unknown, string, unknown>['env'] & T;
cf: CFRequest['cf'];
caches: typeof caches;
};
Expand Down

0 comments on commit 3da5d84

Please sign in to comment.