@karpeleslab/klbfw 0.1.13 → 0.2.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/fw-wrapper.js CHANGED
@@ -1,26 +1,221 @@
1
- module.exports.getPrefix = () => (typeof FW !== "undefined") ? FW.prefix : "";
2
- module.exports.getSettings = () => (typeof FW !== "undefined") ? FW.settings : {};
3
- module.exports.getRealm = () => (typeof FW !== "undefined") ? FW.Realm : {};
4
- module.exports.getLocale = () => (typeof FW !== "undefined") ? FW.Locale : "en-US";
5
- module.exports.getPath = () => (typeof FW !== "undefined") ? FW.path : window.location.pathname;
6
- module.exports.getHostname = () => (typeof FW !== "undefined") ? FW.hostname : window.location.hostname;
7
- module.exports.getCurrency = () => (typeof FW !== "undefined") ? FW.Context.c : "USD";
8
- module.exports.getContext = () => (typeof FW !== "undefined") ? Object.assign({}, FW.Context) : {};
9
- module.exports.setContext = (k, v) => { if (typeof FW !== "undefined") FW.Context[k] = v; };
10
- module.exports.getToken = () => (typeof FW !== "undefined") ? FW.token : undefined;
11
- module.exports.getRegistry = () => (typeof FW !== "undefined") ? FW.Registry : undefined;
12
- module.exports.getUrl = () => (typeof FW !== "undefined") ? FW.URL : {path: window.location.pathname, full: window.location.href};
13
- module.exports.getSiteStatic = () => (typeof FW !== "undefined") ? FW.site_static : true;
14
- module.exports.getCallUrlPrefix = () => (typeof FW !== "undefined") ? FW.call_url_prefix : "https://hub.atonline.com";
15
- module.exports.getUuid = () => (typeof FW !== "undefined") ? FW.uuid : undefined;
16
- module.exports.getInitialState = () => (typeof FW !== "undefined") ? FW.initial : undefined;
17
- module.exports.supported = () => true;
18
- module.exports.GET = (typeof FW !== "undefined") ? FW.GET : {};
19
- module.exports.Get = (key) => {
20
- if(key===undefined)
21
- return (typeof FW !== "undefined") ? FW.GET : undefined;
22
-
23
- return (typeof FW !== "undefined") ? FW.GET[key] : undefined;
24
- }
25
- module.exports.flushGet = () => { if (typeof FW !== "undefined") FW.GET = {}; };
26
- module.exports.getMode = () => (typeof FW !== "undefined") ? FW.mode : "offline";
1
+ 'use strict';
2
+ /**
3
+ * @fileoverview Framework wrapper for KLB Frontend Framework
4
+ *
5
+ * This module provides a wrapper around the global FW object,
6
+ * providing safe access to its properties with fallbacks for
7
+ * environments where FW is not available.
8
+ */
9
+
10
+ /**
11
+ * Gets a property from the global FW object with fallback
12
+ * @private
13
+ * @param {string} property - FW property to retrieve
14
+ * @param {*} fallback - Fallback value if property is not available
15
+ * @returns {*} The property value or fallback
16
+ */
17
+ const getFWProperty = (property, fallback) => {
18
+ if (typeof FW === "undefined") return fallback;
19
+
20
+ // Handle nested properties (e.g., "Context.c")
21
+ if (property.includes('.')) {
22
+ const parts = property.split('.');
23
+ let obj = FW;
24
+
25
+ for (const part of parts) {
26
+ if (obj === undefined || obj === null) return fallback;
27
+ obj = obj[part];
28
+ }
29
+
30
+ return obj !== undefined ? obj : fallback;
31
+ }
32
+
33
+ return FW[property] !== undefined ? FW[property] : fallback;
34
+ };
35
+
36
+ /**
37
+ * Gets the site prefix
38
+ * @returns {string} Site prefix
39
+ */
40
+ const getPrefix = () => getFWProperty('prefix', '');
41
+
42
+ /**
43
+ * Gets site settings
44
+ * @returns {Object} Site settings
45
+ */
46
+ const getSettings = () => getFWProperty('settings', {});
47
+
48
+ /**
49
+ * Gets realm information
50
+ * @returns {Object} Realm information
51
+ */
52
+ const getRealm = () => getFWProperty('Realm', {});
53
+
54
+ /**
55
+ * Gets the current locale
56
+ * @returns {string} Current locale
57
+ */
58
+ const getLocale = () => getFWProperty('Locale', 'en-US');
59
+
60
+ /**
61
+ * Gets the current path
62
+ * @returns {string} Current path
63
+ */
64
+ const getPath = () => {
65
+ if (typeof FW !== "undefined") return FW.path;
66
+ if (typeof window !== "undefined") return window.location.pathname;
67
+ return '/';
68
+ };
69
+
70
+ /**
71
+ * Gets the current hostname
72
+ * @returns {string} Current hostname
73
+ */
74
+ const getHostname = () => {
75
+ if (typeof FW !== "undefined") return FW.hostname;
76
+ if (typeof window !== "undefined") return window.location.hostname;
77
+ return '';
78
+ };
79
+
80
+ /**
81
+ * Gets the current currency
82
+ * @returns {string} Current currency code
83
+ */
84
+ const getCurrency = () => getFWProperty('Context.c', 'USD');
85
+
86
+ /**
87
+ * Gets a copy of the current context
88
+ * @returns {Object} Current context
89
+ */
90
+ const getContext = () => {
91
+ if (typeof FW !== "undefined" && FW.Context) {
92
+ return Object.assign({}, FW.Context);
93
+ }
94
+ return {};
95
+ };
96
+
97
+ /**
98
+ * Sets a value in the context
99
+ * @param {string} key - Context key
100
+ * @param {*} value - Value to set
101
+ */
102
+ const setContext = (key, value) => {
103
+ if (typeof FW !== "undefined" && FW.Context) {
104
+ FW.Context[key] = value;
105
+ }
106
+ };
107
+
108
+ /**
109
+ * Gets the current authentication token
110
+ * @returns {string|undefined} Authentication token
111
+ */
112
+ const getToken = () => getFWProperty('token', undefined);
113
+
114
+ /**
115
+ * Gets the registry
116
+ * @returns {Object|undefined} Registry object
117
+ */
118
+ const getRegistry = () => getFWProperty('Registry', undefined);
119
+
120
+ /**
121
+ * Gets URL information
122
+ * @returns {Object} URL information
123
+ */
124
+ const getUrl = () => {
125
+ if (typeof FW !== "undefined") return FW.URL;
126
+ if (typeof window !== "undefined") {
127
+ return {
128
+ path: window.location.pathname,
129
+ full: window.location.href
130
+ };
131
+ }
132
+ return { path: '/', full: '/' };
133
+ };
134
+
135
+ /**
136
+ * Gets site static flag
137
+ * @returns {boolean} Whether site is static
138
+ */
139
+ const getSiteStatic = () => getFWProperty('site_static', true);
140
+
141
+ /**
142
+ * Gets the API call URL prefix
143
+ * @returns {string} API call URL prefix
144
+ */
145
+ const getCallUrlPrefix = () => getFWProperty('call_url_prefix', 'https://hub.atonline.com');
146
+
147
+ /**
148
+ * Gets the site UUID
149
+ * @returns {string|undefined} Site UUID
150
+ */
151
+ const getUuid = () => getFWProperty('uuid', undefined);
152
+
153
+ /**
154
+ * Gets the initial state
155
+ * @returns {Object|undefined} Initial state
156
+ */
157
+ const getInitialState = () => getFWProperty('initial', undefined);
158
+
159
+ /**
160
+ * Checks if the framework is supported
161
+ * @returns {boolean} Whether the framework is supported
162
+ */
163
+ const supported = () => true;
164
+
165
+ /**
166
+ * Gets the current GET parameters
167
+ * @returns {Object} GET parameters
168
+ */
169
+ const getGET = () => getFWProperty('GET', {});
170
+
171
+ /**
172
+ * Gets a specific GET parameter
173
+ * @param {string} key - Parameter key
174
+ * @returns {string|undefined} Parameter value
175
+ */
176
+ const getParam = (key) => {
177
+ if (key === undefined) {
178
+ return getGET();
179
+ }
180
+
181
+ const params = getGET();
182
+ return params[key];
183
+ };
184
+
185
+ /**
186
+ * Flushes GET parameters
187
+ */
188
+ const flushGet = () => {
189
+ if (typeof FW !== "undefined") {
190
+ FW.GET = {};
191
+ }
192
+ };
193
+
194
+ /**
195
+ * Gets the current mode
196
+ * @returns {string} Current mode
197
+ */
198
+ const getMode = () => getFWProperty('mode', 'offline');
199
+
200
+ // Export functions
201
+ module.exports.getPrefix = getPrefix;
202
+ module.exports.getSettings = getSettings;
203
+ module.exports.getRealm = getRealm;
204
+ module.exports.getLocale = getLocale;
205
+ module.exports.getPath = getPath;
206
+ module.exports.getHostname = getHostname;
207
+ module.exports.getCurrency = getCurrency;
208
+ module.exports.getContext = getContext;
209
+ module.exports.setContext = setContext;
210
+ module.exports.getToken = getToken;
211
+ module.exports.getRegistry = getRegistry;
212
+ module.exports.getUrl = getUrl;
213
+ module.exports.getSiteStatic = getSiteStatic;
214
+ module.exports.getCallUrlPrefix = getCallUrlPrefix;
215
+ module.exports.getUuid = getUuid;
216
+ module.exports.getInitialState = getInitialState;
217
+ module.exports.supported = supported;
218
+ module.exports.GET = getGET();
219
+ module.exports.Get = getParam;
220
+ module.exports.flushGet = flushGet;
221
+ module.exports.getMode = getMode;
package/index.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Type definitions for @karpeleslab/klbfw
3
+ */
4
+
5
+ // Framework wrapper types
6
+ declare function GET(): Record<string, string>;
7
+ declare function Get(key?: string): string | Record<string, string> | undefined;
8
+ declare function flushGet(): void;
9
+ declare function getPrefix(): string;
10
+ declare function getSettings(): Record<string, any>;
11
+ declare function getRealm(): Record<string, any>;
12
+ declare function getContext(): Record<string, any>;
13
+ declare function setContext(key: string, value: any): void;
14
+ declare function getMode(): string;
15
+ declare function getHostname(): string;
16
+ declare function getRegistry(): Record<string, any> | undefined;
17
+ declare function getLocale(): string;
18
+ declare function getUserGroup(): string | undefined;
19
+ declare function getCurrency(): string;
20
+ declare function getToken(): string | undefined;
21
+ declare function getUrl(): { path: string; full: string };
22
+ declare function getPath(): string;
23
+ declare function getUuid(): string | undefined;
24
+ declare function getInitialState(): Record<string, any> | undefined;
25
+
26
+ // Cookie handling types
27
+ declare function getCookie(name: string): string | null;
28
+ declare function hasCookie(name: string): boolean;
29
+ declare function setCookie(name: string, value: string, expires?: Date | number, path?: string, domain?: string, secure?: boolean): void;
30
+
31
+ // REST API types
32
+ declare function rest(name: string, verb: string, params?: Record<string, any> | string, context?: Record<string, any>): Promise<any>;
33
+ declare function rest_get(name: string, params?: Record<string, any> | string): Promise<any>; // Backward compatibility
34
+ declare function restGet(name: string, params?: Record<string, any> | string): Promise<any>;
35
+
36
+ // Upload module types
37
+ interface UploadOptions {
38
+ progress?: (progress: number) => void;
39
+ endpoint?: string;
40
+ headers?: Record<string, string>;
41
+ retry?: number;
42
+ chunk_size?: number;
43
+ params?: Record<string, any>;
44
+ }
45
+
46
+ declare function upload(file: File, options?: UploadOptions): Promise<any>;
47
+
48
+ // Utility types
49
+ declare function getI18N(key: string, args?: Record<string, any>): string;
50
+ declare function trimPrefix(path: string): string;
51
+
52
+ export {
53
+ GET,
54
+ Get,
55
+ flushGet,
56
+ getPrefix,
57
+ getSettings,
58
+ getRealm,
59
+ getContext,
60
+ setContext,
61
+ getMode,
62
+ getHostname,
63
+ getRegistry,
64
+ getLocale,
65
+ getUserGroup,
66
+ getCurrency,
67
+ getToken,
68
+ getUrl,
69
+ getPath,
70
+ getUuid,
71
+ getInitialState,
72
+ getCookie,
73
+ hasCookie,
74
+ setCookie,
75
+ rest,
76
+ rest_get,
77
+ restGet,
78
+ upload,
79
+ getI18N,
80
+ trimPrefix
81
+ };
package/index.js CHANGED
@@ -1,10 +1,18 @@
1
1
  'use strict';
2
+ /**
3
+ * @fileoverview Main entry point for KLB Frontend Framework
4
+ *
5
+ * This module exports all public API functions from the framework's
6
+ * individual modules.
7
+ */
8
+
2
9
  const internalFW = require('./fw-wrapper');
3
10
  const rest = require('./rest');
4
11
  const upload = require('./upload');
5
12
  const util = require('./util');
6
13
  const cookies = require('./cookies');
7
14
 
15
+ // Framework wrapper exports
8
16
  module.exports.GET = internalFW.GET;
9
17
  module.exports.Get = internalFW.Get;
10
18
  module.exports.flushGet = internalFW.flushGet;
@@ -24,14 +32,20 @@ module.exports.getUrl = internalFW.getUrl;
24
32
  module.exports.getPath = internalFW.getPath;
25
33
  module.exports.getUuid = internalFW.getUuid;
26
34
  module.exports.getInitialState = internalFW.getInitialState;
35
+
36
+ // Cookie handling exports
27
37
  module.exports.getCookie = cookies.getCookie;
28
38
  module.exports.hasCookie = cookies.hasCookie;
29
39
  module.exports.setCookie = cookies.setCookie;
30
40
 
41
+ // REST API exports
31
42
  module.exports.rest = rest.rest;
32
- module.exports.rest_get = rest.rest_get;
43
+ module.exports.rest_get = rest.rest_get; // Backward compatibility
44
+ module.exports.restGet = rest.restGet; // New camelCase name
33
45
 
46
+ // Upload module exports
34
47
  module.exports.upload = upload.upload;
35
48
 
49
+ // Utility exports
36
50
  module.exports.getI18N = util.getI18N;
37
- module.exports.trimPrefix = util.trimPrefix;
51
+ module.exports.trimPrefix = util.trimPrefix;