Cookie (@halsp/cookie)

安装 @halsp/cookie 以开启 Halsp 的 Cookie 功能

基于 cookieopen in new windowset-cookie-parseropen in new window

安装

npm install @halsp/cookie

快速开始

startup.ts

import "@halsp/cookie";

startup.useCookie();

获取请求 cookies

ctx 获取

const cookies = ctx.cookies;

等价于从 ctx.req 获取

const cookies = ctx.req.cookies;

也可以获取任一 name 的 cookie

const name = ctx.cookies.name;

设置响应 cookies

ctx.cookies 赋值

ctx.cookies = {
  account: "halsp.org",
};

等价于给 ctx.res.cookies 赋值

ctx.res.cookies = {
  account: "halsp.org",
};

也可以赋值一个对象以支持参数

ctx.cookies = {
  account: {
    value: "halsp.org",
    httpOnly: true,
    maxAge: 0,
  },
};

还可以给某个 name 的 cookie 赋值

ctx.res.cookies.account = "halsp.org";

注意不能这样赋值

ctx.cookies.account = "halsp.org";

ctx.cookies 属性的 get 方法是返回请求头的 cookie,等同于

ctx.req.cookies.account = "halsp.org";

因此这种写法其实是给请求头的 cookie 赋值,不会抛出错误但在控制台会提示一个 error

请求头/响应头

  • 请求的 cookies 从请求头 cookie 取得
  • 响应的 cookies 会写到响应头 Set-Cookie

配置

useCookie 接收一个配置参数,有下面两个字段

import "@halsp/cookie";

startup.useCookie({
  serialize: {},
  parse: {
    httpOnly: true
  },
});

序列化配置也可以单次使用,在设置 cookie 时赋值一个 SetCookieValue 对象

export type SetCookieValueWithArgs = {
  value: string;
  path?: string | undefined;
  expires?: Date | undefined;
  maxAge?: number | undefined;
  domain?: string | undefined;
  secure?: boolean | undefined;
  httpOnly?: boolean | undefined;
  sameSite?: string | undefined;
};
ctx.res.cookies.account = {
  value: "halsp.org",
  httpOnly: true,
  maxAge: 0,
};