From b741f88fe1621ba6451c4876f89ba2967e2a071e Mon Sep 17 00:00:00 2001 From: Mark Kennedy Date: Sat, 4 Apr 2020 15:56:11 -0400 Subject: [PATCH] Convert to TS --- .gitignore | 4 ++- index.d.ts | 50 -------------------------------------- index.js => index.ts | 58 +++++++++++++++++++++++++++++++++++--------- package.json | 4 +-- tsconfig.json | 15 ++++-------- types.ts | 5 ---- 6 files changed, 56 insertions(+), 80 deletions(-) delete mode 100644 index.d.ts rename index.js => index.ts (81%) delete mode 100644 types.ts diff --git a/.gitignore b/.gitignore index cb375a3..ee4d8b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ coverage/ node_modules/ -npm-debug.log \ No newline at end of file +npm-debug.log +index.js +index.d.ts \ No newline at end of file diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 73d7d75..0000000 --- a/index.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Parse a cookie header. - * - * Parse the given cookie header string into an object - * The object has the various cookies as keys(names) => values - * - * @param {string} str - * @param {object} [options] - * @return {object} - */ -declare function parse(str: string, options?: any): any; -/** - * Serialize data into a cookie header. - * - * Serialize the a name value pair into a cookie string suitable for - * http headers. An optional options object specified cookie parameters. - * - * serialize('foo', 'bar', { httpOnly: true }) - * => "foo=bar; httpOnly" - * - * @param {string} name - * @param {string} val - * @param {object} [options] - * @return {string} - */ -declare function serialize(name: string, val: string, options?: any): string; -/** - * Try decoding a string using a decoding function. - * - * @param {string} str - * @param {function} decode - */ -declare function tryDecode(str: string, decode: Function): any; -declare function get(): void; -declare function set(): void; -declare function getAll(): void; -/** - * Module variables. - */ -declare var decode: typeof decodeURIComponent; -declare var encode: typeof encodeURIComponent; -declare var pairSplitRegExp: RegExp; -/** - * RegExp to match field-content in RFC 7230 sec 3.2 - * - * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] - * field-vchar = VCHAR / obs-text - * obs-text = %x80-FF - */ -declare var fieldContentRegExp: RegExp; diff --git a/index.js b/index.ts similarity index 81% rename from index.js rename to index.ts index 3f1ed0e..d85fa6f 100644 --- a/index.js +++ b/index.ts @@ -1,5 +1,6 @@ /** * Module variables. + * @private */ var decode = decodeURIComponent; @@ -26,9 +27,10 @@ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; * @param {string} str * @param {object} [options] * @return {object} + * @private */ -function parse(str, options) { +function parse(str, options: any = {}) { if (typeof str !== 'string') { throw new TypeError('argument str must be a string'); } @@ -77,9 +79,10 @@ function parse(str, options) { * @param {string} val * @param {object} [options] * @return {string} + * @private */ -function serialize(name, val, options) { +function serialize(name, val, options: any = {}) { var opt = options || {}; var enc = opt.encode || encode; @@ -169,6 +172,7 @@ function serialize(name, val, options) { * * @param {string} str * @param {function} decode + * @private */ function tryDecode(str, decode) { @@ -179,16 +183,46 @@ function tryDecode(str, decode) { } } -function get() {} - -function set() {} - -function getAll() {} +const CookieStore = { + /** + * Get a cookie. + * + * @param {string} name + * @return {Promise} + */ + get(name) { + return Promise.resolve(parse(document.cookie)[name]); + }, + + /** + * Set a cookie. + * + * @param {string} name + * @param {string} value + * @return {Promise} + */ + set(name, value) { + return new Promise(function (resolve, reject) { + const cookieString = serialize(name, value); + document.cookie = cookieString; + resolve(); + }); + }, + + /** + * Get multiple cookies. + * + * @return {Promise} + */ + getAll() { + throw Error('getAll not implemented, coming soon though.'); + }, +}; if (!window.cookieStore) { - window.cookieStore = { - get: get, - set: set, - getAll: getAll, - }; + window.cookieStore = CookieStore; +} + +interface Window { + cookieStore: typeof CookieStore; } diff --git a/package.json b/package.json index 2c60cfe..f6ae679 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "banner": "banner-cli index.js", "lint": "npm run lint:js && npm run lint:types", "lint:js": "eslint '**/*.{js,ts}'", - "lint:types": "tsc --noEmit", + "build": "tsc", "coveralls": "cat .coverage/lcov.info | coveralls" } -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 694d1e5..42c48bc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,18 +1,13 @@ { "compilerOptions": { - "target": "esnext", - "module": "esnext", - "moduleResolution": "node", - "lib": ["es2017", "dom"], - "allowJs": true, - "checkJs": true, + "target": "es5", + "module": "commonjs", "strict": false, "noImplicitThis": true, "alwaysStrict": true, "esModuleInterop": true, - "noEmit": false, - "declaration": true + "declaration": true, + "lib": ["ES5", "dom", "ES2015"] }, - "include": ["index.js"], - "files": ["types.ts"] + "files": ["index.ts"] } diff --git a/types.ts b/types.ts deleted file mode 100644 index 0d8b412..0000000 --- a/types.ts +++ /dev/null @@ -1,5 +0,0 @@ -interface Window { - cookieStore: any; -} - -window.cookieStore = 'bar';