# sveltekit-typesafe-api

> Adding type safety to SvelteKit's server endpoints and fetches.

Latest version **0.1.2** (published 2024-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install sveltekit-typesafe-api
pnpm add sveltekit-typesafe-api
yarn add sveltekit-typesafe-api
bun add sveltekit-typesafe-api
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2024-01-18 |
| First published | 2023-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 46.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Laurent Stéphenne |
| Maintainers | guibi |
| Keywords | sveltekit, typescript, api, type-safety, endpoint, zod, validation |

## Links

- npm: https://www.npmjs.com/package/sveltekit-typesafe-api
- Repository: https://github.com/Guibi1/sveltekit-typesafe-api
- Homepage: https://www.npmjs.com/package/sveltekit-typesafe-api
- Issues: https://github.com/Guibi1/sveltekit-typesafe-api/issues
- npm.io page: https://npm.io/package/sveltekit-typesafe-api

## Dependencies (3)

- [zod](https://npm.io/package/zod.md) ^3.22.4
- [typescript](https://npm.io/package/typescript.md) ^5.3.3
- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) ^2.3.5

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.1.2 (latest) — 2024-01-18
- 0.1.1 — 2024-01-18
- 0.1.0 — 2023-09-01

## README

# SvelteKit TypeSafe API Fetch 🔗🌐

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

## Feature list

-   Type safe `fetch`-like 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.

```sh
npm i sveltekit-typesafe-api 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 :

    ```ts
    // vite.config.ts
    import { sveltekit } from "@sveltejs/kit/vite";
    import { defineConfig } from "vite";
    import { typesafeApi } from "sveltekit-typesafe-api/vite";

    export default defineConfig({
        plugins: [sveltekit(), typesafeApi()],
    });
    ```

2. Create a `zod` object to validate the endpoint's request body, and pass it to the `validate` function.

    ```ts
    // src/routes/api/+server.ts
    import { json } from "@sveltejs/kit";
    import { z } from "zod";
    import { validate } from "sveltekit-typesafe-api/server";
    import type { RequestHandler } from "./$types";

    export const POST = (async ({ request }) => {
        const { data } = await validate(request, {
            email: z.string().email(),
            password: z.string().min(8),
        });

        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` !

    ```svelte
    <script>
        import { api } from "sveltekit-typesafe-api";

        let res: Promise<Response> | undefined;

        const onClick = () => 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](https://github.com/Guibi1/sveltekit-typesafe-api/issues) or as a [discussion](https://github.com/Guibi1/sveltekit-typesafe-api/discussions)!

---
_Source: https://npm.io/package/sveltekit-typesafe-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
