evaengine 1.0.2 → 1.0.4
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/.ai/defaults/ai-coding.md +47 -0
- package/.ai/defaults/preferences.md +45 -0
- package/.ai/memory.md +38 -0
- package/.ai/workflow/design-review.md +38 -0
- package/.ai/workflow/end.md +34 -0
- package/.ai/workflow/start.md +34 -0
- package/.ai/workflow/sync.md +24 -0
- package/README.md +384 -54
- package/bin/engine +1 -1
- package/package.json +2 -44
- package/src/commands/make_entity.js +11 -11
- package/src/di.js +13 -1
- package/src/engine.js +12 -9
- package/src/index.js +1 -3
- package/src/middlewares/session.js +6 -1
- package/src/middlewares/trace.js +26 -1
- package/src/middlewares/view_cache.js +3 -2
- package/src/services/config.js +10 -6
- package/src/services/http_client.js +3 -94
- package/src/services/jwt_token_kong.js +2 -2
- package/src/services/redis.js +9 -1
- package/src/swagger/index.js +1 -6
- package/src/utils/cron.js +208 -0
- package/src/utils/pagination.js +1 -1
- package/src/utils/request_client.js +179 -0
- package/.ai/analysis.md +0 -657
- package/.ai/architecture/runtime.md +0 -504
- package/.ai/contracts/public-api.md +0 -447
- package/.ai/external-project-guide.md +0 -248
- package/.ai/vision.md +0 -314
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A request-promise-native compatible HTTP client built on the global fetch.
|
|
3
|
+
*
|
|
4
|
+
* Keeps the option object and error shape used by the legacy `request`
|
|
5
|
+
* ecosystem so callers of HttpClient/RestClient see no interface change:
|
|
6
|
+
* - resolves with the response body (or the full response with
|
|
7
|
+
* `resolveWithFullResponse`)
|
|
8
|
+
* - rejects with an error carrying `.name` (`StatusCodeError`/`RequestError`),
|
|
9
|
+
* `.statusCode`, `.response` and `.request`
|
|
10
|
+
* - supports `url`, `method`, `headers`, `body`, `json`, `qs`, `form`,
|
|
11
|
+
* `formData`, `simple`, `resolveWithFullResponse` and `timeout`
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const MAX_BODY_LENGTH = parseInt(process.env.MAX_REQUEST_DEBUG_BODY, 10) || 3000;
|
|
15
|
+
const TOO_LONG_BODY = '____TLDR____';
|
|
16
|
+
|
|
17
|
+
let debugId = 0;
|
|
18
|
+
|
|
19
|
+
const toPlainHeaders = (headers = {}) => {
|
|
20
|
+
const result = {};
|
|
21
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
22
|
+
result[key] = String(value);
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const buildQueryString = (qs) => {
|
|
28
|
+
if (qs === undefined || qs === null) {
|
|
29
|
+
return '';
|
|
30
|
+
}
|
|
31
|
+
if (typeof qs === 'string') {
|
|
32
|
+
return qs.startsWith('?') ? qs : `?${qs}`;
|
|
33
|
+
}
|
|
34
|
+
return `?${new URLSearchParams(qs).toString()}`;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const buildBody = (params) => {
|
|
38
|
+
if (params.formData) {
|
|
39
|
+
const form = new FormData();
|
|
40
|
+
Object.entries(params.formData).forEach(([key, value]) => {
|
|
41
|
+
form.append(key, value);
|
|
42
|
+
});
|
|
43
|
+
return { body: form, contentType: null };
|
|
44
|
+
}
|
|
45
|
+
if (params.form) {
|
|
46
|
+
return {
|
|
47
|
+
body: new URLSearchParams(params.form).toString(),
|
|
48
|
+
contentType: 'application/x-www-form-urlencoded'
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//The legacy `json` option also accepts a value used as the JSON body
|
|
52
|
+
if (params.json !== undefined && params.json !== false && params.json !== true) {
|
|
53
|
+
return { body: JSON.stringify(params.json), contentType: 'application/json' };
|
|
54
|
+
}
|
|
55
|
+
if (params.json === true && params.body !== undefined) {
|
|
56
|
+
return { body: JSON.stringify(params.body), contentType: 'application/json' };
|
|
57
|
+
}
|
|
58
|
+
return { body: params.body, contentType: null };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const truncateBody = (body) => {
|
|
62
|
+
if (typeof body !== 'string') {
|
|
63
|
+
return body ? String(body) : '';
|
|
64
|
+
}
|
|
65
|
+
return body.length > MAX_BODY_LENGTH ? TOO_LONG_BODY : body;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a request-promise-native compatible callable client.
|
|
70
|
+
* @param {Logger} [logger] when given, verbose request/response logs are emitted
|
|
71
|
+
* @returns {function}
|
|
72
|
+
*/
|
|
73
|
+
export const createRequestClient = (logger) => {
|
|
74
|
+
const request = (params = {}) => new Promise((resolve, reject) => {
|
|
75
|
+
const {
|
|
76
|
+
url,
|
|
77
|
+
method: rawMethod = 'GET',
|
|
78
|
+
headers = {},
|
|
79
|
+
resolveWithFullResponse = false,
|
|
80
|
+
simple = true,
|
|
81
|
+
timeout = 0,
|
|
82
|
+
qs
|
|
83
|
+
} = params;
|
|
84
|
+
|
|
85
|
+
const method = String(rawMethod).toUpperCase();
|
|
86
|
+
const targetUrl = `${url}${buildQueryString(qs)}`;
|
|
87
|
+
const { body, contentType } = buildBody(params);
|
|
88
|
+
|
|
89
|
+
const requestHeaders = toPlainHeaders(headers);
|
|
90
|
+
if (contentType
|
|
91
|
+
&& !Object.keys(requestHeaders).some(key => key.toLowerCase() === 'content-type')) {
|
|
92
|
+
requestHeaders['Content-Type'] = contentType;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const abortController = new AbortController();
|
|
96
|
+
const timer = timeout > 0
|
|
97
|
+
? setTimeout(() => abortController.abort(new Error('Request timed out')), timeout)
|
|
98
|
+
: null;
|
|
99
|
+
|
|
100
|
+
const requestForDump = {
|
|
101
|
+
method,
|
|
102
|
+
uri: {
|
|
103
|
+
protocol: new URL(targetUrl).protocol,
|
|
104
|
+
href: targetUrl
|
|
105
|
+
},
|
|
106
|
+
headers: requestHeaders,
|
|
107
|
+
req: {},
|
|
108
|
+
_json: params.json !== undefined && params.json !== false
|
|
109
|
+
? true : body !== undefined,
|
|
110
|
+
formData: params.formData || null,
|
|
111
|
+
body: params.formData
|
|
112
|
+
? new URLSearchParams(params.formData).toString()
|
|
113
|
+
: body
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const id = ++debugId;
|
|
117
|
+
if (logger) {
|
|
118
|
+
logger.verbose('[HTTP_REQUEST_%s] [%s %s] [REQ_HEADERS: %s] [REQ_BODY: %s]', id,
|
|
119
|
+
method, targetUrl, JSON.stringify(requestHeaders), truncateBody(body));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fetch(targetUrl, {
|
|
123
|
+
method,
|
|
124
|
+
headers: requestHeaders,
|
|
125
|
+
body,
|
|
126
|
+
signal: abortController.signal,
|
|
127
|
+
redirect: 'follow'
|
|
128
|
+
}).then(async (response) => {
|
|
129
|
+
if (timer) {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
}
|
|
132
|
+
const rawBody = await response.text();
|
|
133
|
+
let parsedBody = rawBody;
|
|
134
|
+
if (params.json !== undefined && params.json !== false) {
|
|
135
|
+
try {
|
|
136
|
+
parsedBody = JSON.parse(rawBody);
|
|
137
|
+
} catch {
|
|
138
|
+
parsedBody = rawBody;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const responseForDump = {
|
|
143
|
+
statusCode: response.status,
|
|
144
|
+
statusMessage: response.statusText,
|
|
145
|
+
headers: toPlainHeaders(Object.fromEntries(response.headers.entries())),
|
|
146
|
+
body: parsedBody,
|
|
147
|
+
request: requestForDump
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
if (logger) {
|
|
151
|
+
logger.verbose('[HTTP_RESPONSE_%s] [%s %s] [%s] [RES_HEADERS: %s] [RES_BODY: %s]', id,
|
|
152
|
+
method, targetUrl, response.status, JSON.stringify(responseForDump.headers),
|
|
153
|
+
truncateBody(parsedBody));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (response.status >= 400 && simple) {
|
|
157
|
+
const error = new Error(`Response status ${response.status}`);
|
|
158
|
+
error.name = 'StatusCodeError';
|
|
159
|
+
error.statusCode = response.status;
|
|
160
|
+
error.response = responseForDump;
|
|
161
|
+
error.request = requestForDump;
|
|
162
|
+
reject(error);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
resolve(resolveWithFullResponse ? responseForDump : parsedBody);
|
|
167
|
+
}).catch((err) => {
|
|
168
|
+
if (timer) {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
}
|
|
171
|
+
const error = new Error(err.message);
|
|
172
|
+
error.name = 'RequestError';
|
|
173
|
+
error.request = requestForDump;
|
|
174
|
+
reject(error);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return request;
|
|
179
|
+
};
|