lupine.web 1.1.2 → 1.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lupine.web",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "license": "MIT",
5
5
  "author": "uuware.com",
6
6
  "homepage": "https://github.com/uuware/lupine.js",
@@ -8,9 +8,10 @@ import { callPageLoadedEvent } from './page-loaded-events';
8
8
  import { initServerCookies } from './server-cookie';
9
9
  import { IToClientDelivery } from '../models';
10
10
  import { getMetaDataObject, getMetaDataTags, getPageTitle } from './bind-meta';
11
- import { initWebEnv, initWebSetting } from '../lib/web-env';
11
+ import { initWebEnv } from '../lib/web-env';
12
12
  import { _lupineJs, PageProps, PageResultType, setRenderPageProps } from './export-lupine';
13
13
  import { isFrontEnd } from '../lib/is-frontend';
14
+ import { WebConfig } from '../lib/web-config';
14
15
 
15
16
  const logger = new Logger('initialize');
16
17
 
@@ -26,7 +27,8 @@ const generatePage = async (props: PageProps, toClientDelivery: IToClientDeliver
26
27
  setRenderPageProps(props);
27
28
 
28
29
  initWebEnv(toClientDelivery.getWebEnv());
29
- initWebSetting(toClientDelivery.getWebSetting());
30
+ WebConfig.initFromData(toClientDelivery.getWebSetting());
31
+ // initWebSetting(toClientDelivery.getWebSetting());
30
32
  initServerCookies(toClientDelivery.getServerCookie());
31
33
  // callPageResetEvent();
32
34
  callPageLoadedEvent();
@@ -57,12 +59,14 @@ const generatePage = async (props: PageProps, toClientDelivery: IToClientDeliver
57
59
  };
58
60
  _lupineJs.generatePage = generatePage;
59
61
 
60
- let _pageInitialized = false;
62
+ const _initSaved = {
63
+ pageInitialized: false,
64
+ };
61
65
  // this is called in the FE when the document is loaded
62
66
  // to avoid circular reference, bindLinks can't call initializePage directly
63
67
  export const initializePage = async (newUrl?: string) => {
64
- const currentPageInitialized = _pageInitialized;
65
- _pageInitialized = true;
68
+ const currentPageInitialized = _initSaved.pageInitialized;
69
+ _initSaved.pageInitialized = true;
66
70
  logger.log('initializePage: ', newUrl);
67
71
  if (newUrl) {
68
72
  window.history.pushState({ urlPath: newUrl }, '', newUrl);
@@ -209,7 +209,7 @@ export const renderComponent = (type: any, props: any, uniqueClassName?: string,
209
209
  props._html.push(`<style id="sty-${newUniqueClassName}">${cssText}</style>`); // sty means style, and updateStyles has the same name
210
210
  }
211
211
 
212
- if (newProps.children) {
212
+ if (newProps.children || newProps.children === 0) {
213
213
  // if (newProps._lb) {
214
214
  // assignLabels(newProps._lb, newProps.children);
215
215
  // }
package/src/lib/index.ts CHANGED
@@ -3,4 +3,5 @@ export * from './debug-watch';
3
3
  export * from './is-frontend';
4
4
  export * from './logger';
5
5
  export * from './unique-id';
6
+ export * from './web-config';
6
7
  export * from './web-env';
@@ -0,0 +1,77 @@
1
+ import { getRenderPageProps } from 'lupine.web';
2
+
3
+ // for mobile app, it needs the url to fetch the config for the first time
4
+ export const bindWebConfigApi = (webConfigApi: string) => {
5
+ WebConfig.webConfigApi = webConfigApi;
6
+ };
7
+
8
+ export class WebConfig {
9
+ static webConfigApi = '';
10
+ static initialized = false;
11
+ static cfg: { [key: string]: string } = {};
12
+
13
+ // called from generatePage (SSR)
14
+ static initFromData(cfg: { [key: string]: string }) {
15
+ this.initialized = true;
16
+ this.cfg = cfg;
17
+ }
18
+
19
+ static async init(force?: boolean) {
20
+ if (this.initialized && !force) {
21
+ return;
22
+ }
23
+ this.initialized = true;
24
+
25
+ // For web, it's injected in the html by SSR, but for mobile, it's fetched from api
26
+ if (typeof document === 'object' && !force) {
27
+ const json = document.querySelector('#web-setting')?.textContent;
28
+ if (json) {
29
+ this.cfg = JSON.parse(json);
30
+ return;
31
+ }
32
+ }
33
+
34
+ if (!this.webConfigApi) {
35
+ console.error('WebConfig webConfigApi is not set');
36
+ return;
37
+ }
38
+ const url = getRenderPageProps().renderPageFunctions.baseUrl(this.webConfigApi);
39
+ const data = await getRenderPageProps().renderPageFunctions.fetchData(url);
40
+ if (data && data.json && data.json.status === 'ok') {
41
+ this.cfg = data.json.result;
42
+ } else {
43
+ console.error(data?.json?.message || 'Failed to get web config');
44
+ }
45
+ }
46
+
47
+ static async get(key: string, defaultValue: number): Promise<number>;
48
+ static async get(key: string, defaultValue: string): Promise<string>;
49
+ static async get(key: string, defaultValue: boolean): Promise<boolean>;
50
+ static async get(key: string, defaultValue: object): Promise<object>;
51
+ static async get(key: string, defaultValue?: any): Promise<any> {
52
+ await WebConfig.init();
53
+
54
+ if (typeof WebConfig.cfg[key] === 'undefined') {
55
+ return defaultValue;
56
+ }
57
+
58
+ if (typeof defaultValue === 'number') {
59
+ return Number.parseInt(WebConfig.cfg[key]!);
60
+ }
61
+ if (typeof defaultValue === 'boolean') {
62
+ return WebConfig.cfg[key]!.toLocaleLowerCase() === 'true' || WebConfig.cfg[key] === '1';
63
+ }
64
+ if (typeof defaultValue === 'object') {
65
+ if (typeof WebConfig.cfg[key] === 'object') {
66
+ return WebConfig.cfg[key];
67
+ }
68
+ try {
69
+ return JSON.parse(WebConfig.cfg[key]!);
70
+ } catch (error) {
71
+ console.error(`WebConfig JSON.parse error: `, error);
72
+ }
73
+ return defaultValue;
74
+ }
75
+ return WebConfig.cfg[key] || defaultValue;
76
+ }
77
+ }
@@ -47,52 +47,53 @@ function initWebEnv(webEnv: { [key: string]: string }) {
47
47
  _webEnvInitialized = true;
48
48
  }
49
49
 
50
- // _webSetting is for dynamic settings that can be changed without redeploying the app
51
- // _webSetting is json format so the returning can be an object
52
- const _webSetting: { [key: string]: string } = {};
53
- let _webSettingInitialized = false;
54
- function webSetting(key: string, defaultValue: number): number;
55
- function webSetting(key: string, defaultValue: string): string;
56
- function webSetting(key: string, defaultValue: boolean): boolean;
57
- function webSetting(key: string, defaultValue: object): object;
58
- function webSetting(key: string, defaultValue: any): any {
59
- // for SSR, the webSetting should be initialized. But for the FE, it should be initialized by the webSetting script tag
60
- if (!_webSettingInitialized) {
61
- const json = document.querySelector('#web-setting')?.textContent;
62
- if (json) {
63
- _webSettingInitialized = true;
64
- initWebSetting(JSON.parse(json));
65
- }
66
- }
50
+ // _webSetting is removed to WebConfig because for mobile app, it's async to load webconfig from an api
51
+ // // _webSetting is for dynamic settings that can be changed without redeploying the app
52
+ // // _webSetting is json format so the returning can be an object
53
+ // const _webSetting: { [key: string]: string } = {};
54
+ // let _webSettingInitialized = false;
55
+ // function webSetting(key: string, defaultValue: number): number;
56
+ // function webSetting(key: string, defaultValue: string): string;
57
+ // function webSetting(key: string, defaultValue: boolean): boolean;
58
+ // function webSetting(key: string, defaultValue: object): object;
59
+ // function webSetting(key: string, defaultValue: any): any {
60
+ // // for SSR, the webSetting should be initialized. But for the FE, it should be initialized by the webSetting script tag
61
+ // if (!_webSettingInitialized) {
62
+ // const json = document.querySelector('#web-setting')?.textContent;
63
+ // if (json) {
64
+ // _webSettingInitialized = true;
65
+ // initWebSetting(JSON.parse(json));
66
+ // }
67
+ // }
67
68
 
68
- !_webSettingInitialized && console.warn('webSetting has not been initialized yet!');
69
- if (typeof _webSetting[key] === 'undefined') {
70
- return defaultValue;
71
- }
69
+ // !_webSettingInitialized && console.warn('webSetting has not been initialized yet!');
70
+ // if (typeof _webSetting[key] === 'undefined') {
71
+ // return defaultValue;
72
+ // }
72
73
 
73
- if (typeof defaultValue === 'number') {
74
- return Number.parseInt(_webSetting[key]!);
75
- }
76
- if (typeof defaultValue === 'boolean') {
77
- return _webSetting[key]!.toLocaleLowerCase() === 'true' || _webSetting[key] === '1';
78
- }
79
- if (typeof defaultValue === 'object') {
80
- if (typeof _webSetting[key] === 'object') {
81
- return _webSetting[key];
82
- }
83
- try {
84
- return JSON.parse(_webSetting[key]!);
85
- } catch (error) {
86
- console.error(`webSetting JSON.parse error: `, error);
87
- }
88
- return defaultValue;
89
- }
90
- return _webSetting[key] || defaultValue;
91
- }
92
- // this is only called from the server side for SSR
93
- function initWebSetting(webSetting: { [key: string]: string }) {
94
- Object.assign(_webSetting, webSetting);
95
- _webSettingInitialized = true;
96
- }
74
+ // if (typeof defaultValue === 'number') {
75
+ // return Number.parseInt(_webSetting[key]!);
76
+ // }
77
+ // if (typeof defaultValue === 'boolean') {
78
+ // return _webSetting[key]!.toLocaleLowerCase() === 'true' || _webSetting[key] === '1';
79
+ // }
80
+ // if (typeof defaultValue === 'object') {
81
+ // if (typeof _webSetting[key] === 'object') {
82
+ // return _webSetting[key];
83
+ // }
84
+ // try {
85
+ // return JSON.parse(_webSetting[key]!);
86
+ // } catch (error) {
87
+ // console.error(`webSetting JSON.parse error: `, error);
88
+ // }
89
+ // return defaultValue;
90
+ // }
91
+ // return _webSetting[key] || defaultValue;
92
+ // }
93
+ // // this is only called from the server side for SSR
94
+ // function initWebSetting(webSetting: { [key: string]: string }) {
95
+ // Object.assign(_webSetting, webSetting);
96
+ // _webSettingInitialized = true;
97
+ // }
97
98
 
98
- export { initWebEnv, webEnv, initWebSetting, webSetting };
99
+ export { initWebEnv, webEnv };