diff --git a/docCookies/docCookies-tests.ts b/docCookies/docCookies-tests.ts new file mode 100644 index 000000000..f81346b9e --- /dev/null +++ b/docCookies/docCookies-tests.ts @@ -0,0 +1,27 @@ +// Type definitions for docCookies +// Project: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie +// Definitions by: Jon Egerton +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +docCookies.setItem("test0", "Hello world!"); +docCookies.setItem("test1", "Unicode test: \u00E0\u00E8\u00EC\u00F2\u00F9", Infinity); +docCookies.setItem("test2", "Hello world!", new Date(2020, 5, 12)); +docCookies.setItem("test3", "Hello world!", new Date(2027, 2, 3), "/blog"); +docCookies.setItem("test4", "Hello world!", "Sun, 06 Nov 2022 21:43:15 GMT"); +docCookies.setItem("test5", "Hello world!", "Tue, 06 Dec 2022 13:11:07 GMT", "/home"); +docCookies.setItem("test6", "Hello world!", 150); +docCookies.setItem("test7", "Hello world!", 245, "/content"); +docCookies.setItem("test8", "Hello world!", null, null, "example.com"); +docCookies.setItem("test9", "Hello world!", null, null, null, true); + +alert(docCookies.keys().join("\n")); +alert(docCookies.getItem("test1")); +alert(docCookies.getItem("test5")); +docCookies.removeItem("test1"); +docCookies.removeItem("test5", "/home"); +alert(docCookies.getItem("test1")); +alert(docCookies.getItem("test5")); +alert(docCookies.getItem("unexistingCookie")); +//alert(docCookies.getItem()); \ No newline at end of file diff --git a/docCookies/docCookies.d.ts b/docCookies/docCookies.d.ts new file mode 100644 index 000000000..e7ecf1424 --- /dev/null +++ b/docCookies/docCookies.d.ts @@ -0,0 +1,66 @@ +// Type definitions for docCookies +// Project: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie +// Definitions by: Jon Egerton +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface docCookies { + + /** + Create/overwrite a cookie. + @param {string} name (required) The name of the cookie to create/overwrite + @param {string} value (required) The value of the cookie + @param {number} end (optional) The max-age in seconds (e.g. 31536e3 for a year, Infinity for a never-expires cookie). If not specified it will expire at the end of session + @param {string} path (optional) E.g., "/", "/mydir"; if not specified, defaults to the current path of the current document location + @param {string} domain (optional) E.g., "example.com", ".example.com" (includes all subdomains) or "subdomain.example.com"; if not specified, defaults to the host portion of the current document location + @param {boolean} secure (optional) The cookie will be transmitted only over secure protocol as https + */ + setItem(sKey: string, sValue: string, vEnd?: number, sPath?: string, sDomain?: string, bSecure?: boolean): boolean; + + /** + Create/overwrite a cookie. + @param {string} name (required) The name of the cookie to create/overwrite + @param {string} value (required) The value of the cookie + @param {string} end (optional) The expires date in GMTString format. If not specified it will expire at the end of session + @param {string} path (optional) E.g., "/", "/mydir"; if not specified, defaults to the current path of the current document location + @param {string} domain (optional) E.g., "example.com", ".example.com" (includes all subdomains) or "subdomain.example.com"; if not specified, defaults to the host portion of the current document location + @param {boolean} secure (optional) The cookie will be transmitted only over secure protocol as https + */ + setItem(sKey: string, sValue: string, vEnd?: string, sPath?: string, sDomain?: string, bSecure?: boolean): boolean; + + /** + Create/overwrite a cookie. + @param {string} name (required) The name of the cookie to create/overwrite + @param {string} value (required) The value of the cookie + @param {Date} end (optional) The expires date as a Date object. If not specified it will expire at the end of session + @param {string} path (optional) E.g., "/", "/mydir"; if not specified, defaults to the current path of the current document location + @param {string} domain (optional) E.g., "example.com", ".example.com" (includes all subdomains) or "subdomain.example.com"; if not specified, defaults to the host portion of the current document location + @param {boolean} secure (optional) The cookie will be transmitted only over secure protocol as https + */ + setItem(sKey: string, sValue: string, vEnd?: Date, sPath?: string, sDomain?: string, bSecure?: boolean): boolean; + + /** + Read a cookie. If the cookie doesn't exist a null value will be returned. + @param {string} name (required) The name of the cookie to read + */ + getItem(sKey: string): string; + + /** + Delete a cookie. + @param {string} name (required) The name of the cookie to remove + */ + removeItem(sKey: string, sPath?: string): boolean; + + /** + Check if a cookie exists. + @param {string} name (required) The name of the cookie to test + */ + hasItem(sKey: string): boolean; + + /** + Returns an array of all readable cookies from this location. + */ + keys(): string[]; +} + +declare var docCookies: docCookies; +