@magalu/webzord 1.2.0 → 1.3.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/CHANGELOG.md +28 -0
- package/lib/magaluService.d.ts +120 -6
- package/lib/magaluService.js +119 -53
- package/package.json +13 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.3.3](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.3.2...v1.3.3) (2024-09-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* update publish ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/286595a))
|
|
9
|
+
|
|
10
|
+
## [1.3.2](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.3.1...v1.3.2) (2024-09-12)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* fixing publish step on CI ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/bb8c238))
|
|
16
|
+
|
|
17
|
+
## [1.3.1](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.3.0...v1.3.1) (2024-09-10)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* adjust ci ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/5134968))
|
|
23
|
+
|
|
24
|
+
# [1.3.0](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.2.0...v1.3.0) (2024-09-10)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
* adding showLoading option ([](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/commit/d98078b))
|
|
30
|
+
|
|
3
31
|
# [1.2.0](https://gitlab.luizalabs.com/luizalabs/plataforma-mobile/webzord/compare/v1.1.2...v1.2.0) (2021-11-18)
|
|
4
32
|
|
|
5
33
|
|
package/lib/magaluService.d.ts
CHANGED
|
@@ -1,28 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @param {ServiceResponse} result - A callback with the computation from a service
|
|
3
|
+
*/
|
|
4
|
+
export type ServiceCallback = (result: ServiceResponse) => void;
|
|
5
|
+
/**
|
|
6
|
+
* @summary An object that represents a computation from the native SDKs
|
|
7
|
+
* @property {string} callbackId - An unique identifier for a callback function
|
|
8
|
+
* @property {number} code - A result code of this computation, 200...299 for valid computations otherwise it's considered an error
|
|
9
|
+
* @property {string} message - An optional error message
|
|
10
|
+
* @property {*} body - An optional result object of this computation
|
|
11
|
+
*/
|
|
12
|
+
export interface ServiceResponse {
|
|
3
13
|
callbackId: string;
|
|
4
14
|
code: number;
|
|
5
15
|
message?: string;
|
|
6
16
|
body?: any;
|
|
7
17
|
}
|
|
8
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @summary An object with fields required for requesting a service on the host app
|
|
20
|
+
* @property {string} serviceName - A name of the service to be called on the SDK
|
|
21
|
+
* @property {*} serviceArgs - An optional arguments for a service
|
|
22
|
+
* @property {string} callbackId - A unique identifier for a callback function
|
|
23
|
+
* @property {Function=undefined} callback - An optional callback to receive the result of a service call
|
|
24
|
+
*/
|
|
25
|
+
export interface ServiceCall {
|
|
9
26
|
serviceName: string;
|
|
10
27
|
serviceArgs?: any;
|
|
11
28
|
callbackId?: string;
|
|
12
|
-
|
|
29
|
+
showLoading?: boolean;
|
|
30
|
+
callback?: ServiceCallback | undefined;
|
|
13
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* An wrapper for MiniApps to interact with Magazine Luiza's SuperApp
|
|
34
|
+
*/
|
|
14
35
|
declare class MagaluService {
|
|
36
|
+
/**
|
|
37
|
+
* @readonly
|
|
38
|
+
* A token generated on the backend */
|
|
15
39
|
static jwt: string | null;
|
|
40
|
+
/**
|
|
41
|
+
* @readonly
|
|
42
|
+
* @public
|
|
43
|
+
* Hashed value for the MiniApp */
|
|
16
44
|
static hash: string | null;
|
|
45
|
+
/**
|
|
46
|
+
* @deprecated Use `miniapp.deeplink` service instead
|
|
47
|
+
*/
|
|
17
48
|
static deeplink: string | null;
|
|
49
|
+
/**
|
|
50
|
+
* @readonly
|
|
51
|
+
* @private
|
|
52
|
+
* Mapping from `callbackId` to a `callback` function */
|
|
18
53
|
static callbacks: Map<string, ServiceCallback>;
|
|
54
|
+
/**
|
|
55
|
+
* @ignore
|
|
56
|
+
*/
|
|
57
|
+
private constructor();
|
|
58
|
+
/**
|
|
59
|
+
* @param {ServiceCall} - See {@link ServiceCall} for more details
|
|
60
|
+
* ```
|
|
61
|
+
* {
|
|
62
|
+
* serviceName: string,
|
|
63
|
+
* serviceArgs: any,
|
|
64
|
+
* callbackId: string
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @param {boolean} [mockMode=] - Pass true when developing outside a valid webview host
|
|
69
|
+
* @returns {Promise<any>} A promise with either the `body` of the response, or the `error`
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```
|
|
73
|
+
* MagaluService.request({
|
|
74
|
+
* serviceName: 'someService',
|
|
75
|
+
* serviceArgs: { foo: true },
|
|
76
|
+
* callbackId: 'someServiceCallbackId',
|
|
77
|
+
* }).then((result) => {
|
|
78
|
+
* console.log(result);
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static request({ serviceName, serviceArgs, callbackId }: ServiceCall, mockMode?: boolean): Promise<any>;
|
|
83
|
+
/**
|
|
84
|
+
* @param {ServiceCall} - See {@link ServiceCall} for more details
|
|
85
|
+
* ```
|
|
86
|
+
* {
|
|
87
|
+
* serviceName: string,
|
|
88
|
+
* serviceArgs: any,
|
|
89
|
+
* callbackId: string,
|
|
90
|
+
* callback: ServiceCallback
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @param {boolean} [mockMode=] - Pass true when developing outside a valid webview host
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```
|
|
98
|
+
* MagaluService.request({
|
|
99
|
+
* serviceName: 'someService',
|
|
100
|
+
* serviceArgs: { foo: true },
|
|
101
|
+
* callbackId: 'someServiceCallbackId',
|
|
102
|
+
* callback: (result) => {
|
|
103
|
+
* console.log(result);
|
|
104
|
+
* }
|
|
105
|
+
* );
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
19
108
|
static request({ serviceName, serviceArgs, callbackId, callback }: ServiceCall, mockMode?: boolean): void;
|
|
109
|
+
private static returnMockedResponse;
|
|
110
|
+
private static buildPromise;
|
|
111
|
+
private static didFoundSDK;
|
|
112
|
+
private static callSDK;
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} callbackId - The id of the callback to be removed
|
|
115
|
+
*/
|
|
20
116
|
static unregister(callbackId: string): void;
|
|
117
|
+
/**
|
|
118
|
+
* Wipes all the callbacks
|
|
119
|
+
*/
|
|
21
120
|
static unregisterAll(): void;
|
|
121
|
+
/**
|
|
122
|
+
* @protected
|
|
123
|
+
* @param {ServiceResponse} response - An object that wrap all the data from a service computation
|
|
124
|
+
*
|
|
125
|
+
* This method is called from the native SDKs, don't call it yourself
|
|
126
|
+
*/
|
|
22
127
|
static pushResponse(response: ServiceResponse): void;
|
|
23
|
-
|
|
128
|
+
/**
|
|
129
|
+
* @param {ServiceCallback} callback - Called when a custom button has been touched
|
|
130
|
+
*/
|
|
24
131
|
static registerNavbarActionsCallback(callback: ServiceCallback): void;
|
|
25
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* @param {ServiceResponse} result - a response from the SDK
|
|
135
|
+
* @returns true if the result code is between 200...299
|
|
136
|
+
*/
|
|
26
137
|
export declare function isSuccess(result: ServiceResponse): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* @param {string} value - key to be extract from the session storage
|
|
140
|
+
*/
|
|
27
141
|
export declare function extractParam(value: string): string | null;
|
|
28
|
-
export
|
|
142
|
+
export { MagaluService as MagaluService };
|
package/lib/magaluService.js
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractParam = exports.isSuccess = void 0;
|
|
3
|
+
exports.MagaluService = exports.extractParam = exports.isSuccess = void 0;
|
|
4
4
|
const mocks_1 = require("./mocks");
|
|
5
|
+
/**
|
|
6
|
+
* An wrapper for MiniApps to interact with Magazine Luiza's SuperApp
|
|
7
|
+
*/
|
|
5
8
|
class MagaluService {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @ignore
|
|
11
|
+
*/
|
|
12
|
+
constructor() { }
|
|
13
|
+
static request({ serviceName, serviceArgs, callbackId, callback, showLoading }, mockMode) {
|
|
14
|
+
var _a;
|
|
15
|
+
if (!this.didFoundSDK()) {
|
|
16
|
+
console.warn("didn't found any sdk");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const id = callbackId || (Math.random() + new Date().getTime()).toString();
|
|
20
|
+
if (id && callback) {
|
|
21
|
+
this.callbacks.set(id, callback);
|
|
10
22
|
}
|
|
23
|
+
let isUsingPromise = callback === undefined;
|
|
11
24
|
if (mockMode) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
case 'customer.getCustomerAddress':
|
|
19
|
-
bodyResponde = mocks_1.adresses;
|
|
20
|
-
break;
|
|
21
|
-
case 'customer.getCustomerLocation':
|
|
22
|
-
bodyResponde = mocks_1.zipCode;
|
|
23
|
-
break;
|
|
25
|
+
const response = this.returnMockedResponse(serviceName, id);
|
|
26
|
+
if (isUsingPromise) {
|
|
27
|
+
return Promise.resolve(response);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.pushResponse(response);
|
|
24
31
|
}
|
|
25
|
-
response = {
|
|
26
|
-
callbackId: callbackId || '',
|
|
27
|
-
code: 200,
|
|
28
|
-
body: bodyResponde,
|
|
29
|
-
};
|
|
30
|
-
return this.pushResponse(response);
|
|
31
32
|
}
|
|
32
33
|
try {
|
|
33
34
|
const argsString = JSON.stringify(serviceArgs);
|
|
@@ -36,29 +37,92 @@ class MagaluService {
|
|
|
36
37
|
serviceName,
|
|
37
38
|
authId: (_a = this.hash) !== null && _a !== void 0 ? _a : '',
|
|
38
39
|
arguments: argsString,
|
|
40
|
+
showLoading
|
|
39
41
|
};
|
|
40
42
|
const message = JSON.stringify(payload);
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
window.magaluSdk.requestService(message);
|
|
43
|
+
if (isUsingPromise) {
|
|
44
|
+
const promise = this.buildPromise(id);
|
|
45
|
+
this.callSDK(message, id);
|
|
46
|
+
return promise;
|
|
46
47
|
}
|
|
47
48
|
else {
|
|
48
|
-
|
|
49
|
-
callbackId && this.callbacks.delete(callbackId);
|
|
49
|
+
this.callSDK(message, callbackId);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
catch (e) {
|
|
53
53
|
console.error(e);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
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;
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
callbackId: callbackId || '',
|
|
71
|
+
code: 200,
|
|
72
|
+
body: bodyResponde,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
static buildPromise(callbackId) {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
this.callbacks.set(callbackId || '', (result) => {
|
|
78
|
+
if (isSuccess(result)) {
|
|
79
|
+
resolve(result.body);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
reject({
|
|
83
|
+
code: result.code,
|
|
84
|
+
message: result.message
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
static didFoundSDK() {
|
|
91
|
+
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);
|
|
94
|
+
}
|
|
95
|
+
static callSDK(message, callbackId) {
|
|
96
|
+
var _a, _b, _c;
|
|
97
|
+
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) {
|
|
98
|
+
window.webkit.messageHandlers.magaluSdk.postMessage(message);
|
|
99
|
+
}
|
|
100
|
+
else if ((_c = window === null || window === void 0 ? void 0 : window.magaluSdk) === null || _c === void 0 ? void 0 : _c.requestService) {
|
|
101
|
+
window.magaluSdk.requestService(message);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
console.warn("didn't found any sdk");
|
|
105
|
+
callbackId && this.callbacks.delete(callbackId);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* @param {string} callbackId - The id of the callback to be removed
|
|
110
|
+
*/
|
|
56
111
|
static unregister(callbackId) {
|
|
57
112
|
this.callbacks.delete(callbackId);
|
|
58
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Wipes all the callbacks
|
|
116
|
+
*/
|
|
59
117
|
static unregisterAll() {
|
|
60
118
|
this.callbacks.clear();
|
|
61
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* @protected
|
|
122
|
+
* @param {ServiceResponse} response - An object that wrap all the data from a service computation
|
|
123
|
+
*
|
|
124
|
+
* This method is called from the native SDKs, don't call it yourself
|
|
125
|
+
*/
|
|
62
126
|
static pushResponse(response) {
|
|
63
127
|
try {
|
|
64
128
|
const callback = this.callbacks.get(response.callbackId);
|
|
@@ -71,42 +135,45 @@ class MagaluService {
|
|
|
71
135
|
console.error(e);
|
|
72
136
|
}
|
|
73
137
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return this.jwt;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
this.request({
|
|
81
|
-
serviceName: 'customer.requestCredentials',
|
|
82
|
-
callbackId: 'requestCredentials',
|
|
83
|
-
callback: (result) => {
|
|
84
|
-
if (isSuccess(result)) {
|
|
85
|
-
if (typeof window !== 'undefined') {
|
|
86
|
-
document.cookie = `jwt=${result.body.jwt}`;
|
|
87
|
-
sessionStorage.setItem('jwt', result.body.jwt);
|
|
88
|
-
}
|
|
89
|
-
this.jwt = result.body.jwt;
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
138
|
+
/**
|
|
139
|
+
* @param {ServiceCallback} callback - Called when a custom button has been touched
|
|
140
|
+
*/
|
|
96
141
|
static registerNavbarActionsCallback(callback) {
|
|
97
142
|
if (callback) {
|
|
98
143
|
this.callbacks.set('appbar.actionPressed', callback);
|
|
99
144
|
}
|
|
100
145
|
}
|
|
101
146
|
}
|
|
147
|
+
exports.MagaluService = MagaluService;
|
|
148
|
+
/**
|
|
149
|
+
* @readonly
|
|
150
|
+
* A token generated on the backend */
|
|
102
151
|
MagaluService.jwt = extractParam('jwt');
|
|
152
|
+
/**
|
|
153
|
+
* @readonly
|
|
154
|
+
* @public
|
|
155
|
+
* Hashed value for the MiniApp */
|
|
103
156
|
MagaluService.hash = extractParam('hash');
|
|
157
|
+
/**
|
|
158
|
+
* @deprecated Use `miniapp.deeplink` service instead
|
|
159
|
+
*/
|
|
104
160
|
MagaluService.deeplink = extractParam('deeplink');
|
|
161
|
+
/**
|
|
162
|
+
* @readonly
|
|
163
|
+
* @private
|
|
164
|
+
* Mapping from `callbackId` to a `callback` function */
|
|
105
165
|
MagaluService.callbacks = new Map();
|
|
166
|
+
/**
|
|
167
|
+
* @param {ServiceResponse} result - a response from the SDK
|
|
168
|
+
* @returns true if the result code is between 200...299
|
|
169
|
+
*/
|
|
106
170
|
function isSuccess(result) {
|
|
107
171
|
return result.code >= 200 && result.code < 300;
|
|
108
172
|
}
|
|
109
173
|
exports.isSuccess = isSuccess;
|
|
174
|
+
/**
|
|
175
|
+
* @param {string} value - key to be extract from the session storage
|
|
176
|
+
*/
|
|
110
177
|
function extractParam(value) {
|
|
111
178
|
if (typeof window === 'undefined') {
|
|
112
179
|
return null;
|
|
@@ -126,4 +193,3 @@ exports.extractParam = extractParam;
|
|
|
126
193
|
if (typeof window !== 'undefined') {
|
|
127
194
|
window.MagaluService = MagaluService;
|
|
128
195
|
}
|
|
129
|
-
exports.default = MagaluService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magalu/webzord",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Toolkit to consume superapp api",
|
|
5
5
|
"main": "lib/magaluService.js",
|
|
6
6
|
"types": "lib/magaluService.d.ts",
|
|
@@ -23,6 +23,17 @@
|
|
|
23
23
|
"@types/jest": "^26.0.23",
|
|
24
24
|
"jest": "^26.6.3",
|
|
25
25
|
"ts-jest": "^26.5.5",
|
|
26
|
-
"
|
|
26
|
+
"ts-loader": "^9.4.1",
|
|
27
|
+
"typedoc": "^0.23.15",
|
|
28
|
+
"typescript": "^4.8.3",
|
|
29
|
+
"webpack": "^5.74.0",
|
|
30
|
+
"webpack-cli": "^4.10.0"
|
|
31
|
+
},
|
|
32
|
+
"typedocOptions": {
|
|
33
|
+
"mode": "modules",
|
|
34
|
+
"out": "docs"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"registry": "https://registry.npmjs.org"
|
|
27
38
|
}
|
|
28
39
|
}
|