summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/src/runtime.ts
blob: 03c15d4a3e8b67a4bcb415bcfe7747aa455b043c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// TODO: remove `getRuntime()` in Astro 3.0
import type { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';

export type WorkerRuntime<T = unknown> = {
	name: 'cloudflare';
	env: T;
	waitUntil(promise: Promise<any>): void;
	passThroughOnException(): void;
	caches?: CacheStorage & { default: Cache };
	cf?: IncomingRequestCfProperties;
};

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;
	caches?: CacheStorage & { default: Cache };
	cf?: IncomingRequestCfProperties;
};

/**
 * @deprecated since version 6.8.0
 * The `getRuntime` utility has been deprecated and should be updated to the new [`Astro.locals`](https://docs.astro.build/en/guides/middleware/#locals) API.
 * ```diff
 * - import { getRuntime } from '@astrojs/cloudflare/runtime';
 * - getRuntime(Astro.request);
 *
 * + const runtime = Astro.locals.runtime;
 * ```
 */
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'
		);
	}
}