# sveltekit-middleware

> Backend middleware library for SvelteKit.

Latest version **0.0.2** (published 2023-04-22) · 0 weekly downloads

## Install

```sh
npm install sveltekit-middleware
pnpm add sveltekit-middleware
yarn add sveltekit-middleware
bun add sveltekit-middleware
```

## 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.0.2 |
| Published | 2023-04-22 |
| First published | 2023-04-22 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | digital-daniel |

## Links

- npm: https://www.npmjs.com/package/sveltekit-middleware
- npm.io page: https://npm.io/package/sveltekit-middleware

## Recent versions

- 0.0.2 (latest) — 2023-04-22
- 0.0.1 — 2023-04-22

## README

# SvelteKit Middleware
Backend middleware library for SvelteKit.

## How to use
1. Install (`npm i sveltekit-middleware`)

2. Update your `src/app.d.ts`: ([Read more](https://kit.svelte.dev/docs/types#app))
```ts
declare global {
	namespace App {
		// interface Error {}
		interface Locals {
			_headers: Record<string, string>;
			// Other locals
		}
		// interface PageData {}
		// interface Platform {}
	}
}

export {};
```

3. Update `src/hooks.server.ts`, example:
```ts
import { useMiddleware } from "sveltekit-middleware"
import type { Handle } from "@sveltejs/kit"

export const handle: Handle = useMiddleware([
	// If you don't use any middleware, the server will respond from the file system (`src/routes`)
	// Put middleware here
])
```

**Middleware is just a plain JavaScript function. If you return a valid response, the server will send that, otherwise it will go to the next function. It can pass on data with the** `locals` **variable.** `locals._headers` will be appended to the response headers.

Example:
```ts
import { useMiddleware } from "sveltekit-middleware"
import { text, type Handle } from "@sveltejs/kit"

export const handle: Handle = useMiddleware([
	// CORS
	({ locals }) => locals._headers["Access-Control-Allow-Origin"] = "*",
	() => text("Hello world!")
])
```

Use the `match` function if you want to run some middleware on specific requests. Use the `respondFromFileSystem` function to render the response from the Svelte files in the `src/routes` folder.
```ts
import { useMiddleware, match, respondFromFileSystem } from "$lib"
import { text, type Handle, error } from "@sveltejs/kit"

export const handle: Handle = useMiddleware([
	match("/api", "POST", () => text("Hello world")),
	match("/", "GET", () => respondFromFileSystem()),
	() => error(404, "Not Found"),
])
```

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