@flexacms/sveltekit
v0.3.1
Published
SvelteKit integration for the Flexa headless CMS: a server hook that puts a Flexa client on event.locals.
Maintainers
Readme
@flexacms/sveltekit
Flexa for SvelteKit: a server hook that puts a client on event.locals.flexa, available in every server load function and form action.
npm install @flexacms/sveltekit// src/hooks.server.ts
import { env } from '$env/dynamic/private';
import { createFlexaHandle } from '@flexacms/sveltekit';
export const handle = createFlexaHandle({
// url defaults to FLEXA_URL, then https://flexacms.com
url: env.FLEXA_URL,
apiKey: env.FLEXA_API_KEY,
});// src/app.d.ts
import type { FlexaClient } from '@flexacms/sveltekit';
import type { FlexaSchema } from './flexa'; // optional: npx @flexacms/cli generate
declare global {
namespace App {
interface Locals {
flexa: FlexaClient<FlexaSchema>;
}
}
}
export {};// src/routes/blog/[slug]/+page.server.ts
import { error } from '@sveltejs/kit';
export async function load({ locals, params }) {
const post = await locals.flexa.entity('posts').find(params.slug);
if (!post) error(404);
return { post };
}The client uses SvelteKit's event.fetch. Combine the hook with others using sequence() from @sveltejs/kit/hooks.
