From 4dc0a83235f182298a4218c0ac7bf733c2ff2aa3 Mon Sep 17 00:00:00 2001 From: bobrimperator Date: Sun, 22 Dec 2024 18:24:49 +0100 Subject: [PATCH] chore(typescript): utils/serialize-cookie --- .../ember-cookies/src/services/cookies.js | 2 +- .../src/test-support/clear-all-cookies.js | 2 +- ...erialize-cookie.js => serialize-cookie.ts} | 15 +++++++++-- .../unpublished-development-types/index.d.ts | 1 + packages/test-app/package.json | 2 +- pnpm-lock.yaml | 26 ++++++++++++++++++- 6 files changed, 42 insertions(+), 6 deletions(-) rename packages/ember-cookies/src/utils/{serialize-cookie.js => serialize-cookie.ts} (68%) create mode 100644 packages/ember-cookies/unpublished-development-types/index.d.ts diff --git a/packages/ember-cookies/src/services/cookies.js b/packages/ember-cookies/src/services/cookies.js index 5d9e6933..6d3db012 100644 --- a/packages/ember-cookies/src/services/cookies.js +++ b/packages/ember-cookies/src/services/cookies.js @@ -3,7 +3,7 @@ import { get } from '@ember/object'; import { assert } from '@ember/debug'; import { getOwner } from '@ember/application'; import Service from '@ember/service'; -import { serializeCookie } from '../utils/serialize-cookie'; +import { serializeCookie } from '../utils/serialize-cookie.ts'; const { keys } = Object; const DEFAULTS = { raw: false }; const MAX_COOKIE_BYTE_LENGTH = 4096; diff --git a/packages/ember-cookies/src/test-support/clear-all-cookies.js b/packages/ember-cookies/src/test-support/clear-all-cookies.js index 4646c5d7..2ea59556 100644 --- a/packages/ember-cookies/src/test-support/clear-all-cookies.js +++ b/packages/ember-cookies/src/test-support/clear-all-cookies.js @@ -1,6 +1,6 @@ import { assert } from '@ember/debug'; import { isEmpty } from '@ember/utils'; -import { serializeCookie } from '../utils/serialize-cookie'; +import { serializeCookie } from '../utils/serialize-cookie.ts'; export default function clearAllCookies(options = {}) { assert('Cookies cannot be set to be HTTP-only from a browser!', !options.httpOnly); diff --git a/packages/ember-cookies/src/utils/serialize-cookie.js b/packages/ember-cookies/src/utils/serialize-cookie.ts similarity index 68% rename from packages/ember-cookies/src/utils/serialize-cookie.js rename to packages/ember-cookies/src/utils/serialize-cookie.ts index c1f728f6..fe17ed12 100644 --- a/packages/ember-cookies/src/utils/serialize-cookie.js +++ b/packages/ember-cookies/src/utils/serialize-cookie.ts @@ -1,12 +1,23 @@ import { typeOf, isEmpty } from '@ember/utils'; -export const serializeCookie = (name, value, options = {}) => { +interface Options { + maxAge?: number | string; + domain?: string; + expires?: Date; + secure?: boolean; + httpOnly?: boolean; + path?: string; + sameSite?: string; + partitioned?: boolean; +} + +export const serializeCookie = (name: string, value: string, options: Options = {}) => { let cookie = `${name}=${value}`; if (!isEmpty(options.domain)) { cookie = `${cookie}; domain=${options.domain}`; } - if (typeOf(options.expires) === 'date') { + if (options.expires && typeOf(options.expires) === 'date') { cookie = `${cookie}; expires=${options.expires.toUTCString()}`; } if (!isEmpty(options.maxAge)) { diff --git a/packages/ember-cookies/unpublished-development-types/index.d.ts b/packages/ember-cookies/unpublished-development-types/index.d.ts new file mode 100644 index 00000000..507d4797 --- /dev/null +++ b/packages/ember-cookies/unpublished-development-types/index.d.ts @@ -0,0 +1 @@ +import '@types/ember__utils'; diff --git a/packages/test-app/package.json b/packages/test-app/package.json index c943cb9f..42cedca3 100644 --- a/packages/test-app/package.json +++ b/packages/test-app/package.json @@ -89,4 +89,4 @@ "ember": { "edition": "octane" } -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 82803ef3..859b760f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8670,6 +8670,26 @@ snapshots: dependencies: '@types/node': 22.5.5 + '@types/ember@4.0.11': + dependencies: + '@types/ember__application': 4.0.11(@babel/core@7.25.8) + '@types/ember__array': 4.0.10(@babel/core@7.25.8) + '@types/ember__component': 4.0.22(@babel/core@7.25.8) + '@types/ember__controller': 4.0.12(@babel/core@7.25.8) + '@types/ember__debug': 4.0.8 + '@types/ember__engine': 4.0.11 + '@types/ember__error': 4.0.6 + '@types/ember__object': 4.0.12(@babel/core@7.25.8) + '@types/ember__polyfills': 4.0.6 + '@types/ember__routing': 4.0.22(@babel/core@7.25.8) + '@types/ember__runloop': 4.0.10 + '@types/ember__service': 4.0.9 + '@types/ember__string': 3.0.15 + '@types/ember__template': 4.0.7 + '@types/ember__test': 4.0.6(@babel/core@7.25.8) + '@types/ember__utils': 4.0.7(@babel/core@7.25.8) + '@types/rsvp': 4.0.9 + '@types/ember@4.0.11(@babel/core@7.25.8)': dependencies: '@types/ember__application': 4.0.11(@babel/core@7.25.8) @@ -8696,7 +8716,7 @@ snapshots: '@types/ember__application@4.0.11(@babel/core@7.25.8)': dependencies: '@glimmer/component': 1.1.2(@babel/core@7.25.8) - '@types/ember': 4.0.11(@babel/core@7.25.8) + '@types/ember': 4.0.11 '@types/ember__engine': 4.0.11 '@types/ember__object': 4.0.12(@babel/core@7.25.8) '@types/ember__owner': 4.0.9 @@ -8762,6 +8782,10 @@ snapshots: - '@babel/core' - supports-color + '@types/ember__runloop@4.0.10': + dependencies: + '@types/ember': 4.0.11 + '@types/ember__runloop@4.0.10(@babel/core@7.25.8)': dependencies: '@types/ember': 4.0.11(@babel/core@7.25.8)