Skip to content

Commit

Permalink
add response cookies type (#234)
Browse files Browse the repository at this point in the history
* add response cookies type

* add type checking

* Update types/index.d.ts

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>

* Revert "Update types/index.d.ts"

This reverts commit ed76688.

* apply code review

* code review

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
trim21 and Uzlopak authored Dec 6, 2022
1 parent be327ba commit 0c9d75c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ declare namespace inject {
Request?: object,
}

/**
* https://github.com/nfriedly/set-cookie-parser/blob/3eab8b7d5d12c8ed87832532861c1a35520cf5b3/lib/set-cookie.js#L41
*/
interface Cookie {
name: string;
value: string;
expires?: Date;
maxAge?: number;
secure?: boolean;
httpOnly?: boolean;
sameSite?: string;
[name: string]: unknown
}

export interface Response {
raw: {
res: http.ServerResponse,
Expand All @@ -71,7 +85,7 @@ declare namespace inject {
payload: string
body: string
json: <T = any>() => T
cookies: Array<object>
cookies: Array<Cookie>
}

export interface Chain extends Promise<Response> {
Expand Down
11 changes: 10 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ const expectResponse = function (res: Response) {
expectAssignable<Function>(res.json)
expectAssignable<http.ServerResponse>(res.raw.res)
expectAssignable<http.IncomingMessage>(res.raw.req)
console.log(res.cookies)
expectAssignable<Array<any>>(res.cookies)
const cookie = res.cookies[0]
expectType<string>(cookie.name)
expectType<string>(cookie.value)
expectType<Date | undefined>(cookie.expires)
expectType<number | undefined>(cookie.maxAge)
expectType<boolean | undefined>(cookie.httpOnly)
expectType<boolean | undefined>(cookie.secure)
expectType<string | undefined>(cookie.sameSite)
expectType<unknown | undefined>(cookie.additional)
}

expectType<DispatchFunc>(dispatch)
Expand Down

0 comments on commit 0c9d75c

Please sign in to comment.