@pyreon/storage 0.11.5 → 0.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- import { afterEach, beforeEach, describe, expect, it } from "vitest"
2
- import { _resetRegistry, useCookie } from "../index"
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
2
+ import { _resetRegistry, useCookie } from '../index'
3
3
 
4
4
  function clearAllCookies(): void {
5
- for (const cookie of document.cookie.split(";")) {
6
- const name = cookie.split("=")[0]?.trim()
5
+ for (const cookie of document.cookie.split(';')) {
6
+ const name = cookie.split('=')[0]?.trim()
7
7
  if (name) {
8
8
  // biome-ignore lint/suspicious/noDocumentCookie: test cleanup requires direct cookie access
9
9
  document.cookie = `${name}=; max-age=0; path=/`
@@ -11,7 +11,7 @@ function clearAllCookies(): void {
11
11
  }
12
12
  }
13
13
 
14
- describe("useCookie — options", () => {
14
+ describe('useCookie — options', () => {
15
15
  beforeEach(() => {
16
16
  _resetRegistry()
17
17
  clearAllCookies()
@@ -22,165 +22,165 @@ describe("useCookie — options", () => {
22
22
  clearAllCookies()
23
23
  })
24
24
 
25
- describe("maxAge", () => {
26
- it("sets cookie with maxAge in seconds", () => {
27
- const sig = useCookie("session", "token-abc", { maxAge: 3600 })
28
- sig.set("token-xyz")
25
+ describe('maxAge', () => {
26
+ it('sets cookie with maxAge in seconds', () => {
27
+ const sig = useCookie('session', 'token-abc', { maxAge: 3600 })
28
+ sig.set('token-xyz')
29
29
  // Cookie should be written — we can verify the signal value
30
- expect(sig()).toBe("token-xyz")
30
+ expect(sig()).toBe('token-xyz')
31
31
  })
32
32
 
33
- it("maxAge=0 still writes cookie (browser handles expiry)", () => {
34
- const sig = useCookie("ephemeral", "val", { maxAge: 0 })
35
- sig.set("updated")
36
- expect(sig()).toBe("updated")
33
+ it('maxAge=0 still writes cookie (browser handles expiry)', () => {
34
+ const sig = useCookie('ephemeral', 'val', { maxAge: 0 })
35
+ sig.set('updated')
36
+ expect(sig()).toBe('updated')
37
37
  })
38
38
 
39
- it("large maxAge for long-lived cookies", () => {
39
+ it('large maxAge for long-lived cookies', () => {
40
40
  const oneYear = 60 * 60 * 24 * 365
41
- const sig = useCookie("locale", "en", { maxAge: oneYear })
42
- sig.set("de")
43
- expect(sig()).toBe("de")
41
+ const sig = useCookie('locale', 'en', { maxAge: oneYear })
42
+ sig.set('de')
43
+ expect(sig()).toBe('de')
44
44
  })
45
45
  })
46
46
 
47
- describe("path", () => {
48
- it("defaults to / when no path specified", () => {
49
- const sig = useCookie("default-path", "val")
50
- sig.set("updated")
51
- expect(sig()).toBe("updated")
47
+ describe('path', () => {
48
+ it('defaults to / when no path specified', () => {
49
+ const sig = useCookie('default-path', 'val')
50
+ sig.set('updated')
51
+ expect(sig()).toBe('updated')
52
52
  })
53
53
 
54
- it("accepts custom path", () => {
55
- const sig = useCookie("scoped", "val", { path: "/admin" })
56
- sig.set("updated")
57
- expect(sig()).toBe("updated")
54
+ it('accepts custom path', () => {
55
+ const sig = useCookie('scoped', 'val', { path: '/admin' })
56
+ sig.set('updated')
57
+ expect(sig()).toBe('updated')
58
58
  })
59
59
  })
60
60
 
61
- describe("sameSite", () => {
62
- it("defaults to lax when not specified", () => {
63
- const sig = useCookie("lax-default", "val")
64
- sig.set("updated")
65
- expect(sig()).toBe("updated")
61
+ describe('sameSite', () => {
62
+ it('defaults to lax when not specified', () => {
63
+ const sig = useCookie('lax-default', 'val')
64
+ sig.set('updated')
65
+ expect(sig()).toBe('updated')
66
66
  })
67
67
 
68
- it("accepts strict sameSite", () => {
69
- const sig = useCookie("strict-cookie", "val", { sameSite: "strict" })
70
- sig.set("updated")
71
- expect(sig()).toBe("updated")
68
+ it('accepts strict sameSite', () => {
69
+ const sig = useCookie('strict-cookie', 'val', { sameSite: 'strict' })
70
+ sig.set('updated')
71
+ expect(sig()).toBe('updated')
72
72
  })
73
73
 
74
- it("accepts none sameSite", () => {
75
- const sig = useCookie("none-cookie", "val", { sameSite: "none", secure: true })
76
- sig.set("updated")
77
- expect(sig()).toBe("updated")
74
+ it('accepts none sameSite', () => {
75
+ const sig = useCookie('none-cookie', 'val', { sameSite: 'none', secure: true })
76
+ sig.set('updated')
77
+ expect(sig()).toBe('updated')
78
78
  })
79
79
 
80
- it("accepts lax sameSite explicitly", () => {
81
- const sig = useCookie("explicit-lax", "val", { sameSite: "lax" })
82
- sig.set("updated")
83
- expect(sig()).toBe("updated")
80
+ it('accepts lax sameSite explicitly', () => {
81
+ const sig = useCookie('explicit-lax', 'val', { sameSite: 'lax' })
82
+ sig.set('updated')
83
+ expect(sig()).toBe('updated')
84
84
  })
85
85
  })
86
86
 
87
- describe("combined options", () => {
88
- it("maxAge + path + sameSite together", () => {
89
- const sig = useCookie("combined", "default", {
87
+ describe('combined options', () => {
88
+ it('maxAge + path + sameSite together', () => {
89
+ const sig = useCookie('combined', 'default', {
90
90
  maxAge: 86400,
91
- path: "/app",
92
- sameSite: "strict",
91
+ path: '/app',
92
+ sameSite: 'strict',
93
93
  })
94
- sig.set("updated")
95
- expect(sig()).toBe("updated")
94
+ sig.set('updated')
95
+ expect(sig()).toBe('updated')
96
96
  sig.remove()
97
- expect(sig()).toBe("default")
97
+ expect(sig()).toBe('default')
98
98
  })
99
99
 
100
- it("expires + domain + secure together", () => {
100
+ it('expires + domain + secure together', () => {
101
101
  const future = new Date(Date.now() + 86400000)
102
- const sig = useCookie("full-options", "val", {
102
+ const sig = useCookie('full-options', 'val', {
103
103
  expires: future,
104
- domain: "example.com",
104
+ domain: 'example.com',
105
105
  secure: true,
106
- sameSite: "none",
106
+ sameSite: 'none',
107
107
  })
108
- sig.set("updated")
109
- expect(sig()).toBe("updated")
108
+ sig.set('updated')
109
+ expect(sig()).toBe('updated')
110
110
  })
111
111
 
112
- it("all options combined", () => {
113
- const sig = useCookie("all-opts", "val", {
112
+ it('all options combined', () => {
113
+ const sig = useCookie('all-opts', 'val', {
114
114
  maxAge: 7200,
115
- path: "/dashboard",
116
- domain: "example.com",
115
+ path: '/dashboard',
116
+ domain: 'example.com',
117
117
  secure: true,
118
- sameSite: "strict",
118
+ sameSite: 'strict',
119
119
  })
120
- sig.set("new-val")
121
- expect(sig()).toBe("new-val")
120
+ sig.set('new-val')
121
+ expect(sig()).toBe('new-val')
122
122
  })
123
123
  })
124
124
 
125
- describe("custom serializer/deserializer", () => {
126
- it("uses custom serializer for cookie writes", () => {
127
- const sig = useCookie("date-cookie", new Date("2025-01-01"), {
125
+ describe('custom serializer/deserializer', () => {
126
+ it('uses custom serializer for cookie writes', () => {
127
+ const sig = useCookie('date-cookie', new Date('2025-01-01'), {
128
128
  serializer: (d) => d.toISOString(),
129
129
  deserializer: (s) => new Date(s),
130
130
  })
131
- const newDate = new Date("2025-06-15")
131
+ const newDate = new Date('2025-06-15')
132
132
  sig.set(newDate)
133
- expect(sig().toISOString()).toBe("2025-06-15T00:00:00.000Z")
133
+ expect(sig().toISOString()).toBe('2025-06-15T00:00:00.000Z')
134
134
  })
135
135
  })
136
136
 
137
- describe("remove with options", () => {
138
- it("remove() resets signal regardless of cookie options", () => {
139
- const sig = useCookie("removable", "default", {
137
+ describe('remove with options', () => {
138
+ it('remove() resets signal regardless of cookie options', () => {
139
+ const sig = useCookie('removable', 'default', {
140
140
  maxAge: 86400,
141
- path: "/app",
142
- domain: "example.com",
141
+ path: '/app',
142
+ domain: 'example.com',
143
143
  })
144
- sig.set("changed")
144
+ sig.set('changed')
145
145
  sig.remove()
146
- expect(sig()).toBe("default")
146
+ expect(sig()).toBe('default')
147
147
  })
148
148
 
149
- it("after remove(), a new useCookie call creates a fresh signal", () => {
150
- const a = useCookie("temp-cookie", "first")
151
- a.set("modified")
149
+ it('after remove(), a new useCookie call creates a fresh signal', () => {
150
+ const a = useCookie('temp-cookie', 'first')
151
+ a.set('modified')
152
152
  a.remove()
153
153
 
154
- const b = useCookie("temp-cookie", "second")
155
- expect(b()).toBe("second")
154
+ const b = useCookie('temp-cookie', 'second')
155
+ expect(b()).toBe('second')
156
156
  expect(a).not.toBe(b)
157
157
  })
158
158
  })
159
159
 
160
- describe("onError callback", () => {
161
- it("calls onError when deserialization fails", () => {
160
+ describe('onError callback', () => {
161
+ it('calls onError when deserialization fails', () => {
162
162
  // Pre-seed a corrupt cookie
163
163
  // biome-ignore lint/suspicious/noDocumentCookie: test setup
164
- document.cookie = `broken-cookie=${encodeURIComponent("{invalid json")}; path=/`
164
+ document.cookie = `broken-cookie=${encodeURIComponent('{invalid json')}; path=/`
165
165
 
166
166
  const errors: Error[] = []
167
- const sig = useCookie("broken-cookie", "fallback", {
167
+ const sig = useCookie('broken-cookie', 'fallback', {
168
168
  onError: (e) => {
169
169
  errors.push(e)
170
170
  return undefined
171
171
  },
172
172
  })
173
- expect(sig()).toBe("fallback")
173
+ expect(sig()).toBe('fallback')
174
174
  expect(errors).toHaveLength(1)
175
175
  })
176
176
 
177
- it("onError can provide custom fallback", () => {
177
+ it('onError can provide custom fallback', () => {
178
178
  // biome-ignore lint/suspicious/noDocumentCookie: test setup
179
- document.cookie = `bad-cookie=${encodeURIComponent("not-json{")}`
180
- const sig = useCookie("bad-cookie", "default", {
181
- onError: () => "custom-fallback",
179
+ document.cookie = `bad-cookie=${encodeURIComponent('not-json{')}`
180
+ const sig = useCookie('bad-cookie', 'default', {
181
+ onError: () => 'custom-fallback',
182
182
  })
183
- expect(sig()).toBe("custom-fallback")
183
+ expect(sig()).toBe('custom-fallback')
184
184
  })
185
185
  })
186
186
  })
@@ -1,9 +1,9 @@
1
- import { afterEach, beforeEach, describe, expect, it } from "vitest"
2
- import { _resetRegistry, setCookieSource, useCookie } from "../index"
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
2
+ import { _resetRegistry, setCookieSource, useCookie } from '../index'
3
3
 
4
4
  function clearAllCookies(): void {
5
- for (const cookie of document.cookie.split(";")) {
6
- const name = cookie.split("=")[0]?.trim()
5
+ for (const cookie of document.cookie.split(';')) {
6
+ const name = cookie.split('=')[0]?.trim()
7
7
  if (name) {
8
8
  // biome-ignore lint/suspicious/noDocumentCookie: test cleanup requires direct cookie access
9
9
  document.cookie = `${name}=; max-age=0; path=/`
@@ -11,7 +11,7 @@ function clearAllCookies(): void {
11
11
  }
12
12
  }
13
13
 
14
- describe("useCookie", () => {
14
+ describe('useCookie', () => {
15
15
  beforeEach(() => {
16
16
  _resetRegistry()
17
17
  clearAllCookies()
@@ -22,128 +22,128 @@ describe("useCookie", () => {
22
22
  clearAllCookies()
23
23
  })
24
24
 
25
- it("returns default value when cookie does not exist", () => {
26
- const locale = useCookie("locale", "en")
27
- expect(locale()).toBe("en")
25
+ it('returns default value when cookie does not exist', () => {
26
+ const locale = useCookie('locale', 'en')
27
+ expect(locale()).toBe('en')
28
28
  })
29
29
 
30
- it("reads existing cookie value", () => {
30
+ it('reads existing cookie value', () => {
31
31
  // biome-ignore lint/suspicious/noDocumentCookie: test setup requires direct cookie access
32
- document.cookie = `locale=${encodeURIComponent(JSON.stringify("de"))}; path=/`
33
- const locale = useCookie("locale", "en")
34
- expect(locale()).toBe("de")
32
+ document.cookie = `locale=${encodeURIComponent(JSON.stringify('de'))}; path=/`
33
+ const locale = useCookie('locale', 'en')
34
+ expect(locale()).toBe('de')
35
35
  })
36
36
 
37
- it(".set() updates signal and writes cookie", () => {
38
- const locale = useCookie("locale", "en")
39
- locale.set("fr")
40
- expect(locale()).toBe("fr")
41
- expect(document.cookie).toContain("locale")
37
+ it('.set() updates signal and writes cookie', () => {
38
+ const locale = useCookie('locale', 'en')
39
+ locale.set('fr')
40
+ expect(locale()).toBe('fr')
41
+ expect(document.cookie).toContain('locale')
42
42
  })
43
43
 
44
- it(".remove() deletes cookie and resets to default", () => {
45
- const locale = useCookie("locale", "en")
46
- locale.set("fr")
44
+ it('.remove() deletes cookie and resets to default', () => {
45
+ const locale = useCookie('locale', 'en')
46
+ locale.set('fr')
47
47
  locale.remove()
48
- expect(locale()).toBe("en")
48
+ expect(locale()).toBe('en')
49
49
  })
50
50
 
51
- it("returns same signal instance for same key", () => {
52
- const a = useCookie("locale", "en")
53
- const b = useCookie("locale", "en")
51
+ it('returns same signal instance for same key', () => {
52
+ const a = useCookie('locale', 'en')
53
+ const b = useCookie('locale', 'en')
54
54
  expect(a).toBe(b)
55
55
  })
56
56
 
57
- it("works with objects", () => {
58
- const prefs = useCookie("prefs", { dark: false })
57
+ it('works with objects', () => {
58
+ const prefs = useCookie('prefs', { dark: false })
59
59
  prefs.set({ dark: true })
60
60
  expect(prefs()).toEqual({ dark: true })
61
61
  })
62
62
 
63
- it(".update() works", () => {
64
- const count = useCookie("count", 0)
63
+ it('.update() works', () => {
64
+ const count = useCookie('count', 0)
65
65
  count.update((n) => n + 1)
66
66
  expect(count()).toBe(1)
67
67
  })
68
68
 
69
- it(".peek() reads without subscribing", () => {
70
- const locale = useCookie("locale", "en")
71
- expect(locale.peek()).toBe("en")
69
+ it('.peek() reads without subscribing', () => {
70
+ const locale = useCookie('locale', 'en')
71
+ expect(locale.peek()).toBe('en')
72
72
  })
73
73
 
74
- it("respects maxAge option", () => {
75
- const locale = useCookie("locale", "en", { maxAge: 3600 })
76
- locale.set("de")
77
- expect(document.cookie).toContain("locale")
74
+ it('respects maxAge option', () => {
75
+ const locale = useCookie('locale', 'en', { maxAge: 3600 })
76
+ locale.set('de')
77
+ expect(document.cookie).toContain('locale')
78
78
  })
79
79
 
80
- it("respects secure option", () => {
81
- const token = useCookie("token", "", { secure: true })
82
- token.set("abc123")
83
- expect(token()).toBe("abc123")
80
+ it('respects secure option', () => {
81
+ const token = useCookie('token', '', { secure: true })
82
+ token.set('abc123')
83
+ expect(token()).toBe('abc123')
84
84
  })
85
85
 
86
- it("respects expires option", () => {
86
+ it('respects expires option', () => {
87
87
  const future = new Date(Date.now() + 86400000)
88
- const sig = useCookie("exp", "val", { expires: future })
89
- sig.set("updated")
90
- expect(sig()).toBe("updated")
88
+ const sig = useCookie('exp', 'val', { expires: future })
89
+ sig.set('updated')
90
+ expect(sig()).toBe('updated')
91
91
  })
92
92
 
93
- it("respects domain option on set and remove", () => {
94
- const sig = useCookie("dom", "val", { domain: "example.com" })
95
- sig.set("updated")
96
- expect(sig()).toBe("updated")
93
+ it('respects domain option on set and remove', () => {
94
+ const sig = useCookie('dom', 'val', { domain: 'example.com' })
95
+ sig.set('updated')
96
+ expect(sig()).toBe('updated')
97
97
  sig.remove()
98
- expect(sig()).toBe("val")
98
+ expect(sig()).toBe('val')
99
99
  })
100
100
 
101
- it(".subscribe() works", () => {
102
- const sig = useCookie("sub", "a")
101
+ it('.subscribe() works', () => {
102
+ const sig = useCookie('sub', 'a')
103
103
  let called = false
104
104
  const unsub = sig.subscribe(() => {
105
105
  called = true
106
106
  })
107
- sig.set("b")
107
+ sig.set('b')
108
108
  expect(called).toBe(true)
109
109
  unsub()
110
110
  })
111
111
 
112
- it(".direct() works", () => {
113
- const sig = useCookie("dir", "a")
112
+ it('.direct() works', () => {
113
+ const sig = useCookie('dir', 'a')
114
114
  let called = false
115
115
  const unsub = sig.direct(() => {
116
116
  called = true
117
117
  })
118
- sig.set("b")
118
+ sig.set('b')
119
119
  expect(called).toBe(true)
120
120
  unsub()
121
121
  })
122
122
 
123
- it(".debug() returns debug info", () => {
124
- const sig = useCookie("dbg", "test")
125
- expect(sig.debug().value).toBe("test")
123
+ it('.debug() returns debug info', () => {
124
+ const sig = useCookie('dbg', 'test')
125
+ expect(sig.debug().value).toBe('test')
126
126
  })
127
127
 
128
- it(".label can be set and read", () => {
129
- const sig = useCookie("lbl", "val")
130
- sig.label = "my-cookie"
131
- expect(sig.label).toBe("my-cookie")
128
+ it('.label can be set and read', () => {
129
+ const sig = useCookie('lbl', 'val')
130
+ sig.label = 'my-cookie'
131
+ expect(sig.label).toBe('my-cookie')
132
132
  })
133
133
  })
134
134
 
135
- describe("setCookieSource (SSR)", () => {
135
+ describe('setCookieSource (SSR)', () => {
136
136
  beforeEach(() => {
137
137
  _resetRegistry()
138
138
  })
139
139
 
140
140
  afterEach(() => {
141
141
  _resetRegistry()
142
- setCookieSource("")
142
+ setCookieSource('')
143
143
  })
144
144
 
145
- it("stores server cookie string without throwing", () => {
146
- setCookieSource("locale=de; theme=dark")
147
- expect(() => setCookieSource("foo=bar")).not.toThrow()
145
+ it('stores server cookie string without throwing', () => {
146
+ setCookieSource('locale=de; theme=dark')
147
+ expect(() => setCookieSource('foo=bar')).not.toThrow()
148
148
  })
149
149
  })