1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-13 15:10:06 +00:00

feat(compute-api): implement job execution via compute API

This commit is contained in:
Krishna Acondy
2020-07-16 09:06:24 +01:00
parent 92504b0c16
commit 7d84033ad4
11 changed files with 584 additions and 280 deletions

View File

@@ -0,0 +1,33 @@
import { convertToCSV } from "./convertToCsv";
import { splitChunks } from "./splitChunks";
export const formatDataForRequest = (data: any) => {
const sasjsTables = [];
let tableCounter = 0;
const result: any = {};
for (const tableName in data) {
tableCounter++;
sasjsTables.push(tableName);
const csv = convertToCSV(data[tableName]);
if (csv === "ERROR: LARGE STRING LENGTH") {
throw new Error(
"The max length of a string value in SASjs is 32765 characters."
);
}
// if csv has length more then 16k, send in chunks
if (csv.length > 16000) {
const csvChunks = splitChunks(csv);
// append chunks to form data with same key
result[`sasjs${tableCounter}data0`] = csvChunks.length;
csvChunks.forEach((chunk, index) => {
result[`sasjs${tableCounter}data${index + 1}`] = chunk;
});
} else {
result[`sasjs${tableCounter}data`] = csv;
}
}
result["sasjs_tables"] = sasjsTables.join(" ");
return result;
};

View File

@@ -5,11 +5,12 @@ export async function makeRequest<T>(
request: RequestInit,
callback: (value: CsrfToken) => any,
contentType: "text" | "json" = "json"
): Promise<T> {
): Promise<{ result: T; etag: string | null }> {
const responseTransform =
contentType === "json"
? (res: Response) => res.json()
: (res: Response) => res.text();
let etag = null;
const result = await fetch(url, request).then((response) => {
if (!response.ok) {
if (response.status === 403) {
@@ -26,12 +27,16 @@ export async function makeRequest<T>(
...request,
headers: { ...request.headers, [tokenHeader]: token },
};
return fetch(url, retryRequest).then(responseTransform);
return fetch(url, retryRequest).then((res) => {
etag = res.headers.get("ETag");
return responseTransform(res);
});
}
}
} else {
etag = response.headers.get("ETag");
return responseTransform(response);
}
});
return result;
return { result, etag };
}