[js-cookie] update typings to v2.1, enable strict null checks and linting

This commit is contained in:
Dimitri Benin
2017-08-14 20:34:44 +02:00
parent 142a2fc41c
commit 9dbbc5d8cf
4 changed files with 49 additions and 54 deletions

View File

@@ -1,7 +1,9 @@
// Type definitions for js-cookie v2.0
// Type definitions for js-cookie 2.1
// Project: https://github.com/js-cookie/js-cookie
// Definitions by: Theodore Brown <https://github.com/theodorejb>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare namespace Cookies {
interface CookieAttributes {
@@ -39,12 +41,12 @@ declare namespace Cookies {
/**
* Create a cookie
*/
set(name: string, value: string | any, options?: CookieAttributes): void;
set(name: string, value: string | object, options?: CookieAttributes): void;
/**
* Read cookie
*/
get(name: string): string;
get(name: string): string | undefined;
/**
* Read all available cookies
@@ -55,7 +57,7 @@ declare namespace Cookies {
* Returns the parsed representation of the string
* stored in the cookie according to JSON.parse
*/
getJSON(name: string): any;
getJSON(name: string): object;
/**
* Returns the parsed representation of
@@ -85,11 +87,13 @@ declare namespace Cookies {
* will run the converter first for each cookie. The returned
* string will be used as the cookie value.
*/
withConverter(converter: (value: string, name: string) => string): CookiesStatic;
withConverter(converter: CookieConverter | { write: CookieConverter; read: CookieConverter; }): CookiesStatic;
}
type CookieConverter = (value: string, name: string) => string;
}
declare var Cookies: Cookies.CookiesStatic;
declare const Cookies: Cookies.CookiesStatic;
export = Cookies;
export as namespace Cookies;

View File

@@ -1,67 +1,57 @@
import Cookies = require("js-cookie");
// Create a cookie, valid across the entire site
Cookies.set('name', 'value');
// Create a cookie that expires 7 days from now, valid across the entire site
Cookies.set('name', 'value', { expires: 7 });
// Create an expiring cookie, valid to the path of the current page
Cookies.set('name', 'value', { expires: new Date() });
Cookies.set('name', 'value', { expires: 7, path: '' });
// Read cookie
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined
// Read all available cookies
Cookies.get(); // => { name: 'value' }
// Delete cookie
Cookies.remove('name');
// Delete a cookie valid to the path of the current page
Cookies.set('name', 'value', { expires: 7, path: '', domain: '' });
Cookies.set('name', 'value', { expires: 7, path: '', domain: '', secure: true });
Cookies.set('name', 'value', { secure: true });
Cookies.set('name', 'value', { domain: '' });
Cookies.set('name', 'value', { path: '' });
Cookies.remove('name'); // fail!
Cookies.remove('name', { path: '' }); // removed!
// Assign the js-cookie api to a different variable
// and restore the original "window.Cookies"
var Cookies2 = Cookies.noConflict();
Cookies2.set('name', 'value');
// $ExpectType string | undefined
Cookies.get('name');
// $ExpectType { [key: string]: string; }
Cookies.get();
Cookies.remove('name');
Cookies.remove('name', { path: '' });
const Cookies2 = Cookies.noConflict();
Cookies2; // $ExpectType CookiesStatic
// When creating a cookie you can pass an Array or Object Literal
// instead of a string in the value. If you do so, js-cookie will
// store the string representation of the object according to JSON.stringify
Cookies.set('name', { foo: 'bar' });
// When reading a cookie with the Cookies.getJSON api, you receive
// the parsed representation of the string stored in the cookie
// according to JSON.parse
Cookies.getJSON('name'); // => { foo: 'bar' }
// $ExpectType object
Cookies.getJSON('name');
// Define the domain where the cookie is available
Cookies.set('name', 'value', { domain: 'sub.domain.com' });
Cookies.get('name'); // => undefined (need to read at 'sub.domain.com')
// Indicate that the cookie transmission requires (https)
Cookies.set('name', 'value', { secure: true });
Cookies.get('name'); // => 'value'
Cookies.remove('name', { secure: true });
// $ExpectType { [key: string]: any; }
Cookies.getJSON();
document.cookie = 'escaped=%u5317';
document.cookie = 'default=%E5%8C%97';
var cookies = Cookies.withConverter(function (value, name) {
const cookies = Cookies.withConverter((value, name) => {
if ( name === 'escaped' ) {
return decodeURIComponent(value);
}
return value;
});
cookies.get('escaped'); // 北
cookies.get('default'); // 北
cookies.get(); // { escaped: '北', default: '北' }
cookies.get('escaped');
// To remove, set or declare defaults to the path of the
// current page, you just need to declare it as empty:
Cookies.defaults.path = '';
// Deleting the property will fallback to the path: / internally:
delete Cookies.defaults.path;
const PHPCookies = Cookies.withConverter({
write(value) {
return encodeURIComponent(value)
.replace(/%(23|24|26|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
},
read(value) {
return value
.replace(/\+/g, ' ')
.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}
});

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
@@ -20,4 +20,4 @@
"index.d.ts",
"js-cookie-tests.ts"
]
}
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }