musora-content-services 1.2.5 → 1.3.2

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.
Files changed (40) hide show
  1. package/.prettierignore +5 -0
  2. package/.prettierrc +8 -0
  3. package/.yarnrc.yml +1 -0
  4. package/CHANGELOG.md +11 -0
  5. package/README.md +0 -0
  6. package/babel.config.cjs +1 -1
  7. package/docs/config.js.html +0 -0
  8. package/docs/index.html +0 -0
  9. package/docs/module-Config.html +0 -0
  10. package/docs/module-Railcontent-Services.html +0 -0
  11. package/docs/module-Sanity-Services.html +0 -0
  12. package/docs/railcontent.js.html +0 -0
  13. package/docs/sanity.js.html +0 -0
  14. package/jest.config.js +9 -10
  15. package/jsdoc.json +17 -12
  16. package/package.json +2 -1
  17. package/src/contentMetaData.js +1190 -1131
  18. package/src/contentTypeConfig.js +492 -387
  19. package/src/filterBuilder.js +163 -145
  20. package/src/index.d.ts +227 -237
  21. package/src/index.js +226 -236
  22. package/src/services/config.js +12 -12
  23. package/src/services/contentLikes.js +33 -32
  24. package/src/services/contentProgress.js +233 -200
  25. package/src/services/dataContext.js +99 -93
  26. package/src/services/lastUpdated.js +7 -7
  27. package/src/services/railcontent.js +368 -364
  28. package/src/services/sanity.js +983 -955
  29. package/src/services/userPermissions.js +12 -14
  30. package/test/contentLikes.test.js +89 -86
  31. package/test/contentProgress.test.js +229 -236
  32. package/test/initializeTests.js +54 -51
  33. package/test/lastUpdated.test.js +20 -18
  34. package/test/live/contentProgressLive.test.js +135 -137
  35. package/test/live/railcontentLive.test.js +12 -14
  36. package/test/localStorageMock.js +16 -16
  37. package/test/log.js +5 -5
  38. package/test/sanityQueryService.test.js +857 -821
  39. package/test/userPermissions.test.js +15 -15
  40. package/tools/generate-index.cjs +108 -111
@@ -1,110 +1,116 @@
1
- import {globalConfig} from "./config.js";
1
+ import { globalConfig } from './config.js'
2
2
 
3
3
  /**
4
4
  * Exported functions that are excluded from index generation.
5
5
  *
6
6
  * @type {string[]}
7
7
  */
8
- const excludeFromGeneratedIndex = [];
8
+ const excludeFromGeneratedIndex = []
9
9
 
10
10
  //These constants need to match MWP UserDataVersionKeyEnum enum
11
- export const ContentLikesVersionKey = 0;
12
- export const ContentProgressVersionKey = 1;
11
+ export const ContentLikesVersionKey = 0
12
+ export const ContentProgressVersionKey = 1
13
13
 
14
- let cache = null;
14
+ let cache = null
15
15
 
16
16
  export class DataContext {
17
- context = null;
18
- dataPromise = null;
19
-
20
- constructor(dataVersionKey, fetchDataFunction) {
21
- this.dataVersionKey = dataVersionKey;
22
- this.fetchDataFunction = fetchDataFunction;
23
- this.localStorageKey = `dataContext_${this.dataVersionKey.toString()}`;
24
- this.localStorageLastUpdatedKey = `dataContext_${this.dataVersionKey.toString()}_lastUpdated`;
17
+ context = null
18
+ dataPromise = null
19
+
20
+ constructor(dataVersionKey, fetchDataFunction) {
21
+ this.dataVersionKey = dataVersionKey
22
+ this.fetchDataFunction = fetchDataFunction
23
+ this.localStorageKey = `dataContext_${this.dataVersionKey.toString()}`
24
+ this.localStorageLastUpdatedKey = `dataContext_${this.dataVersionKey.toString()}_lastUpdated`
25
+ }
26
+
27
+ async getData() {
28
+ if (!this.dataPromise) {
29
+ this.dataPromise = this.getDataPromise()
25
30
  }
26
-
27
- async getData() {
28
- if (!this.dataPromise) {
29
- this.dataPromise = this.getDataPromise();
30
- }
31
- return this.dataPromise;
31
+ return this.dataPromise
32
+ }
33
+
34
+ async getDataPromise() {
35
+ await this.ensureLocalContextLoaded()
36
+ const shouldVerify = await this.shouldVerifyServerVerions()
37
+
38
+ if (!this.context || shouldVerify) {
39
+ let version = this.version()
40
+ let data = await this.fetchData(version)
41
+ if (data?.version !== 'No Change') {
42
+ this.context = data
43
+ cache.setItem(this.localStorageKey, JSON.stringify(data))
44
+ }
45
+ cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime()?.toString())
32
46
  }
33
-
34
- async getDataPromise() {
35
- await this.ensureLocalContextLoaded();
36
- const shouldVerify = await this.shouldVerifyServerVerions();
37
-
38
- if ((!this.context) || shouldVerify) {
39
- let version = this.version();
40
- let data = await this.fetchData(version);
41
- if (data?.version !== "No Change") {
42
- this.context = data;
43
- cache.setItem(this.localStorageKey, JSON.stringify(data));
44
- }
45
- cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime()?.toString());
46
- }
47
- this.dataPromise = null;
48
- return this.context.data;
47
+ this.dataPromise = null
48
+ return this.context.data
49
+ }
50
+
51
+ async fetchData(version) {
52
+ return await this.fetchDataFunction(version)
53
+ }
54
+
55
+ async ensureLocalContextLoaded() {
56
+ if (this.context) return
57
+ this.verifyConfig()
58
+ let localData = globalConfig.isMA
59
+ ? await cache.getItem(this.localStorageKey)
60
+ : cache.getItem(this.localStorageKey)
61
+ if (localData) {
62
+ this.context = JSON.parse(localData)
49
63
  }
50
-
51
- async fetchData(version) {
52
- return await this.fetchDataFunction(version);
64
+ }
65
+
66
+ verifyConfig() {
67
+ if (!cache) {
68
+ cache = globalConfig.localStorage
69
+ if (!cache)
70
+ throw new Error(
71
+ 'dataContext: LocalStorage cache not configured in musora content services initializeService.'
72
+ )
53
73
  }
54
-
55
- async ensureLocalContextLoaded() {
56
- if (this.context) return;
57
- this.verifyConfig();
58
- let localData = globalConfig.isMA ? await cache.getItem(this.localStorageKey) : cache.getItem(this.localStorageKey);
59
- if (localData) {
60
- this.context = JSON.parse(localData);
61
- }
74
+ }
75
+
76
+ async shouldVerifyServerVerions() {
77
+ let lastUpdated = globalConfig.isMA
78
+ ? await cache.getItem(this.localStorageLastUpdatedKey)
79
+ : cache.getItem(this.localStorageLastUpdatedKey)
80
+ if (!lastUpdated) return false
81
+ const verifyServerTime = 10000 //10 s
82
+ return new Date().getTime() - lastUpdated > verifyServerTime
83
+ }
84
+
85
+ clearCache() {
86
+ this.clearContext()
87
+ cache.removeItem(this.localStorageKey)
88
+ cache.removeItem(this.localStorageLastUpdatedKey)
89
+ }
90
+
91
+ clearContext() {
92
+ this.context = null
93
+ }
94
+
95
+ async update(localUpdateFunction, serverUpdateFunction) {
96
+ await this.ensureLocalContextLoaded()
97
+ if (this.context) {
98
+ await localUpdateFunction(this.context)
99
+ if (this.context) this.context.version++
100
+ let data = JSON.stringify(this.context)
101
+ cache.setItem(this.localStorageKey, data)
102
+ cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString())
62
103
  }
63
-
64
- verifyConfig() {
65
- if (!cache) {
66
- cache = globalConfig.localStorage;
67
- if (!cache) throw new Error('dataContext: LocalStorage cache not configured in musora content services initializeService.');
68
- }
69
- }
70
-
71
- async shouldVerifyServerVerions() {
72
- let lastUpdated = globalConfig.isMA ? await cache.getItem(this.localStorageLastUpdatedKey) : cache.getItem(this.localStorageLastUpdatedKey);
73
- if (!lastUpdated) return false;
74
- const verifyServerTime = 10000; //10 s
75
- return (new Date().getTime() - lastUpdated) > verifyServerTime;
76
- }
77
-
78
- clearCache() {
79
- this.clearContext();
80
- cache.removeItem(this.localStorageKey);
81
- cache.removeItem(this.localStorageLastUpdatedKey);
82
- }
83
-
84
- clearContext() {
85
- this.context = null;
86
- }
87
-
88
- async update(localUpdateFunction, serverUpdateFunction) {
89
- await this.ensureLocalContextLoaded();
90
- if (this.context) {
91
- await localUpdateFunction(this.context);
92
- if (this.context) this.context.version++;
93
- let data = JSON.stringify(this.context);
94
- cache.setItem(this.localStorageKey, data);
95
- cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString());
96
- }
97
- const updatePromise = serverUpdateFunction();
98
- updatePromise.then((response) => {
99
- if (response?.version !== this.version()) {
100
- this.clearCache();
101
- }
102
- });
103
- return updatePromise;
104
- }
105
-
106
- version() {
107
- return this.context?.version ?? -1;
108
- }
109
-
104
+ const updatePromise = serverUpdateFunction()
105
+ updatePromise.then((response) => {
106
+ if (response?.version !== this.version()) {
107
+ this.clearCache()
108
+ }
109
+ })
110
+ return updatePromise
111
+ }
112
+
113
+ version() {
114
+ return this.context?.version ?? -1
115
+ }
110
116
  }
@@ -1,18 +1,18 @@
1
- import {globalConfig} from "./config.js";
1
+ import { globalConfig } from './config.js'
2
2
 
3
3
  /**
4
4
  * Exported functions that are excluded from index generation.
5
5
  *
6
6
  * @type {string[]}
7
7
  */
8
- const excludeFromGeneratedIndex = ['wasLastUpdateOlderThanXSeconds', 'setLastUpdatedTime'];
8
+ const excludeFromGeneratedIndex = ['wasLastUpdateOlderThanXSeconds', 'setLastUpdatedTime']
9
9
  export function wasLastUpdateOlderThanXSeconds(seconds, key) {
10
- let lastUpdated = globalConfig.localStorage.getItem(key);
11
- if (!lastUpdated) return false;
12
- const verifyServerTime = seconds * 1000;
13
- return (new Date().getTime() - lastUpdated) > verifyServerTime;
10
+ let lastUpdated = globalConfig.localStorage.getItem(key)
11
+ if (!lastUpdated) return false
12
+ const verifyServerTime = seconds * 1000
13
+ return new Date().getTime() - lastUpdated > verifyServerTime
14
14
  }
15
15
 
16
16
  export function setLastUpdatedTime(key) {
17
- globalConfig.localStorage.setItem(key, new Date().getTime()?.toString());
17
+ globalConfig.localStorage.setItem(key, new Date().getTime()?.toString())
18
18
  }