@elysiajs/cookie
Advanced tools
Comparing version 0.5.2 to 0.6.0-beta.0
@@ -1,2 +0,2 @@ | ||
import type { Elysia } from 'elysia'; | ||
import { Elysia } from 'elysia'; | ||
import { type CookieSerializeOptions } from 'cookie'; | ||
@@ -14,4 +14,6 @@ export interface SetCookieOptions extends CookieSerializeOptions { | ||
} | ||
export declare const cookie: ({ signed, secret: secretKey, ...defaultOptions }?: CookieOptions) => (app: Elysia) => Elysia<{ | ||
export declare const cookie: (options?: CookieOptions) => Elysia<{ | ||
path: ""; | ||
store: {}; | ||
error: {}; | ||
request: { | ||
@@ -27,4 +29,8 @@ unsignCookie: (value: string) => { | ||
schema: {}; | ||
meta: Record<typeof import("elysia").SCHEMA, {}> & Record<typeof import("elysia").DEFS, {}> & Record<typeof import("elysia").EXPOSED, {}>; | ||
meta: { | ||
schema: {}; | ||
defs: {}; | ||
exposed: {}; | ||
}; | ||
}>; | ||
export default cookie; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cookie = void 0; | ||
const elysia_1 = require("elysia"); | ||
const cookie_1 = require("cookie"); | ||
const cookie_signature_1 = require("cookie-signature"); | ||
const cookie = ({ signed, secret: secretKey, ...defaultOptions } = {}) => (app) => { | ||
const cookie = (options = {}) => { | ||
const { signed, secret: secretKey, ...defaultOptions } = options; | ||
const secret = !secretKey | ||
@@ -13,3 +15,6 @@ ? undefined | ||
const isStringKey = typeof secret === 'string'; | ||
return app | ||
return new elysia_1.Elysia({ | ||
name: '@elysiajs/cookie', | ||
seed: options | ||
}) | ||
.decorate('unsignCookie', ((value) => { | ||
@@ -16,0 +21,0 @@ if (!secret) |
@@ -1,2 +0,2 @@ | ||
import type { Elysia } from 'elysia'; | ||
import { Elysia } from 'elysia'; | ||
import { type CookieSerializeOptions } from 'cookie'; | ||
@@ -14,4 +14,6 @@ export interface SetCookieOptions extends CookieSerializeOptions { | ||
} | ||
export declare const cookie: ({ signed, secret: secretKey, ...defaultOptions }?: CookieOptions) => (app: Elysia) => Elysia<{ | ||
export declare const cookie: (options?: CookieOptions) => Elysia<{ | ||
path: ""; | ||
store: {}; | ||
error: {}; | ||
request: { | ||
@@ -27,4 +29,8 @@ unsignCookie: (value: string) => { | ||
schema: {}; | ||
meta: Record<typeof import("elysia").SCHEMA, {}> & Record<typeof import("elysia").DEFS, {}> & Record<typeof import("elysia").EXPOSED, {}>; | ||
meta: { | ||
schema: {}; | ||
defs: {}; | ||
exposed: {}; | ||
}; | ||
}>; | ||
export default cookie; |
@@ -0,4 +1,6 @@ | ||
import { Elysia } from 'elysia'; | ||
import { serialize, parse } from 'cookie'; | ||
import { sign, unsign } from 'cookie-signature'; | ||
export const cookie = ({ signed, secret: secretKey, ...defaultOptions } = {}) => (app) => { | ||
export const cookie = (options = {}) => { | ||
const { signed, secret: secretKey, ...defaultOptions } = options; | ||
const secret = !secretKey | ||
@@ -10,3 +12,6 @@ ? undefined | ||
const isStringKey = typeof secret === 'string'; | ||
return app | ||
return new Elysia({ | ||
name: '@elysiajs/cookie', | ||
seed: options | ||
}) | ||
.decorate('unsignCookie', ((value) => { | ||
@@ -13,0 +18,0 @@ if (!secret) |
{ | ||
"name": "@elysiajs/cookie", | ||
"version": "0.5.2", | ||
"version": "0.6.0-beta.0", | ||
"description": "Plugin for Elysia that add supports for get/set cookie", | ||
@@ -44,8 +44,8 @@ "author": { | ||
"peerDependencies": { | ||
"elysia": ">= 0.5.12" | ||
"elysia": ">= 0.6.0-alpha.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.1.4", | ||
"bun-types": "^0.5.8", | ||
"elysia": "0.5.12", | ||
"bun-types": "^0.7.0", | ||
"elysia": "^0.6.0-alpha.4", | ||
"eslint": "^8.40.0", | ||
@@ -52,0 +52,0 @@ "rimraf": "4.3", |
182
src/index.ts
@@ -1,2 +0,2 @@ | ||
import type { Elysia, Handler } from 'elysia' | ||
import { Elysia, Handler } from 'elysia' | ||
@@ -29,116 +29,110 @@ import { serialize, parse, type CookieSerializeOptions } from 'cookie' | ||
export const cookie = | ||
({ signed, secret: secretKey, ...defaultOptions }: CookieOptions = {}) => | ||
(app: Elysia) => { | ||
const secret = !secretKey | ||
? undefined | ||
: typeof secretKey === 'string' | ||
? secretKey | ||
: secretKey[0] | ||
export const cookie = (options: CookieOptions = {}) => { | ||
const { signed, secret: secretKey, ...defaultOptions } = options | ||
const isStringKey = typeof secret === 'string' | ||
const secret = !secretKey | ||
? undefined | ||
: typeof secretKey === 'string' | ||
? secretKey | ||
: secretKey[0] | ||
return app | ||
.decorate('unsignCookie', ((value: string) => { | ||
if (!secret) | ||
throw new Error('No secret is provided to cookie plugin') | ||
const isStringKey = typeof secret === 'string' | ||
let unsigned: false | string = isStringKey | ||
? unsign(value, secret) | ||
: false | ||
return new Elysia({ | ||
// For deduplication to work you need to define a plugin name (or anything) | ||
name: '@elysiajs/cookie', | ||
// (optional) define seed, if not provided plugin will only be defined once | ||
seed: options | ||
}) | ||
.decorate('unsignCookie', ((value: string) => { | ||
if (!secret) | ||
throw new Error('No secret is provided to cookie plugin') | ||
if (isStringKey === false) | ||
for (let i = 0; i < secret.length; i++) { | ||
const temp = unsign(value, secret[i]) | ||
let unsigned: false | string = isStringKey | ||
? unsign(value, secret) | ||
: false | ||
if (temp) { | ||
unsigned = temp | ||
break | ||
} | ||
if (isStringKey === false) | ||
for (let i = 0; i < secret.length; i++) { | ||
const temp = unsign(value, secret[i]) | ||
if (temp) { | ||
unsigned = temp | ||
break | ||
} | ||
return { | ||
valid: unsigned !== false, | ||
value: unsigned || undefined | ||
} | ||
}) as (value: string) => | ||
| { | ||
valid: true | ||
value: string | ||
} | ||
| { | ||
valid: false | ||
value: undefined | ||
}) | ||
.derive((context) => { | ||
let _cookie: Record<string, string> | ||
const getCookie = () => { | ||
if (_cookie) return _cookie | ||
return { | ||
valid: unsigned !== false, | ||
value: unsigned || undefined | ||
} | ||
}) as (value: string) => | ||
| { | ||
valid: true | ||
value: string | ||
} | ||
| { | ||
valid: false | ||
value: undefined | ||
}) | ||
.derive((context) => { | ||
let _cookie: Record<string, string> | ||
try { | ||
const headerCookie = | ||
context.request.headers.get('cookie') | ||
const getCookie = () => { | ||
if (_cookie) return _cookie | ||
_cookie = headerCookie ? parse(headerCookie) : {} | ||
} catch (error) { | ||
_cookie = {} | ||
} | ||
try { | ||
const headerCookie = context.request.headers.get('cookie') | ||
return _cookie | ||
_cookie = headerCookie ? parse(headerCookie) : {} | ||
} catch (error) { | ||
_cookie = {} | ||
} | ||
return { | ||
get cookie() { | ||
return getCookie() | ||
}, | ||
setCookie( | ||
name, | ||
value, | ||
{ signed = false, ...options } = {} | ||
) { | ||
if (signed) { | ||
if (!secret) | ||
throw new Error( | ||
'No secret is provided to cookie plugin' | ||
) | ||
return _cookie | ||
} | ||
value = sign(value, secret) | ||
} | ||
return { | ||
get cookie() { | ||
return getCookie() | ||
}, | ||
setCookie(name, value, { signed = false, ...options } = {}) { | ||
if (signed) { | ||
if (!secret) | ||
throw new Error( | ||
'No secret is provided to cookie plugin' | ||
) | ||
if (!Array.isArray(context.set.headers['Set-Cookie'])) | ||
// @ts-ignore | ||
context.set.headers['Set-Cookie'] = [] | ||
value = sign(value, secret) | ||
} | ||
if (!Array.isArray(context.set.headers['Set-Cookie'])) | ||
// @ts-ignore | ||
context.set.headers['Set-Cookie'].push( | ||
serialize(name, value, { | ||
path: '/', | ||
...defaultOptions, | ||
...options | ||
}) | ||
) | ||
context.set.headers['Set-Cookie'] = [] | ||
if (!_cookie) getCookie() | ||
_cookie[name] = value | ||
}, | ||
removeCookie(name: string) { | ||
if (!getCookie()[name]) return | ||
// @ts-ignore | ||
context.set.headers['Set-Cookie'].push( | ||
serialize(name, value, { | ||
path: '/', | ||
...defaultOptions, | ||
...options | ||
}) | ||
) | ||
context.set.headers['Set-Cookie'] = serialize( | ||
name, | ||
'', | ||
{ | ||
expires: new Date( | ||
'Thu, Jan 01 1970 00:00:00 UTC' | ||
) | ||
} | ||
) | ||
if (!_cookie) getCookie() | ||
_cookie[name] = value | ||
}, | ||
removeCookie(name: string) { | ||
if (!getCookie()[name]) return | ||
delete _cookie[name] | ||
} | ||
} as CookieRequest | ||
}) | ||
} | ||
context.set.headers['Set-Cookie'] = serialize(name, '', { | ||
expires: new Date('Thu, Jan 01 1970 00:00:00 UTC') | ||
}) | ||
delete _cookie[name] | ||
} | ||
} as CookieRequest | ||
}) | ||
} | ||
export default cookie |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
347
21286