From affd3f2db83252f3fa988f6550f751fec0ad208e Mon Sep 17 00:00:00 2001 From: Nikolai Lopin Date: Tue, 10 Dec 2019 20:42:28 +0100 Subject: [PATCH] [@types/react-cookies] Add definitions (#40802) --- types/react-cookies/index.d.ts | 66 ++++++++++++++++++++++ types/react-cookies/react-cookies-tests.ts | 38 +++++++++++++ types/react-cookies/tsconfig.json | 23 ++++++++ types/react-cookies/tslint.json | 1 + 4 files changed, 128 insertions(+) create mode 100644 types/react-cookies/index.d.ts create mode 100644 types/react-cookies/react-cookies-tests.ts create mode 100644 types/react-cookies/tsconfig.json create mode 100644 types/react-cookies/tslint.json diff --git a/types/react-cookies/index.d.ts b/types/react-cookies/index.d.ts new file mode 100644 index 0000000000..d99979b407 --- /dev/null +++ b/types/react-cookies/index.d.ts @@ -0,0 +1,66 @@ +// Type definitions for react-cookies 0.1 +// Project: https://github.com/bukinoshita/react-cookies/ +// Definitions by: Nikolai Lopin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { CookieSerializeOptions } from 'cookie'; +import { Request, Response } from 'express'; + +/** + * Load the cookie value. + * Returns undefined if the cookie does not exist. + * Deserialize any cookie starting with { or [ unless `dotNotParse` is true + * + * @param name string name of the cookie + * @param doNotParse optional boolean value if parse is not needed + */ +export function load(name: string, doNotParse?: boolean): any; + +/** + * Load all available cookies. + * Returns an object containing all cookies. + * + * @param doNotParse optional boolean value if parse is not needed + */ +export function loadAll(doNotParse?: boolean): { [key: string]: any }; + +/** + * Find all the cookies with a name that match the regex. + * Returns an object with the cookie name as the key. + * @param regex Regular expression to match + */ +export function select(regex?: RegExp): { [key: string]: any }; + +/** + * Set a cookie + * @param name string Cookie name + * @param val any value to save + * @param opt object of cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1) + */ +export function save(name: string, val: string | number | object, opt: CookieSerializeOptions): void; + +/** + * Remove a cookie + * @param name string + * @param opt object of cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1) + */ +export function remove(name: string, opt?: CookieSerializeOptions): void; + +/** + * Load the user cookies so you can do server-rendering and match the same result. + * Also send back to the user the new cookies. + * Work with connect or express.js by using the cookieParser middleware first. + * Use const unplug = plugToRequest(req, res) just before your renderToString. + * + * Returns unplug() function so it stops setting cookies on the response. + * @param req + * @param res + */ +export function plugToRequest(req: Request, res: Response): () => void; + +/** + * Load the user cookies so you can do server-rendering and match the same result. + * Use setRawCookie(headers.cookie) just before your renderToString. + * Make sure it is the raw string from the request headers. + */ +export function setRawCookie(rawCookie?: string): void; diff --git a/types/react-cookies/react-cookies-tests.ts b/types/react-cookies/react-cookies-tests.ts new file mode 100644 index 0000000000..652f787f65 --- /dev/null +++ b/types/react-cookies/react-cookies-tests.ts @@ -0,0 +1,38 @@ +import reactCookies = require('react-cookies'); +import cookie = require('cookie'); +import express = require('express'); + +reactCookies.load('token'); +reactCookies.load('token', true); +reactCookies.load('token', false); + +reactCookies.loadAll(); +reactCookies.loadAll(true); +reactCookies.loadAll(false); + +reactCookies.select(/\btest(er|ing|ed|s)?\b/g); +reactCookies.select(); + +const options: cookie.CookieSerializeOptions = { + path: '/', + expires: new Date(), + maxAge: 200, + domain: 'example.com', + secure: false, + httpOnly: false, + sameSite: 'strict', +}; + +reactCookies.save('cookie', 'value', options); +reactCookies.save('cookie', 1231, options); +reactCookies.save('cookie', { key: 'value' }, options); + +reactCookies.remove('userId', { path: '/' }); + +const app = express(); +app.use((err: any, req: express.Request, res: express.Response) => { + const unplug = reactCookies.plugToRequest(req, res); + unplug(); +}); + +reactCookies.setRawCookie('Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'); diff --git a/types/react-cookies/tsconfig.json b/types/react-cookies/tsconfig.json new file mode 100644 index 0000000000..6ccd3d8ce7 --- /dev/null +++ b/types/react-cookies/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "react-cookies-tests.ts" + ] +} diff --git a/types/react-cookies/tslint.json b/types/react-cookies/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-cookies/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }