@simitgroup/simpleapp-generator 1.6.6-v-alpha → 1.6.6-w-alpha
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/ReleaseNote.md
CHANGED
package/package.json
CHANGED
|
@@ -26,18 +26,9 @@ export class RunWebhookService {
|
|
|
26
26
|
if (process.env.WEBHOOK_ONOFF && process.env.WEBHOOK_ONOFF.toLowerCase() == 'on') return await this.runRealtimeWebHook(appuser, documentName, actionName, data);
|
|
27
27
|
else this.logger.warn(`Webhook off, not run for ${documentName}:${actionName}`);
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
* get/delete
|
|
31
|
-
* http://myendpoint.com/{orgid}/invoice/{documentid}/delete
|
|
32
|
-
* http://myendpoint.com/{orgid}/invoice/{documentid}?action=delete
|
|
33
|
-
*
|
|
34
|
-
* post/put/patch
|
|
35
|
-
* http://myendpoint.com/{orgid}/invoice/{documentid}
|
|
36
|
-
* {json body}
|
|
37
|
-
*/
|
|
29
|
+
|
|
38
30
|
async runRealtimeWebHook(appuser: UserContext, documentName: string, actionName: string, data: any) {
|
|
39
31
|
this.logger.debug(`Run webhook ${documentName}, ${actionName}`);
|
|
40
|
-
// console.log(`Run webhook ${documentName}, ${actionName}`);
|
|
41
32
|
|
|
42
33
|
const webhooks = appuser.getWebHooks().filter((wh) => wh.resourceName == documentName && wh.eventType == actionName);
|
|
43
34
|
// console.log(webhooks);
|
|
@@ -50,11 +41,15 @@ export class RunWebhookService {
|
|
|
50
41
|
$uid: appuser.getUid(),
|
|
51
42
|
$resourceName: documentName,
|
|
52
43
|
$actionName: actionName,
|
|
44
|
+
$url: process.env.APP_URL ?? 'http://localhost:8080',
|
|
53
45
|
};
|
|
54
46
|
|
|
55
47
|
//same resource,actionName may have multiple webhook, run 1 by 1
|
|
56
48
|
for (let whNo = 0; whNo < webhooks.length; whNo++) {
|
|
57
49
|
const webhook = webhooks[whNo];
|
|
50
|
+
//danger!! system webhook, only private webhook will allow access
|
|
51
|
+
if (webhook._id.includes('private.')) systemVars['$automationApiKey'] = process.env.DEFAULT_AUTOMATION_API_KEY ?? '';
|
|
52
|
+
|
|
58
53
|
const reqMethod = webhook.requestMethod;
|
|
59
54
|
let webhookUrl = webhook.url;
|
|
60
55
|
// this.logger.verbose(webhook);
|
|
@@ -66,7 +61,7 @@ export class RunWebhookService {
|
|
|
66
61
|
|
|
67
62
|
if (Array.isArray(webhook.headers)) {
|
|
68
63
|
webhook.headers.forEach((h) => {
|
|
69
|
-
let headerValue = h.value;
|
|
64
|
+
let headerValue = h.value ?? '';
|
|
70
65
|
Object.keys(systemVars).forEach((k) => {
|
|
71
66
|
headerValue = headerValue.replace('{{' + k + '}}', systemVars[k]);
|
|
72
67
|
});
|
|
@@ -79,18 +74,19 @@ export class RunWebhookService {
|
|
|
79
74
|
|
|
80
75
|
if (webhook.authentication == 'basic') headers['Authorization'] = 'Basic ' + Buffer.from(webhook.basicAuth.user + ':' + webhook.basicAuth.password).toString('base64');
|
|
81
76
|
|
|
82
|
-
// console.log(webhook)
|
|
83
|
-
|
|
84
77
|
let tries: number = webhook.retryAttemps ?? 0;
|
|
85
|
-
|
|
78
|
+
|
|
86
79
|
tries = tries + 1;
|
|
87
|
-
|
|
80
|
+
|
|
88
81
|
if (tries > this.maxRetries) tries = this.maxRetries;
|
|
82
|
+
const maxtries = tries;
|
|
89
83
|
const options: RequestInit = {
|
|
90
84
|
method: reqMethod.toUpperCase(),
|
|
91
85
|
headers: headers,
|
|
92
86
|
};
|
|
93
87
|
|
|
88
|
+
// console.log("options",options)
|
|
89
|
+
|
|
94
90
|
if (['POST', 'PUT', 'PATCH'].includes(options.method)) {
|
|
95
91
|
options.body = this.prepareBody(appuser, webhook, data, systemVars);
|
|
96
92
|
}
|
|
@@ -101,23 +97,23 @@ export class RunWebhookService {
|
|
|
101
97
|
let req: Response;
|
|
102
98
|
let msg = '';
|
|
103
99
|
let statusCode = 0;
|
|
100
|
+
const tryno = maxtries - tries;
|
|
104
101
|
try {
|
|
105
102
|
req = await fetch(webhookUrl, options);
|
|
106
103
|
statusCode = req.status;
|
|
107
104
|
//sucess, break and no more retry
|
|
105
|
+
msg = `Try: ${tryno}/${maxtries} ${documentName}:${actionName}, ${reqMethod} ${webhookUrl}, ${statusCode},${req.statusText}`;
|
|
108
106
|
if (statusCode >= 200 && statusCode <= 300) {
|
|
109
107
|
//success, continue next webhook
|
|
110
|
-
msg = `webhook triggered successfully for ${documentName}:${actionName}. ${reqMethod} ${webhookUrl}, try:${tries}, ${statusCode},${req.statusText}`;
|
|
111
108
|
this.addLog(appuser, documentName, webhook, actionName, data, options, 'success', statusCode, msg);
|
|
112
109
|
break;
|
|
113
110
|
} else {
|
|
114
111
|
//common error like 404
|
|
115
|
-
msg = `webhook ${statusCode} failed, ${documentName}:${actionName}. ${reqMethod} ${webhookUrl}, try:${tries}, ${statusCode},${req.statusText}`;
|
|
116
112
|
this.addLog(appuser, documentName, webhook, actionName, data, options, 'failed', statusCode, msg);
|
|
117
113
|
}
|
|
118
114
|
} catch (e) {
|
|
119
115
|
// exception, usually server not accessible. use error 500
|
|
120
|
-
msg = `
|
|
116
|
+
msg = `Try: ${tryno}/${maxtries}, error ${documentName}:${actionName} (${webhook._id}) ${e} at ${webhookUrl}`;
|
|
121
117
|
this.addLog(appuser, documentName, webhook, actionName, data, options, 'failed', 500, msg);
|
|
122
118
|
}
|
|
123
119
|
|
|
@@ -138,7 +134,7 @@ export class RunWebhookService {
|
|
|
138
134
|
this.logger.warn(msg);
|
|
139
135
|
}
|
|
140
136
|
const logbody = options.body;
|
|
141
|
-
let body = JSON.stringify(logbody,
|
|
137
|
+
let body = JSON.stringify(logbody, );
|
|
142
138
|
|
|
143
139
|
const whlog: Webhooklog = {
|
|
144
140
|
_id: crypto.randomUUID(),
|
|
@@ -153,7 +149,7 @@ export class RunWebhookService {
|
|
|
153
149
|
actionName: actionName,
|
|
154
150
|
status: status,
|
|
155
151
|
statusCode: statusCode,
|
|
156
|
-
body:
|
|
152
|
+
body: logbody,
|
|
157
153
|
msg: msg,
|
|
158
154
|
};
|
|
159
155
|
|