Skip to content

Commit

Permalink
feat(now you can override axios config): added "axiosRequestConfig" f…
Browse files Browse the repository at this point in the history
…ield to override axios client configuration
  • Loading branch information
danibram committed Mar 22, 2021
1 parent dd7515f commit a84f7e2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 73 deletions.
2 changes: 2 additions & 0 deletions build/main/Interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AxiosRequestConfig } from 'axios';
export declare type MethodInterface = {
method: string;
dataFields: string[];
Expand All @@ -15,4 +16,5 @@ export declare type ConfigInterface = {
specs?: {
[key: string]: MethodInterface;
};
axiosRequestConfig?: AxiosRequestConfig;
};
2 changes: 2 additions & 0 deletions build/main/lib/Client.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { AxiosRequestConfig } from 'axios';
import { ConfigInterface } from '../Interfaces';
export declare class Client {
endpoint: string;
environment: 'development' | 'production' | 'prod' | 'p';
username: string;
password: string;
axiosRequestConfig: AxiosRequestConfig | {};
privateKeyPath: string | undefined;
publicKeyPath: string;
privateKey: string | undefined;
Expand Down
33 changes: 14 additions & 19 deletions build/main/lib/Client.js

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions build/module/lib/Client.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { AxiosRequestConfig } from 'axios'

export type MethodInterface = {
method: string
Expand All @@ -18,5 +18,5 @@ export type ConfigInterface = {
specs?: {
[key: string]: MethodInterface
}
axiosRequestConfig?: Pick<AxiosRequestConfig, 'timeout'>
axiosRequestConfig?: AxiosRequestConfig
}
89 changes: 56 additions & 33 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import axios, { AxiosRequestConfig } from 'axios';
import { ConfigInterface } from '../Interfaces';
import { accountPayout, approveWithdrawal, balance, charge, denyWithdrawal, deposit, refund, selectAccount, withdraw } from '../specs';
import { serialize } from './trustlySerializeData';
import { parseError, readFile, root, sign, verify } from './utils';
const uuidv4 = require('uuid/v4')

import axios, { AxiosRequestConfig } from 'axios'
import { v4 as uuidv4 } from 'uuid'
import { ConfigInterface } from '../Interfaces'
import {
accountPayout,
approveWithdrawal,
balance,
charge,
denyWithdrawal,
deposit,
refund,
selectAccount,
withdraw,
} from '../specs'
import { serialize } from './trustlySerializeData'
import { parseError, readFile, root, sign, verify } from './utils'

export class Client {
endpoint: string = 'https://test.trustly.com/api/1'
environment: 'development' | 'production' | 'prod' | 'p' = 'development'
username: string = ''
password: string = ''

axiosRequestConfig: AxiosRequestConfig | undefined
axiosRequestConfig: AxiosRequestConfig | {}

privateKeyPath: string | undefined
publicKeyPath: string
Expand All @@ -33,8 +42,8 @@ export class Client {
this.publicKeyPath = config.publicKeyPath
? config.publicKeyPath
: isProd
? root('keys', 'trustly.com.public.pem')
: root('keys', 'test.trustly.com.public.pem')
? root('keys', 'trustly.com.public.pem')
: root('keys', 'test.trustly.com.public.pem')

this.endpoint = isProd
? 'https://trustly.com/api/1'
Expand All @@ -57,12 +66,11 @@ export class Client {
this.password = config.password
this.privateKeyPath = config.privateKeyPath
this.privateKey = config.privateKey
this.axiosRequestConfig = config.axiosRequestConfig;

this.axiosRequestConfig = config.axiosRequestConfig || {}
this.ready = this._init()
}

public _createMethod = method => async (params, attributes) => {
public _createMethod = (method) => async (params, attributes) => {
await this.ready
let req = this._prepareRequest(method, params, attributes)
return this._makeRequest(req)
Expand All @@ -72,21 +80,21 @@ export class Client {
let req = {
method,
params: {},
version: '1.1'
version: '1.1',
}

let UUID = uuidv4()

let Data = Object.assign({}, data, {
Attributes: attributes ? attributes : null,
Username: this.username,
Password: this.password
Password: this.password,
})

req.params = {
Data: Data,
UUID: UUID,
Signature: sign(serialize(method, UUID, Data), this.privateKey)
Signature: sign(serialize(method, UUID, Data), this.privateKey),
}

return req
Expand All @@ -100,15 +108,18 @@ export class Client {
}
}

_prepareNotificationResponse = function (notification, status: 'OK' | 'FAILED' = 'OK') {
_prepareNotificationResponse = function (
notification,
status: 'OK' | 'FAILED' = 'OK'
) {
let req = {
result: {
signature: '',
uuid: notification.params.uuid,
method: notification.method,
data: { status }
data: { status },
},
version: '1.1'
version: '1.1',
}

req.result.signature = sign(
Expand All @@ -123,7 +134,10 @@ export class Client {
return req
}

createNotificationResponse = async (notification, status: 'OK' | 'FAILED' = 'OK') => {
createNotificationResponse = async (
notification,
status: 'OK' | 'FAILED' = 'OK'
) => {
await this.ready

let lastNotification = null
Expand Down Expand Up @@ -159,12 +173,12 @@ export class Client {
} catch (err) {
throw {
error: err,
lastNotification: lastNotification
lastNotification: lastNotification,
}
}
}

_makeRequest = reqParams => {
_makeRequest = (reqParams) => {
this._lastRequest = reqParams
this._lastResponse = null

Expand All @@ -174,7 +188,7 @@ export class Client {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
data: reqParams,
timeout: 2000,
...(this.axiosRequestConfig || {})
...this.axiosRequestConfig,
})
.then(({ data }) => {
this._lastResponse = data
Expand All @@ -194,20 +208,29 @@ export class Client {

throw 'Cant parse the response, check the lastResponse.'
})
.catch(error => {
.catch((error) => {
parseError(error, this._lastRequest, this._lastResponse)
})
}

deposit = (data, attributes?) => this._createMethod(deposit.method)(data, attributes)
refund = (data, attributes?) => this._createMethod(refund.method)(data, attributes)
selectAccount = (data, attributes?) => this._createMethod(selectAccount.method)(data, attributes)
charge = (data, attributes?) => this._createMethod(charge.method)(data, attributes)
withdraw = (data, attributes?) => this._createMethod(withdraw.method)(data, attributes)
approveWithdrawal = (data, attributes?) => this._createMethod(approveWithdrawal.method)(data, attributes)
denyWithdrawal = (data, attributes?) => this._createMethod(denyWithdrawal.method)(data, attributes)
accountPayout = (data, attributes?) => this._createMethod(accountPayout.method)(data, attributes)
balance = (data, attributes?) => this._createMethod(balance.method)(data, attributes)
deposit = (data, attributes?) =>
this._createMethod(deposit.method)(data, attributes)
refund = (data, attributes?) =>
this._createMethod(refund.method)(data, attributes)
selectAccount = (data, attributes?) =>
this._createMethod(selectAccount.method)(data, attributes)
charge = (data, attributes?) =>
this._createMethod(charge.method)(data, attributes)
withdraw = (data, attributes?) =>
this._createMethod(withdraw.method)(data, attributes)
approveWithdrawal = (data, attributes?) =>
this._createMethod(approveWithdrawal.method)(data, attributes)
denyWithdrawal = (data, attributes?) =>
this._createMethod(denyWithdrawal.method)(data, attributes)
accountPayout = (data, attributes?) =>
this._createMethod(accountPayout.method)(data, attributes)
balance = (data, attributes?) =>
this._createMethod(balance.method)(data, attributes)
request = (method, params, attributes?) =>
this._createMethod(method)(params, attributes)

Expand Down

0 comments on commit a84f7e2

Please sign in to comment.