@magalu/webzord 1.4.0 → 1.5.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # Changelog
2
2
 
3
+ # [1.5.0](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.4.2...v1.5.0) (2026-04-13)
4
+
5
+
6
+ ### Features
7
+
8
+ * adding lifecycle callback register function ([e0ffd1d](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/e0ffd1d))
9
+
10
+ ## [1.4.2](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.4.1...v1.4.2) (2026-03-26)
11
+
12
+
13
+ ### Improvements
14
+
15
+ * implementação de auto-tagueamento mapa-labs webzord ([e6434e0](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/e6434e0))
16
+ * refactor mockmode to use lazy load ([dc7737c](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/dc7737c))
17
+
18
+ ## [1.4.1](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.4.0...v1.4.1) (2025-04-08)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * fix env ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/14739fb))
24
+
25
+
26
+ ### Continuous Integration
27
+
28
+ * update dependency.yaml ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/b0e8c26))
29
+
30
+
31
+ ### Improvements
32
+
33
+ * add authid as argument in request ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/8a9751a))
34
+
3
35
  # [1.4.0](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.3.3...v1.4.0) (2024-12-17)
4
36
 
5
37
 
@@ -0,0 +1,233 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * An wrapper for MiniApps to interact with Magazine Luiza's SuperApp
12
+ */
13
+ class MagaluService {
14
+ /**
15
+ * @ignore
16
+ */
17
+ constructor() { }
18
+ static request({ serviceName, serviceArgs, callbackId, callback, showLoading, authId, }, mockMode) {
19
+ var _a, _b;
20
+ const id = callbackId || (Math.random() + new Date().getTime()).toString();
21
+ let isUsingPromise = callback === undefined;
22
+ if (mockMode) {
23
+ return this.handleMockRequest(serviceName, id, callback, isUsingPromise);
24
+ }
25
+ if (!this.didFoundSDK()) {
26
+ console.warn("didn't found any sdk");
27
+ return;
28
+ }
29
+ if (id && callback) {
30
+ this.callbacks.set(id, callback);
31
+ }
32
+ try {
33
+ const argsString = JSON.stringify(serviceArgs);
34
+ const payload = {
35
+ callbackId,
36
+ serviceName,
37
+ authId: (_b = (_a = this.hash) !== null && _a !== void 0 ? _a : authId) !== null && _b !== void 0 ? _b : '',
38
+ arguments: argsString,
39
+ showLoading,
40
+ };
41
+ const message = JSON.stringify(payload);
42
+ if (isUsingPromise) {
43
+ const promise = this.buildPromise(id);
44
+ this.callSDK(message, id);
45
+ return promise;
46
+ }
47
+ else {
48
+ this.callSDK(message, callbackId);
49
+ }
50
+ }
51
+ catch (e) {
52
+ console.error(e);
53
+ }
54
+ }
55
+ static handleMockRequest(serviceName, callbackId, callback, isUsingPromise) {
56
+ if (isProductionBuild()) {
57
+ const message = 'mockMode is disabled in production builds';
58
+ if (isUsingPromise) {
59
+ return Promise.reject({ code: 500, message });
60
+ }
61
+ console.warn(message);
62
+ return;
63
+ }
64
+ if (callbackId && callback) {
65
+ this.callbacks.set(callbackId, callback);
66
+ }
67
+ return this.returnMockedResponse(serviceName, callbackId).then((response) => {
68
+ if (isUsingPromise) {
69
+ return response;
70
+ }
71
+ this.pushResponse(response);
72
+ return;
73
+ });
74
+ }
75
+ static loadMockData() {
76
+ if (!this.mockDataPromise) {
77
+ this.mockDataPromise = import('./mocks');
78
+ }
79
+ return this.mockDataPromise;
80
+ }
81
+ static returnMockedResponse(serviceName, callbackId) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const { adresses, customerInfo, zipCode } = yield this.loadMockData();
84
+ let bodyResponde;
85
+ switch (serviceName) {
86
+ case 'customer.getCustomerInfo':
87
+ bodyResponde = customerInfo;
88
+ break;
89
+ case 'customer.getCustomerAddress':
90
+ bodyResponde = adresses;
91
+ break;
92
+ case 'customer.getCustomerLocation':
93
+ bodyResponde = zipCode;
94
+ break;
95
+ }
96
+ return {
97
+ callbackId: callbackId || '',
98
+ code: 200,
99
+ body: bodyResponde,
100
+ };
101
+ });
102
+ }
103
+ static buildPromise(callbackId) {
104
+ return new Promise((resolve, reject) => {
105
+ this.callbacks.set(callbackId || '', (result) => {
106
+ if (isSuccess(result)) {
107
+ resolve(result.body);
108
+ }
109
+ else {
110
+ reject({
111
+ code: result.code,
112
+ message: result.message,
113
+ });
114
+ }
115
+ });
116
+ });
117
+ }
118
+ static didFoundSDK() {
119
+ var _a, _b, _c;
120
+ return (((_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.magaluSdk) ||
121
+ ((_c = window === null || window === void 0 ? void 0 : window.magaluSdk) === null || _c === void 0 ? void 0 : _c.requestService));
122
+ }
123
+ static callSDK(message, callbackId) {
124
+ var _a, _b, _c;
125
+ if ((_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.magaluSdk) {
126
+ window.webkit.messageHandlers.magaluSdk.postMessage(message);
127
+ }
128
+ else if ((_c = window === null || window === void 0 ? void 0 : window.magaluSdk) === null || _c === void 0 ? void 0 : _c.requestService) {
129
+ window.magaluSdk.requestService(message);
130
+ }
131
+ else {
132
+ console.warn("didn't found any sdk");
133
+ callbackId && this.callbacks.delete(callbackId);
134
+ }
135
+ }
136
+ /**
137
+ * @param {string} callbackId - The id of the callback to be removed
138
+ */
139
+ static unregister(callbackId) {
140
+ this.callbacks.delete(callbackId);
141
+ }
142
+ /**
143
+ * Wipes all the callbacks
144
+ */
145
+ static unregisterAll() {
146
+ this.callbacks.clear();
147
+ }
148
+ /**
149
+ * @protected
150
+ * @param {ServiceResponse} response - An object that wrap all the data from a service computation
151
+ *
152
+ * This method is called from the native SDKs, don't call it yourself
153
+ */
154
+ static pushResponse(response) {
155
+ try {
156
+ const callback = this.callbacks.get(response.callbackId);
157
+ if (!callback) {
158
+ return;
159
+ }
160
+ return callback(response);
161
+ }
162
+ catch (e) {
163
+ console.error(e);
164
+ }
165
+ }
166
+ /**
167
+ * @param {ServiceCallback} callback - Called when a custom button has been touched
168
+ */
169
+ static registerNavbarActionsCallback(callback) {
170
+ if (callback) {
171
+ this.callbacks.set('appbar.actionPressed', callback);
172
+ }
173
+ }
174
+ /**
175
+ * @param {ServiceCallback} callback - Called when there is a lifecycle event on the device
176
+ */
177
+ static registerLifecycleCallback(callback) {
178
+ if (callback) {
179
+ this.callbacks.set("lifecycle.callback", callback);
180
+ }
181
+ }
182
+ }
183
+ /**
184
+ * @readonly
185
+ * A token generated on the backend */
186
+ MagaluService.jwt = extractParam('jwt');
187
+ /**
188
+ * @readonly
189
+ * @public
190
+ * Hashed value for the MiniApp */
191
+ MagaluService.hash = extractParam('hash');
192
+ /**
193
+ * @deprecated Use `miniapp.deeplink` service instead
194
+ */
195
+ MagaluService.deeplink = extractParam('deeplink');
196
+ /**
197
+ * @readonly
198
+ * @private
199
+ * Mapping from `callbackId` to a `callback` function */
200
+ MagaluService.callbacks = new Map();
201
+ /**
202
+ * @param {ServiceResponse} result - a response from the SDK
203
+ * @returns true if the result code is between 200...299
204
+ */
205
+ export function isSuccess(result) {
206
+ return result.code >= 200 && result.code < 300;
207
+ }
208
+ /**
209
+ * @param {string} value - key to be extract from the session storage
210
+ */
211
+ export function extractParam(value) {
212
+ if (typeof window === 'undefined') {
213
+ return null;
214
+ }
215
+ const queryString = window.location.search;
216
+ const urlParams = new URLSearchParams(queryString);
217
+ let param = urlParams.get(value);
218
+ if (param === null || param === void 0 ? void 0 : param.length) {
219
+ sessionStorage.setItem(value, param);
220
+ }
221
+ else {
222
+ param = sessionStorage.getItem(value);
223
+ }
224
+ return param;
225
+ }
226
+ function isProductionBuild() {
227
+ var _a, _b;
228
+ return ((_b = (_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.process) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'production';
229
+ }
230
+ if (typeof window !== 'undefined') {
231
+ window.MagaluService = MagaluService;
232
+ }
233
+ export { MagaluService as MagaluService };
@@ -0,0 +1,30 @@
1
+ const adresses = {
2
+ addresses: [
3
+ {
4
+ address: 'Rua voluntários da patria',
5
+ number: '3515',
6
+ complement: 'AP 111',
7
+ city: 'Sao Paulo',
8
+ state: 'Sao Paulo',
9
+ village: 'Jardins',
10
+ reference: 'Edificio Brasilia IX',
11
+ zipcode: '02402300',
12
+ phone: '11999299999',
13
+ phone_area_code: '011',
14
+ is_default: false
15
+ },
16
+ ],
17
+ };
18
+ const customerInfo = {
19
+ name: 'User Test',
20
+ email: 'user_test@gmail.com',
21
+ cpf_cnpj: '795.702.590-00',
22
+ cell_phone: '11999990000',
23
+ birth_date: '27/06/1992',
24
+ };
25
+ const zipCode = {
26
+ zipcode: '02402300',
27
+ latitude: '-23.489150',
28
+ longitude: '-46.626411',
29
+ };
30
+ export { adresses, customerInfo, zipCode };
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @param {ServiceResponse} result - A callback with the computation from a service
3
- */
3
+ */
4
4
  export type ServiceCallback = (result: ServiceResponse) => void;
5
5
  /**
6
6
  * @summary An object that represents a computation from the native SDKs
@@ -27,6 +27,7 @@ export interface ServiceCall {
27
27
  serviceArgs?: any;
28
28
  callbackId?: string;
29
29
  showLoading?: boolean;
30
+ authId?: string;
30
31
  callback?: ServiceCallback | undefined;
31
32
  }
32
33
  /**
@@ -43,14 +44,16 @@ declare class MagaluService {
43
44
  * Hashed value for the MiniApp */
44
45
  static hash: string | null;
45
46
  /**
46
- * @deprecated Use `miniapp.deeplink` service instead
47
- */
47
+ * @deprecated Use `miniapp.deeplink` service instead
48
+ */
48
49
  static deeplink: string | null;
49
50
  /**
50
51
  * @readonly
51
52
  * @private
52
- * Mapping from `callbackId` to a `callback` function */
53
+ * Mapping from `callbackId` to a `callback` function */
53
54
  static callbacks: Map<string, ServiceCallback>;
55
+ /** Cache for lazy-loaded mock module. */
56
+ private static mockDataPromise?;
54
57
  /**
55
58
  * @ignore
56
59
  */
@@ -106,6 +109,8 @@ declare class MagaluService {
106
109
  * ```
107
110
  */
108
111
  static request({ serviceName, serviceArgs, callbackId, callback }: ServiceCall, mockMode?: boolean): void;
112
+ private static handleMockRequest;
113
+ private static loadMockData;
109
114
  private static returnMockedResponse;
110
115
  private static buildPromise;
111
116
  static didFoundSDK(): boolean;
@@ -129,6 +134,10 @@ declare class MagaluService {
129
134
  * @param {ServiceCallback} callback - Called when a custom button has been touched
130
135
  */
131
136
  static registerNavbarActionsCallback(callback: ServiceCallback): void;
137
+ /**
138
+ * @param {ServiceCallback} callback - Called when there is a lifecycle event on the device
139
+ */
140
+ static registerLifecycleCallback(callback: ServiceCallback): void;
132
141
  }
133
142
  /**
134
143
  * @param {ServiceResponse} result - a response from the SDK
@@ -1,7 +1,15 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.MagaluService = exports.extractParam = exports.isSuccess = void 0;
4
- const mocks_1 = require("./mocks");
5
13
  /**
6
14
  * An wrapper for MiniApps to interact with Magazine Luiza's SuperApp
7
15
  */
@@ -10,34 +18,28 @@ class MagaluService {
10
18
  * @ignore
11
19
  */
12
20
  constructor() { }
13
- static request({ serviceName, serviceArgs, callbackId, callback, showLoading }, mockMode) {
14
- var _a;
21
+ static request({ serviceName, serviceArgs, callbackId, callback, showLoading, authId, }, mockMode) {
22
+ var _a, _b;
23
+ const id = callbackId || (Math.random() + new Date().getTime()).toString();
24
+ let isUsingPromise = callback === undefined;
25
+ if (mockMode) {
26
+ return this.handleMockRequest(serviceName, id, callback, isUsingPromise);
27
+ }
15
28
  if (!this.didFoundSDK()) {
16
29
  console.warn("didn't found any sdk");
17
30
  return;
18
31
  }
19
- const id = callbackId || (Math.random() + new Date().getTime()).toString();
20
32
  if (id && callback) {
21
33
  this.callbacks.set(id, callback);
22
34
  }
23
- let isUsingPromise = callback === undefined;
24
- if (mockMode) {
25
- const response = this.returnMockedResponse(serviceName, id);
26
- if (isUsingPromise) {
27
- return Promise.resolve(response);
28
- }
29
- else {
30
- this.pushResponse(response);
31
- }
32
- }
33
35
  try {
34
36
  const argsString = JSON.stringify(serviceArgs);
35
37
  const payload = {
36
38
  callbackId,
37
39
  serviceName,
38
- authId: (_a = this.hash) !== null && _a !== void 0 ? _a : '',
40
+ authId: (_b = (_a = this.hash) !== null && _a !== void 0 ? _a : authId) !== null && _b !== void 0 ? _b : '',
39
41
  arguments: argsString,
40
- showLoading
42
+ showLoading,
41
43
  };
42
44
  const message = JSON.stringify(payload);
43
45
  if (isUsingPromise) {
@@ -53,24 +55,53 @@ class MagaluService {
53
55
  console.error(e);
54
56
  }
55
57
  }
56
- static returnMockedResponse(serviceName, callbackId) {
57
- let bodyResponde;
58
- switch (serviceName) {
59
- case 'customer.getCustomerInfo':
60
- bodyResponde = mocks_1.customerInfo;
61
- break;
62
- case 'customer.getCustomerAddress':
63
- bodyResponde = mocks_1.adresses;
64
- break;
65
- case 'customer.getCustomerLocation':
66
- bodyResponde = mocks_1.zipCode;
67
- break;
58
+ static handleMockRequest(serviceName, callbackId, callback, isUsingPromise) {
59
+ if (isProductionBuild()) {
60
+ const message = 'mockMode is disabled in production builds';
61
+ if (isUsingPromise) {
62
+ return Promise.reject({ code: 500, message });
63
+ }
64
+ console.warn(message);
65
+ return;
68
66
  }
69
- return {
70
- callbackId: callbackId || '',
71
- code: 200,
72
- body: bodyResponde,
73
- };
67
+ if (callbackId && callback) {
68
+ this.callbacks.set(callbackId, callback);
69
+ }
70
+ return this.returnMockedResponse(serviceName, callbackId).then((response) => {
71
+ if (isUsingPromise) {
72
+ return response;
73
+ }
74
+ this.pushResponse(response);
75
+ return;
76
+ });
77
+ }
78
+ static loadMockData() {
79
+ if (!this.mockDataPromise) {
80
+ this.mockDataPromise = Promise.resolve().then(() => require('./mocks'));
81
+ }
82
+ return this.mockDataPromise;
83
+ }
84
+ static returnMockedResponse(serviceName, callbackId) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ const { adresses, customerInfo, zipCode } = yield this.loadMockData();
87
+ let bodyResponde;
88
+ switch (serviceName) {
89
+ case 'customer.getCustomerInfo':
90
+ bodyResponde = customerInfo;
91
+ break;
92
+ case 'customer.getCustomerAddress':
93
+ bodyResponde = adresses;
94
+ break;
95
+ case 'customer.getCustomerLocation':
96
+ bodyResponde = zipCode;
97
+ break;
98
+ }
99
+ return {
100
+ callbackId: callbackId || '',
101
+ code: 200,
102
+ body: bodyResponde,
103
+ };
104
+ });
74
105
  }
75
106
  static buildPromise(callbackId) {
76
107
  return new Promise((resolve, reject) => {
@@ -81,7 +112,7 @@ class MagaluService {
81
112
  else {
82
113
  reject({
83
114
  code: result.code,
84
- message: result.message
115
+ message: result.message,
85
116
  });
86
117
  }
87
118
  });
@@ -89,8 +120,8 @@ class MagaluService {
89
120
  }
90
121
  static didFoundSDK() {
91
122
  var _a, _b, _c;
92
- return ((_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.magaluSdk)
93
- || ((_c = window === null || window === void 0 ? void 0 : window.magaluSdk) === null || _c === void 0 ? void 0 : _c.requestService);
123
+ return (((_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.magaluSdk) ||
124
+ ((_c = window === null || window === void 0 ? void 0 : window.magaluSdk) === null || _c === void 0 ? void 0 : _c.requestService));
94
125
  }
95
126
  static callSDK(message, callbackId) {
96
127
  var _a, _b, _c;
@@ -143,6 +174,14 @@ class MagaluService {
143
174
  this.callbacks.set('appbar.actionPressed', callback);
144
175
  }
145
176
  }
177
+ /**
178
+ * @param {ServiceCallback} callback - Called when there is a lifecycle event on the device
179
+ */
180
+ static registerLifecycleCallback(callback) {
181
+ if (callback) {
182
+ this.callbacks.set("lifecycle.callback", callback);
183
+ }
184
+ }
146
185
  }
147
186
  exports.MagaluService = MagaluService;
148
187
  /**
@@ -155,13 +194,13 @@ MagaluService.jwt = extractParam('jwt');
155
194
  * Hashed value for the MiniApp */
156
195
  MagaluService.hash = extractParam('hash');
157
196
  /**
158
- * @deprecated Use `miniapp.deeplink` service instead
159
- */
197
+ * @deprecated Use `miniapp.deeplink` service instead
198
+ */
160
199
  MagaluService.deeplink = extractParam('deeplink');
161
200
  /**
162
201
  * @readonly
163
202
  * @private
164
- * Mapping from `callbackId` to a `callback` function */
203
+ * Mapping from `callbackId` to a `callback` function */
165
204
  MagaluService.callbacks = new Map();
166
205
  /**
167
206
  * @param {ServiceResponse} result - a response from the SDK
@@ -190,6 +229,10 @@ function extractParam(value) {
190
229
  return param;
191
230
  }
192
231
  exports.extractParam = extractParam;
232
+ function isProductionBuild() {
233
+ var _a, _b;
234
+ return ((_b = (_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.process) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'production';
235
+ }
193
236
  if (typeof window !== 'undefined') {
194
237
  window.MagaluService = MagaluService;
195
238
  }
package/package.json CHANGED
@@ -1,16 +1,26 @@
1
1
  {
2
2
  "name": "@magalu/webzord",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Toolkit to consume superapp api",
5
5
  "main": "lib/magaluService.js",
6
+ "module": "lib/esm/magaluService.js",
6
7
  "types": "lib/magaluService.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/magaluService.d.ts",
11
+ "import": "./lib/esm/magaluService.js",
12
+ "require": "./lib/magaluService.js",
13
+ "default": "./lib/magaluService.js"
14
+ }
15
+ },
7
16
  "files": [
8
17
  "lib/**/*"
9
18
  ],
10
19
  "scripts": {
11
20
  "test": "jest --config jestconfig.json",
12
21
  "test:cov": "jest --coverage --watchAll=false --config jestconfig.json",
13
- "build": "tsc"
22
+ "build": "tsc && tsc -p tsconfig.esm.json",
23
+ "publish:beta": "./scripts/beta-release.sh"
14
24
  },
15
25
  "repository": {
16
26
  "type": "git",