0.0.8 • Published 2 years ago

@svuick/cookie v0.0.8

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

@svuick/cookie

npm (scoped)

Install

Install with npm

npm i @svuick/cookie

Install with pnpm

pnpm i @svuick/cookie

Install with yarn

yarn add @svuick/cookie

Setup

src/hooks.ts

import { register } from '@svuick/core/hooks';
import cookie from '@svuick/cookie/hooks';

register(cookie);

export { handle } from '@svuick/core/hooks';

The plugin provides a cookie object in the request locals.

src/routes/api/endpoint.ts

export const get: Svuick.RequestHandler = (request) => {
	console.log(request.locals.cookie);
	return {
        body: request.locals.cookie["key"] // or ...cookie.key
		status: 200
	};
};

Documentation

setCookie

src/routes/api/endpoint.ts

import { setCookie } from '@svuick/cookie';

export const post: Svuick.RequestHandler = () => {
	const { headers } = setCookie('key', 'value', {
		maxAge: 12345
        secure: false,
        sameSite: 'lax',
        httpOnly: true
	});

	return { headers };
};

src/hooks.ts

import { setCookie } from '@svuick/cookie';

export const handle: Suick.Handle = async ({ request, resolve }) => {
    const response = await resolve(request);
	return setCookie(response, 'key', 'value', {
		maxAge: 12345
        secure: false,
        sameSite: 'lax',
        httpOnly: true
	});
};

clearCookie

src/routes/api/endpoint.ts

import { clearCookie } from '@svuick/cookie';

export const post: Svuick.RequestHandler = () => {
	const { headers } = clearCookie('key', {
		maxAge: 12345
        secure: false,
        sameSite: 'lax',
        httpOnly: true
	});

	return { headers };
};

src/hooks.ts

import { clearCookie } from '@svuick/cookie';

export const handle: Suick.Handle = async ({ request, resolve }) => {
    const response = await resolve(request);
	return clearCookie(response, 'key', {
		maxAge: 12345
        secure: false,
        sameSite: 'lax',
        httpOnly: true
	});
};

encryptCookie

src/routes/api/endpoint.ts

import { encryptCookie, setCookie } from '@svuick/cookie';

export const post: Svuick.RequestHandler = (request) => {
	const cookieValue = 'secret-data';
	const secureValue = encryptCookie(cookieValue, { key: 'long-and-secure-key' });
	const { headers } = setCookie('key', secureValue);
	return {
		headers,
		status: 204
	};
};

decryptCookie

src/routes/api/endpoint.ts

import { decryptCookie } from '@svuick/cookie';

export const get: Svuick.RequestHandler = (request) => {
	const cookieValue = request.locals.cookie['key'];
	const value = decryptCookie(cookieValue, { key: 'long-and-secure-key' });
	return {
		body: value,
		status: 200
	};
};

Licence

Licensed under MIT.

0.0.3

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago