fansunited-data-layer 0.13.0 → 0.14.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 (242) hide show
  1. package/api/fansunited/constants.js +5 -0
  2. package/api/fansunited/constants.js.map +1 -0
  3. package/api/fansunited/football/competition/index.js +21 -0
  4. package/api/fansunited/football/competition/index.js.map +1 -0
  5. package/api/fansunited/football/competition/transformer.js +41 -0
  6. package/api/fansunited/football/competition/transformer.js.map +1 -0
  7. package/api/fansunited/football/competitions/index.js +56 -0
  8. package/api/fansunited/football/competitions/index.js.map +1 -0
  9. package/api/fansunited/football/competitions/transformer.js +68 -0
  10. package/api/fansunited/football/competitions/transformer.js.map +1 -0
  11. package/api/fansunited/football/http.js +7 -0
  12. package/api/fansunited/football/http.js.map +1 -0
  13. package/api/fansunited/football/matches/index.js +132 -0
  14. package/api/fansunited/football/matches/index.js.map +1 -0
  15. package/api/fansunited/football/matches/transformer.js +148 -0
  16. package/api/fansunited/football/matches/transformer.js.map +1 -0
  17. package/api/fansunited/football/players/index.js +60 -0
  18. package/api/fansunited/football/players/index.js.map +1 -0
  19. package/api/fansunited/football/players/transformer.js +85 -0
  20. package/api/fansunited/football/players/transformer.js.map +1 -0
  21. package/api/fansunited/football/search/index.js +64 -0
  22. package/api/fansunited/football/search/index.js.map +1 -0
  23. package/api/fansunited/football/search/transformer.js +114 -0
  24. package/api/fansunited/football/search/transformer.js.map +1 -0
  25. package/api/fansunited/football/teams/index.js +66 -0
  26. package/api/fansunited/football/teams/index.js.map +1 -0
  27. package/api/fansunited/football/teams/transformer.js +53 -0
  28. package/api/fansunited/football/teams/transformer.js.map +1 -0
  29. package/api/fansunited/http.d.ts.map +1 -1
  30. package/api/fansunited/http.js +70 -0
  31. package/api/fansunited/http.js.map +1 -0
  32. package/api/fansunited/search/constants.js +5 -0
  33. package/api/fansunited/search/constants.js.map +1 -0
  34. package/api/fansunited/search/http.js +7 -0
  35. package/api/fansunited/search/http.js.map +1 -0
  36. package/api/fansunited/search/index.js +308 -0
  37. package/api/fansunited/search/index.js.map +1 -0
  38. package/api/fansunited/search/transformer.js +374 -0
  39. package/api/fansunited/search/transformer.js.map +1 -0
  40. package/api/fansunited-sdk/loyalty/matches.js +21 -0
  41. package/api/fansunited-sdk/loyalty/matches.js.map +1 -0
  42. package/api/fansunited-sdk/loyalty/template.js +52 -0
  43. package/api/fansunited-sdk/loyalty/template.js.map +1 -0
  44. package/api/fansunited-sdk/odds/matches.js +109 -0
  45. package/api/fansunited-sdk/odds/matches.js.map +1 -0
  46. package/api/sportal365-sports/basketball/games/index.js +32 -0
  47. package/api/sportal365-sports/basketball/games/index.js.map +1 -0
  48. package/api/sportal365-sports/basketball/games/transformers/game.js +137 -0
  49. package/api/sportal365-sports/basketball/games/transformers/game.js.map +1 -0
  50. package/api/sportal365-sports/basketball/http.js +7 -0
  51. package/api/sportal365-sports/basketball/http.js.map +1 -0
  52. package/api/sportal365-sports/constants.js +15 -0
  53. package/api/sportal365-sports/constants.js.map +1 -0
  54. package/api/sportal365-sports/football/competitions/index.js +52 -0
  55. package/api/sportal365-sports/football/competitions/index.js.map +1 -0
  56. package/api/sportal365-sports/football/competitions/utils.js +92 -0
  57. package/api/sportal365-sports/football/competitions/utils.js.map +1 -0
  58. package/api/sportal365-sports/football/http.js +7 -0
  59. package/api/sportal365-sports/football/http.js.map +1 -0
  60. package/api/sportal365-sports/football/matches/index.js +206 -0
  61. package/api/sportal365-sports/football/matches/index.js.map +1 -0
  62. package/api/sportal365-sports/football/matches/transformers/commentary.js +27 -0
  63. package/api/sportal365-sports/football/matches/transformers/commentary.js.map +1 -0
  64. package/api/sportal365-sports/football/matches/transformers/lineup.js +74 -0
  65. package/api/sportal365-sports/football/matches/transformers/lineup.js.map +1 -0
  66. package/api/sportal365-sports/football/matches/transformers/match-event.js +60 -0
  67. package/api/sportal365-sports/football/matches/transformers/match-event.js.map +1 -0
  68. package/api/sportal365-sports/football/matches/transformers/match.js +195 -0
  69. package/api/sportal365-sports/football/matches/transformers/match.js.map +1 -0
  70. package/api/sportal365-sports/football/matches/transformers/odds.js +11 -0
  71. package/api/sportal365-sports/football/matches/transformers/odds.js.map +1 -0
  72. package/api/sportal365-sports/football/matches/transformers/statistics.js +41 -0
  73. package/api/sportal365-sports/football/matches/transformers/statistics.js.map +1 -0
  74. package/api/sportal365-sports/football/search/index.js +42 -0
  75. package/api/sportal365-sports/football/search/index.js.map +1 -0
  76. package/api/sportal365-sports/football/search/search.transformer.js +166 -0
  77. package/api/sportal365-sports/football/search/search.transformer.js.map +1 -0
  78. package/api/sportal365-sports/football/standings/index.js +28 -0
  79. package/api/sportal365-sports/football/standings/index.js.map +1 -0
  80. package/api/sportal365-sports/football/standings/standing.transformer.js +107 -0
  81. package/api/sportal365-sports/football/standings/standing.transformer.js.map +1 -0
  82. package/api/sportal365-sports/football/statistics/index.js +154 -0
  83. package/api/sportal365-sports/football/statistics/index.js.map +1 -0
  84. package/api/sportal365-sports/football/statistics/player-career.transformer.js +77 -0
  85. package/api/sportal365-sports/football/statistics/player-career.transformer.js.map +1 -0
  86. package/api/sportal365-sports/football/statistics/player-recent.transformer.js +54 -0
  87. package/api/sportal365-sports/football/statistics/player-recent.transformer.js.map +1 -0
  88. package/api/sportal365-sports/football/statistics/player-season.transformer.js +147 -0
  89. package/api/sportal365-sports/football/statistics/player-season.transformer.js.map +1 -0
  90. package/api/sportal365-sports/football/teams/index.js +73 -0
  91. package/api/sportal365-sports/football/teams/index.js.map +1 -0
  92. package/api/sportal365-sports/football/teams/utils.js +196 -0
  93. package/api/sportal365-sports/football/teams/utils.js.map +1 -0
  94. package/api/sportal365-sports/http.js +58 -0
  95. package/api/sportal365-sports/http.js.map +1 -0
  96. package/api/sportal365-sports/search/http.js +7 -0
  97. package/api/sportal365-sports/search/http.js.map +1 -0
  98. package/api/sportal365-sports/search/index.js +61 -0
  99. package/api/sportal365-sports/search/index.js.map +1 -0
  100. package/api/sportal365-sports/search/search.transformer.js +254 -0
  101. package/api/sportal365-sports/search/search.transformer.js.map +1 -0
  102. package/api/sportal365-sports/shared/odds.transformer.js +65 -0
  103. package/api/sportal365-sports/shared/odds.transformer.js.map +1 -0
  104. package/api/sportal365-sports/shared/providerRef.helper.js +31 -0
  105. package/api/sportal365-sports/shared/providerRef.helper.js.map +1 -0
  106. package/api/sportal365-sports/standings/http.js +7 -0
  107. package/api/sportal365-sports/standings/http.js.map +1 -0
  108. package/api/sportal365-sports/standings/index.js +29 -0
  109. package/api/sportal365-sports/standings/index.js.map +1 -0
  110. package/api/sportal365-sports/standings/standing.transformer.js +185 -0
  111. package/api/sportal365-sports/standings/standing.transformer.js.map +1 -0
  112. package/api/sportal365-sports/statistics/http.js +7 -0
  113. package/api/sportal365-sports/statistics/http.js.map +1 -0
  114. package/api/sportal365-sports/statistics/index.js +25 -0
  115. package/api/sportal365-sports/statistics/index.js.map +1 -0
  116. package/api/sportal365-sports/statistics/team-stats.transformer.js +16 -0
  117. package/api/sportal365-sports/statistics/team-stats.transformer.js.map +1 -0
  118. package/api/sportal365-sports/tennis/http.js +7 -0
  119. package/api/sportal365-sports/tennis/http.js.map +1 -0
  120. package/api/sportal365-sports/tennis/matches/index.js +34 -0
  121. package/api/sportal365-sports/tennis/matches/index.js.map +1 -0
  122. package/api/sportal365-sports/tennis/matches/transformers/match.js +193 -0
  123. package/api/sportal365-sports/tennis/matches/transformers/match.js.map +1 -0
  124. package/cache/cache-manager.js +136 -0
  125. package/cache/cache-manager.js.map +1 -0
  126. package/cache/memory-store.js +29 -0
  127. package/cache/memory-store.js.map +1 -0
  128. package/client.js +38 -12337
  129. package/client.js.map +1 -0
  130. package/config/index.js +20 -0
  131. package/config/index.js.map +1 -0
  132. package/fansunited-data-layer.js +146 -3553
  133. package/fansunited-data-layer.js.map +1 -0
  134. package/helpers/competition.helpers.js +11 -0
  135. package/helpers/competition.helpers.js.map +1 -0
  136. package/helpers/player.helpers.js +165 -0
  137. package/helpers/player.helpers.js.map +1 -0
  138. package/helpers/player.hooks.js +34 -0
  139. package/helpers/player.hooks.js.map +1 -0
  140. package/helpers/team.helpers.js +216 -0
  141. package/helpers/team.helpers.js.map +1 -0
  142. package/package.json +6 -8
  143. package/providers/competition/hooks/useCompetitionStats.js +10 -0
  144. package/providers/competition/hooks/useCompetitionStats.js.map +1 -0
  145. package/providers/competition/hooks/useGoalDistribution.js +23 -0
  146. package/providers/competition/hooks/useGoalDistribution.js.map +1 -0
  147. package/providers/competition/hooks/useMatchHelpers.js +85 -0
  148. package/providers/competition/hooks/useMatchHelpers.js.map +1 -0
  149. package/providers/competition/hooks/useStandingsCalculations.js +89 -0
  150. package/providers/competition/hooks/useStandingsCalculations.js.map +1 -0
  151. package/providers/competition/hooks/useStandingsHelpers.js +61 -0
  152. package/providers/competition/hooks/useStandingsHelpers.js.map +1 -0
  153. package/providers/competition/hooks/useTeamPosition.js +15 -0
  154. package/providers/competition/hooks/useTeamPosition.js.map +1 -0
  155. package/providers/competition/hooks/useTeamResultsTable.js +86 -0
  156. package/providers/competition/hooks/useTeamResultsTable.js.map +1 -0
  157. package/providers/competition/utils/competitionStats.js +109 -0
  158. package/providers/competition/utils/competitionStats.js.map +1 -0
  159. package/providers/competition/utils/goalDistribution.js +64 -0
  160. package/providers/competition/utils/goalDistribution.js.map +1 -0
  161. package/providers/competition/utils/standingsCalculations.js +152 -0
  162. package/providers/competition/utils/standingsCalculations.js.map +1 -0
  163. package/providers/competition.context.js +14 -0
  164. package/providers/competition.context.js.map +1 -0
  165. package/providers/competition.provider.js +187 -0
  166. package/providers/competition.provider.js.map +1 -0
  167. package/providers/fansunited-config.context.js +6 -0
  168. package/providers/fansunited-config.context.js.map +1 -0
  169. package/providers/fansunited-config.hooks.js +20 -0
  170. package/providers/fansunited-config.hooks.js.map +1 -0
  171. package/providers/fansunited-config.provider.js +9 -0
  172. package/providers/fansunited-config.provider.js.map +1 -0
  173. package/providers/fansunited-sdk.hook.js +50 -0
  174. package/providers/fansunited-sdk.hook.js.map +1 -0
  175. package/providers/leaderboard/hooks/useMatchHelpers.js +30 -0
  176. package/providers/leaderboard/hooks/useMatchHelpers.js.map +1 -0
  177. package/providers/leaderboard/hooks/useTemplateHelpers.js +39 -0
  178. package/providers/leaderboard/hooks/useTemplateHelpers.js.map +1 -0
  179. package/providers/leaderboard.context.js +14 -0
  180. package/providers/leaderboard.context.js.map +1 -0
  181. package/providers/leaderboard.provider.js +98 -0
  182. package/providers/leaderboard.provider.js.map +1 -0
  183. package/providers/match/hooks/useBatchMatchOdds.js +72 -0
  184. package/providers/match/hooks/useBatchMatchOdds.js.map +1 -0
  185. package/providers/match/hooks/useEventHelpers.js +46 -0
  186. package/providers/match/hooks/useEventHelpers.js.map +1 -0
  187. package/providers/match/hooks/useHeadToHeadHelpers.js +52 -0
  188. package/providers/match/hooks/useHeadToHeadHelpers.js.map +1 -0
  189. package/providers/match/hooks/useLineupHelpers.js +53 -0
  190. package/providers/match/hooks/useLineupHelpers.js.map +1 -0
  191. package/providers/match/hooks/useMatchStandingsCalculations.js +332 -0
  192. package/providers/match/hooks/useMatchStandingsCalculations.js.map +1 -0
  193. package/providers/match/hooks/useMatchStatus.js +37 -0
  194. package/providers/match/hooks/useMatchStatus.js.map +1 -0
  195. package/providers/match/hooks/useOddsHelpers.js +54 -0
  196. package/providers/match/hooks/useOddsHelpers.js.map +1 -0
  197. package/providers/match/hooks/useScoreHelpers.js +34 -0
  198. package/providers/match/hooks/useScoreHelpers.js.map +1 -0
  199. package/providers/match/hooks/useStandingsHelpers.js +53 -0
  200. package/providers/match/hooks/useStandingsHelpers.js.map +1 -0
  201. package/providers/match/hooks/useStatisticsHelpers.js +40 -0
  202. package/providers/match/hooks/useStatisticsHelpers.js.map +1 -0
  203. package/providers/match/hooks/useTeamHelpers.js +38 -0
  204. package/providers/match/hooks/useTeamHelpers.js.map +1 -0
  205. package/providers/match/hooks/useTeamStatsHelpers.js +141 -0
  206. package/providers/match/hooks/useTeamStatsHelpers.js.map +1 -0
  207. package/providers/match.context.js +14 -0
  208. package/providers/match.context.js.map +1 -0
  209. package/providers/match.provider.js +176 -0
  210. package/providers/match.provider.js.map +1 -0
  211. package/providers/team/hooks/useFormStats.js +116 -0
  212. package/providers/team/hooks/useFormStats.js.map +1 -0
  213. package/providers/team/hooks/useMatchHelpers.js +46 -0
  214. package/providers/team/hooks/useMatchHelpers.js.map +1 -0
  215. package/providers/team/hooks/useSquadHelpers.js +67 -0
  216. package/providers/team/hooks/useSquadHelpers.js.map +1 -0
  217. package/providers/team.context.js +14 -0
  218. package/providers/team.context.js.map +1 -0
  219. package/providers/team.provider.js +98 -0
  220. package/providers/team.provider.js.map +1 -0
  221. package/utilities/stats/core/helpers.js +105 -0
  222. package/utilities/stats/core/helpers.js.map +1 -0
  223. package/utilities/stats/core/types.js +5 -0
  224. package/utilities/stats/core/types.js.map +1 -0
  225. package/utilities/stats/match/headToHead.js +83 -0
  226. package/utilities/stats/match/headToHead.js.map +1 -0
  227. package/utilities/stats/match/homeVsAway.js +140 -0
  228. package/utilities/stats/match/homeVsAway.js.map +1 -0
  229. package/utilities/stats/match/overUnder.js +91 -0
  230. package/utilities/stats/match/overUnder.js.map +1 -0
  231. package/utilities/stats/match/result.js +21 -0
  232. package/utilities/stats/match/result.js.map +1 -0
  233. package/utilities/stats/team/commonOpponents.js +77 -0
  234. package/utilities/stats/team/commonOpponents.js.map +1 -0
  235. package/utilities/stats/team/goalStats.js +50 -0
  236. package/utilities/stats/team/goalStats.js.map +1 -0
  237. package/utilities/stats/team/streaks.js +183 -0
  238. package/utilities/stats/team/streaks.js.map +1 -0
  239. package/client.cjs +0 -8
  240. package/fansunited-data-layer.cjs +0 -1
  241. package/matches-BvHU-bP0.js +0 -1795
  242. package/matches-Ci0Ci_Mw.cjs +0 -1
@@ -0,0 +1,136 @@
1
+ import { memoryStore } from "./memory-store.js";
2
+ let l2 = null;
3
+ function initL2(store) {
4
+ l2 = store;
5
+ }
6
+ const ENTITY_TTL_CONFIG = {
7
+ competitions: { staleTTL: 24 * 3600, maxTTL: 72 * 3600 },
8
+ teams: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
9
+ athletes: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
10
+ venues: { staleTTL: 72 * 3600, maxTTL: 168 * 3600 },
11
+ countries: { staleTTL: 96 * 3600, maxTTL: 168 * 3600 },
12
+ coaches: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
13
+ search: { staleTTL: 300, maxTTL: 1800 }
14
+ };
15
+ const refreshing = /* @__PURE__ */ new Set();
16
+ function getConfig(entity) {
17
+ const config = ENTITY_TTL_CONFIG[entity];
18
+ if (!config) {
19
+ throw new Error(`Unknown entity type: ${entity}`);
20
+ }
21
+ return config;
22
+ }
23
+ function isFresh(entry, staleTTL) {
24
+ return Date.now() - entry.storedAt < staleTTL * 1e3;
25
+ }
26
+ function isExpired(entry, maxTTL) {
27
+ return Date.now() - entry.storedAt >= maxTTL * 1e3;
28
+ }
29
+ function backgroundRefresh(key, entity, fetcher) {
30
+ if (refreshing.has(key)) return;
31
+ refreshing.add(key);
32
+ fetcher().then((data) => {
33
+ memoryStore.set(key, data);
34
+ if (l2?.isInitialized) {
35
+ l2.set(key, entity, data);
36
+ }
37
+ }).catch((err) => {
38
+ console.error(`[cache] Background refresh failed for ${key}:`, err);
39
+ }).finally(() => {
40
+ refreshing.delete(key);
41
+ });
42
+ }
43
+ async function cached(key, entity, fetcher) {
44
+ const { staleTTL, maxTTL } = getConfig(entity);
45
+ const l1 = memoryStore.get(key);
46
+ if (l1) {
47
+ if (isFresh(l1, staleTTL)) {
48
+ return l1.data;
49
+ }
50
+ if (!isExpired(l1, maxTTL)) {
51
+ backgroundRefresh(key, entity, fetcher);
52
+ return l1.data;
53
+ }
54
+ }
55
+ if (l2?.isInitialized) {
56
+ const l2Entry = l2.get(key);
57
+ if (l2Entry && !isExpired(l2Entry, maxTTL)) {
58
+ memoryStore.set(key, l2Entry.data);
59
+ if (isFresh(l2Entry, staleTTL)) {
60
+ return l2Entry.data;
61
+ }
62
+ backgroundRefresh(key, entity, fetcher);
63
+ return l2Entry.data;
64
+ }
65
+ }
66
+ const data = await fetcher();
67
+ memoryStore.set(key, data);
68
+ if (l2?.isInitialized) {
69
+ l2.set(key, entity, data);
70
+ }
71
+ return data;
72
+ }
73
+ async function cachedBatch(ids, entity, batchFetcher, keyFn) {
74
+ const { staleTTL, maxTTL } = getConfig(entity);
75
+ const results = /* @__PURE__ */ new Map();
76
+ const missingIds = [];
77
+ for (const id of ids) {
78
+ const key = keyFn(id);
79
+ let found = false;
80
+ const l1 = memoryStore.get(key);
81
+ if (l1 && !isExpired(l1, maxTTL)) {
82
+ results.set(id, l1.data);
83
+ if (!isFresh(l1, staleTTL)) {
84
+ backgroundRefresh(key, entity, async () => {
85
+ const fetched = await batchFetcher([id]);
86
+ const item = fetched.get(id);
87
+ if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);
88
+ return item;
89
+ });
90
+ }
91
+ found = true;
92
+ }
93
+ if (!found && l2?.isInitialized) {
94
+ const l2Entry = l2.get(key);
95
+ if (l2Entry && !isExpired(l2Entry, maxTTL)) {
96
+ memoryStore.set(key, l2Entry.data);
97
+ results.set(id, l2Entry.data);
98
+ if (!isFresh(l2Entry, staleTTL)) {
99
+ backgroundRefresh(key, entity, async () => {
100
+ const fetched = await batchFetcher([id]);
101
+ const item = fetched.get(id);
102
+ if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);
103
+ return item;
104
+ });
105
+ }
106
+ found = true;
107
+ }
108
+ }
109
+ if (!found) {
110
+ missingIds.push(id);
111
+ }
112
+ }
113
+ if (missingIds.length > 0) {
114
+ const fetched = await batchFetcher(missingIds);
115
+ for (const [id, data] of fetched) {
116
+ const key = keyFn(id);
117
+ memoryStore.set(key, data);
118
+ if (l2?.isInitialized) {
119
+ l2.set(key, entity, data);
120
+ }
121
+ results.set(id, data);
122
+ }
123
+ }
124
+ return results;
125
+ }
126
+ function invalidate(key) {
127
+ memoryStore.delete(key);
128
+ }
129
+ export {
130
+ ENTITY_TTL_CONFIG,
131
+ cached,
132
+ cachedBatch,
133
+ initL2,
134
+ invalidate
135
+ };
136
+ //# sourceMappingURL=cache-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-manager.js","sources":["../../src/lib/cache/cache-manager.ts"],"sourcesContent":["/**\n * Cache Manager - Orchestrates L1 (memory) + optional L2 with SWR pattern\n *\n * L2 (SQLite) is never statically imported here — it's injected at runtime\n * via initL2() so that browser bundles don't pull in better-sqlite3.\n */\n\nimport { memoryStore } from \"./memory-store\";\nimport type { CacheEntry, EntityCacheConfig, EntityType } from \"./types\";\n\n/** Interface that L2 stores must satisfy */\nexport interface L2Store {\n get isInitialized(): boolean;\n get<T>(key: string): CacheEntry<T> | undefined;\n set(key: string, entity: string, data: unknown): void;\n setMany(entries: { key: string; entity: string; data: unknown }[]): void;\n cleanup(entity: string, maxTTLSeconds: number): number;\n clear(): void;\n}\n\n/** The active L2 store — null until initL2() is called */\nlet l2: L2Store | null = null;\n\n/**\n * Initialize L2 cache layer. Call on server startup.\n * Client-side apps skip this — L2 is never loaded.\n */\nexport function initL2(store: L2Store): void {\n l2 = store;\n}\n\n/** Get the L2 store (if initialized) */\nexport function getL2(): L2Store | null {\n return l2;\n}\n\nconst ENTITY_TTL_CONFIG: Record<EntityType, EntityCacheConfig> = {\n competitions: { staleTTL: 24 * 3600, maxTTL: 72 * 3600 },\n teams: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n athletes: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n venues: { staleTTL: 72 * 3600, maxTTL: 168 * 3600 },\n countries: { staleTTL: 96 * 3600, maxTTL: 168 * 3600 },\n coaches: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n search: { staleTTL: 300, maxTTL: 1800 },\n};\n\n/** Set of keys currently being refreshed in the background (thundering herd prevention) */\nconst refreshing = new Set<string>();\n\nfunction getConfig(entity: EntityType): EntityCacheConfig {\n const config = ENTITY_TTL_CONFIG[entity];\n if (!config) {\n throw new Error(`Unknown entity type: ${entity}`);\n }\n return config;\n}\n\nfunction isFresh(entry: CacheEntry<unknown>, staleTTL: number): boolean {\n return Date.now() - entry.storedAt < staleTTL * 1000;\n}\n\nfunction isExpired(entry: CacheEntry<unknown>, maxTTL: number): boolean {\n return Date.now() - entry.storedAt >= maxTTL * 1000;\n}\n\nfunction backgroundRefresh<T>(key: string, entity: EntityType, fetcher: () => Promise<T>): void {\n if (refreshing.has(key)) return;\n refreshing.add(key);\n\n fetcher()\n .then((data) => {\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n })\n .catch((err) => {\n console.error(`[cache] Background refresh failed for ${key}:`, err);\n })\n .finally(() => {\n refreshing.delete(key);\n });\n}\n\n/**\n * Single-key cache-through with SWR (Stale-While-Revalidate)\n */\nexport async function cached<T>(key: string, entity: EntityType, fetcher: () => Promise<T>): Promise<T> {\n const { staleTTL, maxTTL } = getConfig(entity);\n\n // L1 check\n const l1 = memoryStore.get<T>(key);\n if (l1) {\n if (isFresh(l1, staleTTL)) {\n return l1.data;\n }\n if (!isExpired(l1, maxTTL)) {\n backgroundRefresh(key, entity, fetcher);\n return l1.data;\n }\n }\n\n // L2 check\n if (l2?.isInitialized) {\n const l2Entry = l2.get<T>(key);\n if (l2Entry && !isExpired(l2Entry, maxTTL)) {\n // Promote to L1\n memoryStore.set(key, l2Entry.data);\n if (isFresh(l2Entry, staleTTL)) {\n return l2Entry.data;\n }\n backgroundRefresh(key, entity, fetcher);\n return l2Entry.data;\n }\n }\n\n // Full miss — await fetcher\n const data = await fetcher();\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n return data;\n}\n\n/**\n * Smart batch cache — only fetches uncached IDs from the API\n */\nexport async function cachedBatch<T>(\n ids: string[],\n entity: EntityType,\n batchFetcher: (missingIds: string[]) => Promise<Map<string, T>>,\n keyFn: (id: string) => string\n): Promise<Map<string, T>> {\n const { staleTTL, maxTTL } = getConfig(entity);\n const results = new Map<string, T>();\n const missingIds: string[] = [];\n\n for (const id of ids) {\n const key = keyFn(id);\n let found = false;\n\n // L1 check\n const l1 = memoryStore.get<T>(key);\n if (l1 && !isExpired(l1, maxTTL)) {\n results.set(id, l1.data);\n if (!isFresh(l1, staleTTL)) {\n backgroundRefresh(key, entity, async () => {\n const fetched = await batchFetcher([id]);\n const item = fetched.get(id);\n if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);\n return item;\n });\n }\n found = true;\n }\n\n // L2 check\n if (!found && l2?.isInitialized) {\n const l2Entry = l2.get<T>(key);\n if (l2Entry && !isExpired(l2Entry, maxTTL)) {\n memoryStore.set(key, l2Entry.data);\n results.set(id, l2Entry.data);\n if (!isFresh(l2Entry, staleTTL)) {\n backgroundRefresh(key, entity, async () => {\n const fetched = await batchFetcher([id]);\n const item = fetched.get(id);\n if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);\n return item;\n });\n }\n found = true;\n }\n }\n\n if (!found) {\n missingIds.push(id);\n }\n }\n\n // Fetch missing IDs in a single batch\n if (missingIds.length > 0) {\n const fetched = await batchFetcher(missingIds);\n for (const [id, data] of fetched) {\n const key = keyFn(id);\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n results.set(id, data);\n }\n }\n\n return results;\n}\n\n/**\n * Remove an entry from both cache layers\n */\nexport function invalidate(key: string): void {\n memoryStore.delete(key);\n}\n\n// Export for testing\nexport { ENTITY_TTL_CONFIG };\n"],"names":[],"mappings":";AAqBA,IAAI,KAAqB;AAMlB,SAAS,OAAO,OAAsB;AACzC,OAAK;AACT;AAOA,MAAM,oBAA2D;AAAA,EAC7D,cAAc,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAClD,OAAO,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAC3C,UAAU,EAAE,UAAU,KAAK,MAAM,QAAQ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B,YAAM,UAAU,GAAG,IAAO,GAAG;AAC7B,UAAI,WAAW,CAAC,UAAU,SAAS,MAAM,GAAG;AACxC,oBAAY,IAAI,KAAK,QAAQ,IAAI;AACjC,gBAAQ,IAAI,IAAI,QAAQ,IAAI;AAC5B,YAAI,CAAC,QAAQ,SAAS,QAAQ,GAAG;AAC7B,4BAAkB,KAAK,QAAQ,YAAY;AACvC,kBAAM,UAAU,MAAM,aAAa,CAAC,EAAE,CAAC;AACvC,kBAAM,OAAO,QAAQ,IAAI,EAAE;AAC3B,gBAAI,CAAC,KAAM,OAAM,IAAI,MAAM,oCAAoC,EAAE,EAAE;AACnE,mBAAO;AAAA,UACX,CAAC;AAAA,QACL;AACA,gBAAQ;AAAA,MACZ;AAAA,IACJ;AAEA,QAAI,CAAC,OAAO;AACR,iBAAW,KAAK,EAAE;AAAA,IACtB;AAAA,EACJ;AAGA,MAAI,WAAW,SAAS,GAAG;AACvB,UAAM,UAAU,MAAM,aAAa,UAAU;AAC7C,eAAW,CAAC,IAAI,IAAI,KAAK,SAAS;AAC9B,YAAM,MAAM,MAAM,EAAE;AACpB,kBAAY,IAAI,KAAK,IAAI;AACzB,UAAI,IAAI,eAAe;AACnB,WAAG,IAAI,KAAK,QAAQ,IAAI;AAAA,MAC5B;AACA,cAAQ,IAAI,IAAI,IAAI;AAAA,IACxB;AAAA,EACJ;AAEA,SAAO;AACX;AAKO,SAAS,WAAW,KAAmB;AAC1C,cAAY,OAAO,GAAG;AAC1B;"}
@@ -0,0 +1,29 @@
1
+ import { LRUCache } from "lru-cache";
2
+ const MAX_SIZE_BYTES = 50 * 1024 * 1024;
3
+ const cache = new LRUCache({
4
+ maxSize: MAX_SIZE_BYTES,
5
+ sizeCalculation: (entry) => {
6
+ return JSON.stringify(entry.data).length;
7
+ }
8
+ });
9
+ const memoryStore = {
10
+ get(key) {
11
+ return cache.get(key);
12
+ },
13
+ set(key, data) {
14
+ cache.set(key, { data, storedAt: Date.now() });
15
+ },
16
+ delete(key) {
17
+ cache.delete(key);
18
+ },
19
+ clear() {
20
+ cache.clear();
21
+ },
22
+ get size() {
23
+ return cache.size;
24
+ }
25
+ };
26
+ export {
27
+ memoryStore
28
+ };
29
+ //# sourceMappingURL=memory-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory-store.js","sources":["../../src/lib/cache/memory-store.ts"],"sourcesContent":["/**\n * L1 Cache - LRU in-memory store\n */\n\nimport { LRUCache } from \"lru-cache\";\nimport type { CacheEntry } from \"./types\";\n\nconst MAX_SIZE_BYTES = 50 * 1024 * 1024; // 50MB\n\nconst cache = new LRUCache<string, CacheEntry<unknown>>({\n maxSize: MAX_SIZE_BYTES,\n sizeCalculation: (entry) => {\n return JSON.stringify(entry.data).length;\n },\n});\n\nexport const memoryStore = {\n get<T>(key: string): CacheEntry<T> | undefined {\n return cache.get(key) as CacheEntry<T> | undefined;\n },\n\n set<T>(key: string, data: T): void {\n cache.set(key, { data, storedAt: Date.now() });\n },\n\n delete(key: string): void {\n cache.delete(key);\n },\n\n clear(): void {\n cache.clear();\n },\n\n get size(): number {\n return cache.size;\n },\n};\n"],"names":[],"mappings":";AAOA,MAAM,iBAAiB,KAAK,OAAO;AAEnC,MAAM,QAAQ,IAAI,SAAsC;AAAA,EACpD,SAAS;AAAA,EACT,iBAAiB,CAAC,UAAU;AACxB,WAAO,KAAK,UAAU,MAAM,IAAI,EAAE;AAAA,EACtC;AACJ,CAAC;AAEM,MAAM,cAAc;AAAA,EACvB,IAAO,KAAwC;AAC3C,WAAO,MAAM,IAAI,GAAG;AAAA,EACxB;AAAA,EAEA,IAAO,KAAa,MAAe;AAC/B,UAAM,IAAI,KAAK,EAAE,MAAM,UAAU,KAAK,IAAA,GAAO;AAAA,EACjD;AAAA,EAEA,OAAO,KAAmB;AACtB,UAAM,OAAO,GAAG;AAAA,EACpB;AAAA,EAEA,QAAc;AACV,UAAM,MAAA;AAAA,EACV;AAAA,EAEA,IAAI,OAAe;AACf,WAAO,MAAM;AAAA,EACjB;AACJ;"}