Skip to content

Commit 10f2105

Browse files
authored
adapter types (#5386)
* adapter types * rename ambient.d.ts -> placeholders.d.ts * add ambient types to adapter-cloudflare-workers * update README * import ambient types * changeset
1 parent c8c8afc commit 10f2105

File tree

14 files changed

+106
-45
lines changed

14 files changed

+106
-45
lines changed

.changeset/rude-grapes-perform.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
'@sveltejs/adapter-cloudflare-workers': patch
4+
---
5+
6+
Expose App interfaces

packages/adapter-cloudflare-workers/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ Then, you can build your app and deploy it:
6969
wrangler publish
7070
```
7171

72+
## Environment variables
73+
74+
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:
75+
76+
```js
77+
export async function post({ request, platform }) {
78+
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
79+
}
80+
```
81+
82+
To make these types available to your app, reference them in your `src/app.d.ts`:
83+
84+
```diff
85+
/// <reference types="@sveltejs/kit" />
86+
+/// <reference types="@sveltejs/adapter-cloudflare-workers" />
87+
88+
declare namespace App {
89+
interface Platform {
90+
+ env?: {
91+
+ YOUR_KV_NAMESPACE: KVNamespace;
92+
+ YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
93+
+ };
94+
}
95+
}
96+
```
97+
98+
> `platform.env` is only available in the production build. Use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler) to test it locally
99+
72100
## Changelog
73101

74102
[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare-workers/CHANGELOG.md).
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
declare module 'SERVER' {
2-
export { Server } from '@sveltejs/kit';
3-
}
4-
5-
declare module 'MANIFEST' {
6-
import { SSRManifest } from '@sveltejs/kit';
7-
8-
export const manifest: SSRManifest;
9-
export const prerendered: Map<string, { file: string }>;
10-
}
1+
/// <reference types="@cloudflare/workers-types" />
112

12-
declare module '__STATIC_CONTENT_MANIFEST' {
13-
const json: string;
14-
export default json;
3+
declare namespace App {
4+
export interface Platform {
5+
context?: {
6+
waitUntil(promise: Promise<any>): void;
7+
};
8+
caches?: CacheStorage & { default: Cache };
9+
}
1510
}

packages/adapter-cloudflare-workers/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
3030
},
3131
"dependencies": {
32+
"@cloudflare/workers-types": "^3.14.0",
3233
"@iarna/toml": "^2.2.5",
3334
"esbuild": "^0.14.42"
3435
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare module 'SERVER' {
2+
export { Server } from '@sveltejs/kit';
3+
}
4+
5+
declare module 'MANIFEST' {
6+
import { SSRManifest } from '@sveltejs/kit';
7+
8+
export const manifest: SSRManifest;
9+
export const prerendered: Map<string, { file: string }>;
10+
}
11+
12+
declare module '__STATIC_CONTENT_MANIFEST' {
13+
const json: string;
14+
export default json;
15+
}

packages/adapter-cloudflare-workers/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"@sveltejs/kit": ["../kit/types/index"]
1313
}
1414
},
15-
"include": ["**/*.js", "ambient.d.ts"]
15+
"include": ["**/*.js", "placeholders.d.ts"]
1616
}

packages/adapter-cloudflare/README.md

+17-22
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,27 @@ When configuring your project settings, you must use the following settings:
5353
5454
## Environment variables
5555

56-
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:
56+
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:
5757

58-
```diff
59-
// src/app.d.ts
60-
declare namespace App {
61-
interface Locals {}
62-
63-
+ interface Platform {
64-
+ env: {
65-
+ COUNTER: DurableObjectNamespace;
66-
+ };
67-
+ context: {
68-
+ waitUntil(promise: Promise<any>): void;
69-
+ };
70-
+ caches: CacheStorage & { default: Cache }
71-
+ }
72-
73-
interface Session {}
74-
75-
interface Stuff {}
58+
```js
59+
export async function post({ request, platform }) {
60+
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
7661
}
7762
```
7863

79-
```js
80-
export async function post({ request, platform }) {
81-
const counter = platform.env.COUNTER.idFromName('A');
64+
To make these types available to your app, reference them in your `src/app.d.ts`:
65+
66+
```diff
67+
/// <reference types="@sveltejs/kit" />
68+
+/// <reference types="@sveltejs/adapter-cloudflare" />
69+
70+
declare namespace App {
71+
interface Platform {
72+
+ env?: {
73+
+ YOUR_KV_NAMESPACE: KVNamespace;
74+
+ YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
75+
+ };
76+
}
8277
}
8378
```
8479

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
declare module 'SERVER' {
2-
export { Server } from '@sveltejs/kit';
3-
}
4-
5-
declare module 'MANIFEST' {
6-
import { SSRManifest } from '@sveltejs/kit';
1+
/// <reference types="@cloudflare/workers-types" />
72

8-
export const manifest: SSRManifest;
9-
export const prerendered: Set<string>;
3+
declare namespace App {
4+
export interface Platform {
5+
context?: {
6+
waitUntil(promise: Promise<any>): void;
7+
};
8+
caches?: CacheStorage & { default: Cache };
9+
}
1010
}
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { Adapter } from '@sveltejs/kit';
2+
import './ambient.js';
23

34
export default function plugin(): Adapter;

packages/adapter-cloudflare/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"prepublishOnly": "npm run build"
3333
},
3434
"dependencies": {
35+
"@cloudflare/workers-types": "^3.14.0",
3536
"esbuild": "^0.14.42",
3637
"worktop": "0.8.0-next.14"
3738
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module 'SERVER' {
2+
export { Server } from '@sveltejs/kit';
3+
}
4+
5+
declare module 'MANIFEST' {
6+
import { SSRManifest } from '@sveltejs/kit';
7+
8+
export const manifest: SSRManifest;
9+
export const prerendered: Set<string>;
10+
}

packages/adapter-cloudflare/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"@sveltejs/kit": ["../kit/types/index"]
1212
}
1313
},
14-
"include": ["index.js", "ambient.d.ts", "src/worker.ts"]
14+
"include": ["index.js", "placeholders.ts", "src/worker.ts"]
1515
}

packages/adapter-netlify/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { Adapter } from '@sveltejs/kit';
2+
import './ambient.js';
23

34
export default function plugin(opts?: { split?: boolean; edge?: boolean }): Adapter;

pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)