Skip to content

Commit 7178fec

Browse files
committed
add telefunc
1 parent 63459f6 commit 7178fec

File tree

9 files changed

+80
-1
lines changed

9 files changed

+80
-1
lines changed

examples/svelte-kit/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"lint": "prettier --plugin-search-dir . --check .",
1212
"format": "prettier --plugin-search-dir . --write ."
1313
},
14+
"dependencies": {
15+
"telefunc": "0.1.36"
16+
},
1417
"devDependencies": {
1518
"@sveltejs/adapter-auto": "next",
1619
"@sveltejs/kit": "next",
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const database = {
2+
value: 42
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { database } from '$lib/database';
2+
3+
export { load };
4+
5+
// For the page's initial data, we use SvelteKit instead of Telefunc; see https://telefunc.com/svelte-kit#initial-page-data
6+
const load: import('./$types').PageServerLoad = async () => {
7+
const { value } = database;
8+
return {
9+
value
10+
};
11+
};
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
<script>
2+
import Counter from './Counter.svelte';
3+
/** @type {import('./$types').PageData} */
4+
export let data;
5+
</script>
6+
17
<h1>Welcome to SvelteKit</h1>
28
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
9+
<p><Counter count={data.value} /></p>
10+
<p>Refresh the page and observe that the counter value is preserved.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { onCounterIncrement } from './Counter.telefunc';
3+
export let count: number;
4+
</script>
5+
6+
<div class="counter">
7+
<span>Counter: {count}</span>
8+
<button on:click={async () => {count = await onCounterIncrement(-1)}}>-1</button>
9+
<button on:click={async () => {count = await onCounterIncrement(+1)}}>+1</button>
10+
<button on:click={async () => {count = await onCounterIncrement(-10)}}>-10</button>
11+
<button on:click={async () => {count = await onCounterIncrement(+10)}}>+10</button>
12+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { database } from '$lib/database';
2+
3+
export { onCounterIncrement };
4+
5+
// Telefunc ensures that `diff` is a `number` at runtime, see https://telefunc.com/shield#typescript
6+
async function onCounterIncrement(diff: number) {
7+
database.value = database.value + diff;
8+
return database.value;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { telefunc } from 'telefunc';
2+
import type { RequestHandler } from './$types';
3+
4+
const GET: RequestHandler = async (event) => {
5+
const response = await telefunc({
6+
url: event.request.url,
7+
method: event.request.method,
8+
body: await event.request.text(),
9+
context: {
10+
// We pass the `context` object here, see https://telefunc.com/getContext
11+
someContext: 'hello'
12+
}
13+
});
14+
return new Response(response.body, {
15+
headers: new Headers({ contentType: response.contentType }),
16+
status: response.statusCode
17+
});
18+
};
19+
20+
export { GET, GET as POST };

examples/svelte-kit/src/telefunc.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'telefunc';
2+
3+
declare module 'telefunc' {
4+
namespace Telefunc {
5+
interface Context {
6+
/* Globally define the type of the `context` object here, see https://telefunc.com/getContext#typescript
7+
* For example:
8+
user: null | { id: number, name: string }
9+
*/
10+
}
11+
}
12+
}

examples/svelte-kit/vite.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { sveltekit } from '@sveltejs/kit/vite';
22
import type { UserConfig } from 'vite';
3+
import { telefunc } from 'telefunc/vite';
34

45
const config: UserConfig = {
5-
plugins: [sveltekit()]
6+
plugins: [sveltekit(), telefunc()]
67
};
78

89
export default config;

0 commit comments

Comments
 (0)