@abapify/adt-client 0.4.33 → 0.4.38
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 +19 -265
- package/dist/index.d.mts +5 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +202 -146
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,12 @@ import { dirname, join } from "node:path";
|
|
|
7
7
|
|
|
8
8
|
// src/utils/session.ts
|
|
9
9
|
/**
|
|
10
|
+
* Push `current` onto `cookies` when it carries a non-empty name=value pair.
|
|
11
|
+
*/
|
|
12
|
+
function pushCookieIfNonEmpty(cookies, current) {
|
|
13
|
+
if (current.trim()) cookies.push(current.trim());
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
10
16
|
* Cookie Store - Manages HTTP cookies for stateful sessions
|
|
11
17
|
*/
|
|
12
18
|
var CookieStore = class {
|
|
@@ -32,7 +38,7 @@ var CookieStore = class {
|
|
|
32
38
|
for (let i = 0; i < setCookieHeader.length; i++) {
|
|
33
39
|
const char = setCookieHeader[i];
|
|
34
40
|
if (char === "," && !inExpires) {
|
|
35
|
-
|
|
41
|
+
pushCookieIfNonEmpty(cookies, current);
|
|
36
42
|
current = "";
|
|
37
43
|
} else {
|
|
38
44
|
current += char;
|
|
@@ -40,7 +46,7 @@ var CookieStore = class {
|
|
|
40
46
|
if (inExpires && (char === ";" || i === setCookieHeader.length - 1)) inExpires = false;
|
|
41
47
|
}
|
|
42
48
|
}
|
|
43
|
-
|
|
49
|
+
pushCookieIfNonEmpty(cookies, current);
|
|
44
50
|
return cookies;
|
|
45
51
|
}
|
|
46
52
|
/**
|
|
@@ -94,7 +100,7 @@ var CsrfTokenManager = class {
|
|
|
94
100
|
if (!xsrfEntry) return;
|
|
95
101
|
const cookieValue = xsrfEntry[1];
|
|
96
102
|
const decodedToken = decodeURIComponent(cookieValue);
|
|
97
|
-
const tokenMatch =
|
|
103
|
+
const tokenMatch = /^([A-Za-z0-9+/_-]+=*)/.exec(decodedToken);
|
|
98
104
|
const actualToken = tokenMatch ? tokenMatch[1] : decodedToken;
|
|
99
105
|
if (actualToken && actualToken !== "Required" && actualToken !== "fetch" && actualToken !== "Fetch") return actualToken;
|
|
100
106
|
}
|
|
@@ -286,7 +292,7 @@ var SessionManager = class SessionManager {
|
|
|
286
292
|
}
|
|
287
293
|
}
|
|
288
294
|
extractTrustedSessionPath({ baseUrl, body }) {
|
|
289
|
-
const sessionPath =
|
|
295
|
+
const sessionPath = /href="([^"]*\/sessions\/[^"]*)"/.exec(body)?.[1];
|
|
290
296
|
if (!sessionPath || !this.isTrustedSessionPath({
|
|
291
297
|
baseUrl,
|
|
292
298
|
sessionPath
|
|
@@ -618,6 +624,23 @@ var AdtError = class AdtError extends Error {
|
|
|
618
624
|
}
|
|
619
625
|
};
|
|
620
626
|
/**
|
|
627
|
+
* Map a parsed schema exception to the flat AdtExceptionData shape.
|
|
628
|
+
* Note: ts-xsd uses $value for simpleContent text values.
|
|
629
|
+
*/
|
|
630
|
+
function mapExceptionData(exc) {
|
|
631
|
+
const result = {};
|
|
632
|
+
if (exc.namespace?.id) result.namespace = exc.namespace.id;
|
|
633
|
+
if (exc.type?.id) result.type = exc.type.id;
|
|
634
|
+
if (exc.message?.$value) result.message = exc.message.$value;
|
|
635
|
+
if (exc.localizedMessage?.$value) result.localizedMessage = exc.localizedMessage.$value;
|
|
636
|
+
if (exc.properties?.entry) {
|
|
637
|
+
result.properties = {};
|
|
638
|
+
const entries = Array.isArray(exc.properties.entry) ? exc.properties.entry : [exc.properties.entry];
|
|
639
|
+
for (const entry of entries) if (entry.key && entry.$value) result.properties[entry.key] = entry.$value;
|
|
640
|
+
}
|
|
641
|
+
return result;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
621
644
|
* Parse ADT exception XML into structured data
|
|
622
645
|
*
|
|
623
646
|
* Uses the generated exception schema from @abapify/adt-schemas for type-safe parsing.
|
|
@@ -627,16 +650,7 @@ function parseAdtException(xml) {
|
|
|
627
650
|
try {
|
|
628
651
|
const exc = exception.parse(xml).exception;
|
|
629
652
|
if (!exc) return;
|
|
630
|
-
const result =
|
|
631
|
-
if (exc.namespace?.id) result.namespace = exc.namespace.id;
|
|
632
|
-
if (exc.type?.id) result.type = exc.type.id;
|
|
633
|
-
if (exc.message?.$value) result.message = exc.message.$value;
|
|
634
|
-
if (exc.localizedMessage?.$value) result.localizedMessage = exc.localizedMessage.$value;
|
|
635
|
-
if (exc.properties?.entry) {
|
|
636
|
-
result.properties = {};
|
|
637
|
-
const entries = Array.isArray(exc.properties.entry) ? exc.properties.entry : [exc.properties.entry];
|
|
638
|
-
for (const entry of entries) if (entry.key && entry.$value) result.properties[entry.key] = entry.$value;
|
|
639
|
-
}
|
|
653
|
+
const result = mapExceptionData(exc);
|
|
640
654
|
if (!result.namespace && !result.type && !result.message && !result.localizedMessage) return;
|
|
641
655
|
return result;
|
|
642
656
|
} catch {
|
|
@@ -752,9 +766,21 @@ function executionAbortController() {
|
|
|
752
766
|
dispose: () => executionSignal.removeEventListener("abort", abortFromExecution)
|
|
753
767
|
};
|
|
754
768
|
}
|
|
769
|
+
/** Parse the response body according to its content type. */
|
|
770
|
+
function parseResponseBody(contentType, rawText, serializableSchema) {
|
|
771
|
+
if (contentType.includes("application/json") || contentType.includes("+json")) return JSON.parse(rawText);
|
|
772
|
+
if (contentType.includes("text/") || contentType.includes("xml")) {
|
|
773
|
+
if (serializableSchema && contentType.includes("xml")) return serializableSchema.parse(rawText);
|
|
774
|
+
}
|
|
775
|
+
return rawText;
|
|
776
|
+
}
|
|
755
777
|
function createAdtAdapter(config) {
|
|
756
|
-
const { baseUrl, username, password, cookieHeader, authorizationHeader, client, language, logger, plugins = [], onSessionExpired, headersTimeoutMs } = config;
|
|
757
|
-
const dispatcher = headersTimeoutMs === void 0 ? void 0 : new Agent({
|
|
778
|
+
const { baseUrl, username, password, cookieHeader, authorizationHeader, client, language, logger, plugins = [], onSessionExpired, headersTimeoutMs, connectTimeoutMs, insecure } = config;
|
|
779
|
+
const dispatcher = headersTimeoutMs === void 0 && connectTimeoutMs === void 0 && insecure !== true ? void 0 : new Agent({
|
|
780
|
+
headersTimeout: headersTimeoutMs,
|
|
781
|
+
connectTimeout: connectTimeoutMs,
|
|
782
|
+
connect: insecure ? { rejectUnauthorized: false } : void 0
|
|
783
|
+
});
|
|
758
784
|
/** Merge the optional Undici dispatcher into a fetch RequestInit. */
|
|
759
785
|
function withDispatcher(init) {
|
|
760
786
|
return dispatcher ? {
|
|
@@ -786,6 +812,136 @@ function createAdtAdapter(config) {
|
|
|
786
812
|
if (resolved.origin !== baseOrigin) throw new RangeError(`Refusing to fetch ${resolved.origin} — ADT requests must target the configured base origin ${baseOrigin}`);
|
|
787
813
|
return resolved;
|
|
788
814
|
}
|
|
815
|
+
/** Add query parameters, SAP client and language to the request URL. */
|
|
816
|
+
function appendQueryParams(url, options) {
|
|
817
|
+
if (options.query) Object.entries(options.query).forEach(([key, value]) => {
|
|
818
|
+
(Array.isArray(value) ? value : [value]).forEach((entry) => {
|
|
819
|
+
if (entry !== void 0) url.searchParams.append(key, String(entry));
|
|
820
|
+
});
|
|
821
|
+
});
|
|
822
|
+
if (client) url.searchParams.append("sap-client", client);
|
|
823
|
+
if (language) url.searchParams.append("sap-language", language);
|
|
824
|
+
}
|
|
825
|
+
/** Body schema when it is Serializable (has a build method from adt-schemas). */
|
|
826
|
+
function bodySchemaOf(options) {
|
|
827
|
+
if (options.bodySchema && typeof options.bodySchema === "object") {
|
|
828
|
+
if ("build" in options.bodySchema && typeof options.bodySchema.build === "function") return options.bodySchema;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Extract response schema from responses object (passed by speci).
|
|
833
|
+
* Schemas from adt-schemas have parse() method (Serializable interface).
|
|
834
|
+
*/
|
|
835
|
+
function responseSchemaOf(options) {
|
|
836
|
+
if (!options.responses) return void 0;
|
|
837
|
+
const schema200 = options.responses[200];
|
|
838
|
+
logger?.debug("Schema detection:", {
|
|
839
|
+
hasSchema: !!schema200,
|
|
840
|
+
type: typeof schema200,
|
|
841
|
+
keys: schema200 && typeof schema200 === "object" ? Object.keys(schema200) : []
|
|
842
|
+
});
|
|
843
|
+
if (schema200 && typeof schema200 === "object" && "parse" in schema200 && typeof schema200.parse === "function") return schema200;
|
|
844
|
+
}
|
|
845
|
+
/** Debug-log the request shape before body serialization. */
|
|
846
|
+
function logPreBodyDebug(options, bodySerializableSchema) {
|
|
847
|
+
logger?.debug("Request URL: " + options.url);
|
|
848
|
+
logger?.debug("Request method: " + options.method);
|
|
849
|
+
logger?.debug("options.body: " + (options.body ? JSON.stringify(options.body).substring(0, 500) : "UNDEFINED"));
|
|
850
|
+
logger?.debug("options.bodySchema: " + (options.bodySchema ? "present" : "undefined"));
|
|
851
|
+
logger?.debug("bodySerializableSchema: " + (bodySerializableSchema ? "present" : "undefined"));
|
|
852
|
+
}
|
|
853
|
+
/** Build the request body using the schema if available. */
|
|
854
|
+
function buildRequestBody(body, bodySerializableSchema) {
|
|
855
|
+
logger?.debug("Body type: " + typeof body);
|
|
856
|
+
logger?.debug("Body value: " + (body ? JSON.stringify(body).substring(0, 500) : "UNDEFINED/NULL"));
|
|
857
|
+
if (body === void 0 || body === null) return void 0;
|
|
858
|
+
if (typeof body === "string") {
|
|
859
|
+
logger?.debug("Body: using raw string");
|
|
860
|
+
logger?.debug("Body content (first 200 chars):", body.substring(0, 200));
|
|
861
|
+
return body;
|
|
862
|
+
}
|
|
863
|
+
if (bodySerializableSchema) try {
|
|
864
|
+
logger?.debug("Calling bodySerializableSchema.build with body: " + JSON.stringify(body).substring(0, 300));
|
|
865
|
+
const requestBody = bodySerializableSchema.build(body);
|
|
866
|
+
logger?.debug("Body: serialized using Serializable.build()");
|
|
867
|
+
logger?.debug("Body output type: " + typeof requestBody);
|
|
868
|
+
logger?.debug("Body output length: " + requestBody?.length);
|
|
869
|
+
logger?.debug("Body content (first 500 chars): " + requestBody?.substring(0, 500));
|
|
870
|
+
return requestBody;
|
|
871
|
+
} catch (buildError) {
|
|
872
|
+
logger?.error("Schema build() threw error: " + (buildError instanceof Error ? buildError.message : String(buildError)));
|
|
873
|
+
logger?.error("Stack: " + (buildError instanceof Error ? buildError.stack : "N/A"));
|
|
874
|
+
throw buildError;
|
|
875
|
+
}
|
|
876
|
+
logger?.debug("Body: serialized as JSON");
|
|
877
|
+
return JSON.stringify(body);
|
|
878
|
+
}
|
|
879
|
+
/** Build and throw the structured ADT error for a failed response. */
|
|
880
|
+
async function throwResponseError(response, url, options, headers) {
|
|
881
|
+
const errorBody = await response.text();
|
|
882
|
+
const adtError = createAdtError(response.status, response.statusText, url.toString(), options.method, errorBody);
|
|
883
|
+
if (response.status === 412) {
|
|
884
|
+
const sentETag = headers["If-Match"] ?? "(none)";
|
|
885
|
+
const diag = `[If-Match: ${sentETag}, URL: ${url.pathname}]`;
|
|
886
|
+
logger?.error(`ETag mismatch: ${options.method} ${url.pathname} sent If-Match: ${sentETag}`);
|
|
887
|
+
Object.defineProperty(adtError, "message", {
|
|
888
|
+
value: `${adtError.message} ${diag}`,
|
|
889
|
+
writable: true,
|
|
890
|
+
configurable: true
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
logger?.error(`Request failed - ${adtError.message} (${options.method} ${url.toString()})`);
|
|
894
|
+
if (adtError.exception) logger?.error(`ADT Exception: namespace=${adtError.exception.namespace}, type=${adtError.exception.type}`);
|
|
895
|
+
if (errorBody && !adtError.exception) logger?.error(`Response body: ${errorBody}`);
|
|
896
|
+
if (response.status === 403) {
|
|
897
|
+
if (!(adtError.exception?.type === "ExceptionResourceNoAccess" && (adtError.message.includes("currently editing") || adtError.message.includes("is being edited")))) {
|
|
898
|
+
sessionManager.clear();
|
|
899
|
+
logger?.warn("Session cleared due to 403 Forbidden response");
|
|
900
|
+
} else logger?.warn("Lock conflict (403) — session preserved (object locked by another session)");
|
|
901
|
+
}
|
|
902
|
+
throw adtError;
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Handle a SAML redirect (expired session cookie): clear the session,
|
|
906
|
+
* optionally re-authenticate and retry through `retry`.
|
|
907
|
+
*/
|
|
908
|
+
async function handleSamlRedirect(retry) {
|
|
909
|
+
sessionManager.clear();
|
|
910
|
+
logger?.warn("SAML redirect detected - session expired");
|
|
911
|
+
if (onSessionExpired) {
|
|
912
|
+
logger?.info("Attempting automatic re-authentication...");
|
|
913
|
+
try {
|
|
914
|
+
const newCookie = await onSessionExpired();
|
|
915
|
+
sessionManager.injectCookie(newCookie);
|
|
916
|
+
logger?.info("Re-authentication successful, retrying request...");
|
|
917
|
+
return await retry();
|
|
918
|
+
} catch (refreshError) {
|
|
919
|
+
logger?.error("Re-authentication failed");
|
|
920
|
+
throw new Error("Session expired - re-authentication failed", { cause: refreshError });
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
throw new Error("Session expired - SAML re-authentication required");
|
|
924
|
+
}
|
|
925
|
+
/** Run response plugins in sequence; each may replace the parsed data. */
|
|
926
|
+
async function applyPlugins(data, rawText, url, options, contentType) {
|
|
927
|
+
if (plugins.length === 0) return data;
|
|
928
|
+
let current = data;
|
|
929
|
+
const context = {
|
|
930
|
+
rawText,
|
|
931
|
+
parsedData: current,
|
|
932
|
+
url: url.toString(),
|
|
933
|
+
method: options.method,
|
|
934
|
+
contentType
|
|
935
|
+
};
|
|
936
|
+
for (const plugin of plugins) {
|
|
937
|
+
const result = await plugin.process(context);
|
|
938
|
+
if (result !== void 0) {
|
|
939
|
+
current = result;
|
|
940
|
+
context.parsedData = result;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return current;
|
|
944
|
+
}
|
|
789
945
|
const isSamlAuth = !!cookieHeader;
|
|
790
946
|
const basicCredentials = `${username}:${password}`;
|
|
791
947
|
const authHeader = authorizationHeader ?? (isSamlAuth ? void 0 : `Basic ${Buffer.from(basicCredentials).toString("base64")}`);
|
|
@@ -801,13 +957,7 @@ function createAdtAdapter(config) {
|
|
|
801
957
|
logger?.debug("options.body:", options.body ? "present" : "undefined");
|
|
802
958
|
logger?.debug("options.bodySchema:", options.bodySchema ? "present" : "undefined");
|
|
803
959
|
const url = resolveAdtUrl(options.url);
|
|
804
|
-
|
|
805
|
-
(Array.isArray(value) ? value : [value]).forEach((entry) => {
|
|
806
|
-
if (entry !== void 0) url.searchParams.append(key, String(entry));
|
|
807
|
-
});
|
|
808
|
-
});
|
|
809
|
-
if (client) url.searchParams.append("sap-client", client);
|
|
810
|
-
if (language) url.searchParams.append("sap-language", language);
|
|
960
|
+
appendQueryParams(url, options);
|
|
811
961
|
if ([
|
|
812
962
|
"POST",
|
|
813
963
|
"PUT",
|
|
@@ -828,54 +978,14 @@ function createAdtAdapter(config) {
|
|
|
828
978
|
...options.headers
|
|
829
979
|
};
|
|
830
980
|
if (authHeader) headers.Authorization = authHeader;
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (options.responses) {
|
|
837
|
-
const schema200 = options.responses[200];
|
|
838
|
-
logger?.debug("Schema detection:", {
|
|
839
|
-
hasSchema: !!schema200,
|
|
840
|
-
type: typeof schema200,
|
|
841
|
-
keys: schema200 && typeof schema200 === "object" ? Object.keys(schema200) : []
|
|
842
|
-
});
|
|
843
|
-
if (schema200 && typeof schema200 === "object" && "parse" in schema200 && typeof schema200.parse === "function") serializableSchema = schema200;
|
|
844
|
-
}
|
|
845
|
-
let requestBody;
|
|
846
|
-
const body = options.body;
|
|
847
|
-
logger?.debug("Request URL: " + options.url);
|
|
848
|
-
logger?.debug("Request method: " + options.method);
|
|
849
|
-
logger?.debug("options.body: " + (options.body ? JSON.stringify(options.body).substring(0, 500) : "UNDEFINED"));
|
|
850
|
-
logger?.debug("options.bodySchema: " + (options.bodySchema ? "present" : "undefined"));
|
|
851
|
-
logger?.debug("bodySerializableSchema: " + (bodySerializableSchema ? "present" : "undefined"));
|
|
852
|
-
logger?.debug("Body type: " + typeof body);
|
|
853
|
-
logger?.debug("Body value: " + (body ? JSON.stringify(body).substring(0, 500) : "UNDEFINED/NULL"));
|
|
854
|
-
if (body !== void 0 && body !== null) {
|
|
855
|
-
if (typeof body === "string") {
|
|
856
|
-
requestBody = body;
|
|
857
|
-
logger?.debug("Body: using raw string");
|
|
858
|
-
logger?.debug("Body content (first 200 chars):", requestBody.substring(0, 200));
|
|
859
|
-
} else if (bodySerializableSchema) try {
|
|
860
|
-
logger?.debug("Calling bodySerializableSchema.build with body: " + JSON.stringify(body).substring(0, 300));
|
|
861
|
-
requestBody = bodySerializableSchema.build(body);
|
|
862
|
-
logger?.debug("Body: serialized using Serializable.build()");
|
|
863
|
-
logger?.debug("Body output type: " + typeof requestBody);
|
|
864
|
-
logger?.debug("Body output length: " + requestBody?.length);
|
|
865
|
-
logger?.debug("Body content (first 500 chars): " + requestBody?.substring(0, 500));
|
|
866
|
-
} catch (buildError) {
|
|
867
|
-
logger?.error("Schema build() threw error: " + (buildError instanceof Error ? buildError.message : String(buildError)));
|
|
868
|
-
logger?.error("Stack: " + (buildError instanceof Error ? buildError.stack : "N/A"));
|
|
869
|
-
throw buildError;
|
|
870
|
-
}
|
|
871
|
-
else {
|
|
872
|
-
requestBody = JSON.stringify(body);
|
|
873
|
-
logger?.debug("Body: serialized as JSON");
|
|
874
|
-
}
|
|
875
|
-
}
|
|
981
|
+
logger?.debug(`Adapter auth: mode=${isSamlAuth ? "cookie" : "basic"}, authorizationHeader=${authorizationHeader ? "present" : "none"}, basicCredentials=${basicCredentials ? "present" : "none"}, authHeader=${authHeader ? "set" : "unset"}, cookieHeader=${cookieHeader ? "present" : "none"}`);
|
|
982
|
+
const bodySerializableSchema = bodySchemaOf(options);
|
|
983
|
+
const serializableSchema = responseSchemaOf(options);
|
|
984
|
+
logPreBodyDebug(options, bodySerializableSchema);
|
|
985
|
+
const requestBody = buildRequestBody(options.body, bodySerializableSchema);
|
|
876
986
|
logger?.debug(`HTTP ${options.method} ${url.toString()}`);
|
|
877
987
|
{
|
|
878
|
-
const headerNames = Object.keys(headers).sort();
|
|
988
|
+
const headerNames = Object.keys(headers).sort((a, b) => a.localeCompare(b));
|
|
879
989
|
logger?.debug("Request headers (names only): " + JSON.stringify(headerNames));
|
|
880
990
|
}
|
|
881
991
|
const response = await requestFetch(url.toString(), withDispatcher({
|
|
@@ -887,72 +997,11 @@ function createAdtAdapter(config) {
|
|
|
887
997
|
sessionManager.processResponse(response, url.pathname);
|
|
888
998
|
logger?.debug(`Response: ${response.status} ${response.statusText}`);
|
|
889
999
|
logger?.debug("Response headers: " + JSON.stringify(Object.fromEntries(response.headers.entries()), null, 2));
|
|
890
|
-
if (!response.ok)
|
|
891
|
-
const errorBody = await response.text();
|
|
892
|
-
const adtError = createAdtError(response.status, response.statusText, url.toString(), options.method, errorBody);
|
|
893
|
-
if (response.status === 412) {
|
|
894
|
-
const sentETag = headers["If-Match"] ?? "(none)";
|
|
895
|
-
const diag = `[If-Match: ${sentETag}, URL: ${url.pathname}]`;
|
|
896
|
-
logger?.error(`ETag mismatch: ${options.method} ${url.pathname} sent If-Match: ${sentETag}`);
|
|
897
|
-
Object.defineProperty(adtError, "message", {
|
|
898
|
-
value: `${adtError.message} ${diag}`,
|
|
899
|
-
writable: true,
|
|
900
|
-
configurable: true
|
|
901
|
-
});
|
|
902
|
-
}
|
|
903
|
-
logger?.error(`Request failed - ${adtError.message} (${options.method} ${url.toString()})`);
|
|
904
|
-
if (adtError.exception) logger?.error(`ADT Exception: namespace=${adtError.exception.namespace}, type=${adtError.exception.type}`);
|
|
905
|
-
if (errorBody && !adtError.exception) logger?.error(`Response body: ${errorBody}`);
|
|
906
|
-
if (response.status === 403) {
|
|
907
|
-
if (!(adtError.exception?.type === "ExceptionResourceNoAccess" && (adtError.message.includes("currently editing") || adtError.message.includes("is being edited")))) {
|
|
908
|
-
sessionManager.clear();
|
|
909
|
-
logger?.warn("Session cleared due to 403 Forbidden response");
|
|
910
|
-
} else logger?.warn("Lock conflict (403) — session preserved (object locked by another session)");
|
|
911
|
-
}
|
|
912
|
-
throw adtError;
|
|
913
|
-
}
|
|
1000
|
+
if (!response.ok) await throwResponseError(response, url, options, headers);
|
|
914
1001
|
const contentType = response.headers.get("content-type") || "";
|
|
915
1002
|
const rawText = await response.text();
|
|
916
|
-
if (contentType.includes("text/html") && rawText.includes("SAMLRequest"))
|
|
917
|
-
|
|
918
|
-
logger?.warn("SAML redirect detected - session expired");
|
|
919
|
-
if (onSessionExpired) {
|
|
920
|
-
logger?.info("Attempting automatic re-authentication...");
|
|
921
|
-
try {
|
|
922
|
-
const newCookie = await onSessionExpired();
|
|
923
|
-
sessionManager.injectCookie(newCookie);
|
|
924
|
-
logger?.info("Re-authentication successful, retrying request...");
|
|
925
|
-
return this.request(options);
|
|
926
|
-
} catch (refreshError) {
|
|
927
|
-
logger?.error("Re-authentication failed");
|
|
928
|
-
throw new Error("Session expired - re-authentication failed", { cause: refreshError });
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
throw new Error("Session expired - SAML re-authentication required");
|
|
932
|
-
}
|
|
933
|
-
let data;
|
|
934
|
-
if (contentType.includes("application/json") || contentType.includes("+json")) data = JSON.parse(rawText);
|
|
935
|
-
else if (contentType.includes("text/") || contentType.includes("xml")) {
|
|
936
|
-
if (serializableSchema && contentType.includes("xml")) data = serializableSchema.parse(rawText);
|
|
937
|
-
else data = rawText;
|
|
938
|
-
} else data = rawText;
|
|
939
|
-
if (plugins.length > 0) {
|
|
940
|
-
const context = {
|
|
941
|
-
rawText,
|
|
942
|
-
parsedData: data,
|
|
943
|
-
url: url.toString(),
|
|
944
|
-
method: options.method,
|
|
945
|
-
contentType
|
|
946
|
-
};
|
|
947
|
-
for (const plugin of plugins) {
|
|
948
|
-
const result = await plugin.process(context);
|
|
949
|
-
if (result !== void 0) {
|
|
950
|
-
data = result;
|
|
951
|
-
context.parsedData = result;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
return data;
|
|
1003
|
+
if (contentType.includes("text/html") && rawText.includes("SAMLRequest")) return handleSamlRedirect(() => this.request(options));
|
|
1004
|
+
return await applyPlugins(parseResponseBody(contentType, rawText, serializableSchema), rawText, url, options, contentType);
|
|
956
1005
|
},
|
|
957
1006
|
clearETag(url) {
|
|
958
1007
|
sessionManager.clearETag(url);
|
|
@@ -987,6 +1036,8 @@ function createAdtAdapter(config) {
|
|
|
987
1036
|
}
|
|
988
1037
|
|
|
989
1038
|
// src/services/transports.ts
|
|
1039
|
+
/** Explicit `String()` coercion for untyped node values. */
|
|
1040
|
+
const asText = String;
|
|
990
1041
|
/**
|
|
991
1042
|
* Transport Service - orchestrates CTS operations
|
|
992
1043
|
*/
|
|
@@ -1010,12 +1061,13 @@ var TransportService = class {
|
|
|
1010
1061
|
const data = (response && typeof response === "object" && "root" in response ? response.root : response) ?? response;
|
|
1011
1062
|
const wanted = trkorr.trim().toUpperCase();
|
|
1012
1063
|
const siblingRequest = (Array.isArray(data?.request) ? data.request[0] : data?.request) ?? void 0;
|
|
1013
|
-
const siblingRequestNumber = siblingRequest ?
|
|
1064
|
+
const siblingRequestNumber = siblingRequest ? asText(siblingRequest.number ?? "").toUpperCase() : "";
|
|
1014
1065
|
const selected = this.findUnit(data?.task, wanted) ?? this.findUnit(data?.request, wanted) ?? data?.request ?? (Array.isArray(data?.task) ? data.task[0] : data?.task) ?? data;
|
|
1015
1066
|
if (selected && typeof selected === "object") {
|
|
1016
1067
|
const sel = selected;
|
|
1017
1068
|
const hasParent = Boolean(sel.parent);
|
|
1018
|
-
|
|
1069
|
+
const singleTaskNode = data?.task ? [data.task] : [];
|
|
1070
|
+
if ((Array.isArray(data?.task) ? data.task : singleTaskNode).includes(selected) && !hasParent && siblingRequestNumber) sel.parent = siblingRequestNumber;
|
|
1019
1071
|
}
|
|
1020
1072
|
return this.mapToTransport(selected);
|
|
1021
1073
|
}
|
|
@@ -1026,8 +1078,8 @@ var TransportService = class {
|
|
|
1026
1078
|
* For now, this is a placeholder that throws a helpful error.
|
|
1027
1079
|
* Use ADK's AdkTransportRequest.create() for full functionality.
|
|
1028
1080
|
*/
|
|
1029
|
-
|
|
1030
|
-
|
|
1081
|
+
create(_options) {
|
|
1082
|
+
return Promise.reject(/* @__PURE__ */ new Error("Transport creation via service layer not yet implemented. Use ADK's AdkTransportRequest.create() for now."));
|
|
1031
1083
|
}
|
|
1032
1084
|
/**
|
|
1033
1085
|
* Delete a transport request
|
|
@@ -1041,8 +1093,8 @@ var TransportService = class {
|
|
|
1041
1093
|
* Note: Release requires POST with specific action parameter.
|
|
1042
1094
|
* This is a placeholder for the full implementation.
|
|
1043
1095
|
*/
|
|
1044
|
-
|
|
1045
|
-
|
|
1096
|
+
release(_trkorr) {
|
|
1097
|
+
return Promise.reject(/* @__PURE__ */ new Error("Transport release via service layer not yet implemented."));
|
|
1046
1098
|
}
|
|
1047
1099
|
/**
|
|
1048
1100
|
* Find a request/task unit whose number matches the wanted transport id.
|
|
@@ -1052,7 +1104,7 @@ var TransportService = class {
|
|
|
1052
1104
|
if (!node) return void 0;
|
|
1053
1105
|
return (Array.isArray(node) ? node : [node]).find((item) => {
|
|
1054
1106
|
const u = item;
|
|
1055
|
-
return
|
|
1107
|
+
return asText(u?.trkorr ?? u?.number ?? "").toUpperCase() === wanted;
|
|
1056
1108
|
});
|
|
1057
1109
|
}
|
|
1058
1110
|
/**
|
|
@@ -1365,8 +1417,12 @@ var SourceHistoryService = class {
|
|
|
1365
1417
|
assertAdtUri(versionsUri);
|
|
1366
1418
|
return normalizeSourceVersionFeed(await this.adt.repository.sourceversions.list({ versionsUri }), versionsUri);
|
|
1367
1419
|
}
|
|
1368
|
-
|
|
1369
|
-
|
|
1420
|
+
readVersionSource(sourceUri) {
|
|
1421
|
+
try {
|
|
1422
|
+
assertAdtUri(sourceUri);
|
|
1423
|
+
} catch (error) {
|
|
1424
|
+
return Promise.reject(error);
|
|
1425
|
+
}
|
|
1370
1426
|
return this.adt.repository.sourceversions.get({ sourceUri });
|
|
1371
1427
|
}
|
|
1372
1428
|
async readVersionSourceBounded(sourceUri, maxBytes) {
|
|
@@ -1401,7 +1457,7 @@ function createAdtClient(config) {
|
|
|
1401
1457
|
baseUrl: config.baseUrl,
|
|
1402
1458
|
adapter
|
|
1403
1459
|
});
|
|
1404
|
-
const fetchFn =
|
|
1460
|
+
const fetchFn = (url, options) => {
|
|
1405
1461
|
const requestOptions = {
|
|
1406
1462
|
method: options?.method || "GET",
|
|
1407
1463
|
url,
|
|
@@ -1517,7 +1573,7 @@ var FileLoggingPlugin = class {
|
|
|
1517
1573
|
path = path.replace(/^\/sap\/bc\/adt\/?/, "");
|
|
1518
1574
|
if (!path || path === "discovery") return "./adt/core/discovery.xml";
|
|
1519
1575
|
const [basePath] = path.split("?");
|
|
1520
|
-
const segments = basePath.split("/").filter(
|
|
1576
|
+
const segments = basePath.split("/").filter(Boolean);
|
|
1521
1577
|
const sourceTypes = [
|
|
1522
1578
|
"main",
|
|
1523
1579
|
"definitions",
|
|
@@ -1540,8 +1596,8 @@ var FileLoggingPlugin = class {
|
|
|
1540
1596
|
* Get metadata file path for a response file
|
|
1541
1597
|
*/
|
|
1542
1598
|
getMetadataPath(responsePath) {
|
|
1543
|
-
if (
|
|
1544
|
-
if (
|
|
1599
|
+
if (responsePath.endsWith("-response.xml")) return responsePath.replace(/-response\.xml$/, "-metadata.json");
|
|
1600
|
+
if (responsePath.endsWith(".xml")) return responsePath.replace(/\.xml$/, ".metadata.json");
|
|
1545
1601
|
return `${responsePath}.metadata.json`;
|
|
1546
1602
|
}
|
|
1547
1603
|
};
|