3.3.0 • Published 2 years ago

sveltekit-api-fetch v3.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

SvelteKit API Fetch 🔗🌐

Making SvelteKit fetch and validation of server endpoints easier than ever!

Feature list

  • Type safe fetch functions to create a better coding experience.
  • Usage of the powerful zod library to parse the incomming data.
  • Plug and play and opt-in structure.

Installation

Install the package with your favorite NodeJs package manager.

npm i sveltekit-api-fetch zod

Get started

Follow these 3 simple steps to harnest the power of zod and TypeScript in your API endpoints:

  1. Simply add the vite plugin :

    // vite.config.ts
    import { sveltekit } from "@sveltejs/kit/vite";
    import { defineConfig } from "vite";
    import { apiFetch } from "sveltekit-api-fetch/vite";
    
    export default defineConfig({
        plugins: [sveltekit(), apiFetch()],
    });
  2. Create a zod object to validate the endpoint's request body, and pass it to the apiValidate function.

    // src/routes/api/+server.ts
    import { json } from "@sveltejs/kit";
    import { z } from "zod";
    import { apiValidate, type EndpointSchema } from "sveltekit-api-fetch/server";
    import type { RequestHandler } from "./$types";
    
    const postSchema = {
        email: z.string().email(),
        password: z.string().min(8),
    } } satisfies EndpointSchema;
    
    export const POST = (async ({ request }) => {
        const { data } = await apiValidate(request, postSchema);
    
        return json({
            success: true,
            jwt: db.createJWT({ email: data.email, password: data.password }),
        });
    }) satisfies RequestHandler;
  3. All done, you can finally enjoy the new type safe api !

    <script>
        import { api } from "sveltekit-api-fetch";
        import { onMount } from "svelte";
    
        let res: Promise<Response> | undefined;
    
        onMount(() => res = api.POST("/api", { body: { email: "laurent@guibi.ca", password: "******" } }));
    </sricpt>

Contributing

This package is still in beta. Do not hesitate to contact me if you have feedback of any kind! :)

Ideas, bug reports, PRs and the likes are welcome as a Github issue or as a discussion!

3.3.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago