bankson-js-mb
Version:
Bankson.fi Node client, Mad Booster fork
143 lines • 5.07 kB
JavaScript
import NodeRSA from 'node-rsa';
import ApiKeys from './ext/apikeys.js';
import BankAccountStatements from './ext/bank-account-statements.js';
import BankAccounts from './ext/bank-accounts.js';
import Certificates from './ext/bank-certificates.js';
import Calls from './ext/calls.js';
import InboundPayments from './ext/inbound-payments.js';
import Payments from './ext/outbound-payments.js';
import Webhooks from './ext/webhooks.js';
class InternalError extends Error {
status;
statusText;
}
class ValidationError extends Error {
status;
body;
}
export default class Client {
beforeRequest;
bearerToken;
baseUrl;
testMode;
privateKey;
apiKey;
webhooks;
certificates;
calls;
bankAccounts;
bankAccountStatements;
outboundPayments;
apikeys;
inboundPayments;
constructor(opts = {}) {
this.webhooks = new Webhooks(this);
this.certificates = new Certificates(this);
this.calls = new Calls(this);
this.bankAccounts = new BankAccounts(this);
this.bankAccountStatements = new BankAccountStatements(this);
this.outboundPayments = new Payments(this);
this.apikeys = new ApiKeys(this);
this.inboundPayments = new InboundPayments(this);
this.beforeRequest = opts.beforeRequest ?? (() => Promise.resolve({}));
this.bearerToken = opts.bearerToken || '-';
this.baseUrl = opts.baseUrl || 'https://api.bankson.fi';
this.testMode = opts.test ?? false;
if (opts.privateKey && opts.apiKey) {
// ApiKey authentication
this.bearerToken = false;
this.privateKey = new NodeRSA();
this.privateKey.importKey(opts.privateKey, 'private');
if (!this.privateKey.isPrivate())
throw new Error('Invalid private key');
this.apiKey = opts.apiKey;
}
}
me() {
return this.get('/me');
}
meV2() {
return this.get('/v2/me');
}
authorizationHeader(bearerToken) {
if (this.bearerToken)
return 'Bearer ' + bearerToken;
const timestamp = Date.now();
const str = this.apiKey + timestamp;
const signature = this.privateKey.sign(str, 'base64');
return 'BanksonRSA ' + [
'ApiKey=' + this.apiKey,
'Timestamp=' + timestamp,
'Signature=' + signature,
].join(', ');
}
async headers(additionalHeaders = {}) {
const result = await this.beforeRequest?.();
const bearerToken = result?.bearerToken || this.bearerToken;
const banksonTest = result?.test ?? this.testMode;
const headers = new Headers();
headers.append('Accept', additionalHeaders.Accept || 'application/json');
headers.append('Authorization', this.authorizationHeader(bearerToken));
if (banksonTest)
headers.append('X-Bankson-Environment', 'Test');
return headers;
}
async get(path, options = {}) {
const headers = await this.headers(options.headers);
const resp = await fetch(`${this.baseUrl}${path}`, { headers });
return this.handleResponse(resp, options);
}
post(path, data) {
return this.request('POST', path, data);
}
put(path, data) {
return this.request('PUT', path, data);
}
async request(method, path, data) {
const headers = await this.headers();
const isFormData = data instanceof FormData;
if (!isFormData) {
headers.append('Content-Type', 'application/json');
}
const resp = await fetch(`${this.baseUrl}${path}`, {
method,
body: isFormData ? data : JSON.stringify(data),
headers,
});
return this.handleResponse(resp);
}
async delete(path) {
const headers = await this.headers();
const resp = await fetch(`${this.baseUrl}${path}`, {
method: 'DELETE',
headers,
});
return this.handleResponse(resp);
}
handleResponse(resp, options = {}) {
if (!resp.ok) {
if (resp.status >= 500 || resp.status < 400) {
const err = new InternalError(`Internal error (${resp.status}): ${resp.statusText}`);
err.status = resp.status;
err.statusText = resp.statusText;
throw err;
}
return getBody(resp).then(json => {
const err = new ValidationError('Request unsuccessfull');
err.status = resp.status;
err.body = json;
throw err;
});
}
return getBody(resp);
async function getBody(resp) {
if (!(resp.headers.get('Content-Type') ?? '').includes('application/json')) {
if (options.responseType === 'arraybuffer')
return resp.arrayBuffer();
return resp.text();
}
return resp.json();
}
}
}
//# sourceMappingURL=client.js.map