@wepublish/api
Version:
API core for we.publish.
49 lines • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayrexxClient = void 0;
const tslib_1 = require("tslib");
const crypto_1 = tslib_1.__importDefault(require("crypto"));
const qs_1 = tslib_1.__importDefault(require("qs"));
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
class PayrexxClient {
constructor(baseUrl, instance, secret) {
this.baseUrl = baseUrl;
this.instance = instance;
this.secret = secret;
}
buildSignedQueryString(queryParams = {}) {
return qs_1.default.stringify(Object.assign(Object.assign({}, queryParams), { ApiSignature: crypto_1.default
.createHmac('sha256', this.secret)
.update(qs_1.default.stringify(queryParams))
.digest('base64') }));
}
buildBaseUrl(path) {
return `${this.baseUrl}${path}?instance=${this.instance}`;
}
get(path, queryParams = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const method = 'GET', queryStrSigned = this.buildSignedQueryString(queryParams), baseUrl = `${this.buildBaseUrl(path)}&${queryStrSigned}`;
//console.log({message: 'Payrexx GET request issued', path, queryParams})
const response = yield (0, node_fetch_1.default)(baseUrl, { method });
return yield this.validateResponse(response);
});
}
post(path, queryParams = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const method = 'POST', body = this.buildSignedQueryString(queryParams), baseUrl = this.buildBaseUrl(path);
//console.log({message: 'Payrexx POST request issued', path, queryParams})
const response = yield (0, node_fetch_1.default)(baseUrl, { method, body });
return yield this.validateResponse(response);
});
}
validateResponse(response) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (response.status !== 200) {
throw new Error(`Payrexx response is NOK with status ${response.status}`);
}
return yield response.json();
});
}
}
exports.PayrexxClient = PayrexxClient;
//# sourceMappingURL=payrexx-client.js.map