@telia-ace/widget-services 0.0.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/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createStorageWriter, LegacyStorage as StorageService, LegacyStorageProvider as StorageProvider, readStorage, StorageCategory, StorageMedium, storagePolicyConfigurationApi, StorageScope, } from './storage';
2
+ export type { Duration, StorageWriter, StorageWriterOptions } from './storage';
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@telia-ace/widget-utilities"),L=require("@webprovisions/platform");var l=(e=>(e.Cookie="cookie",e.Local="local",e.Session="session",e))(l||{}),f=(e=>(e.Tenant="tenant",e.Implementation="implementation",e.Widget="widget",e))(f||{}),d=(e=>(e.Necessary="necessary",e.Functional="functional",e.Analytical="analytical",e.Marketing="marketing",e))(d||{});const K=(e,t,n,o={})=>k(e,n).then(s=>(process.env.NODE_ENV!=="production"&&s&&c.Logger.warn(`createStorageWriter: Attempted to create storage writer using unallowed category.
2
+ Key: ${t}
3
+ Category: ${JSON.stringify(n)}
4
+ Options: ${JSON.stringify(o)}`),r=>(D(e),k(e,n).then(i=>{if(i){process.env.NODE_ENV!=="production"&&c.Logger.warn(`storageWriter: Attempted to write to storage using unallowed category.
5
+ Key: ${t}
6
+ Category: ${JSON.stringify(n)}
7
+ Value: ${JSON.stringify(r)}
8
+ Options: ${JSON.stringify(o)}`);return}const{scope:g=f.Widget,medium:a=l.Session,duration:u}=o,m=T();return m[g][t]=J(r,n,u),a===l.Cookie?M(e,m):q(e,a,m)})))),b=(e,t,n=l.Session)=>(D(e),typeof t>"u"?(process.env.NODE_ENV!=="production"&&c.Logger.warn("readStorage: Undefined key passed when reading storage. ",t,n),Promise.resolve(void 0)):n===l.Cookie?j(e).then(o=>h(o,t)):U(e,n).then(o=>h(o,t))),y=e=>!!Object.entries(e).filter(([t,n])=>typeof n<"u").length,$=e=>Object.entries(e).filter(([t,n])=>typeof n<"u").forEach(([t,{value:n}])=>{typeof n>"u"&&delete e[t]}),_=(e,t)=>{const n={tenant:{...e.tenant,...t.tenant},implementation:{...e.implementation,...t.implementation},widget:{...e.widget,...t.widget}};return $(n.tenant),$(n.implementation),$(n.widget),n},x=e=>Object.entries(e).reduce((n,o)=>{const[s,r]=o;return Object.entries(r).forEach(i=>{const[g,{expiration:a}]=i;a&&n.push(a)}),n},[]).slice().sort((n,o)=>new Date(o).valueOf()-new Date(n).valueOf()).shift()||v(),O=e=>{const{name:t,implementation:{name:n,tenant:o}}=e.get("$widget");return[`humany_${o}`,`humany_${c.hash(o+n)}`,`humany_${c.hash(o+n+t)}`]},E=e=>{const t=new RegExp(`(?:^|;)\\s?${e}=(.*?)(?:;|$)`,"i"),n=document.cookie.match(t);return n&&n[1]&&JSON.parse(n[1])||void 0},A=(e,t,n)=>{const o=`${e}=${JSON.stringify(t).trim()}`,s=`expires=${new Date(n).toUTCString()}`,r="path=/",i="SameSite=None;Secure";return document.cookie=`${o}; ${s}; ${r}; ${i}`},P=e=>{const[t,n,o]=O(e),s=E(t)||{},r=E(n)||{},i=E(o)||{};return{tenant:s,implementation:r,widget:i}},I=(e,t)=>{const n=P(e),o=_(n,t),[s,r,i]=O(e),{tenant:g,implementation:a,widget:u}=o,m=x(t),p=v({days:-1});A(s,g,y(g)?m:p),A(r,a,y(a)?m:p),A(i,u,y(u)?m:p)},N=(e,t)=>{const[n,o,s]=O(e),r=t.getItem(n)||"{}",i=t.getItem(o)||"{}",g=t.getItem(s)||"{}";return{tenant:JSON.parse(r),implementation:JSON.parse(i),widget:JSON.parse(g)}},C=(e,t,n)=>{const o=N(e,t),s=_(o,n),[r,i,g]=O(e),{tenant:a,implementation:u,widget:m}=s;y(a)?t.setItem(r,JSON.stringify(a)):t.removeItem(r),y(u)?t.setItem(i,JSON.stringify(u)):t.removeItem(i),y(m)?t.setItem(g,JSON.stringify(m)):t.removeItem(g);const{events:p}=e.get("$widget");p.dispatch("storage:changed",{})},T=()=>({tenant:{},implementation:{},widget:{}}),V=e=>e?!!e&&new Date>new Date(e):!1,h=(e,t)=>{const n=e.widget[t]||e.implementation[t]||e.tenant[t];return n==null?void 0:n.value},J=(e,t,n)=>n?{value:e,category:t,expiration:typeof n=="object"?v(n):n}:{value:e,category:t},S=(e,t)=>{const n=c.deepClone(e);return Object.entries(n).forEach(([o,s])=>{Object.entries(s).forEach(([r,i])=>{const{expiration:g,category:a}=i;(V(g)||W(t,a))&&(s[r]=J(void 0,a,g))})}),n},k=(e,t)=>b(e,"storagePolicy",l.Cookie).then((n=[d.Necessary])=>!!t&&n.indexOf(t)===-1),W=(e=[d.Necessary],t)=>!!t&&e.indexOf(t)===-1,D=e=>{const t=P(e),n=N(e,window.localStorage),o=N(e,sessionStorage),s=h(t,"storagePolicy"),[r,i,g]=[t,n,o].map(a=>S(a,s));I(e,r),C(e,window.localStorage,i),C(e,window.sessionStorage,g)},v=(e={days:30})=>{const{days:t=0,hours:n=0,minutes:o=0,seconds:s=0}=e,r=new Date;return r.setTime(r.getTime()+n*60*60*1e3),r.setTime(r.getTime()+t*24*60*60*1e3),r.setTime(r.getTime()+o*60*1e3),r.setTime(r.getTime()+s*1e3),r.toISOString()},j=e=>c.lock(document)(()=>P(e)),M=(e,t)=>c.lock(document)(()=>I(e,t)),U=(e,t)=>{const n=t===l.Local?window.localStorage:window.sessionStorage;return c.lock(n)(()=>N(e,n))},q=(e,t,n)=>{const o=t===l.Local?window.localStorage:window.sessionStorage;return c.lock(o)(()=>C(e,o,n))};class w{constructor(){this.memory={tenant:{},implementation:{},widget:{}}}async setItem(t,n,o={}){return c.lock(this)(()=>{const s=S(this.memory,[d.Necessary]),{scope:r=f.Tenant,expiration:i}=o;s[r][t]=J(n,d.Necessary,(i==null?void 0:i.toISOString())||v()),this.memory=s,process.env.NODE_ENV!=="production"&&c.Logger.warn(`StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value stored temporarily in memory.
9
+ Key: ${t}
10
+ Value: ${JSON.stringify(n)}
11
+ Options: ${JSON.stringify(o)}
12
+ Memory: ${JSON.stringify(s)}`)})}async getItem(t){return c.lock(this)(()=>{const n=S(this.memory,[d.Necessary]);process.env.NODE_ENV!=="production"&&c.Logger.warn(`StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value fetched from legacy memory storage.
13
+ Key: ${t}
14
+ Memory: ${JSON.stringify(n)}`);const o=h(n,t);return this.memory=n,o})}async removeItem(t){return c.lock(this)(()=>{const n=S(this.memory,[d.Necessary]);delete n.tenant[t],delete n.implementation[t],delete n.widget[t],this.memory=n,process.env.NODE_ENV!=="production"&&c.Logger.warn(`StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value removed from memory.
15
+ Key: ${t}
16
+ Memory: ${JSON.stringify(n)}`)})}async clear(){return Promise.resolve()}}class F{constructor(){this.cookies=new w,this.session=new w,this.local=new w}}const R=L.createConfigurationApi(e=>{e.createCommand("storage",t=>(n={})=>{const{consent:o}=n;if(typeof o<"u"){if(typeof o!="number"&&typeof o!="string"&&!Array.isArray(o)){process.env.NODE_ENV!=="production"&&c.Logger.warn(`storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.
17
+ Passed consent: ${JSON.stringify(o)}`);return}if(Array.isArray(o)&&!o.every(r=>typeof r=="number"||typeof r=="string")){process.env.NODE_ENV!=="production"&&c.Logger.warn(`storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.
18
+ Passed consent: ${JSON.stringify(o)}`);return}const s=[d.Necessary,d.Functional,d.Analytical,d.Marketing].filter((r,i,g)=>typeof o=="number"?o>i:typeof o=="string"?g.indexOf(o)+1>i:Array.isArray(o)?o.findIndex(a=>a===i+1||a===r)>-1:!0);return s.indexOf(d.Necessary)===-1&&(process.env.NODE_ENV!=="production"&&c.Logger.warn(`storagePolicyConfigurationApi: passed consent does not include category: '${d.Necessary}'. Consent has to atleast include the '${d.Necessary}' category.
19
+ Passed consent: ${JSON.stringify(o)}`),s.unshift(d.Necessary)),K(t.container,"storagePolicy",d.Necessary,{medium:l.Cookie,scope:f.Tenant,duration:{days:365}}).then(r=>r(s)).then(()=>{D(t.container),t.events.dispatch("storage:policy-changed",s)})}})});exports.StorageCategory=d;exports.StorageMedium=l;exports.StorageProvider=F;exports.StorageScope=f;exports.StorageService=w;exports.createStorageWriter=K;exports.readStorage=b;exports.storagePolicyConfigurationApi=R;
package/index.mjs ADDED
@@ -0,0 +1,265 @@
1
+ import { Logger as g, lock as y, deepClone as k, hash as _ } from "@telia-ace/widget-utilities";
2
+ import { createConfigurationApi as T } from "@webprovisions/platform";
3
+ var l = /* @__PURE__ */ ((e) => (e.Cookie = "cookie", e.Local = "local", e.Session = "session", e))(l || {}), h = /* @__PURE__ */ ((e) => (e.Tenant = "tenant", e.Implementation = "implementation", e.Widget = "widget", e))(h || {}), d = /* @__PURE__ */ ((e) => (e.Necessary = "necessary", e.Functional = "functional", e.Analytical = "analytical", e.Marketing = "marketing", e))(d || {});
4
+ const V = (e, t, n, r = {}) => I(e, n).then((s) => (process.env.NODE_ENV !== "production" && s && g.warn(
5
+ `createStorageWriter: Attempted to create storage writer using unallowed category.
6
+ Key: ${t}
7
+ Category: ${JSON.stringify(n)}
8
+ Options: ${JSON.stringify(r)}`
9
+ ), (o) => (K(e), I(e, n).then((i) => {
10
+ if (i) {
11
+ process.env.NODE_ENV !== "production" && g.warn(
12
+ `storageWriter: Attempted to write to storage using unallowed category.
13
+ Key: ${t}
14
+ Category: ${JSON.stringify(n)}
15
+ Value: ${JSON.stringify(o)}
16
+ Options: ${JSON.stringify(r)}`
17
+ );
18
+ return;
19
+ }
20
+ const {
21
+ scope: c = h.Widget,
22
+ medium: a = l.Session,
23
+ duration: u
24
+ } = r, m = j();
25
+ return m[c][t] = D(o, n, u), a === l.Cookie ? R(e, m) : z(e, a, m);
26
+ })))), L = (e, t, n = l.Session) => (K(e), typeof t > "u" ? (process.env.NODE_ENV !== "production" && g.warn(
27
+ "readStorage: Undefined key passed when reading storage. ",
28
+ t,
29
+ n
30
+ ), Promise.resolve(void 0)) : n === l.Cookie ? F(e).then((r) => N(r, t)) : q(e, n).then(
31
+ (r) => N(r, t)
32
+ )), p = (e) => !!Object.entries(e).filter(([t, n]) => typeof n < "u").length, $ = (e) => Object.entries(e).filter(([t, n]) => typeof n < "u").forEach(([t, { value: n }]) => {
33
+ typeof n > "u" && delete e[t];
34
+ }), b = (e, t) => {
35
+ const n = {
36
+ tenant: { ...e.tenant, ...t.tenant },
37
+ implementation: { ...e.implementation, ...t.implementation },
38
+ widget: { ...e.widget, ...t.widget }
39
+ };
40
+ return $(n.tenant), $(n.implementation), $(n.widget), n;
41
+ }, W = (e) => Object.entries(e).reduce(
42
+ (n, r) => {
43
+ const [s, o] = r;
44
+ return Object.entries(o).forEach((i) => {
45
+ const [c, { expiration: a }] = i;
46
+ a && n.push(a);
47
+ }), n;
48
+ },
49
+ []
50
+ ).slice().sort((n, r) => new Date(r).valueOf() - new Date(n).valueOf()).shift() || v(), O = (e) => {
51
+ const {
52
+ name: t,
53
+ implementation: { name: n, tenant: r }
54
+ } = e.get("$widget");
55
+ return [
56
+ `humany_${r}`,
57
+ `humany_${_(r + n)}`,
58
+ `humany_${_(r + n + t)}`
59
+ ];
60
+ }, E = (e) => {
61
+ const t = new RegExp(`(?:^|;)\\s?${e}=(.*?)(?:;|$)`, "i"), n = document.cookie.match(t);
62
+ return n && n[1] && JSON.parse(n[1]) || void 0;
63
+ }, A = (e, t, n) => {
64
+ const r = `${e}=${JSON.stringify(t).trim()}`, s = `expires=${new Date(n).toUTCString()}`, o = "path=/", i = "SameSite=None;Secure";
65
+ return document.cookie = `${r}; ${s}; ${o}; ${i}`;
66
+ }, P = (e) => {
67
+ const [t, n, r] = O(e), s = E(t) || {}, o = E(n) || {}, i = E(r) || {};
68
+ return {
69
+ tenant: s,
70
+ implementation: o,
71
+ widget: i
72
+ };
73
+ }, x = (e, t) => {
74
+ const n = P(e), r = b(n, t), [s, o, i] = O(e), { tenant: c, implementation: a, widget: u } = r, m = W(t), f = v({ days: -1 });
75
+ A(
76
+ s,
77
+ c,
78
+ p(c) ? m : f
79
+ ), A(
80
+ o,
81
+ a,
82
+ p(a) ? m : f
83
+ ), A(
84
+ i,
85
+ u,
86
+ p(u) ? m : f
87
+ );
88
+ }, w = (e, t) => {
89
+ const [n, r, s] = O(e), o = t.getItem(n) || "{}", i = t.getItem(r) || "{}", c = t.getItem(s) || "{}";
90
+ return {
91
+ tenant: JSON.parse(o),
92
+ implementation: JSON.parse(i),
93
+ widget: JSON.parse(c)
94
+ };
95
+ }, J = (e, t, n) => {
96
+ const r = w(e, t), s = b(r, n), [o, i, c] = O(e), { tenant: a, implementation: u, widget: m } = s;
97
+ p(a) ? t.setItem(o, JSON.stringify(a)) : t.removeItem(o), p(u) ? t.setItem(
98
+ i,
99
+ JSON.stringify(u)
100
+ ) : t.removeItem(i), p(m) ? t.setItem(c, JSON.stringify(m)) : t.removeItem(c);
101
+ const { events: f } = e.get("$widget");
102
+ f.dispatch("storage:changed", {});
103
+ }, j = () => ({
104
+ tenant: {},
105
+ implementation: {},
106
+ widget: {}
107
+ }), M = (e) => e ? !!e && /* @__PURE__ */ new Date() > new Date(e) : !1, N = (e, t) => {
108
+ const n = e.widget[t] || e.implementation[t] || e.tenant[t];
109
+ return n == null ? void 0 : n.value;
110
+ }, D = (e, t, n) => n ? {
111
+ value: e,
112
+ category: t,
113
+ expiration: typeof n == "object" ? v(n) : n
114
+ } : {
115
+ value: e,
116
+ category: t
117
+ }, S = (e, t) => {
118
+ const n = k(e);
119
+ return Object.entries(n).forEach(([r, s]) => {
120
+ Object.entries(s).forEach(([o, i]) => {
121
+ const { expiration: c, category: a } = i;
122
+ (M(c) || U(t, a)) && (s[o] = D(void 0, a, c));
123
+ });
124
+ }), n;
125
+ }, I = (e, t) => L(
126
+ e,
127
+ "storagePolicy",
128
+ l.Cookie
129
+ ).then(
130
+ (n = [d.Necessary]) => !!t && n.indexOf(t) === -1
131
+ ), U = (e = [d.Necessary], t) => !!t && e.indexOf(t) === -1, K = (e) => {
132
+ const t = P(e), n = w(e, window.localStorage), r = w(e, sessionStorage), s = N(t, "storagePolicy"), [o, i, c] = [
133
+ t,
134
+ n,
135
+ r
136
+ ].map((a) => S(a, s));
137
+ x(e, o), J(e, window.localStorage, i), J(e, window.sessionStorage, c);
138
+ }, v = (e = { days: 30 }) => {
139
+ const { days: t = 0, hours: n = 0, minutes: r = 0, seconds: s = 0 } = e, o = /* @__PURE__ */ new Date();
140
+ return o.setTime(o.getTime() + n * 60 * 60 * 1e3), o.setTime(o.getTime() + t * 24 * 60 * 60 * 1e3), o.setTime(o.getTime() + r * 60 * 1e3), o.setTime(o.getTime() + s * 1e3), o.toISOString();
141
+ }, F = (e) => y(document)(() => P(e)), R = (e, t) => y(document)(() => x(e, t)), q = (e, t) => {
142
+ const n = t === l.Local ? window.localStorage : window.sessionStorage;
143
+ return y(n)(() => w(e, n));
144
+ }, z = (e, t, n) => {
145
+ const r = t === l.Local ? window.localStorage : window.sessionStorage;
146
+ return y(r)(() => J(e, r, n));
147
+ };
148
+ class C {
149
+ constructor() {
150
+ this.memory = {
151
+ tenant: {},
152
+ implementation: {},
153
+ widget: {}
154
+ };
155
+ }
156
+ async setItem(t, n, r = {}) {
157
+ return y(this)(() => {
158
+ const s = S(this.memory, [
159
+ d.Necessary
160
+ ]), { scope: o = h.Tenant, expiration: i } = r;
161
+ s[o][t] = D(
162
+ n,
163
+ d.Necessary,
164
+ (i == null ? void 0 : i.toISOString()) || v()
165
+ ), this.memory = s, process.env.NODE_ENV !== "production" && g.warn(
166
+ `StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value stored temporarily in memory.
167
+ Key: ${t}
168
+ Value: ${JSON.stringify(n)}
169
+ Options: ${JSON.stringify(r)}
170
+ Memory: ${JSON.stringify(s)}`
171
+ );
172
+ });
173
+ }
174
+ async getItem(t) {
175
+ return y(this)(() => {
176
+ const n = S(this.memory, [
177
+ d.Necessary
178
+ ]);
179
+ process.env.NODE_ENV !== "production" && g.warn(
180
+ `StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value fetched from legacy memory storage.
181
+ Key: ${t}
182
+ Memory: ${JSON.stringify(n)}`
183
+ );
184
+ const r = N(n, t);
185
+ return this.memory = n, r;
186
+ });
187
+ }
188
+ async removeItem(t) {
189
+ return y(this)(() => {
190
+ const n = S(this.memory, [
191
+ d.Necessary
192
+ ]);
193
+ delete n.tenant[t], delete n.implementation[t], delete n.widget[t], this.memory = n, process.env.NODE_ENV !== "production" && g.warn(
194
+ `StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value removed from memory.
195
+ Key: ${t}
196
+ Memory: ${JSON.stringify(n)}`
197
+ );
198
+ });
199
+ }
200
+ async clear() {
201
+ return Promise.resolve();
202
+ }
203
+ }
204
+ class H {
205
+ constructor() {
206
+ this.cookies = new C(), this.session = new C(), this.local = new C();
207
+ }
208
+ }
209
+ const Q = T((e) => {
210
+ e.createCommand(
211
+ "storage",
212
+ (t) => (n = {}) => {
213
+ const { consent: r } = n;
214
+ if (typeof r < "u") {
215
+ if (typeof r != "number" && typeof r != "string" && !Array.isArray(r)) {
216
+ process.env.NODE_ENV !== "production" && g.warn(
217
+ `storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.
218
+ Passed consent: ${JSON.stringify(r)}`
219
+ );
220
+ return;
221
+ }
222
+ if (Array.isArray(r) && !r.every(
223
+ (o) => typeof o == "number" || typeof o == "string"
224
+ )) {
225
+ process.env.NODE_ENV !== "production" && g.warn(
226
+ `storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.
227
+ Passed consent: ${JSON.stringify(r)}`
228
+ );
229
+ return;
230
+ }
231
+ const s = [
232
+ d.Necessary,
233
+ d.Functional,
234
+ d.Analytical,
235
+ d.Marketing
236
+ ].filter((o, i, c) => typeof r == "number" ? r > i : typeof r == "string" ? c.indexOf(r) + 1 > i : Array.isArray(r) ? r.findIndex((a) => a === i + 1 || a === o) > -1 : !0);
237
+ return s.indexOf(d.Necessary) === -1 && (process.env.NODE_ENV !== "production" && g.warn(
238
+ `storagePolicyConfigurationApi: passed consent does not include category: '${d.Necessary}'. Consent has to atleast include the '${d.Necessary}' category.
239
+ Passed consent: ${JSON.stringify(r)}`
240
+ ), s.unshift(d.Necessary)), V(
241
+ t.container,
242
+ "storagePolicy",
243
+ d.Necessary,
244
+ {
245
+ medium: l.Cookie,
246
+ scope: h.Tenant,
247
+ duration: { days: 365 }
248
+ }
249
+ ).then((o) => o(s)).then(() => {
250
+ K(t.container), t.events.dispatch("storage:policy-changed", s);
251
+ });
252
+ }
253
+ }
254
+ );
255
+ });
256
+ export {
257
+ d as StorageCategory,
258
+ l as StorageMedium,
259
+ H as StorageProvider,
260
+ h as StorageScope,
261
+ C as StorageService,
262
+ V as createStorageWriter,
263
+ L as readStorage,
264
+ Q as storagePolicyConfigurationApi
265
+ };
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@telia-ace/widget-services",
3
+ "version": "0.0.1",
4
+ "dependencies": {
5
+ "@telia-ace/widget-utilities": "*",
6
+ "@webprovisions/platform": "^1.1.4"
7
+ },
8
+ "main": "./index.js",
9
+ "module": "./index.mjs",
10
+ "typings": "./index.d.ts"
11
+ }
@@ -0,0 +1,5 @@
1
+ export { default as LegacyStorageProvider, LegacyStorage, } from './legacy-storage-provider';
2
+ export { createStorageWriter, readStorage } from './storage';
3
+ export { default as storagePolicyConfigurationApi } from './storage-policy-configuration-api';
4
+ export { StorageCategory, StorageMedium, StorageScope } from './types';
5
+ export type { Duration, StorageWriter, StorageWriterOptions } from './types';
@@ -0,0 +1,26 @@
1
+ import { StorageScope } from './types';
2
+
3
+ type LegacyOptions = {
4
+ scope?: StorageScope;
5
+ expiration?: Date;
6
+ };
7
+ interface LegacyMedium {
8
+ setItem(key: string, value: any, options?: LegacyOptions): Promise<void>;
9
+ getItem(key: string): Promise<any>;
10
+ removeItem(key: string): Promise<void>;
11
+ clear(): Promise<void>;
12
+ }
13
+ export declare class LegacyStorage implements LegacyMedium {
14
+ private memory;
15
+ setItem(key: string, value: any, options?: LegacyOptions): Promise<void>;
16
+ getItem(key: string): Promise<any>;
17
+ removeItem(key: string): Promise<void>;
18
+ clear(): Promise<void>;
19
+ }
20
+ export default class LegacyStorageProvider {
21
+ cookies: LegacyStorage;
22
+ session: LegacyStorage;
23
+ local: LegacyStorage;
24
+ constructor();
25
+ }
26
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import('@webprovisions/platform').ConfigurationApiDefinition;
2
+ export default _default;
@@ -0,0 +1,20 @@
1
+ import { Container } from '@webprovisions/platform';
2
+ import { StorageCategory, StorageMedium, StorageWriter, StorageWriterOptions } from './types';
3
+
4
+ /**
5
+ * Creates a writer to be used to write to cookie, local or session storage.
6
+ * @param container Widget Container.
7
+ * @param key Key to use when writing.
8
+ * @param category StorageCategory to use when writing.
9
+ * @param options StorageWriterOptions to use when writing default {scope: StorageScope.Widget, medium = StorageMedium.Session, duration = infinite }.
10
+ * @returns a function to be used to write a value to the storage using given settings.
11
+ */
12
+ export declare const createStorageWriter: (container: Container, key: string, category: StorageCategory, options?: StorageWriterOptions) => Promise<StorageWriter>;
13
+ /**
14
+ * Read the widget cookie, local or session storage.
15
+ * @param container Widget Container.
16
+ * @param key Key to read.
17
+ * @param medium Storage medium to read from (default: session).
18
+ * @returns the value stored with passed key.
19
+ */
20
+ export declare const readStorage: <Type>(container: Container, key: string, medium?: StorageMedium) => Promise<Type | undefined>;
@@ -0,0 +1,45 @@
1
+ export declare enum StorageMedium {
2
+ Cookie = "cookie",
3
+ Local = "local",
4
+ Session = "session"
5
+ }
6
+ export declare enum StorageScope {
7
+ Tenant = "tenant",
8
+ Implementation = "implementation",
9
+ Widget = "widget"
10
+ }
11
+ export type Store = {
12
+ tenant: StorageContainer;
13
+ implementation: StorageContainer;
14
+ widget: StorageContainer;
15
+ };
16
+ export type StorageContainer = {
17
+ [key: string]: StoredValue;
18
+ };
19
+ export type StoredValue = {
20
+ value: any;
21
+ expiration?: string;
22
+ category: StorageCategory;
23
+ };
24
+ export declare enum StorageCategory {
25
+ Necessary = "necessary",
26
+ Functional = "functional",
27
+ Analytical = "analytical",
28
+ Marketing = "marketing"
29
+ }
30
+ export type StorageWriter = (value?: any) => Promise<void>;
31
+ export type StorageConfigurationOptions = {
32
+ consent?: StorageConsent;
33
+ };
34
+ export type StorageConsent = number | number[] | StorageCategory | StorageCategory[];
35
+ export type Duration = {
36
+ days?: number;
37
+ hours?: number;
38
+ minutes?: number;
39
+ seconds?: number;
40
+ };
41
+ export type StorageWriterOptions = {
42
+ scope?: StorageScope;
43
+ duration?: Duration;
44
+ medium?: StorageMedium;
45
+ };
@@ -0,0 +1,37 @@
1
+ import { Container } from '@webprovisions/platform';
2
+ import { Duration, StorageCategory, StorageMedium, Store, StoredValue } from './types';
3
+
4
+ export declare const createEmptyStore: () => Store;
5
+ export declare const isExpired: (isoDateString?: string) => boolean;
6
+ export declare const getValue: <Type>(store: Store, key: string) => Type | undefined;
7
+ export declare const createValue: (value: any, category: StorageCategory, duration?: Duration | string) => StoredValue;
8
+ export declare const processStore: (store: Store, policy?: StorageCategory[]) => Store;
9
+ /**
10
+ * @param category Storage Category.
11
+ * @param policy Current Storage Policy.
12
+ * @returns true if not allowed, false if allowed.
13
+ */
14
+ export declare const isNotAllowedAsync: (container: Container, category?: StorageCategory) => Promise<boolean>;
15
+ /**
16
+ * @param category Storage Category.
17
+ * @param policy Current Storage Policy.
18
+ * @returns true if not allowed, false if allowed.
19
+ */
20
+ export declare const isNotAllowed: (policy?: StorageCategory[], category?: StorageCategory) => boolean;
21
+ /**
22
+ * Processes storage and removes items if they are expired or if
23
+ * the storage policy is defined and the category of the stored
24
+ * value is not allowed.
25
+ * @param container Widget Container
26
+ */
27
+ export declare const processStorage: (container: Container) => void;
28
+ /**
29
+ * Creates an expiration date string with the passed options.
30
+ * @param options Duration options.
31
+ * @returns ISO formatted date string.
32
+ */
33
+ export declare const createExpirationDate: (options?: Duration) => string;
34
+ export declare const getCookieStoreAsync: (container: Container) => Promise<Store>;
35
+ export declare const setCookieStoreAsync: (container: Container, store: Store) => Promise<any>;
36
+ export declare const getApplicationStoreAsync: (container: Container, type: StorageMedium.Local | StorageMedium.Session) => Promise<Store>;
37
+ export declare const setApplicationStoreAsync: (container: Container, type: StorageMedium.Local | StorageMedium.Session, store: Store) => Promise<void>;