astro/packages/integrations/cloudflare/src/runtime.ts
Daniel d151d9f3f2
enable access to cloudflare runtime (#5103)
* enable access to cloudflare runtime

* added get runtime api
added context to the runtime in "advanced" mode

* added typings and adjusted some return vars

* added default types

* added usage description to changeset and readme

Co-authored-by: AirBorne04 <unknown>
Co-authored-by: AirBorne04 <>
2022-10-26 09:46:25 -04:00

28 lines
697 B
TypeScript

export type WorkerRuntime<T = unknown> = {
name: 'cloudflare';
env: T;
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
};
export type PagesRuntime<T = unknown, U = unknown> = {
name: 'cloudflare';
env: T;
functionPath: string;
params: Record<string, string>;
data: U;
waitUntil(promise: Promise<any>): void;
next(request: Request): void;
};
export function getRuntime<T = unknown, U = unknown>(
request: Request
): WorkerRuntime<T> | PagesRuntime<T, U> {
if (!!request) {
return Reflect.get(request, Symbol.for('runtime'));
} else {
throw new Error(
'To retrieve the current cloudflare runtime you need to pass in the Astro request object'
);
}
}