@thumbmarkjs/thumbmarkjs 1.5.1 → 1.6.1
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.
- package/README.md +4 -4
- package/dist/thumbmark.cjs.js +1 -1
- package/dist/thumbmark.cjs.js.map +1 -1
- package/dist/thumbmark.esm.d.ts +19 -2
- package/dist/thumbmark.esm.js +1 -1
- package/dist/thumbmark.esm.js.map +1 -1
- package/dist/thumbmark.umd.js +1 -1
- package/dist/thumbmark.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/functions/api.test.ts +71 -0
- package/src/functions/api.ts +49 -5
- package/src/functions/index.ts +2 -3
- package/src/options.test.ts +10 -0
- package/src/options.ts +30 -4
- package/src/utils/cache.test.ts +94 -0
- package/src/utils/cache.ts +59 -0
- package/src/utils/visitorId.test.ts +12 -3
- package/src/utils/visitorId.ts +31 -8
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getVisitorId, setVisitorId } from './visitorId';
|
|
2
|
-
import {
|
|
2
|
+
import { defaultOptions, options, OptionsAfterDefaults } from '../options';
|
|
3
3
|
|
|
4
4
|
describe('visitorId storage tests', () => {
|
|
5
5
|
beforeEach(() => {
|
|
@@ -21,7 +21,7 @@ describe('visitorId storage tests', () => {
|
|
|
21
21
|
|
|
22
22
|
test('should use custom storage property name', () => {
|
|
23
23
|
const visitorId = 'custom-visitor-456';
|
|
24
|
-
const customOptions:
|
|
24
|
+
const customOptions: OptionsAfterDefaults = {
|
|
25
25
|
...defaultOptions,
|
|
26
26
|
storage_property_name: 'my_custom_visitor_key'
|
|
27
27
|
};
|
|
@@ -37,7 +37,7 @@ describe('visitorId storage tests', () => {
|
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
test('should return null when storage property does not exist', () => {
|
|
40
|
-
const options:
|
|
40
|
+
const options: OptionsAfterDefaults = {
|
|
41
41
|
...defaultOptions,
|
|
42
42
|
storage_property_name: 'nonexistent_key'
|
|
43
43
|
};
|
|
@@ -56,6 +56,15 @@ describe('visitorId storage tests', () => {
|
|
|
56
56
|
setVisitorId(newVisitorId, options);
|
|
57
57
|
expect(getVisitorId(options)).toBe(newVisitorId);
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
test('should migrate from old value in case new prefix is set', () => {
|
|
61
|
+
const visitorId = 'test-visitor-123';
|
|
62
|
+
setVisitorId(visitorId, options);
|
|
63
|
+
expect(getVisitorId({
|
|
64
|
+
property_name_factory: (name) => `custom_prefix_${name}`,
|
|
65
|
+
} as OptionsAfterDefaults)).toBe(visitorId);
|
|
66
|
+
expect(localStorage.getItem(`custom_prefix_visitor_id`)).toBe(visitorId);
|
|
67
|
+
})
|
|
59
68
|
});
|
|
60
69
|
|
|
61
70
|
describe('error handling', () => {
|
package/src/utils/visitorId.ts
CHANGED
|
@@ -1,18 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DEFAULT_STORAGE_PREFIX, OptionsAfterDefaults } from "../options";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Visitor ID storage utilities - localStorage only, server generates IDs
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const DEFAULT_STORAGE_PROPERTY_NAME = '
|
|
7
|
+
const DEFAULT_STORAGE_PROPERTY_NAME = 'visitor_id';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Get the storage property name for visitor id
|
|
11
|
+
* @param _options
|
|
12
|
+
*/
|
|
13
|
+
export function getVisitorIdPropertyName(
|
|
14
|
+
_options: Pick<OptionsAfterDefaults, 'storage_property_name' | 'property_name_factory'>
|
|
15
|
+
): string {
|
|
16
|
+
if(_options.storage_property_name) {
|
|
17
|
+
return _options.storage_property_name;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return _options.property_name_factory(DEFAULT_STORAGE_PROPERTY_NAME);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const DEFAULT_VISITOR_ID_NAME = `${DEFAULT_STORAGE_PREFIX}_${DEFAULT_STORAGE_PROPERTY_NAME}`;
|
|
9
24
|
/**
|
|
10
25
|
* Gets visitor ID from localStorage, returns null if unavailable
|
|
11
26
|
*/
|
|
12
|
-
export function getVisitorId(_options:
|
|
13
|
-
const storagePropertyName = _options.storage_property_name || DEFAULT_STORAGE_PROPERTY_NAME;
|
|
27
|
+
export function getVisitorId(_options: OptionsAfterDefaults): string | null {
|
|
14
28
|
try {
|
|
15
|
-
|
|
29
|
+
const propertyName = getVisitorIdPropertyName(_options);
|
|
30
|
+
let visitorId = localStorage.getItem(propertyName);
|
|
31
|
+
if(!visitorId && propertyName !== DEFAULT_VISITOR_ID_NAME) {
|
|
32
|
+
// Migration case in case going from thumbmark prefix to a custom one
|
|
33
|
+
visitorId = localStorage.getItem(DEFAULT_VISITOR_ID_NAME);
|
|
34
|
+
if(visitorId) {
|
|
35
|
+
setVisitorId(visitorId, _options);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return visitorId;
|
|
16
40
|
} catch {
|
|
17
41
|
return null;
|
|
18
42
|
}
|
|
@@ -21,10 +45,9 @@ export function getVisitorId(_options: optionsInterface): string | null {
|
|
|
21
45
|
/**
|
|
22
46
|
* Sets visitor ID in localStorage
|
|
23
47
|
*/
|
|
24
|
-
export function setVisitorId(visitorId: string, _options:
|
|
25
|
-
const storagePropertyName = _options.storage_property_name || DEFAULT_STORAGE_PROPERTY_NAME;
|
|
48
|
+
export function setVisitorId(visitorId: string, _options: OptionsAfterDefaults): void {
|
|
26
49
|
try {
|
|
27
|
-
localStorage.setItem(
|
|
50
|
+
localStorage.setItem(getVisitorIdPropertyName(_options), visitorId);
|
|
28
51
|
} catch {
|
|
29
52
|
// Ignore storage errors
|
|
30
53
|
}
|