feat(@astro/cloudflare): improve DX for runtime typing (#8560)
This commit is contained in:
parent
c9bbd304ca
commit
3da5d8404e
4 changed files with 19 additions and 7 deletions
5
.changeset/thin-pigs-mate.md
Normal file
5
.changeset/thin-pigs-mate.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/cloudflare': patch
|
||||
---
|
||||
|
||||
add the option to type environment variables using a generic
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue