Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@astro/cloudflare): improve DX for runtime typing #8560

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .changeset/thin-pigs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
'@astrojs/cloudflare': patch
---

add option to type environment variables using a generic

```ts
// src/env.d.ts
/// <reference types="astro/client" />
import type { AdvancedRuntime } from '@astrojs/cloudflare';

type ENV = {
SERVER_URL: string;
}

declare namespace App {
interface Locals extends AdvancedRuntime<ENV> {
user: {
name: string;
surname: string;
};
}
}
```


```ts
// src/env.d.ts
/// <reference types="astro/client" />
import type { DirectoryRuntime } from '@astrojs/cloudflare';

type ENV = {
SERVER_URL: string;
}

declare namespace App {
interface Locals extends DirectoryRuntime<ENV> {
user: {
name: string;
surname: string;
};
}
}
```
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
4 changes: 2 additions & 2 deletions packages/integrations/cloudflare/src/server.advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type Env = {
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