1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const axios_1 = require("axios");
|
4 | const CustomError_1 = require("./Interfaces/CustomError");
|
5 | const RequestLimitFactory_1 = require("./RequestLimitFactory");
|
6 | const Request_1 = require("./HTTP/Request");
|
7 | const SignRequestHandler_1 = require("./HTTP/SignRequestHandler");
|
8 | const EncryptRequestHandler_1 = require("./HTTP/EncryptRequestHandler");
|
9 | const VerifyResponseHandler_1 = require("./HTTP/VerifyResponseHandler");
|
10 | const ErrorCodes_1 = require("./Helpers/ErrorCodes");
|
11 | exports.BUNQ_SERVER_SIGNATURE_HEADER_KEY = "X-Bunq-Server-Signature";
|
12 | exports.BUNQ_REQUEST_SIGNATURE_HEADER_KEY = "X-Bunq-Client-Signature";
|
13 | exports.BUNQ_REQUEST_AUTHENTICATION_HEADER_KEY = "X-Bunq-Client-Authentication";
|
14 | class ApiAdapter {
|
15 | constructor(Session, loggerInterface, BunqJSClient) {
|
16 | |
17 |
|
18 |
|
19 |
|
20 | this.sessionValidationCheck = async () => {
|
21 |
|
22 | if (this.BunqJSClient.fetchingNewSession) {
|
23 |
|
24 | await this.BunqJSClient.fetchingNewSession;
|
25 | }
|
26 | else {
|
27 |
|
28 | if (this.BunqJSClient.keepAlive === false) {
|
29 |
|
30 | await this.BunqJSClient.registerSession();
|
31 | }
|
32 | }
|
33 |
|
34 | const expiresInMilliseconds = this.BunqJSClient.calculateSessionExpiry();
|
35 | if (expiresInMilliseconds < 30000) {
|
36 |
|
37 | const extendByMilliseconds = this.BunqJSClient.calculateSessionExpiry(true);
|
38 |
|
39 | const currentDate = new Date();
|
40 | currentDate.setTime(currentDate.getTime() + extendByMilliseconds);
|
41 |
|
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 |
|
61 |
|
62 |
|
63 |
|
64 |
|
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 |
|
72 |
|
73 |
|
74 |
|
75 |
|
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 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
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 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
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 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | async request(url, method = "GET", data = {}, headers = {}, options = {}, axiosInstance = false) {
|
115 |
|
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 |
|
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 |
|
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 |
|
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 |
|
161 | response.data = JSON.parse(response.data);
|
162 | return response;
|
163 | }
|
164 | catch (error) { }
|
165 | return response;
|
166 | }
|
167 | |
168 |
|
169 |
|
170 |
|
171 | requestErrorHandler(error) {
|
172 |
|
173 | if (error.response && error.response.data) {
|
174 |
|
175 | try {
|
176 |
|
177 | error.response.data = JSON.parse(error.response.data);
|
178 | throw error;
|
179 | }
|
180 | catch (error) { }
|
181 | }
|
182 |
|
183 | throw error;
|
184 | }
|
185 | }
|
186 | exports.default = ApiAdapter;
|