UNPKG

8.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const axios_1 = require("axios");
4const CustomError_1 = require("./Interfaces/CustomError");
5const RequestLimitFactory_1 = require("./RequestLimitFactory");
6const Request_1 = require("./HTTP/Request");
7const SignRequestHandler_1 = require("./HTTP/SignRequestHandler");
8const EncryptRequestHandler_1 = require("./HTTP/EncryptRequestHandler");
9const VerifyResponseHandler_1 = require("./HTTP/VerifyResponseHandler");
10const ErrorCodes_1 = require("./Helpers/ErrorCodes");
11exports.BUNQ_SERVER_SIGNATURE_HEADER_KEY = "X-Bunq-Server-Signature";
12exports.BUNQ_REQUEST_SIGNATURE_HEADER_KEY = "X-Bunq-Client-Signature";
13exports.BUNQ_REQUEST_AUTHENTICATION_HEADER_KEY = "X-Bunq-Client-Authentication";
14class ApiAdapter {
15 constructor(Session, loggerInterface, BunqJSClient) {
16 /**
17 * Checks if the session is valid and waits for it to be refreshed
18 * @returns {Promise<void>}
19 */
20 this.sessionValidationCheck = async () => {
21 // check if a new session is being fetched
22 if (this.BunqJSClient.fetchingNewSession) {
23 // wait for the new session to be loaded
24 await this.BunqJSClient.fetchingNewSession;
25 }
26 else {
27 // check if keepAlive is enabled and continue if it isn't
28 if (this.BunqJSClient.keepAlive === false) {
29 // check if a valid session is set
30 await this.BunqJSClient.registerSession();
31 }
32 }
33 // calculate amount of milliseconds until expire time
34 const expiresInMilliseconds = this.BunqJSClient.calculateSessionExpiry();
35 if (expiresInMilliseconds < 30000) {
36 // this request will extend the expiry timer
37 const extendByMilliseconds = this.BunqJSClient.calculateSessionExpiry(true);
38 // add milliseconds to current time
39 const currentDate = new Date();
40 currentDate.setTime(currentDate.getTime() + extendByMilliseconds);
41 // set updated session expiry time
42 this.Session.sessionExpiryTime = currentDate;
43 this.logger.debug(`Request in last 30 seconds: (${expiresInMilliseconds / 1000})`);
44 this.logger.debug(`Set session expiry to ${this.Session.sessionExpiryTime}`);
45 }
46 };
47 this.Session = Session;
48 this.logger = loggerInterface;
49 this.BunqJSClient = BunqJSClient;
50 this.RequestLimitFactory = new RequestLimitFactory_1.default(this.logger);
51 this.SignRequestHandler = new SignRequestHandler_1.default(this.Session, this.logger, this.BunqJSClient);
52 this.EncryptRequestHandler = new EncryptRequestHandler_1.default(this.Session, this.logger, this.BunqJSClient);
53 this.VerifyResponseHandler = new VerifyResponseHandler_1.default(this.Session, this.logger, this.BunqJSClient);
54 this.language = "en_US";
55 this.region = "nl_NL";
56 this.geoLocation = "0 0 0 0 000";
57 }
58 async setup() { }
59 /**
60 * @param {string} url
61 * @param headers
62 * @param {ApiAdapterOptions} options
63 * @param {AxiosInstance | false} axiosInstance
64 * @returns {Promise<void>}
65 */
66 async get(url, headers = {}, options = {}, axiosInstance = false) {
67 const response = await this.request(url, "GET", "", headers, options, axiosInstance);
68 return response.data;
69 }
70 /**
71 * @param {string} url
72 * @param headers
73 * @param {ApiAdapterOptions} options
74 * @param {AxiosInstance | false} axiosInstance
75 * @returns {Promise<void>}
76 */
77 async delete(url, headers = {}, options = {}, axiosInstance = false) {
78 const response = await this.request(url, "DELETE", {}, headers, options, axiosInstance);
79 return response.data;
80 }
81 /**
82 * @param {string} url
83 * @param data
84 * @param headers
85 * @param {ApiAdapterOptions} options
86 * @param {AxiosInstance | false} axiosInstance
87 * @returns {Promise<void>}
88 */
89 async post(url, data = {}, headers = {}, options = {}, axiosInstance = false) {
90 const response = await this.request(url, "POST", data, headers, options, axiosInstance);
91 return response.data;
92 }
93 /**
94 * @param {string} url
95 * @param data
96 * @param headers
97 * @param {ApiAdapterOptions} options
98 * @param {AxiosInstance | false} axiosInstance
99 * @returns {Promise<void>}
100 */
101 async put(url, data = {}, headers = {}, options = {}, axiosInstance = false) {
102 const response = await this.request(url, "PUT", data, headers, options, axiosInstance);
103 return response.data;
104 }
105 /**
106 * @param {string} url
107 * @param {string} method
108 * @param data
109 * @param headers
110 * @param {ApiAdapterOptions} options
111 * @param {AxiosInstance | false} axiosInstance
112 * @returns {Promise<any>}
113 */
114 async request(url, method = "GET", data = {}, headers = {}, options = {}, axiosInstance = false) {
115 // default to standard axios instance
116 if (!axiosInstance)
117 axiosInstance = axios_1.default;
118 if (!options.axiosOptions)
119 options.axiosOptions = {};
120 const paramsString = options.axiosOptions.params ? JSON.stringify(options.axiosOptions.params) : "";
121 this.logger.debug(`${method}: ${url} ${paramsString}`);
122 const request = new Request_1.default(url, method, data, headers, options.axiosOptions);
123 if (!options.skipSessionCheck) {
124 await this.sessionValidationCheck();
125 }
126 if (options.disableAuthentication !== true) {
127 // use session token or fallback to install taken if we have one
128 if (this.Session.sessionToken !== null) {
129 request.setAuthenticated(this.Session.sessionToken);
130 }
131 else if (this.Session.installToken !== null) {
132 request.setAuthenticated(this.Session.installToken);
133 }
134 }
135 if (options.isEncrypted === true) {
136 await this.EncryptRequestHandler.encryptRequest(request, options);
137 }
138 if (options.disableSigning !== true) {
139 await this.SignRequestHandler.signRequest(request, options);
140 }
141 // complete relative urls
142 if (request.url[0] === "/") {
143 request.setUrl(`${this.Session.environmentUrl}${request.url}`);
144 }
145 let response;
146 try {
147 response = await axiosInstance.request(request.requestConfig);
148 }
149 catch (error) {
150 this.requestErrorHandler(error);
151 }
152 if (options.disableVerification !== true) {
153 const verifyResult = await this.VerifyResponseHandler.verifyResponse(response);
154 if (!verifyResult && (!process.env.ENV_CI || process.env.ENV_CI === "false")) {
155 // invalid response in a non-ci environment
156 throw new CustomError_1.default("We couldn't verify the received response", response, ErrorCodes_1.default.INVALID_RESPONSE_RECEIVED);
157 }
158 }
159 try {
160 // attempt to turn string result back into json when possible
161 response.data = JSON.parse(response.data);
162 return response;
163 }
164 catch (error) { }
165 return response;
166 }
167 /**
168 * Attempts to improve the error data and defaults to rethrowing it
169 * @param error
170 */
171 requestErrorHandler(error) {
172 // get the data from the request if it fails
173 if (error.response && error.response.data) {
174 // parse json response if possible
175 try {
176 // attempt to turn string result back into json when possible
177 error.response.data = JSON.parse(error.response.data);
178 throw error;
179 }
180 catch (error) { }
181 }
182 // rethrow if no json data could be found
183 throw error;
184 }
185}
186exports.default = ApiAdapter;