plugin-document-understanding 1.1.13 → 1.1.15
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/README.md +29 -29
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -7
- package/dist/locale/en-US.json +3 -1
- package/dist/locale/zh-CN.json +3 -1
- package/dist/node_modules/form-data/lib/form_data.js +2 -2
- package/dist/node_modules/form-data/node_modules/es-define-property/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/package.json +81 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/test/index.js +56 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.js +11 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.d.ts +7 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.js +10 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.js +4 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.js +6 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/package.json +80 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/test/index.js +38 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.d.ts +10 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.js +35 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/package.json +78 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/test/index.js +85 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.eslintrc +42 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/index.js +378 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.eslintrc +11 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/package.json +111 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.js +45 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/index.js +22 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/core-js.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/get-own-property-symbols.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/tests.js +58 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/package.json +97 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/test/GetIntrinsic.js +274 -0
- package/dist/node_modules/form-data/node_modules/gopd/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/gopd/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.d.ts +1 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.js +4 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.d.ts +5 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.js +15 -0
- package/dist/node_modules/form-data/node_modules/gopd/package.json +77 -0
- package/dist/node_modules/form-data/node_modules/gopd/test/index.js +36 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/package.json +108 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/index.js +21 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/core-js.js +31 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/get-own-property-symbols.js +30 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/tests.js +15 -0
- package/dist/node_modules/form-data/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/hasown/package.json +91 -0
- package/dist/node_modules/form-data/package.json +1 -1
- package/dist/node_modules/mime-types/index.js +3 -3
- package/dist/node_modules/mime-types/package.json +1 -1
- package/dist/server/actions.js +45 -35
- package/package.json +57 -46
- package/src/client/index.tsx +15 -15
- package/src/client-v2/components/EndpointsTab.tsx +578 -578
- package/src/client-v2/components/JobsTab.tsx +294 -294
- package/src/client-v2/components/PipelinesTab.tsx +962 -962
- package/src/client-v2/components/PluginSettings.tsx +50 -50
- package/src/client-v2/components/ServiceConfigTab.tsx +139 -139
- package/src/client-v2/index.tsx +1 -1
- package/src/client-v2/locale.ts +21 -21
- package/src/client-v2/models/index.ts +12 -12
- package/src/client-v2/plugin.tsx +31 -21
- package/src/client-v2/types.ts +32 -32
- package/src/index.ts +2 -2
- package/src/locale/en-US.json +3 -1
- package/src/locale/zh-CN.json +3 -1
- package/src/server/__tests__/actions.test.ts +121 -98
- package/src/server/__tests__/cluster-safety.test.ts +259 -259
- package/src/server/__tests__/document-understanding-service.test.ts +82 -82
- package/src/server/__tests__/dugate-integration.test.ts +221 -221
- package/src/server/__tests__/smoke.test.ts +78 -55
- package/src/server/__tests__/upload-security.test.ts +128 -0
- package/src/server/actions.ts +53 -51
- package/src/server/collections/config.ts +18 -18
- package/src/server/collections/endpoints.ts +33 -33
- package/src/server/collections/jobs.ts +42 -42
- package/src/server/collections/pipeline-steps.ts +27 -27
- package/src/server/collections/pipelines.ts +19 -19
- package/src/server/dugate-seed.ts +491 -491
- package/src/server/index.ts +1 -1
- package/src/server/plugin.ts +143 -143
- package/src/server/services/AsyncJobManager.ts +238 -238
- package/src/server/services/DocumentUnderstandingService.ts +332 -332
- package/src/server/services/ExternalApiClient.ts +178 -178
- package/src/server/services/PipelineExecutor.ts +593 -593
- package/src/server/tools/document-understanding-tool.ts +181 -181
- package/src/server/types.ts +90 -90
|
@@ -1,178 +1,178 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
-
import { ServiceConfig, EndpointDef } from '../types';
|
|
3
|
-
import FormData from 'form-data';
|
|
4
|
-
|
|
5
|
-
export interface FileInput {
|
|
6
|
-
fieldName: string;
|
|
7
|
-
buffer: Buffer;
|
|
8
|
-
filename: string;
|
|
9
|
-
mimeType: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ApiCallOptions {
|
|
13
|
-
endpoint: EndpointDef;
|
|
14
|
-
body?: Record<string, any>;
|
|
15
|
-
files?: FileInput[];
|
|
16
|
-
overrideHeaders?: Record<string, string>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class ExternalApiClient {
|
|
20
|
-
private config: ServiceConfig;
|
|
21
|
-
private client: AxiosInstance;
|
|
22
|
-
private static readonly HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
23
|
-
|
|
24
|
-
constructor(config: ServiceConfig) {
|
|
25
|
-
this.config = config;
|
|
26
|
-
this.client = axios.create({
|
|
27
|
-
baseURL: config.baseUrl,
|
|
28
|
-
timeout: config.defaultTimeout,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
private buildAuthHeaders(): Record<string, string> {
|
|
33
|
-
const headers: Record<string, string> = {};
|
|
34
|
-
if (!this.config.authType || this.config.authType === 'none') {
|
|
35
|
-
return headers;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (this.config.authType === 'bearer' && this.config.authKey) {
|
|
39
|
-
headers['Authorization'] = `Bearer ${this.config.authKey}`;
|
|
40
|
-
} else if (this.config.authType === 'api_key' && this.config.authKey) {
|
|
41
|
-
// Assuming api_key is sent in Authorization header or x-api-key based on common patterns.
|
|
42
|
-
// Usually authHeaderName handles custom API keys, but we'll default to x-api-key if not provided.
|
|
43
|
-
const name = this.config.authHeaderName || 'x-api-key';
|
|
44
|
-
headers[name] = this.config.authKey;
|
|
45
|
-
} else if (this.config.authType === 'custom_header' && this.config.authHeaderName && this.config.authKey) {
|
|
46
|
-
headers[this.config.authHeaderName] = this.config.authKey;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return headers;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private sanitizeHeaders(headers?: Record<string, any>): Record<string, string> {
|
|
53
|
-
const normalized: Record<string, string> = {};
|
|
54
|
-
if (!headers || typeof headers !== 'object' || Array.isArray(headers)) {
|
|
55
|
-
return normalized;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
for (const [rawName, rawValue] of Object.entries(headers)) {
|
|
59
|
-
const name = rawName.trim();
|
|
60
|
-
if (!name) continue;
|
|
61
|
-
if (!ExternalApiClient.HEADER_NAME_RE.test(name)) {
|
|
62
|
-
throw new Error(`Invalid HTTP header name: ${rawName}`);
|
|
63
|
-
}
|
|
64
|
-
if (rawValue === undefined || rawValue === null) continue;
|
|
65
|
-
normalized[name] = String(rawValue);
|
|
66
|
-
}
|
|
67
|
-
return normalized;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Appends the endpoint discriminator (if configured) to the request body so
|
|
72
|
-
* DUGate-style gateways can route to the correct sub-case/profile.
|
|
73
|
-
*/
|
|
74
|
-
private withDiscriminator(endpoint: EndpointDef, body?: Record<string, any>): Record<string, any> | undefined {
|
|
75
|
-
const field = endpoint.discriminatorField?.trim();
|
|
76
|
-
if (!field || !endpoint.discriminatorValue) {
|
|
77
|
-
return body;
|
|
78
|
-
}
|
|
79
|
-
const merged: Record<string, any> = { ...(body || {}) };
|
|
80
|
-
if (merged[field] !== undefined && merged[field] !== endpoint.discriminatorValue) {
|
|
81
|
-
throw new Error(
|
|
82
|
-
`Discriminator field '${field}' is locked by endpoint configuration (expected '${endpoint.discriminatorValue}', got '${merged[field]}').`,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
merged[field] = endpoint.discriminatorValue;
|
|
86
|
-
return merged;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Appends the DUGate sync flag (?sync=true) for endpoints configured with
|
|
91
|
-
* `syncQueryParam`. Kept separate from other query parameters so GET/DELETE
|
|
92
|
-
* params in `body` are not polluted by the discriminator logic.
|
|
93
|
-
*/
|
|
94
|
-
private syncQueryParams(endpoint: EndpointDef): Record<string, string> {
|
|
95
|
-
const param = endpoint.syncQueryParam?.trim();
|
|
96
|
-
if (!param) return {};
|
|
97
|
-
return { [param]: 'true' };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async call(options: ApiCallOptions): Promise<{ status: number; data: any; headers: any }> {
|
|
101
|
-
const { endpoint, body, files, overrideHeaders } = options;
|
|
102
|
-
|
|
103
|
-
const url = endpoint.subpath;
|
|
104
|
-
const method = endpoint.method.toLowerCase();
|
|
105
|
-
|
|
106
|
-
let requestData: any = body;
|
|
107
|
-
let headers: Record<string, string> = {
|
|
108
|
-
...this.sanitizeHeaders(this.buildAuthHeaders()),
|
|
109
|
-
...this.sanitizeHeaders(endpoint.customHeaders),
|
|
110
|
-
...this.sanitizeHeaders(overrideHeaders),
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
if (endpoint.fileInputMode === 'multipart' && files && files.length > 0) {
|
|
114
|
-
const formData = new FormData();
|
|
115
|
-
const bodyWithDiscriminator = this.withDiscriminator(endpoint, body);
|
|
116
|
-
if (bodyWithDiscriminator) {
|
|
117
|
-
for (const [key, value] of Object.entries(bodyWithDiscriminator)) {
|
|
118
|
-
formData.append(key, typeof value === 'object' ? JSON.stringify(value) : String(value));
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
for (const file of files) {
|
|
122
|
-
formData.append(file.fieldName, file.buffer, {
|
|
123
|
-
filename: file.filename,
|
|
124
|
-
contentType: file.mimeType,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
requestData = formData;
|
|
128
|
-
headers = { ...headers, ...formData.getHeaders() };
|
|
129
|
-
} else if (endpoint.fileInputMode === 'base64' && files && files.length > 0) {
|
|
130
|
-
// Place base64 files under their fieldName keys within the body.
|
|
131
|
-
// PipelineExecutor's inputMapping should map $files[n].base64 to the correct body path.
|
|
132
|
-
// Here we nest them under a `_files` key to avoid overwriting body fields.
|
|
133
|
-
const base64Files: Record<string, { data: string; filename: string; mimeType: string }> = {};
|
|
134
|
-
for (const f of files) {
|
|
135
|
-
base64Files[f.fieldName] = {
|
|
136
|
-
data: f.buffer.toString('base64'),
|
|
137
|
-
filename: f.filename,
|
|
138
|
-
mimeType: f.mimeType,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
requestData = { ...this.withDiscriminator(endpoint, body), _files: base64Files };
|
|
142
|
-
headers['Content-Type'] = 'application/json';
|
|
143
|
-
} else {
|
|
144
|
-
requestData = this.withDiscriminator(endpoint, body);
|
|
145
|
-
headers['Content-Type'] = 'application/json';
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
const isBodyless = ['get', 'delete'].includes(method);
|
|
150
|
-
const response: AxiosResponse = await this.client.request({
|
|
151
|
-
url,
|
|
152
|
-
method,
|
|
153
|
-
data: isBodyless ? undefined : requestData,
|
|
154
|
-
params: isBodyless
|
|
155
|
-
? { ...(requestData || {}), ...this.syncQueryParams(endpoint) }
|
|
156
|
-
: this.syncQueryParams(endpoint),
|
|
157
|
-
headers,
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
status: response.status,
|
|
162
|
-
data: response.data,
|
|
163
|
-
headers: response.headers,
|
|
164
|
-
};
|
|
165
|
-
} catch (error: any) {
|
|
166
|
-
if (error.response) {
|
|
167
|
-
throw new Error(`API Error [${error.response.status}]: ${JSON.stringify(error.response.data)}`);
|
|
168
|
-
}
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async get(url: string, overrideHeaders?: Record<string, string>): Promise<any> {
|
|
174
|
-
const headers = { ...this.buildAuthHeaders(), ...(overrideHeaders || {}) };
|
|
175
|
-
const response = await this.client.get(url, { headers });
|
|
176
|
-
return response.data;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
1
|
+
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
+
import { ServiceConfig, EndpointDef } from '../types';
|
|
3
|
+
import FormData from 'form-data';
|
|
4
|
+
|
|
5
|
+
export interface FileInput {
|
|
6
|
+
fieldName: string;
|
|
7
|
+
buffer: Buffer;
|
|
8
|
+
filename: string;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ApiCallOptions {
|
|
13
|
+
endpoint: EndpointDef;
|
|
14
|
+
body?: Record<string, any>;
|
|
15
|
+
files?: FileInput[];
|
|
16
|
+
overrideHeaders?: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class ExternalApiClient {
|
|
20
|
+
private config: ServiceConfig;
|
|
21
|
+
private client: AxiosInstance;
|
|
22
|
+
private static readonly HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
23
|
+
|
|
24
|
+
constructor(config: ServiceConfig) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.client = axios.create({
|
|
27
|
+
baseURL: config.baseUrl,
|
|
28
|
+
timeout: config.defaultTimeout,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private buildAuthHeaders(): Record<string, string> {
|
|
33
|
+
const headers: Record<string, string> = {};
|
|
34
|
+
if (!this.config.authType || this.config.authType === 'none') {
|
|
35
|
+
return headers;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (this.config.authType === 'bearer' && this.config.authKey) {
|
|
39
|
+
headers['Authorization'] = `Bearer ${this.config.authKey}`;
|
|
40
|
+
} else if (this.config.authType === 'api_key' && this.config.authKey) {
|
|
41
|
+
// Assuming api_key is sent in Authorization header or x-api-key based on common patterns.
|
|
42
|
+
// Usually authHeaderName handles custom API keys, but we'll default to x-api-key if not provided.
|
|
43
|
+
const name = this.config.authHeaderName || 'x-api-key';
|
|
44
|
+
headers[name] = this.config.authKey;
|
|
45
|
+
} else if (this.config.authType === 'custom_header' && this.config.authHeaderName && this.config.authKey) {
|
|
46
|
+
headers[this.config.authHeaderName] = this.config.authKey;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return headers;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private sanitizeHeaders(headers?: Record<string, any>): Record<string, string> {
|
|
53
|
+
const normalized: Record<string, string> = {};
|
|
54
|
+
if (!headers || typeof headers !== 'object' || Array.isArray(headers)) {
|
|
55
|
+
return normalized;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const [rawName, rawValue] of Object.entries(headers)) {
|
|
59
|
+
const name = rawName.trim();
|
|
60
|
+
if (!name) continue;
|
|
61
|
+
if (!ExternalApiClient.HEADER_NAME_RE.test(name)) {
|
|
62
|
+
throw new Error(`Invalid HTTP header name: ${rawName}`);
|
|
63
|
+
}
|
|
64
|
+
if (rawValue === undefined || rawValue === null) continue;
|
|
65
|
+
normalized[name] = String(rawValue);
|
|
66
|
+
}
|
|
67
|
+
return normalized;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Appends the endpoint discriminator (if configured) to the request body so
|
|
72
|
+
* DUGate-style gateways can route to the correct sub-case/profile.
|
|
73
|
+
*/
|
|
74
|
+
private withDiscriminator(endpoint: EndpointDef, body?: Record<string, any>): Record<string, any> | undefined {
|
|
75
|
+
const field = endpoint.discriminatorField?.trim();
|
|
76
|
+
if (!field || !endpoint.discriminatorValue) {
|
|
77
|
+
return body;
|
|
78
|
+
}
|
|
79
|
+
const merged: Record<string, any> = { ...(body || {}) };
|
|
80
|
+
if (merged[field] !== undefined && merged[field] !== endpoint.discriminatorValue) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Discriminator field '${field}' is locked by endpoint configuration (expected '${endpoint.discriminatorValue}', got '${merged[field]}').`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
merged[field] = endpoint.discriminatorValue;
|
|
86
|
+
return merged;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Appends the DUGate sync flag (?sync=true) for endpoints configured with
|
|
91
|
+
* `syncQueryParam`. Kept separate from other query parameters so GET/DELETE
|
|
92
|
+
* params in `body` are not polluted by the discriminator logic.
|
|
93
|
+
*/
|
|
94
|
+
private syncQueryParams(endpoint: EndpointDef): Record<string, string> {
|
|
95
|
+
const param = endpoint.syncQueryParam?.trim();
|
|
96
|
+
if (!param) return {};
|
|
97
|
+
return { [param]: 'true' };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async call(options: ApiCallOptions): Promise<{ status: number; data: any; headers: any }> {
|
|
101
|
+
const { endpoint, body, files, overrideHeaders } = options;
|
|
102
|
+
|
|
103
|
+
const url = endpoint.subpath;
|
|
104
|
+
const method = endpoint.method.toLowerCase();
|
|
105
|
+
|
|
106
|
+
let requestData: any = body;
|
|
107
|
+
let headers: Record<string, string> = {
|
|
108
|
+
...this.sanitizeHeaders(this.buildAuthHeaders()),
|
|
109
|
+
...this.sanitizeHeaders(endpoint.customHeaders),
|
|
110
|
+
...this.sanitizeHeaders(overrideHeaders),
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (endpoint.fileInputMode === 'multipart' && files && files.length > 0) {
|
|
114
|
+
const formData = new FormData();
|
|
115
|
+
const bodyWithDiscriminator = this.withDiscriminator(endpoint, body);
|
|
116
|
+
if (bodyWithDiscriminator) {
|
|
117
|
+
for (const [key, value] of Object.entries(bodyWithDiscriminator)) {
|
|
118
|
+
formData.append(key, typeof value === 'object' ? JSON.stringify(value) : String(value));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const file of files) {
|
|
122
|
+
formData.append(file.fieldName, file.buffer, {
|
|
123
|
+
filename: file.filename,
|
|
124
|
+
contentType: file.mimeType,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
requestData = formData;
|
|
128
|
+
headers = { ...headers, ...formData.getHeaders() };
|
|
129
|
+
} else if (endpoint.fileInputMode === 'base64' && files && files.length > 0) {
|
|
130
|
+
// Place base64 files under their fieldName keys within the body.
|
|
131
|
+
// PipelineExecutor's inputMapping should map $files[n].base64 to the correct body path.
|
|
132
|
+
// Here we nest them under a `_files` key to avoid overwriting body fields.
|
|
133
|
+
const base64Files: Record<string, { data: string; filename: string; mimeType: string }> = {};
|
|
134
|
+
for (const f of files) {
|
|
135
|
+
base64Files[f.fieldName] = {
|
|
136
|
+
data: f.buffer.toString('base64'),
|
|
137
|
+
filename: f.filename,
|
|
138
|
+
mimeType: f.mimeType,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
requestData = { ...this.withDiscriminator(endpoint, body), _files: base64Files };
|
|
142
|
+
headers['Content-Type'] = 'application/json';
|
|
143
|
+
} else {
|
|
144
|
+
requestData = this.withDiscriminator(endpoint, body);
|
|
145
|
+
headers['Content-Type'] = 'application/json';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const isBodyless = ['get', 'delete'].includes(method);
|
|
150
|
+
const response: AxiosResponse = await this.client.request({
|
|
151
|
+
url,
|
|
152
|
+
method,
|
|
153
|
+
data: isBodyless ? undefined : requestData,
|
|
154
|
+
params: isBodyless
|
|
155
|
+
? { ...(requestData || {}), ...this.syncQueryParams(endpoint) }
|
|
156
|
+
: this.syncQueryParams(endpoint),
|
|
157
|
+
headers,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
status: response.status,
|
|
162
|
+
data: response.data,
|
|
163
|
+
headers: response.headers,
|
|
164
|
+
};
|
|
165
|
+
} catch (error: any) {
|
|
166
|
+
if (error.response) {
|
|
167
|
+
throw new Error(`API Error [${error.response.status}]: ${JSON.stringify(error.response.data)}`);
|
|
168
|
+
}
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async get(url: string, overrideHeaders?: Record<string, string>): Promise<any> {
|
|
174
|
+
const headers = { ...this.buildAuthHeaders(), ...(overrideHeaders || {}) };
|
|
175
|
+
const response = await this.client.get(url, { headers });
|
|
176
|
+
return response.data;
|
|
177
|
+
}
|
|
178
|
+
}
|