Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Move cookies.js to cookies.ts and add types #167

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions lib/cookie.js → lib/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import { CookieOptions } from './types';

/**
* Module dependencies.
*/
Expand All @@ -17,21 +19,15 @@ var topDomain = require('@segment/top-domain');
* @param {Object} options
*/

function Cookie(options) {
function Cookie(options?: CookieOptions) {
this.options(options);
}

/**
* Get or set the cookie options.
*
* @param {Object} options
* @field {Number} maxage (1 year)
* @field {String} domain
* @field {String} path
* @field {Boolean} secure
*/

Cookie.prototype.options = function(options) {
Cookie.prototype.options = function(options: CookieOptions) {
if (arguments.length === 0) return this._options;

options = options || {};
Expand Down Expand Up @@ -64,13 +60,9 @@ Cookie.prototype.options = function(options) {

/**
* Set a `key` and `value` in our cookie.
*
* @param {String} key
* @param {Object} value
* @return {Boolean} saved
*/

Cookie.prototype.set = function(key, value) {
Cookie.prototype.set = function(key: string, value?: object | string): boolean {
try {
value = window.JSON.stringify(value);
cookie(key, value === 'null' ? null : value, clone(this._options));
Expand All @@ -82,12 +74,9 @@ Cookie.prototype.set = function(key, value) {

/**
* Get a value from our cookie by `key`.
*
* @param {String} key
* @return {Object} value
*/

Cookie.prototype.get = function(key) {
Cookie.prototype.get = function(key: string): object {
try {
var value = cookie(key);
value = value ? window.JSON.parse(value) : null;
Expand All @@ -99,12 +88,9 @@ Cookie.prototype.get = function(key) {

/**
* Remove a value from our cookie by `key`.
*
* @param {String} key
* @return {Boolean} removed
*/

Cookie.prototype.remove = function(key) {
Cookie.prototype.remove = function(key: string): boolean {
try {
cookie(key, null, clone(this._options));
return true;
Expand Down