-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
134 lines (121 loc) · 3.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import fetch from "node-fetch";
import { encode } from "querystring";
import { constructEvent } from "./webhooks";
const SANDBOX_URL = "https://api.sandbox.transferwise.tech";
const LIVE_URL = "https://api.transferwise.com";
const VERSION = "v1";
class TransferWise {
constructor({ token, sandbox = false } = {}) {
if (!token) throw new Error("token is required");
this.token = token;
this.sandbox = sandbox;
this.url = sandbox ? SANDBOX_URL : LIVE_URL;
}
request({ method = "GET", path = "", body, version } = {}) {
const apiVersion = `/${version || VERSION}`;
const url = this.url + apiVersion + path;
const fetchOptions = {
method,
json: true,
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json",
"cache-control": "no-cache"
}
};
if (body) fetchOptions.body = JSON.stringify(body);
if (method === "DELETE") return fetch(url, fetchOptions);
return fetch(url, fetchOptions).then(resp => resp.json());
}
profiles() {
return this.request({ path: "/profiles" });
}
borderlessAccounts(profileId) {
return this.request({
path: `/borderless-accounts?profileId=${profileId}`
});
}
get recipientAccounts() {
return {
create: accountDetails =>
this.request({
method: "POST",
body: accountDetails,
path: "/accounts"
}),
get: accountId => this.request({ path: `/accounts/${accountId}` }),
delete: accountId =>
this.request({ method: "DELETE", path: `/accounts/${accountId}` }),
list: params => this.request({ path: `/accounts?${encode(params)}` })
};
}
get quotes() {
return {
temporary: params => this.request({ path: `/quotes?${encode(params)}` }),
create: quoteDetails =>
this.request({ method: "POST", body: quoteDetails, path: "/quotes" }),
get: quoteId => this.request({ path: `/quotes/${quoteId}` })
};
}
get transfers() {
return {
create: transferDetails =>
this.request({
method: "POST",
body: transferDetails,
path: "/transfers"
}),
cancel: transferId =>
this.request({
method: "PUT",
path: `/transfers/${transferId}/cancel`
}),
get: transferId => this.request({ path: `/transfers/${transferId}` }),
issues: transferId =>
this.request({ path: `/transfers/${transferId}/issues` }),
fund: (profileId, transferId) =>
this.request({
method: "POST",
version: "v3",
body: { type: "BALANCE" },
path: `/profiles/${profileId}/transfers/${transferId}/payments`
}),
deliveryEstimate: transferId =>
this.request({ path: `/delivery-estimates/${transferId}` }),
list: params => this.request({ path: `/transfers?${encode(params)}` })
};
}
get simulation() {
return {
transfers: {
processing: transferId =>
this.request({
path: `/simulation/transfers/${transferId}/processing`
}),
fundsConverted: transferId =>
this.request({
path: `/simulation/transfers/${transferId}/funds_converted`
}),
outgoingPaymentSent: transferId =>
this.request({
path: `/simulation/transfers/${transferId}/outgoing_payment_sent`
}),
bouncedBack: transferId =>
this.request({
path: `/simulation/transfers/${transferId}/bounced_back`
}),
fundsRefunded: transferId =>
this.request({
path: `/simulation/transfers/${transferId}/funds_refunded`
})
}
};
}
get webhooks() {
return {
constructEvent: (body, signature) =>
constructEvent(this.sandbox, body, signature)
};
}
}
export default TransferWise;