Skip to content

Commit

Permalink
feat: add deleteCookie utility (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: pooya parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Mar 11, 2022
1 parent 860efc5 commit dd3c855
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Instead of adding helpers to `req` and `res`, h3 exposes them as composable util
- `useCookies(req)`
- `useCookie(req, name)`
- `setCookie(res, name, value, opts?)`
- `deleteCookie(res, name, opts?)`
- `useQuery(req)`
- `send(res, data, type?)`
- `sendRedirect(res, location, code=302)`
Expand Down
16 changes: 16 additions & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ export function setCookie (res: ServerResponse, name: string, value: string, ser
const cookieStr = serialize(name, value, serializeOptions)
appendHeader(res, 'Set-Cookie', cookieStr)
}

/**
* Set a cookie value by name.
* @param res {ServerResponse} A ServerResponse object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
* @param name Name of the cookie to delete
* @param serializeOptions {CookieSerializeOptions} Cookie options
* ```ts
* deleteCookie(res, 'SessionId')
* ```
*/
export function deleteCookie (res: ServerResponse, name: string, serializeOptions?: CookieSerializeOptions) {
setCookie(res, name, '', {
...serializeOptions,
maxAge: 0
})
}

0 comments on commit dd3c855

Please sign in to comment.