@routup/cookie
Version:
Cookie plugin for routup.
1 lines • 4.87 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../src/utils.ts","../src/request.ts","../src/handler.ts","../src/module.ts","../src/response.ts","../src/index.ts"],"sourcesContent":["import type { IAppEvent } from 'routup';\nimport { parse } from 'cookie-es';\nimport type { ParseOptions } from './types';\n\nexport function parseRequestCookies(event: IAppEvent, options?: ParseOptions) {\n return parse(event.headers.get('cookie') || '', options || {});\n}\n\nexport function isObject(item: unknown) : item is Record<string, any> {\n return (\n !!item &&\n typeof item === 'object' &&\n !Array.isArray(item)\n );\n}\n","import type { IAppEvent } from 'routup';\nimport { isObject } from './utils';\n\nconst CookieSymbol = Symbol.for('@routup/cookie:ReqCookie');\n\nexport function useRequestCookies(\n event: IAppEvent,\n) : Record<string, string> {\n if (CookieSymbol in event.store) {\n return event.store[CookieSymbol] as Record<string, string>;\n }\n\n return {};\n}\n\nexport function hasRequestCookies(event: IAppEvent) {\n return CookieSymbol in event.store &&\n isObject(event.store[CookieSymbol]);\n}\n\nexport function useRequestCookie(event: IAppEvent, name: string) : string | undefined {\n return useRequestCookies(event)[name];\n}\n\nexport function setRequestCookies(event: IAppEvent, key: string, value: unknown) : void;\nexport function setRequestCookies(event: IAppEvent, record: Record<string, any>) : void;\nexport function setRequestCookies(event: IAppEvent, key: Record<string, any> | string, value?: unknown) : void {\n if (isObject(key)) {\n event.store[CookieSymbol] = key;\n return;\n }\n\n const existing = CookieSymbol in event.store ?\n event.store[CookieSymbol] as Record<string, any> :\n {};\n event.store[CookieSymbol] = { ...existing, [key]: value };\n}\n","import {\n defineCoreHandler,\n} from 'routup';\n\nimport {\n hasRequestCookies,\n setRequestCookies,\n} from './request';\n\nimport type { ParseOptions } from './types';\nimport { parseRequestCookies } from './utils';\n\nexport function createHandler(options?: ParseOptions) {\n return defineCoreHandler((event) => {\n if (hasRequestCookies(event)) {\n return event.next();\n }\n\n setRequestCookies(event, parseRequestCookies(event, options));\n\n return event.next();\n });\n}\n","import type { Plugin } from 'routup';\nimport { createHandler } from './handler';\nimport type { Options } from './types';\n\nexport function cookie(options: Options = {}) : Plugin {\n return {\n name: 'cookie',\n install: (router) => {\n router.use(createHandler(options.parse));\n },\n };\n}\n","import type { IAppEvent } from 'routup';\nimport { appendResponseHeader } from 'routup';\nimport { serialize } from 'cookie-es';\nimport type { SerializeOptions } from './types';\n\nexport function setResponseCookie(event: IAppEvent, name: string, value: string, options?: SerializeOptions) {\n appendResponseHeader(event, 'set-cookie', serialize(name, value, {\n path: '/',\n ...(options || {}),\n }));\n}\n\n/* istanbul ignore next */\nexport function unsetResponseCookie(event: IAppEvent, name: string, options?: SerializeOptions) {\n setResponseCookie(event, name, '', {\n ...(options || {}),\n maxAge: 0,\n });\n}\n","import {\n cookie,\n} from './module';\n\nexport * from './module';\nexport * from './request';\nexport * from './response';\nexport * from './types';\nexport * from './utils';\n\nexport default cookie;\n"],"mappings":";;;AAIA,SAAgB,oBAAoB,OAAkB,SAAwB;CAC1E,OAAO,MAAM,MAAM,QAAQ,IAAI,QAAQ,KAAK,IAAI,WAAW,CAAC,CAAC;AACjE;AAEA,SAAgB,SAAS,MAA6C;CAClE,OACI,CAAC,CAAC,QACF,OAAO,SAAS,YAChB,CAAC,MAAM,QAAQ,IAAI;AAE3B;;;ACXA,MAAM,eAAe,OAAO,IAAI,0BAA0B;AAE1D,SAAgB,kBACZ,OACuB;CACvB,IAAI,gBAAgB,MAAM,OACtB,OAAO,MAAM,MAAM;CAGvB,OAAO,CAAC;AACZ;AAEA,SAAgB,kBAAkB,OAAkB;CAChD,OAAO,gBAAgB,MAAM,SACzB,SAAS,MAAM,MAAM,aAAa;AAC1C;AAEA,SAAgB,iBAAiB,OAAkB,MAAmC;CAClF,OAAO,kBAAkB,KAAK,EAAE;AACpC;AAIA,SAAgB,kBAAkB,OAAkB,KAAmC,OAAwB;CAC3G,IAAI,SAAS,GAAG,GAAG;EACf,MAAM,MAAM,gBAAgB;EAC5B;CACJ;CA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