@odata2ts/odata2ts
Version:
Flexible generator to produce various TypeScript artefacts (from simple model interfaces to complete odata clients) from OData metadata files
53 lines • 2.26 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import axios from "axios";
import deepmerge from "deepmerge";
const METADATA_PATH = "$metadata";
const PWD_BLIND_TEXT = { password: "xxx hidden xxx" };
function evaluateRequestConfig(url, config) {
const defaultReqConfig = {
url: url,
method: "GET",
headers: {
// Added Accept: "application/xml" to ensure OData API servers
// return metadata in XML format, as axios defaults to application/json.
Accept: "application/xml",
},
};
const reqConfig = config.custom
? config.custom
: typeof config.username === "string" && typeof (config === null || config === void 0 ? void 0 : config.password) === "string"
? { auth: { username: config.username, password: config.password } }
: {};
return deepmerge(defaultReqConfig, reqConfig);
}
/**
* Retrieves the metadata from the given URL using the given configuration.
*
* Failure handling must be implemented by consumer.
* Exception is thrown for failed requests (400, 401, ...).
*
* @param sourceUrl
* @param sourceConfig
* @param debug
*/
export function downloadMetadata(sourceUrl, sourceConfig = {}, debug = false) {
return __awaiter(this, void 0, void 0, function* () {
// add the $metadata suffix
const url = sourceUrl.endsWith(METADATA_PATH)
? sourceUrl
: sourceUrl + (sourceUrl.endsWith("/") ? "" : "/") + METADATA_PATH;
console.log(`Reading metadata from URL:`, url);
// evaluate configured request config options
const config = evaluateRequestConfig(url, sourceConfig);
// log request configuration without password
if (debug) {
const { auth } = config, loggable = __rest(config, ["auth"]);
const safeConfig = auth ? Object.assign(Object.assign({}, loggable), { auth: Object.assign(Object.assign({}, auth), (auth.password ? PWD_BLIND_TEXT : {})) }) : loggable;
console.log(`Request configuration:`, safeConfig);
}
// execute the request & return response data
const response = yield axios.request(config);
return response.data;
});
}
//# sourceMappingURL=downloadMetadata.js.map