Merge pull request #32830 from webbiesdk/jscookie-small-fixes

jscookie: small changes to make it more correct. Might break existing code?
This commit is contained in:
Mine Starks
2019-02-12 12:34:42 -08:00
committed by GitHub
2 changed files with 12 additions and 9 deletions
+6 -5
View File
@@ -33,7 +33,7 @@ declare namespace Cookies {
secure?: boolean;
}
interface CookiesStatic {
interface CookiesStatic<T extends object = object> {
/**
* Allows default cookie attributes to be accessed, changed, or reset
*/
@@ -42,7 +42,7 @@ declare namespace Cookies {
/**
* Create a cookie
*/
set(name: string, value: string | object, options?: CookieAttributes): void;
set(name: string, value: string | T, options?: CookieAttributes): void;
/**
* Read cookie
@@ -79,7 +79,7 @@ declare namespace Cookies {
* or SDK. Note: The noConflict method is not necessary when using
* AMD or CommonJS, thus it is not exposed in those environments.
*/
noConflict(): CookiesStatic;
noConflict?(): CookiesStatic<T>;
/**
* Create a new instance of the api that overrides the default
@@ -88,10 +88,11 @@ declare namespace Cookies {
* will run the converter first for each cookie. The returned
* string will be used as the cookie value.
*/
withConverter(converter: CookieConverter | { write: CookieConverter; read: CookieConverter; }): CookiesStatic;
withConverter<TConv extends object>(converter: CookieReadConverter | { write: CookieWriteConverter<TConv>; read: CookieReadConverter; }): CookiesStatic<TConv>;
}
type CookieConverter = (value: string, name: string) => string;
type CookieWriteConverter<T extends object> = (value: string | T, name: string) => string;
type CookieReadConverter = (value: string, name: string) => string;
}
declare const Cookies: Cookies.CookiesStatic;
+6 -4
View File
@@ -19,8 +19,8 @@ Cookies.get();
Cookies.remove('name');
Cookies.remove('name', { path: '' });
const Cookies2 = Cookies.noConflict();
Cookies2; // $ExpectType CookiesStatic
const Cookies2 = Cookies.noConflict!();
Cookies2; // $ExpectType CookiesStatic<object>
Cookies.set('name', { foo: 'bar' });
@@ -40,12 +40,14 @@ cookies.get('escaped');
Cookies.defaults.path = '';
delete Cookies.defaults.path;
const PHPCookies = Cookies.withConverter({
const PHPCookies = Cookies.withConverter<object>({
write(value) {
return encodeURIComponent(value)
value; // $ExpectType string | object
return encodeURIComponent(value as string)
.replace(/%(23|24|26|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
},
read(value) {
value; // $ExpectType string
return value
.replace(/\+/g, ' ')
.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);