# svecure

> Secure your load functions with wrappers that will handle errorchecking for you.

Latest version **0.0.3** (published 2023-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install svecure
pnpm add svecure
yarn add svecure
bun add svecure
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2023-03-03 |
| First published | 2023-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Axel Froborg |
| Maintainers | afroborg |
| Keywords | svelte, sveltekit, load, data-fetching |

## Links

- npm: https://www.npmjs.com/package/svecure
- Repository: https://github.com/afroborg/svecure
- Homepage: https://github.com/afroborg/svecure#readme
- Issues: https://github.com/afroborg/svecure/issues
- npm.io page: https://npm.io/package/svecure

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2023-03-03
- 0.0.2 — 2023-03-02
- 0.0.1 — 2023-03-02

## README

# 🔐 Svecure - Secure your load functions

Secure your load functions with wrappers that will handle errorchecking for you.

## Installation

```bash
npm install svecure

# or yarn
yarn add svecure

# or pnpm
pnpm add svecure
```

## Usage

```ts
// +(page|layout).ts

import { createLoadVerifier } from 'svecure';
import { PageLoad } from './$types';

const withVerification = createLoadVerifier<PageLoad>(() => {
	// ...your verification logic
	return true;
});

export const load = withVerification(() => {
	return {
		// ...your data
	};
});
```

```ts
// +(page|layout).server.ts

import { createLoadVerifier } from 'svecure';
import { PageServerLoad } from './$types';

const withVerification = createLoadVerifier<PageServerLoad>(() => {
	// ...your verification logic
	return true;
});

export const load = withVerification(() => {
	return {
		// ...your data
	};
});
```

### Custom error messages

If the verification fails, the load functino will by default throw a SvelteKit [`error`](https://kit.svelte.dev/docs/modules#sveltejs-kit-error) with a status of `401` and a message of `Unauthorized`. You can customize this by passing a second argument to `createLoadVerifier`:

```ts
const withVerification = createLoadVerifier<PageLoad>(
	() => {
		// ...your verification logic
		return false;
	},
	{
		status: 403,
		message: 'You are not allowed to access this page'
	}
);
```

Or with a custom function (if you for example need to redirect to a login page):

```ts
import { redirect } from '@sveltejs/kit';

const withVerification = createLoadVerifier<PageLoad>(
	() => {
		// ...your verification logic
		return false;
	},
	() => {
		throw redirect(302, '/login');
	}
);
```

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