This is a simple RPC provider for Sveltekit
+ adapter-node
, inspired by telefunc
.
Disclaimer: It has only been tested and used with vite-plugin-svelte: 3.x
We found telefunc
to be magical and hence brittle at times. This plugin intends to be more simple and straightforward.
Usage is very similar to telefunc
. See the examples/
directory for a complete project setup.
# To install the dev time utilities
npm install --save-dev vite-plugin-sveltekit-telephone-dev
# To install the production time utilities
npm install --save vite-plugin-sveltekit-telephone-dev
Define remotely callable functions in any file with telephone.ts
extension. Recommeded location is src/lib/tele/*.telephone.ts
.
/_telephone
// src/routes/_telephone/+server.ts
import { json, type RequestHandler, error as skError } from '@sveltejs/kit'
import { functionMap } from '$lib/client/_vite-sveltekit-telephone'
import { handleRoute } from 'vite-plugin-sveltekit-telephone-prod';
const GET: RequestHandler = async (event) => {
const cookies = event.cookies;
const response = await handleRoute(functionMap, {
url: event.request.url,
method: event.request.method,
body: await event.request.json(),
context: {
cookies,
},
})
return json(response);
}
export { GET, GET as POST }
When the RPC functions are called on the client side, they are automatically proxied through the /_telephone
route.
Use getContext()
to get the context that you can inject via the hanlder for /_telephone
route.
When the RPC functions are called on the server side, they are processed as direct function calls.
getContext()
will throw an exception in this case. Use getContextOrNull()
, which returns null instead of throwing an exception.
Else, use withContext(context, fn)
to inject the given context before calling fn
.
/// src/types/TelephoneContext.d.ts
import type { Cookies } from '@sveltejs/kit'
declare module 'telephone' {
namespace Telephone {
interface Context {
cookies: Cookies
}
}
}