Skip to content

Commit

Permalink
chore(typescript): utils/serialize-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
BobrImperator committed Dec 22, 2024
1 parent d976b53 commit 4dc0a83
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/ember-cookies/src/services/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@types/ember__utils';
2 changes: 1 addition & 1 deletion packages/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
"ember": {
"edition": "octane"
}
}
}
26 changes: 25 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4dc0a83

Please sign in to comment.