forked from ChipperCash/flopay-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
131 lines (118 loc) · 3.86 KB
/
client.ts
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
import { default as axios, AxiosInstance } from 'axios'
import * as auth from './auth'
export enum ApiVersion {
One = 'v1'
}
// A Flopay API v1 client.
// It is initialized without a valid authentication,
// which means that `authorize` should be the first
// call made before using it to interact with the API.
export class Client {
static readonly baseURL: string = 'https://api.flopay.io'
static readonly version: ApiVersion = ApiVersion.One
readonly cred: auth.Cred
readonly transport: AxiosInstance
private auth: auth.Auth
constructor (id: string, secret: string, timeout = 2000) {
this.cred = { id, secret } as auth.Cred
this.transport = axios.create({
baseURL: `${Client.baseURL}/${Client.version}/`,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
timeout
})
}
/**
* Makes a new client using the FLOPAY_CLIENT_{ID, SECRET}
* environment variables as the client credentials. If
* the variables are not found, an exception is thrown.
*
* @method fromEnv
* @return {Client}
*/
static fromEnv () {
const id = process.env.FLOPAY_CLIENT_ID
const secret = process.env.FLOPAY_CLIENT_SECRET
if (id === undefined || secret === undefined) {
throw new InvalidCredError()
}
return new Client(id, secret)
}
/**
* Acquires a valid access token for this client.
* The granted token is valid for 3600s from now.
* It's safe to be re-called when the token has
* expired an a new one is needed. It could also
* be called to acquire a new token even before the
* current one has expired.
*
* @method authorize
*/
async authorize () {
const req = new auth.Request(this.cred)
const { data } = await this.transport.post(auth.path, req.body)
this.auth = new auth.Auth(data as auth.Response)
this.transport.defaults.headers['Authorization'] = `Bearer ${this.auth.token}`
}
/**
* True if the client is authorized, and the
* acquired authorization can be used to make requests.
* False otherwise.
*
* @property authorized
*/
get authorized (): boolean {
return !!this.auth && !this.auth.expired
}
get accessToken (): string {
return this.auth && this.auth.token
}
/**
* Performs a request and sets the response received
* on the given request. Sometimes (roughly hourly),
* these call will take slightly longer because the
* client re-authorizes (acquires a new token). Hopefully
* this shouldn't be noticeable. But if it is then
* maybe we can get rid of the compulsory renewal.
*
* @method do
* @param {String} to Relative endpoint
* @param {Req} req The request to perform
*/
async do (req: Req) {
if (!this.authorized) {
// Looks like our current authorization is no longer good.
// Getting a new one before making the request. Not slow.
await this.authorize()
}
const { method, to, body } = req
switch (method) {
case 'POST': {
const { data } = await this.transport.post(to, body)
req.response = data
break
}
case 'GET': {
const { data } = await this.transport.get(to, { params: body })
req.response = data
break
}
}
}
}
// Req is any request that can be performed by the client.
// It specific the endpoint (as relative path), the body
// as an Object, and waits for the response to be set
// after the request has been performed.
export interface Req {
method: string // HTTP method
to: string // endpoint
body: { [k: string]: any } // request body, as JSON
response?: { [k: string]: any } // request response, as JSON
}
export class InvalidCredError extends Error {
constructor () {
super(
`FLOPAY_CLIENT_{ID, SECRET} env vars expected but not found. Please set them and try again. Or use the other constructor that accepts the client id and client secret as arguments.`
)
}
}