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

This commit is contained in:
Alexander Niebuhr 2023-09-18 11:44:19 +02:00 committed by GitHub
parent c9bbd304ca
commit 3da5d8404e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---
add the option to type environment variables using a generic

View file

@ -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;
@ -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;

View file

@ -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;
};

View file

@ -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;
};