UNPKG

@firefly-exchange/firefly-client

Version:

The Firefly Client Library allows traders to sign, create, retrieve and listen to orders on Firefly Exchange.

148 lines 7.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.APIService = void 0; const axios_1 = __importDefault(require("axios")); const library_1 = require("@firefly-exchange/library"); const package_json_1 = require("../../package.json"); class APIService { constructor(url) { this.token = undefined; this.apiToken = undefined; this.walletAddress = undefined; this.uuid = ""; this.baseUrl = undefined; this.setAuthToken = (token) => __awaiter(this, void 0, void 0, function* () { this.token = token; }); this.setAPIToken = (apiToken) => __awaiter(this, void 0, void 0, function* () { this.apiToken = apiToken; }); this.setUUID = (uuid) => __awaiter(this, void 0, void 0, function* () { this.uuid = uuid; }); this.setWalletAddress = (address) => __awaiter(this, void 0, void 0, function* () { this.walletAddress = address; }); //= ==============================================================// // PRIVATE HELPER FUNCTIONS //= ==============================================================// this.transformRequest = (data, headers) => { if (this.apiToken) { headers["x-api-token"] = this.apiToken; } else { headers.Authorization = `Bearer ${this.token}`; } if (this.uuid && this.uuid != "") { headers["x-mm-id"] = this.uuid; } headers["x-wallet-address"] = this.walletAddress || ""; return JSON.stringify(data); }; this.baseUrl = url; this.apiService = axios_1.default.create({ headers: { "Content-Type": "application/json", "x-bluefin-client-version": package_json_1.version, }, validateStatus: () => true, }); } get(url, queryParams, config, baseUrl) { return __awaiter(this, void 0, void 0, function* () { if (!baseUrl) baseUrl = this.baseUrl; url = baseUrl + url; const response = yield this.apiService.get(url, Object.assign(Object.assign({ params: queryParams }, config), { transformRequest: (config === null || config === void 0 ? void 0 : config.isAuthenticationRequired) ? this.transformRequest : undefined })); return this.handleResponse(response); }); } post(url, data, config, baseUrl) { return __awaiter(this, void 0, void 0, function* () { if (!baseUrl) baseUrl = this.baseUrl; url = baseUrl + url; const response = yield this.apiService.post(url, data, Object.assign(Object.assign({}, config), { transformRequest: (config === null || config === void 0 ? void 0 : config.isAuthenticationRequired) ? this.transformRequest : undefined })); return this.handleResponse(response); }); } put(url, data, config, baseUrl) { return __awaiter(this, void 0, void 0, function* () { if (!baseUrl) baseUrl = this.baseUrl; url = baseUrl + url; const response = yield this.apiService.put(url, data, Object.assign(Object.assign({}, config), { transformRequest: (config === null || config === void 0 ? void 0 : config.isAuthenticationRequired) ? this.transformRequest : undefined })); return this.handleResponse(response); }); } patch(url, data, config, baseUrl) { return __awaiter(this, void 0, void 0, function* () { if (!baseUrl) baseUrl = this.baseUrl; url = baseUrl + url; const response = yield this.apiService.patch(url, data, Object.assign(Object.assign({}, config), { transformRequest: (config === null || config === void 0 ? void 0 : config.isAuthenticationRequired) ? this.transformRequest : undefined })); return this.handleResponse(response); }); } delete(url, data, config, baseUrl) { return __awaiter(this, void 0, void 0, function* () { if (!baseUrl) baseUrl = this.baseUrl; url = baseUrl + url; const response = yield this.apiService.delete(url, Object.assign(Object.assign({}, config), { data, transformRequest: (config === null || config === void 0 ? void 0 : config.isAuthenticationRequired) ? this.transformRequest : undefined })); return this.handleResponse(response); }); } // TODO; create interface for response handleResponse(response) { const mutatedResponse = { // TODO:needs to be implemented properly (BE have to change response model first ) ok: response.statusText === "OK" || (response.status >= 200 && response.status < 300), status: response.status, response: { data: (0, library_1.getValue)(response.data, "error.data", response.data), message: (0, library_1.getValue)(response.data, "error.message", "success"), errorCode: (0, library_1.getValue)(response.data, "error.code", null), }, }; const data = (0, library_1.getValue)(response, "data", undefined); if (mutatedResponse.ok) { return Object.assign(Object.assign({}, mutatedResponse), { data }); } return Object.assign(Object.assign({}, mutatedResponse), { data: !(0, library_1.isEmpty)(data) ? data : undefined }); } transformAPItoResponseSchema(APIResponse) { const mutatedResponse = { ok: APIResponse.ok, data: APIResponse.response.data, message: APIResponse.response.message, code: APIResponse.status, }; return mutatedResponse; } } exports.APIService = APIService; //# sourceMappingURL=apiService.js.map