rosetta-sdk-typescript
Version:
Typescript SDK to create and interact with Rosetta API implementations.
111 lines (110 loc) • 4.73 kB
JavaScript
;
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.RosettaRestClientFactory = exports.exceptionHandlingMiddleware = exports.RestClientCallError = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const rosetta_sdk_typescript_1 = require("rosetta-sdk-typescript");
/**
* When the rest client raises an error, the error will be wrapped in this exception.
*/
class RestClientCallError extends Error {
/**
*
* @param message - the message resolved from the error response
* @param statusCode - the response status code
* @param statusMessage - the response status message
* @param body - the body as string
*/
constructor(message, statusCode, statusMessage, body) {
super(message);
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.body = body;
}
}
exports.RestClientCallError = RestClientCallError;
/**
* Basic implementation of the exception handling middleware.
*/
exports.exceptionHandlingMiddleware = {
post(context) {
return __awaiter(this, void 0, void 0, function* () {
const response = context.response;
if (response.status >= 200 && response.status < 300) {
return response;
}
throw yield RosettaRestClientFactory.getErrorFromFetchResponse(response);
});
},
};
/**
* Main class used to create Rosetta rest clients.
*
* These rest client would most likely be used for Rosetta e2e testing as this sdk brings server side dependencies (e.g. express) you may not want in a Rosetta client.
*
*/
class RosettaRestClientFactory {
constructor(configs) {
const fetchApi = configs.fetchApi || (typeof window !== 'undefined' && window.fetch.bind(window)) || node_fetch_1.default;
this.configuration = new rosetta_sdk_typescript_1.Configuration({
basePath: configs.url,
fetchApi: fetchApi,
middleware: configs.middleware || [exports.exceptionHandlingMiddleware],
});
}
static getErrorFromFetchResponse(error) {
return __awaiter(this, void 0, void 0, function* () {
const statusCode = (error === null || error === void 0 ? void 0 : error.status) || 0;
const statusMessage = ((error === null || error === void 0 ? void 0 : error.statusText) || 'Unknown Error').toString();
const body = yield error.text();
const getMessage = () => {
const defaultMessage = `${statusCode} - ${statusMessage}`;
try {
const modelError = rosetta_sdk_typescript_1.ModelErrorFromJSON(JSON.parse(body));
return [modelError.code, modelError.message, modelError.description].join(' - ');
}
catch (e) {
return defaultMessage;
}
};
const message = getMessage();
return new RestClientCallError(message, statusCode, statusMessage, body);
});
}
account() {
return new rosetta_sdk_typescript_1.AccountApi(this.configuration);
}
block() {
return new rosetta_sdk_typescript_1.BlockApi(this.configuration);
}
events() {
return new rosetta_sdk_typescript_1.EventsApi(this.configuration);
}
construction() {
return new rosetta_sdk_typescript_1.ConstructionApi(this.configuration);
}
call() {
return new rosetta_sdk_typescript_1.CallApi(this.configuration);
}
mempool() {
return new rosetta_sdk_typescript_1.MempoolApi(this.configuration);
}
network() {
return new rosetta_sdk_typescript_1.NetworkApi(this.configuration);
}
search() {
return new rosetta_sdk_typescript_1.SearchApi(this.configuration);
}
}
exports.RosettaRestClientFactory = RosettaRestClientFactory;