Merge pull request #19059 from BendingBender/cookies

[cookies] update typings to v0.7
This commit is contained in:
Daniel Rosenwasser
2017-08-18 13:07:53 -07:00
committed by GitHub
2 changed files with 39 additions and 8 deletions

View File

@@ -1,23 +1,35 @@
import * as Cookies from 'cookies';
import * as http from 'http';
import * as Keygrip from 'keygrip';
import * as express from 'express';
import * as connect from 'connect';
const server = http.createServer((req, res) => {
const cookies = new Cookies(req, res);
new Cookies(req, res, {keys: []});
new Cookies(req, res, {keys: new Keygrip([])});
new Cookies(req, res, {secure: true});
let unsigned: string;
let signed: string;
let tampered: string;
if (req.url === "/set") {
cookies
// set a regular cookie
// set a regular cookie
.set("unsigned", "foo", { httpOnly: false })
// set a signed cookie
// set a signed cookie
.set("signed", "bar", { signed: true })
// mimic a signed cookie, but with a bogus signature
// mimic a signed cookie, but with a bogus signature
.set("tampered", "baz")
.set("tampered.sig", "bogus");
.set("tampered.sig", "bogus")
// sameSite option
.set("samesite", "same", {sameSite: 'lax'})
.set("samesite", "same", {sameSite: 'strict'})
.set("samesite", "same", {sameSite: false});
res.writeHead(302, { Location: "/" });
return res.end("Now let's check.");
@@ -37,3 +49,9 @@ const server = http.createServer((req, res) => {
"tampered: " + tampered + "\n\n"
);
});
const eApp = express();
eApp.use(Cookies.express([]));
const cApp = connect();
cApp.use(Cookies.connect([]));

View File

@@ -1,11 +1,15 @@
// Type definitions for cookies 0.6
// Type definitions for cookies 0.7
// Project: https://github.com/pillarjs/cookies
// Definitions by: Wang Zishi <https://github.com/WangZishi/>, jKey Lu <https://github.com/jkeylu>
// Definitions by: Wang Zishi <https://github.com/WangZishi/>
// jKey Lu <https://github.com/jkeylu>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { IncomingMessage, ServerResponse } from 'http';
import * as Keygrip from 'keygrip';
import * as express from 'express';
import * as connect from 'connect';
interface Cookies {
secure: boolean;
@@ -38,7 +42,7 @@ declare namespace Cookies {
type IOptions = SetOption;
interface Option {
keys: string[] | Keygrip;
keys?: string[] | Keygrip;
secure?: boolean;
}
@@ -78,6 +82,11 @@ declare namespace Cookies {
* and not made available to client JavaScript (true by default).
*/
httpOnly?: boolean;
/**
* a boolean or string indicating whether the cookie is a "same site" cookie (false by default).
* This can be set to 'strict', 'lax', or true (which maps to 'strict').
*/
sameSite?: 'strict' | 'lax' | boolean;
/**
* a boolean indicating whether the cookie is to be signed (false by default).
* If this is true, another cookie of the same name with the .sig suffix
@@ -102,7 +111,7 @@ declare namespace Cookies {
name: string;
value: string;
/**
* "maxage"" is deprecated, use "maxAge" instead
* "maxage" is deprecated, use "maxAge" instead
*/
maxage: number;
maxAge: number;
@@ -111,6 +120,7 @@ declare namespace Cookies {
domain: string;
secure: boolean;
httpOnly: boolean;
sameSite: boolean;
overwrite: boolean;
toString(): string;
@@ -146,6 +156,9 @@ interface CookiesFunction {
Cookie: {
new (name: string, value?: string, attrs?: Cookies.CookieAttr): Cookies.Cookie;
};
express(keys: string[] | Keygrip): express.Handler;
connect(keys: string[] | Keygrip): connect.NextHandleFunction;
}
declare const Cookies: CookiesFunction;