tradly
Version:
Tradly JS SDK
161 lines (146 loc) • 3.89 kB
JavaScript
;
import { to } from "await-to-js";
import axios from "axios";
import { APPCONSTANT } from "../Constants/AppConstant.js";
import { REFRESH } from "../Constants/PathConstant.js";
import Environment, { ImageConfig } from "../Helper/APIParam.js";
import {
getAuthKeyFromAuth,
getPKKeyFromAuth,
getEnvFromAuth,
getBaseUrlFromAuth,
} from "../Helper/AuthHelper.js";
export const Method = {
GET: "GET",
DELETE: "DELETE",
POST: "POST",
PUT: "PUT",
PATCH: "PATCH",
};
export var TradlyConfig = {
path: "",
method: Method,
param: "",
authKey: "",
refreshKey: "",
currency: "",
language: "",
barrow_access_key: "",
pkKey: "",
baseURL: "",
};
class NetworkManager {
networkCall = async (config = TradlyConfig) => {
// Sync environment from auth if available and APPCONSTANT.ENVIRONMENT is empty
if (!APPCONSTANT.ENVIRONMENT) {
const authEnv = getEnvFromAuth();
if (authEnv) {
APPCONSTANT.ENVIRONMENT = authEnv;
}
}
// Get base URL - prefer auth's baseUrl if available, otherwise use beluga's Environment function
let BaseURL = null;
const authBaseUrl = getBaseUrlFromAuth();
if (authBaseUrl) {
BaseURL = authBaseUrl;
} else {
BaseURL = Environment(APPCONSTANT.ENVIRONMENT);
}
if(config.baseURL) {
BaseURL = config.baseURL;
}
var url = BaseURL + config.path;
var header = {
Accept: "application/json",
"Content-Type": "application/json",
"X-TRADLY-AGENT": 3,
};
// Auto-get PK key from auth if not provided and auth is available
let pkKey = APPCONSTANT.TOKEN;
if (config?.pkKey) {
pkKey = config?.pkKey;
}
if (!pkKey && !APPCONSTANT.IS_SKIP_PK_BY_DOMAIN) {
const authPKKey = getPKKeyFromAuth();
if (authPKKey) {
pkKey = authPKKey;
APPCONSTANT.TOKEN = pkKey; // Cache it for future use
}
}
if (pkKey && !APPCONSTANT.IS_SKIP_PK_BY_DOMAIN) {
header["Authorization"] = "Bearer " + pkKey;
}
// Auto-get auth key from auth package if not provided
let authKey = config.authKey;
if (
(authKey === null ||
authKey === undefined ||
authKey?.length === 0) &&
!APPCONSTANT.IS_SKIP_PK_BY_DOMAIN
) {
const authKeyFromAuth = getAuthKeyFromAuth();
if (authKeyFromAuth) {
authKey = authKeyFromAuth;
}
}
if (
authKey !== null &&
authKey !== undefined &&
authKey?.length > 0
) {
header["x-auth-key"] = authKey;
}
if (config.currency != undefined || config.currency == "") {
header["x-currency"] = config.currency;
}
if (config.refreshKey != undefined || config.refreshKey == "") {
header["X-Refresh-Key"] = config.refreshKey;
}
if (config.language != undefined || config.language == "") {
header["X-Language"] = config.language;
}
if (
config?.barrow_access_key != undefined &&
config?.barrow_access_key !== ""
) {
header["barrow_access_key"] = config?.barrow_access_key;
}
if (Object.keys(APPCONSTANT.CUSTOM_HEADER).length > 0) {
Object.keys(APPCONSTANT.CUSTOM_HEADER).forEach((key) => {
header[key] = APPCONSTANT.CUSTOM_HEADER[key];
});
}
const [err, response] = await to(
axios({
url: url,
method: config.method,
responseType: "json",
headers: header,
data: config.param,
})
);
if (err) {
let errData = err["response"]["data"];
return [errData, undefined];
} else {
return [undefined, response["data"]];
}
};
uploadImage = async (param = ImageConfig) => {
const [err, response] = await to(
axios({
url: param.signedUrl,
method: "put",
headers: { "Content-Type": param.mime },
data: param.blob_body,
})
);
if (err) {
return false;
} else {
return true;
}
};
}
const network = new NetworkManager();
export default network;