Skip to content

Commit 4664b5b

Browse files
iamfaranraheeliftikhar5
authored andcommitted
fix linter errors
1 parent 56e5247 commit 4664b5b

File tree

1 file changed

+91
-72
lines changed

1 file changed

+91
-72
lines changed

client/packages/lowcoder/src/comps/queries/httpQuery/sseHttpQuery.tsx

Lines changed: 91 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { trans } from "i18n";
55
import { includes } from "lodash";
66
import { CompAction, MultiBaseComp } from "lowcoder-core";
77
import { keyValueListControl } from "../../controls/keyValueListControl";
8-
import { ParamsJsonControl, ParamsStringControl } from "../../controls/paramsControl";
8+
import { ParamsJsonControl, ParamsStringControl, ParamsControlType } from "../../controls/paramsControl";
99
import { withTypeAndChildrenAbstract } from "../../generators/withType";
1010
import { QueryResult } from "../queryComp";
1111
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "constants/queryConstants";
1212
import { JSONValue } from "util/jsonTypes";
13+
import { FunctionProperty } from "../queryCompUtils";
1314
import {
1415
HttpHeaderPropertyView,
1516
HttpParametersPropertyView,
@@ -81,26 +82,43 @@ export class SseHttpQuery extends SseHttpTmpQuery {
8182
}
8283

8384
override getView() {
85+
const children = this.children;
86+
const params = [
87+
...children.headers.getQueryParams(),
88+
...children.params.getQueryParams(),
89+
...children.bodyFormData.getQueryParams(),
90+
...children.path.getQueryParams(),
91+
...children.body.getQueryParams(),
92+
];
93+
94+
return this.createStreamingQueryView(params);
95+
}
96+
97+
private createStreamingQueryView(params: FunctionProperty[]) {
8498
return async (props: {
99+
queryId: string;
100+
applicationId: string;
101+
applicationPath: string[];
85102
args?: Record<string, unknown>;
103+
variables?: any;
104+
timeout: InstanceType<ParamsControlType>;
86105
callback?: (result: QueryResult) => void;
87106
}): Promise<QueryResult> => {
88-
const children = this.children;
89107

90108
try {
91109
const timer = performance.now();
92110

93-
// Build the complete URL with parameters
94-
const baseUrl = this.buildUrl(props.args);
95-
const headers = this.buildHeaders(props.args);
96-
const method = children.httpMethod.getView();
111+
// Process parameters like toQueryView does
112+
const processedParams = this.processParameters(params, props);
113+
114+
// Build request from processed parameters
115+
const { url, headers, method, body } = this.buildRequestFromParams(processedParams);
97116

98-
// For GET requests, use EventSource API (standard SSE)
117+
// Execute streaming logic
99118
if (method === "GET") {
100-
return this.handleEventSource(baseUrl, headers, props, timer);
119+
return this.handleEventSource(url, headers, props, timer);
101120
} else {
102-
// For POST/PUT/etc, use fetch with streaming response
103-
return this.handleStreamingFetch(baseUrl, headers, method, props, timer);
121+
return this.handleStreamingFetch(url, headers, method, body, props, timer);
104122
}
105123

106124
} catch (error) {
@@ -109,6 +127,67 @@ export class SseHttpQuery extends SseHttpTmpQuery {
109127
};
110128
}
111129

130+
private processParameters(params: FunctionProperty[], props: any) {
131+
let mappedVariables: Array<{key: string, value: string}> = [];
132+
Object.keys(props.variables || {})
133+
.filter(k => k !== "$queryName")
134+
.forEach(key => {
135+
const value = Object.hasOwn(props.variables[key], 'value') ? props.variables[key].value : props.variables[key];
136+
mappedVariables.push({
137+
key: `${key}.value`,
138+
value: value || ""
139+
});
140+
});
141+
142+
return [
143+
...params.filter(param => {
144+
return !mappedVariables.map(v => v.key).includes(param.key);
145+
}).map(({ key, value }) => ({ key, value: value(props.args) })),
146+
...Object.entries(props.timeout.getView()).map(([key, value]) => ({
147+
key,
148+
value: (value as any)(props.args),
149+
})),
150+
...mappedVariables,
151+
];
152+
}
153+
154+
private buildRequestFromParams(processedParams: Array<{key: string, value: any}>) {
155+
debugger;
156+
const paramMap = new Map(processedParams.map(p => [p.key, p.value]));
157+
158+
// Extract URL
159+
const baseUrl = paramMap.get('path') || '';
160+
const url = new URL(baseUrl);
161+
162+
// Add query parameters
163+
Object.entries(paramMap).forEach(([key, value]) => {
164+
if (key.startsWith('params.') && key.endsWith('.value')) {
165+
const paramName = key.replace('params.', '').replace('.value', '');
166+
if (value) url.searchParams.append(paramName, String(value));
167+
}
168+
});
169+
170+
// Build headers
171+
const headers: Record<string, string> = {};
172+
Object.entries(paramMap).forEach(([key, value]) => {
173+
if (key.startsWith('headers.') && key.endsWith('.value')) {
174+
const headerName = key.replace('headers.', '').replace('.value', '');
175+
if (value) headers[headerName] = String(value);
176+
}
177+
});
178+
179+
// Get method and body
180+
const method = paramMap.get('httpMethod') || 'GET';
181+
const bodyType = paramMap.get('bodyType');
182+
let body: string | FormData | undefined;
183+
184+
if (bodyType === 'application/json' || bodyType === 'text/plain') {
185+
body = paramMap.get('body') as string;
186+
}
187+
188+
return { url: url.toString(), headers, method, body };
189+
}
190+
112191
private async handleEventSource(
113192
url: string,
114193
headers: Record<string, string>,
@@ -146,6 +225,7 @@ export class SseHttpQuery extends SseHttpTmpQuery {
146225
url: string,
147226
headers: Record<string, string>,
148227
method: string,
228+
body: string | FormData | undefined,
149229
props: any,
150230
timer: number
151231
): Promise<QueryResult> {
@@ -161,7 +241,7 @@ export class SseHttpQuery extends SseHttpTmpQuery {
161241
'Accept': 'text/event-stream',
162242
'Cache-Control': 'no-cache',
163243
},
164-
body: this.buildRequestBody(props.args),
244+
body,
165245
signal: this.controller.signal,
166246
});
167247

@@ -236,67 +316,6 @@ export class SseHttpQuery extends SseHttpTmpQuery {
236316
}
237317
}
238318

239-
private buildUrl(args?: Record<string, unknown>): string {
240-
const children = this.children;
241-
const basePath = children.path.children.text.getView();
242-
const params = children.params.getView();
243-
244-
// Build URL with parameters
245-
const url = new URL(basePath);
246-
params.forEach((param: any) => {
247-
if (param.key && param.value) {
248-
const value = typeof param.value === 'function' ? param.value(args) : param.value;
249-
url.searchParams.append(param.key, String(value));
250-
}
251-
});
252-
253-
return url.toString();
254-
}
255-
256-
private buildHeaders(args?: Record<string, unknown>): Record<string, string> {
257-
const headers: Record<string, string> = {};
258-
259-
this.children.headers.getView().forEach((header: any) => {
260-
if (header.key && header.value) {
261-
const value = typeof header.value === 'function' ? header.value(args) : header.value;
262-
headers[header.key] = String(value);
263-
}
264-
});
265-
266-
return headers;
267-
}
268-
269-
private buildRequestBody(args?: Record<string, unknown>): string | FormData | undefined {
270-
const bodyType = this.children.bodyType.getView();
271-
272-
switch (bodyType) {
273-
case "application/json":
274-
return this.children.body.children.text.getView() as string;
275-
case "text/plain":
276-
return this.children.body.children.text.getView() as string;
277-
case "application/x-www-form-urlencoded":
278-
const formData = new URLSearchParams();
279-
this.children.bodyFormData.getView().forEach((item: any) => {
280-
if (item.key && item.value) {
281-
const value = typeof item.value === 'function' ? item.value(args) : item.value;
282-
formData.append(item.key, String(value));
283-
}
284-
});
285-
return formData.toString();
286-
case "multipart/form-data":
287-
const multipartData = new FormData();
288-
this.children.bodyFormData.getView().forEach((item: any) => {
289-
if (item.key && item.value) {
290-
const value = typeof item.value === 'function' ? item.value(args) : item.value;
291-
multipartData.append(item.key, String(value));
292-
}
293-
});
294-
return multipartData;
295-
default:
296-
return undefined;
297-
}
298-
}
299-
300319
private createSuccessResponse(data: JSONValue, runTime?: number): QueryResult {
301320
return {
302321
data,

0 commit comments

Comments
 (0)