diff --git a/.changeset/rude-grapes-perform.md b/.changeset/rude-grapes-perform.md new file mode 100644 index 000000000000..c358a3842d2c --- /dev/null +++ b/.changeset/rude-grapes-perform.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-cloudflare': patch +'@sveltejs/adapter-cloudflare-workers': patch +--- + +Expose App interfaces diff --git a/packages/adapter-cloudflare-workers/README.md b/packages/adapter-cloudflare-workers/README.md index 6d77a06ef720..888d20af2681 100644 --- a/packages/adapter-cloudflare-workers/README.md +++ b/packages/adapter-cloudflare-workers/README.md @@ -69,6 +69,34 @@ Then, you can build your app and deploy it: wrangler publish ``` +## Environment variables + +The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV/DO namespaces etc, is passed to SvelteKit via the `platform` property along with `context` and `caches`, meaning you can access it in hooks and endpoints: + +```js +export async function post({ request, platform }) { + const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x'); +} +``` + +To make these types available to your app, reference them in your `src/app.d.ts`: + +```diff +/// ++/// + +declare namespace App { + interface Platform { ++ env?: { ++ YOUR_KV_NAMESPACE: KVNamespace; ++ YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace; ++ }; + } +} +``` + +> `platform.env` is only available in the production build. Use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler) to test it locally + ## Changelog [The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare-workers/CHANGELOG.md). diff --git a/packages/adapter-cloudflare-workers/ambient.d.ts b/packages/adapter-cloudflare-workers/ambient.d.ts index 78ceb0e62887..dba9b7151295 100644 --- a/packages/adapter-cloudflare-workers/ambient.d.ts +++ b/packages/adapter-cloudflare-workers/ambient.d.ts @@ -1,15 +1,10 @@ -declare module 'SERVER' { - export { Server } from '@sveltejs/kit'; -} - -declare module 'MANIFEST' { - import { SSRManifest } from '@sveltejs/kit'; - - export const manifest: SSRManifest; - export const prerendered: Map; -} +/// -declare module '__STATIC_CONTENT_MANIFEST' { - const json: string; - export default json; +declare namespace App { + export interface Platform { + context?: { + waitUntil(promise: Promise): void; + }; + caches?: CacheStorage & { default: Cache }; + } } diff --git a/packages/adapter-cloudflare-workers/package.json b/packages/adapter-cloudflare-workers/package.json index 7cfb395d423d..da419518353e 100644 --- a/packages/adapter-cloudflare-workers/package.json +++ b/packages/adapter-cloudflare-workers/package.json @@ -29,6 +29,7 @@ "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore" }, "dependencies": { + "@cloudflare/workers-types": "^3.14.0", "@iarna/toml": "^2.2.5", "esbuild": "^0.14.42" }, diff --git a/packages/adapter-cloudflare-workers/placeholders.d.ts b/packages/adapter-cloudflare-workers/placeholders.d.ts new file mode 100644 index 000000000000..78ceb0e62887 --- /dev/null +++ b/packages/adapter-cloudflare-workers/placeholders.d.ts @@ -0,0 +1,15 @@ +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; +} + +declare module 'MANIFEST' { + import { SSRManifest } from '@sveltejs/kit'; + + export const manifest: SSRManifest; + export const prerendered: Map; +} + +declare module '__STATIC_CONTENT_MANIFEST' { + const json: string; + export default json; +} diff --git a/packages/adapter-cloudflare-workers/tsconfig.json b/packages/adapter-cloudflare-workers/tsconfig.json index af2a0c525acf..f4c98fd3be96 100644 --- a/packages/adapter-cloudflare-workers/tsconfig.json +++ b/packages/adapter-cloudflare-workers/tsconfig.json @@ -12,5 +12,5 @@ "@sveltejs/kit": ["../kit/types/index"] } }, - "include": ["**/*.js", "ambient.d.ts"] + "include": ["**/*.js", "placeholders.d.ts"] } diff --git a/packages/adapter-cloudflare/README.md b/packages/adapter-cloudflare/README.md index eb1deb4c433f..c04ee3155334 100644 --- a/packages/adapter-cloudflare/README.md +++ b/packages/adapter-cloudflare/README.md @@ -53,32 +53,27 @@ When configuring your project settings, you must use the following settings: ## Environment variables -The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV namespaces etc, is passed to SvelteKit via the `platform` property along with `context` and `caches`, meaning you can access it in hooks and endpoints: +The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV/DO namespaces etc, is passed to SvelteKit via the `platform` property along with `context` and `caches`, meaning you can access it in hooks and endpoints: -```diff -// src/app.d.ts -declare namespace App { - interface Locals {} - -+ interface Platform { -+ env: { -+ COUNTER: DurableObjectNamespace; -+ }; -+ context: { -+ waitUntil(promise: Promise): void; -+ }; -+ caches: CacheStorage & { default: Cache } -+ } - - interface Session {} - - interface Stuff {} +```js +export async function post({ request, platform }) { + const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x'); } ``` -```js -export async function post({ request, platform }) { - const counter = platform.env.COUNTER.idFromName('A'); +To make these types available to your app, reference them in your `src/app.d.ts`: + +```diff +/// ++/// + +declare namespace App { + interface Platform { ++ env?: { ++ YOUR_KV_NAMESPACE: KVNamespace; ++ YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace; ++ }; + } } ``` diff --git a/packages/adapter-cloudflare/ambient.d.ts b/packages/adapter-cloudflare/ambient.d.ts index 3689c3f5e51e..dba9b7151295 100644 --- a/packages/adapter-cloudflare/ambient.d.ts +++ b/packages/adapter-cloudflare/ambient.d.ts @@ -1,10 +1,10 @@ -declare module 'SERVER' { - export { Server } from '@sveltejs/kit'; -} - -declare module 'MANIFEST' { - import { SSRManifest } from '@sveltejs/kit'; +/// - export const manifest: SSRManifest; - export const prerendered: Set; +declare namespace App { + export interface Platform { + context?: { + waitUntil(promise: Promise): void; + }; + caches?: CacheStorage & { default: Cache }; + } } diff --git a/packages/adapter-cloudflare/index.d.ts b/packages/adapter-cloudflare/index.d.ts index 90af12a05cc1..ed89676dadc4 100644 --- a/packages/adapter-cloudflare/index.d.ts +++ b/packages/adapter-cloudflare/index.d.ts @@ -1,3 +1,4 @@ import { Adapter } from '@sveltejs/kit'; +import './ambient.js'; export default function plugin(): Adapter; diff --git a/packages/adapter-cloudflare/package.json b/packages/adapter-cloudflare/package.json index fe7d3b5807aa..8370e33a9530 100644 --- a/packages/adapter-cloudflare/package.json +++ b/packages/adapter-cloudflare/package.json @@ -32,6 +32,7 @@ "prepublishOnly": "npm run build" }, "dependencies": { + "@cloudflare/workers-types": "^3.14.0", "esbuild": "^0.14.42", "worktop": "0.8.0-next.14" }, diff --git a/packages/adapter-cloudflare/placeholders.ts b/packages/adapter-cloudflare/placeholders.ts new file mode 100644 index 000000000000..3689c3f5e51e --- /dev/null +++ b/packages/adapter-cloudflare/placeholders.ts @@ -0,0 +1,10 @@ +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; +} + +declare module 'MANIFEST' { + import { SSRManifest } from '@sveltejs/kit'; + + export const manifest: SSRManifest; + export const prerendered: Set; +} diff --git a/packages/adapter-cloudflare/tsconfig.json b/packages/adapter-cloudflare/tsconfig.json index d90692441a52..b5a3f7c9ffb7 100644 --- a/packages/adapter-cloudflare/tsconfig.json +++ b/packages/adapter-cloudflare/tsconfig.json @@ -11,5 +11,5 @@ "@sveltejs/kit": ["../kit/types/index"] } }, - "include": ["index.js", "ambient.d.ts", "src/worker.ts"] + "include": ["index.js", "placeholders.ts", "src/worker.ts"] } diff --git a/packages/adapter-netlify/index.d.ts b/packages/adapter-netlify/index.d.ts index 6bdc8db882dc..aef282740b2f 100644 --- a/packages/adapter-netlify/index.d.ts +++ b/packages/adapter-netlify/index.d.ts @@ -1,3 +1,4 @@ import { Adapter } from '@sveltejs/kit'; +import './ambient.js'; export default function plugin(opts?: { split?: boolean; edge?: boolean }): Adapter; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1dd3fc1d92bb..24fd3b95f7c5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -59,12 +59,14 @@ importers: packages/adapter-cloudflare: specifiers: + '@cloudflare/workers-types': ^3.14.0 '@types/node': ^16.11.36 '@types/ws': ^8.5.3 esbuild: ^0.14.42 typescript: ^4.7.2 worktop: 0.8.0-next.14 dependencies: + '@cloudflare/workers-types': 3.14.0 esbuild: 0.14.42 worktop: 0.8.0-next.14 devDependencies: @@ -75,11 +77,13 @@ importers: packages/adapter-cloudflare-workers: specifiers: '@cloudflare/kv-asset-handler': ^0.2.0 + '@cloudflare/workers-types': ^3.14.0 '@iarna/toml': ^2.2.5 '@types/node': ^16.11.36 esbuild: ^0.14.42 typescript: ^4.7.2 dependencies: + '@cloudflare/workers-types': 3.14.0 '@iarna/toml': 2.2.5 esbuild: 0.14.42 devDependencies: @@ -773,6 +777,10 @@ packages: mime: 3.0.0 dev: true + /@cloudflare/workers-types/3.14.0: + resolution: {integrity: sha512-F6lZuzu/vNl9AxOq1U3x2UCi68bcE1O59cKGQ5ntRh3lGQfd3vers6NUag02T7P+qFCHaAp78QN6fRmq12Y3iA==} + dev: false + /@eslint/eslintrc/1.3.0: resolution: {integrity: sha512-UWW0TMTmk2d7hLcWD1/e2g5HDM/HQ3csaLSqXCfqwh4uNDuNqlaKWXmEsL4Cs41Z0KnILNvwbHAah3C2yt06kw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}