fca-nx 6.6.6 → 6.6.8
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 +1 -1
- package/src/api/action/addfriend.js +109 -0
- package/src/api/action/changeCover.js +106 -221
- package/src/api/http/httpGet.js +33 -13
- package/src/api/http/httpPost.js +32 -14
- package/src/api/messaging/editMessage.js +50 -34
- package/src/api/messaging/sendMessage.js +19 -8
- package/src/api/socket/listenMqtt.js +52 -33
- package/src/api/users/getUserInfoV2.js +145 -786
- package/src/app/createFcaClient.js +0 -1
- package/src/api/action/addFriend.js +0 -145
- /package/{LICENCE → LICENSE} +0 -0
package/package.json
CHANGED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send a friend request to a Facebook user.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const log = require("../../../func/logAdapter");
|
|
8
|
+
const { parseAndCheckLogin } = require("../../utils/client");
|
|
9
|
+
const { formatID } = require("../../utils/format");
|
|
10
|
+
|
|
11
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
12
|
+
return function addFriend(userID, callback) {
|
|
13
|
+
let resolveFunc;
|
|
14
|
+
let rejectFunc;
|
|
15
|
+
|
|
16
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
|
17
|
+
resolveFunc = resolve;
|
|
18
|
+
rejectFunc = reject;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const handleCallback = (err, data) => {
|
|
22
|
+
if (typeof callback === "function") {
|
|
23
|
+
callback(err, data);
|
|
24
|
+
}
|
|
25
|
+
if (err) {
|
|
26
|
+
rejectFunc(err);
|
|
27
|
+
} else {
|
|
28
|
+
resolveFunc(data);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* Validate user ID
|
|
34
|
+
*/
|
|
35
|
+
let targetID;
|
|
36
|
+
try {
|
|
37
|
+
targetID = formatID(userID);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
handleCallback(err);
|
|
40
|
+
return returnPromise;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!targetID) {
|
|
44
|
+
handleCallback(new Error("Invalid user ID"));
|
|
45
|
+
return returnPromise;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (String(targetID) === String(ctx.userID)) {
|
|
49
|
+
handleCallback(new Error("You cannot send a friend request to yourself"));
|
|
50
|
+
return returnPromise;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/*
|
|
54
|
+
* Facebook friend request payload
|
|
55
|
+
*/
|
|
56
|
+
const form = {
|
|
57
|
+
uid: String(targetID),
|
|
58
|
+
viewer_id: String(ctx.userID),
|
|
59
|
+
source: "profile_button",
|
|
60
|
+
ref: "profile",
|
|
61
|
+
floc: "profile",
|
|
62
|
+
nctr: "profile"
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
defaultFuncs
|
|
66
|
+
.post(
|
|
67
|
+
"https://www.facebook.com/ajax/friends/requests/send/",
|
|
68
|
+
ctx.jar,
|
|
69
|
+
form
|
|
70
|
+
)
|
|
71
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
72
|
+
.then(function (resData) {
|
|
73
|
+
if (!resData) {
|
|
74
|
+
throw new Error("Empty response from Facebook");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (resData.error || resData.errors || resData.errorDescription) {
|
|
78
|
+
throw resData;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const payload = resData.payload;
|
|
82
|
+
|
|
83
|
+
if (payload) {
|
|
84
|
+
if (payload.error || payload.err) {
|
|
85
|
+
throw payload;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (payload.errorDescription) {
|
|
89
|
+
throw new Error(payload.errorDescription);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const result = {
|
|
94
|
+
success: true,
|
|
95
|
+
userID: String(targetID),
|
|
96
|
+
response: resData
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return handleCallback(null, result);
|
|
100
|
+
})
|
|
101
|
+
.catch(function (err) {
|
|
102
|
+
log.error("addFriend", err?.message || err);
|
|
103
|
+
return handleCallback(err);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return returnPromise;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**d
|
|
2
|
+
* Change Facebook Profile Cover Photo via GraphQL Mutation.
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
"use strict";
|
|
2
6
|
|
|
3
7
|
const log = require("../../../func/logAdapter");
|
|
@@ -5,13 +9,14 @@ const { isReadableStream } = require("../../utils/constants");
|
|
|
5
9
|
const { parseAndCheckLogin } = require("../../utils/client");
|
|
6
10
|
const { getType } = require("../../utils/format");
|
|
7
11
|
|
|
8
|
-
module.exports = function(defaultFuncs, api, ctx) {
|
|
9
|
-
|
|
12
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
10
13
|
function handleUpload(image, callback) {
|
|
14
|
+
const userID = ctx.i_userID || ctx.userID;
|
|
15
|
+
|
|
11
16
|
const form = {
|
|
12
|
-
profile_id:
|
|
17
|
+
profile_id: userID,
|
|
13
18
|
photo_source: 57,
|
|
14
|
-
av:
|
|
19
|
+
av: userID,
|
|
15
20
|
file: image
|
|
16
21
|
};
|
|
17
22
|
|
|
@@ -22,255 +27,135 @@ module.exports = function(defaultFuncs, api, ctx) {
|
|
|
22
27
|
form,
|
|
23
28
|
{}
|
|
24
29
|
)
|
|
25
|
-
.then(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
!resData ||
|
|
31
|
-
resData.error ||
|
|
32
|
-
resData.errors ||
|
|
33
|
-
!resData.payload
|
|
34
|
-
) {
|
|
35
|
-
throw resData || new Error(
|
|
36
|
-
"Cover image upload failed"
|
|
37
|
-
);
|
|
30
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
31
|
+
.then(function (resData) {
|
|
32
|
+
if (!resData || resData.error || resData.errors || !resData.payload) {
|
|
33
|
+
throw resData || new Error("Cover image upload failed");
|
|
38
34
|
}
|
|
39
35
|
|
|
40
36
|
if (!resData.payload.fbid) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
"Facebook did not return photo ID"
|
|
43
|
-
);
|
|
37
|
+
throw new Error("Facebook did not return photo ID");
|
|
44
38
|
}
|
|
45
39
|
|
|
46
40
|
callback(null, resData);
|
|
47
41
|
})
|
|
48
|
-
.catch(function(err) {
|
|
42
|
+
.catch(function (err) {
|
|
49
43
|
log.error("handleCoverUpload", err);
|
|
50
44
|
callback(err);
|
|
51
45
|
});
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
return function changeCover(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
) {
|
|
58
|
-
|
|
59
|
-
let resolveFunc = function() {};
|
|
60
|
-
let rejectFunc = function() {};
|
|
48
|
+
return function changeCover(image, callback) {
|
|
49
|
+
let resolveFunc;
|
|
50
|
+
let rejectFunc;
|
|
61
51
|
|
|
62
|
-
const returnPromise = new Promise(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
);
|
|
52
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
|
53
|
+
resolveFunc = resolve;
|
|
54
|
+
rejectFunc = reject;
|
|
55
|
+
});
|
|
68
56
|
|
|
69
|
-
/*
|
|
70
|
-
* Callback handling
|
|
71
|
-
*/
|
|
72
57
|
if (
|
|
73
58
|
!callback &&
|
|
74
|
-
(
|
|
75
|
-
getType(image) === "Function" ||
|
|
76
|
-
getType(image) === "AsyncFunction"
|
|
77
|
-
)
|
|
59
|
+
(getType(image) === "Function" || getType(image) === "AsyncFunction")
|
|
78
60
|
) {
|
|
79
61
|
callback = image;
|
|
80
62
|
image = null;
|
|
81
63
|
}
|
|
82
64
|
|
|
83
|
-
|
|
84
|
-
callback
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
65
|
+
const handleCallback = (err, data) => {
|
|
66
|
+
if (typeof callback === "function") {
|
|
67
|
+
callback(err, data);
|
|
68
|
+
}
|
|
69
|
+
if (err) {
|
|
70
|
+
rejectFunc(err);
|
|
71
|
+
} else {
|
|
89
72
|
resolveFunc(data);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
92
75
|
|
|
93
|
-
/*
|
|
94
|
-
* Validate image
|
|
95
|
-
*/
|
|
96
76
|
if (!isReadableStream(image)) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
"Image is not a readable stream"
|
|
100
|
-
)
|
|
101
|
-
);
|
|
77
|
+
handleCallback(new Error("Image is not a readable stream"));
|
|
78
|
+
return returnPromise;
|
|
102
79
|
}
|
|
103
80
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
image,
|
|
109
|
-
function(err, payload) {
|
|
110
|
-
|
|
111
|
-
if (err) {
|
|
112
|
-
return callback(err);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const userID =
|
|
116
|
-
ctx.i_userID ||
|
|
117
|
-
ctx.userID;
|
|
118
|
-
|
|
119
|
-
const photoID =
|
|
120
|
-
payload?.payload?.fbid;
|
|
121
|
-
|
|
122
|
-
if (!photoID) {
|
|
123
|
-
return callback(
|
|
124
|
-
new Error(
|
|
125
|
-
"Cover photo ID not found"
|
|
126
|
-
)
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/*
|
|
131
|
-
* ProfileCometCoverPhotoUpdateMutation
|
|
132
|
-
*/
|
|
133
|
-
const variables = {
|
|
134
|
-
input: {
|
|
135
|
-
attribution_id_v2:
|
|
136
|
-
`ProfileCometCollectionRoot.react,` +
|
|
137
|
-
`comet.profile.collection.photos_by,unexpected,` +
|
|
138
|
-
`${Date.now()},770083,,;` +
|
|
139
|
-
|
|
140
|
-
`ProfileCometCollectionRoot.react,` +
|
|
141
|
-
`comet.profile.collection.photos_albums,unexpected,` +
|
|
142
|
-
`${Date.now()},470774,,;` +
|
|
143
|
-
|
|
144
|
-
`ProfileCometCollectionRoot.react,` +
|
|
145
|
-
`comet.profile.collection.photos,unexpected,` +
|
|
146
|
-
`${Date.now()},94740,,;` +
|
|
147
|
-
|
|
148
|
-
`ProfileCometCollectionRoot.react,` +
|
|
149
|
-
`comet.profile.collection.saved_reels_on_profile,unexpected,` +
|
|
150
|
-
`${Date.now()},89669,,;` +
|
|
151
|
-
|
|
152
|
-
`ProfileCometCollectionRoot.react,` +
|
|
153
|
-
`comet.profile.collection.reels_tab,unexpected,` +
|
|
154
|
-
`${Date.now()},152201,,`,
|
|
155
|
-
|
|
156
|
-
cover_photo_id: String(photoID),
|
|
81
|
+
handleUpload(image, function (err, payload) {
|
|
82
|
+
if (err) {
|
|
83
|
+
return handleCallback(err);
|
|
84
|
+
}
|
|
157
85
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
y: 1
|
|
161
|
-
},
|
|
86
|
+
const userID = ctx.i_userID || ctx.userID;
|
|
87
|
+
const photoID = payload?.payload?.fbid;
|
|
162
88
|
|
|
163
|
-
|
|
164
|
-
|
|
89
|
+
if (!photoID) {
|
|
90
|
+
return handleCallback(new Error("Cover photo ID not found"));
|
|
91
|
+
}
|
|
165
92
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
93
|
+
const variables = {
|
|
94
|
+
input: {
|
|
95
|
+
attribution_id_v2:
|
|
96
|
+
`ProfileCometCollectionRoot.react,` +
|
|
97
|
+
`comet.profile.collection.photos_by,unexpected,` +
|
|
98
|
+
`${Date.now()},770083,,;` +
|
|
99
|
+
`ProfileCometCollectionRoot.react,` +
|
|
100
|
+
`comet.profile.collection.photos_albums,unexpected,` +
|
|
101
|
+
`${Date.now()},470774,,;` +
|
|
102
|
+
`ProfileCometCollectionRoot.react,` +
|
|
103
|
+
`comet.profile.collection.photos,unexpected,` +
|
|
104
|
+
`${Date.now()},94740,,;` +
|
|
105
|
+
`ProfileCometCollectionRoot.react,` +
|
|
106
|
+
`comet.profile.collection.saved_reels_on_profile,unexpected,` +
|
|
107
|
+
`${Date.now()},89669,,;` +
|
|
108
|
+
`ProfileCometCollectionRoot.react,` +
|
|
109
|
+
`comet.profile.collection.reels_tab,unexpected,` +
|
|
110
|
+
`${Date.now()},152201,,`,
|
|
111
|
+
|
|
112
|
+
cover_photo_id: String(photoID),
|
|
113
|
+
focus: {
|
|
114
|
+
x: 0.5,
|
|
115
|
+
y: 1
|
|
170
116
|
},
|
|
117
|
+
target_user_id: String(userID),
|
|
118
|
+
actor_id: String(userID),
|
|
119
|
+
client_mutation_id: Math.round(Math.random() * 19).toString()
|
|
120
|
+
},
|
|
121
|
+
scale: 1,
|
|
122
|
+
contextualProfileContext: null
|
|
123
|
+
};
|
|
171
124
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
"ProfileCometCoverPhotoUpdateMutation",
|
|
181
|
-
|
|
182
|
-
fb_api_caller_class:
|
|
183
|
-
"RelayModern",
|
|
184
|
-
|
|
185
|
-
doc_id:
|
|
186
|
-
"8247793861913071",
|
|
187
|
-
|
|
188
|
-
server_timestamps: true,
|
|
189
|
-
|
|
190
|
-
variables:
|
|
191
|
-
JSON.stringify(variables)
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
defaultFuncs
|
|
195
|
-
.post(
|
|
196
|
-
"https://www.facebook.com/api/graphql/",
|
|
197
|
-
ctx.jar,
|
|
198
|
-
form
|
|
199
|
-
)
|
|
200
|
-
.then(
|
|
201
|
-
parseAndCheckLogin(
|
|
202
|
-
ctx,
|
|
203
|
-
defaultFuncs
|
|
204
|
-
)
|
|
205
|
-
)
|
|
206
|
-
.then(function(resData) {
|
|
207
|
-
|
|
208
|
-
if (
|
|
209
|
-
!resData ||
|
|
210
|
-
resData.error ||
|
|
211
|
-
resData.errors
|
|
212
|
-
) {
|
|
213
|
-
throw resData || new Error(
|
|
214
|
-
"Cover photo update failed"
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/*
|
|
219
|
-
* Facebook response
|
|
220
|
-
*/
|
|
221
|
-
const coverURL =
|
|
222
|
-
resData
|
|
223
|
-
?.data
|
|
224
|
-
?.user_update_cover_photo
|
|
225
|
-
?.user
|
|
226
|
-
?.cover_photo
|
|
227
|
-
?.photo
|
|
228
|
-
?.url;
|
|
229
|
-
|
|
230
|
-
/*
|
|
231
|
-
* কিছু response array আকারে আসতে পারে
|
|
232
|
-
*/
|
|
233
|
-
const finalData =
|
|
234
|
-
Array.isArray(resData)
|
|
235
|
-
? resData[0]
|
|
236
|
-
: resData;
|
|
237
|
-
|
|
238
|
-
const url =
|
|
239
|
-
coverURL ||
|
|
240
|
-
finalData
|
|
241
|
-
?.data
|
|
242
|
-
?.user_update_cover_photo
|
|
243
|
-
?.user
|
|
244
|
-
?.cover_photo
|
|
245
|
-
?.photo
|
|
246
|
-
?.url;
|
|
247
|
-
|
|
248
|
-
if (!url) {
|
|
249
|
-
/*
|
|
250
|
-
* Mutation সফল হলেও URL না দিলে
|
|
251
|
-
* পুরো response return করছি।
|
|
252
|
-
*/
|
|
253
|
-
return callback(
|
|
254
|
-
null,
|
|
255
|
-
finalData
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return callback(
|
|
260
|
-
null,
|
|
261
|
-
url
|
|
262
|
-
);
|
|
263
|
-
})
|
|
264
|
-
.catch(function(err) {
|
|
265
|
-
log.error(
|
|
266
|
-
"changeCover",
|
|
267
|
-
err
|
|
268
|
-
);
|
|
125
|
+
const form = {
|
|
126
|
+
av: String(userID),
|
|
127
|
+
fb_api_req_friendly_name: "ProfileCometCoverPhotoUpdateMutation",
|
|
128
|
+
fb_api_caller_class: "RelayModern",
|
|
129
|
+
doc_id: "8247793861913071",
|
|
130
|
+
server_timestamps: true,
|
|
131
|
+
variables: JSON.stringify(variables)
|
|
132
|
+
};
|
|
269
133
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
134
|
+
defaultFuncs
|
|
135
|
+
.post("https://www.facebook.com/api/graphql/", ctx.jar, form)
|
|
136
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
137
|
+
.then(function (resData) {
|
|
138
|
+
if (!resData || resData.error || resData.errors) {
|
|
139
|
+
throw resData || new Error("Cover photo update failed");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const finalData = Array.isArray(resData) ? resData[0] : resData;
|
|
143
|
+
|
|
144
|
+
const coverURL =
|
|
145
|
+
finalData?.data?.user_update_cover_photo?.user?.cover_photo?.photo?.url ||
|
|
146
|
+
resData?.data?.user_update_cover_photo?.user?.cover_photo?.photo?.url;
|
|
147
|
+
|
|
148
|
+
if (!coverURL) {
|
|
149
|
+
return handleCallback(null, finalData);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return handleCallback(null, coverURL);
|
|
153
|
+
})
|
|
154
|
+
.catch(function (err) {
|
|
155
|
+
log.error("changeCover", err?.message || err);
|
|
156
|
+
return handleCallback(err);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
274
159
|
|
|
275
160
|
return returnPromise;
|
|
276
161
|
};
|
package/src/api/http/httpGet.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modified by Xalman
|
|
3
|
+
* HTTP Get Helper Function with Clean Promise & Callback Handling
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
"use strict";
|
|
2
7
|
|
|
3
8
|
const { getType } = require("../../utils/format");
|
|
@@ -5,14 +10,15 @@ const { get } = require("../../utils/request");
|
|
|
5
10
|
|
|
6
11
|
const httpGetFactory = function (defaultFuncs, api, ctx) {
|
|
7
12
|
return function httpGet(url, form, callback, notAPI) {
|
|
8
|
-
let resolveFunc
|
|
9
|
-
let rejectFunc
|
|
13
|
+
let resolveFunc;
|
|
14
|
+
let rejectFunc;
|
|
10
15
|
|
|
11
16
|
const returnPromise = new Promise((resolve, reject) => {
|
|
12
17
|
resolveFunc = resolve;
|
|
13
18
|
rejectFunc = reject;
|
|
14
19
|
});
|
|
15
20
|
|
|
21
|
+
// Handle optional 'form' parameter when callback is passed as second argument
|
|
16
22
|
if (
|
|
17
23
|
!callback &&
|
|
18
24
|
(getType(form) === "Function" || getType(form) === "AsyncFunction")
|
|
@@ -23,20 +29,34 @@ const httpGetFactory = function (defaultFuncs, api, ctx) {
|
|
|
23
29
|
|
|
24
30
|
form = form || {};
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
callback
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
const handleCallback = (err, data) => {
|
|
33
|
+
if (typeof callback === "function") {
|
|
34
|
+
callback(err, data);
|
|
35
|
+
}
|
|
36
|
+
if (err) {
|
|
37
|
+
rejectFunc(err);
|
|
38
|
+
} else {
|
|
30
39
|
resolveFunc(data);
|
|
31
|
-
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
32
42
|
|
|
33
|
-
const executor = notAPI ? get : defaultFuncs
|
|
43
|
+
const executor = notAPI ? get : (defaultFuncs?.get || get);
|
|
34
44
|
|
|
35
|
-
executor(url, ctx.jar, form)
|
|
36
|
-
.then((resData) =>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
executor(url, ctx.jar, form, ctx.globalOptions || {})
|
|
46
|
+
.then((resData) => {
|
|
47
|
+
let data = resData?.data ?? resData;
|
|
48
|
+
if (typeof data === "object") {
|
|
49
|
+
try {
|
|
50
|
+
data = JSON.stringify(data, null, 2);
|
|
51
|
+
} catch {
|
|
52
|
+
// Retain object if stringify fails
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
handleCallback(null, data);
|
|
56
|
+
})
|
|
57
|
+
.catch((err) => {
|
|
58
|
+
console.error("httpGet error:", err?.message || err);
|
|
59
|
+
handleCallback(err, null);
|
|
40
60
|
});
|
|
41
61
|
|
|
42
62
|
return returnPromise;
|
package/src/api/http/httpPost.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modified by Xalman
|
|
3
|
+
* HTTP Post Helper Function with Clean Promise/Callback Handling
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
"use strict";
|
|
2
7
|
|
|
3
8
|
const { post } = require("../../utils/request");
|
|
@@ -5,14 +10,15 @@ const { getType } = require("../../utils/format");
|
|
|
5
10
|
|
|
6
11
|
const httpPostFactory = function (defaultFuncs, api, ctx) {
|
|
7
12
|
return function httpPost(url, form, callback, notAPI) {
|
|
8
|
-
let resolveFunc
|
|
9
|
-
let rejectFunc
|
|
13
|
+
let resolveFunc;
|
|
14
|
+
let rejectFunc;
|
|
10
15
|
|
|
11
16
|
const returnPromise = new Promise((resolve, reject) => {
|
|
12
17
|
resolveFunc = resolve;
|
|
13
18
|
rejectFunc = reject;
|
|
14
19
|
});
|
|
15
20
|
|
|
21
|
+
// Callback parameter handling
|
|
16
22
|
if (
|
|
17
23
|
!callback &&
|
|
18
24
|
(getType(form) === "Function" || getType(form) === "AsyncFunction")
|
|
@@ -21,28 +27,39 @@ const httpPostFactory = function (defaultFuncs, api, ctx) {
|
|
|
21
27
|
form = {};
|
|
22
28
|
}
|
|
23
29
|
|
|
30
|
+
if (typeof callback !== "function") {
|
|
31
|
+
callback = () => {};
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
form = form || {};
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
callback
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
const handleCallback = (err, data) => {
|
|
37
|
+
callback(err, data);
|
|
38
|
+
if (err) {
|
|
39
|
+
rejectFunc(err);
|
|
40
|
+
} else {
|
|
30
41
|
resolveFunc(data);
|
|
31
|
-
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
32
44
|
|
|
33
|
-
const executor = notAPI ? post : defaultFuncs
|
|
45
|
+
const executor = notAPI ? post : (defaultFuncs?.post || post);
|
|
34
46
|
|
|
35
|
-
|
|
47
|
+
// Dynamic execution
|
|
48
|
+
executor(url, ctx.jar, form, ctx.globalOptions || {})
|
|
36
49
|
.then((resData) => {
|
|
37
|
-
let data = resData
|
|
50
|
+
let data = resData?.data ?? resData;
|
|
38
51
|
if (typeof data === "object") {
|
|
39
|
-
|
|
52
|
+
try {
|
|
53
|
+
data = JSON.stringify(data, null, 2);
|
|
54
|
+
} catch {
|
|
55
|
+
// retain object if JSON stringify fails
|
|
56
|
+
}
|
|
40
57
|
}
|
|
41
|
-
|
|
58
|
+
handleCallback(null, data);
|
|
42
59
|
})
|
|
43
60
|
.catch((err) => {
|
|
44
|
-
console.error("httpPost", err);
|
|
45
|
-
|
|
61
|
+
console.error("httpPost error:", err?.message || err);
|
|
62
|
+
handleCallback(err, null);
|
|
46
63
|
});
|
|
47
64
|
|
|
48
65
|
return returnPromise;
|
|
@@ -50,3 +67,4 @@ const httpPostFactory = function (defaultFuncs, api, ctx) {
|
|
|
50
67
|
};
|
|
51
68
|
|
|
52
69
|
module.exports = httpPostFactory;
|
|
70
|
+
|