-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.ts
179 lines (153 loc) · 4.25 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import axios, { AxiosRequestConfig, Method } from "axios";
import { DISCOVERY_APP_URL, DISCOVERY_URL, HEADERS } from "../constants";
type Route = {
href: string;
};
type Env = "node" | "rn" | "web";
export type Routes = Record<string, Route>;
export type AuthState = {
accessToken?: string;
refreshToken?: string;
refreshBefore?: Date;
privateUrls: Routes;
publicUrls: Record<string, string>;
};
export type HttpClientConstructor = {
clientName?: string;
cert?: Buffer;
accessToken?: string;
refreshToken?: string;
refreshBefore?: string;
privateUrls?: Routes;
publicUrls?: Record<string, string>;
env?: Env;
};
export class HttpClient {
private _clientName: string;
private _cert?: Buffer;
private _accessToken: string = "";
private _refreshToken: string = "";
private _refreshBefore?: Date;
private _privateUrls: Routes;
private _publicUrls: Record<string, string>;
private _env: Env;
public get authState(): AuthState {
return {
accessToken: this._accessToken,
refreshToken: this._refreshToken,
refreshBefore: this._refreshBefore,
privateUrls: this._privateUrls,
publicUrls: this._publicUrls,
};
}
public get clientName(): string {
return this._clientName;
}
public set accessToken(accessToken: string) {
this._accessToken = accessToken;
}
public set refreshToken(refreshToken: string) {
this._refreshToken = refreshToken;
}
public set refreshBefore(datetime: string) {
this._refreshBefore = new Date(datetime);
}
public set cert(cert: Buffer) {
this._cert = cert;
}
public set privateUrls(privateUrls: Routes) {
this._privateUrls = privateUrls;
}
public constructor(params: HttpClientConstructor = {}) {
this._clientName = params?.clientName ?? "Nubank API";
this._cert = params?.cert;
this.accessToken = params?.accessToken ?? "";
this.refreshToken = params?.refreshToken ?? "";
this.refreshBefore = params?.refreshBefore ?? "";
this._privateUrls = params?.privateUrls ?? {};
this._publicUrls = params?.publicUrls ?? {};
this._env = params?.env ?? "node";
}
public async ready(): Promise<void> {
const numberOfUrls: number = Object.keys(this._publicUrls).length;
if (numberOfUrls > 0) {
return;
}
const [baseUrls, appUrls] = await Promise.all([
axios.get(DISCOVERY_URL).then((r) => r.data),
axios.get(DISCOVERY_APP_URL).then((r) => r.data),
]);
this._publicUrls = { ...baseUrls, ...appUrls };
}
public async request(
method: Method,
id: string,
body?: any,
params?: any
): Promise<any> {
const { data } = await this.rawRequest(method, id, body, params);
return data;
}
public async rawRequest(
method: Method,
id: string,
body?: any,
params?: any
): Promise<any> {
const url: string = this.isUrl(id) ? id : await this.getUrl(id);
const headers = { ...HEADERS };
if (this._accessToken) {
// tslint:disable-next-line
headers["Authorization"] = `Bearer ${this._accessToken}`;
}
let httpsAgent: any;
if (this._cert) {
const { Agent } = await import("https");
httpsAgent = new Agent({
rejectUnauthorized: false,
passphrase: "",
pfx: this._cert,
});
}
const options: AxiosRequestConfig = {
data: body,
headers,
method,
params,
url,
httpsAgent,
};
return axios(options);
}
public async graphql(query: string, variables?: any): Promise<any> {
try {
const response = await this.request("post", "ghostflame", {
query,
variables,
});
return response;
} catch (e) {
const error = e as any;
throw error.response.data.errors[0] ?? error;
}
}
public async getUrl(id: string): Promise<string> {
await this.ready();
if (this._publicUrls[id]) {
return this._publicUrls[id];
}
if (this._privateUrls[id]) {
return this._privateUrls[id].href;
}
throw new Error(`URL for '${id}' does not exist.`);
}
public clearSession(): void {
this._accessToken = "";
this._refreshToken = "";
this._refreshBefore = undefined;
this._privateUrls = {};
}
private isUrl(url: string): boolean {
return url.startsWith("http");
}
}