-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
120 lines (96 loc) · 3.59 KB
/
index.ts
File metadata and controls
120 lines (96 loc) · 3.59 KB
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
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
export type Environment = 'test' | 'production';
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
export interface TokenResponse {
token: string;
}
export interface ScopeRequest {
scopes: string[];
ttl?: number;
}
export class OauthClient {
private client: AxiosInstance;
private baseUrl: string;
constructor(username: string, apiKey: string, isTest: boolean = false) {
this.baseUrl = isTest ? 'https://test.oauth.altravia.com' : 'https://oauth.altravia.com';
const auth = Buffer.from(`${username}:${apiKey}`).toString('base64');
this.client = axios.create({
baseURL: this.baseUrl,
headers: {
'Authorization': `Basic ${auth}`,
'Content-Type': 'application/json'
}
});
}
async createToken(scopes: string[], ttl: number = 3600): Promise<string> {
const body = { scopes, ttl };
const response = await this.client.post('/token', body);
return response.data;
}
async getTokens(scope?: string): Promise<string> {
const params = scope ? { scope } : {};
const response = await this.client.get('/token', { params });
return response.data;
}
async deleteToken(token: string): Promise<string> {
const response = await this.client.delete(`/token/${token}`);
return response.data;
}
async getCounters(period: string, date: string): Promise<string> {
const response = await this.client.get(`/counters/${period}/${date}`);
return response.data;
}
async getScopes(limit?: boolean): Promise<string> {
const params = limit !== undefined ? { limit } : {};
const response = await this.client.get('/scopes', { params });
return response.data;
}
}
export class Client {
private client: AxiosInstance;
constructor(token: string) {
this.client = axios.create({
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
}
async request<T = any>(
method: HttpMethod,
url: string,
payload?: any,
params?: Record<string, any>,
config?: AxiosRequestConfig
): Promise<T> {
const requestConfig: AxiosRequestConfig = {
method,
url,
...config
};
if (payload) {
requestConfig.data = payload;
}
if (params) {
requestConfig.params = params;
}
const response = await this.client.request(requestConfig);
return response.data;
}
async get<T = any>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<T> {
return this.request<T>('GET', url, undefined, params, config);
}
async post<T = any>(url: string, payload?: any, config?: AxiosRequestConfig): Promise<T> {
return this.request<T>('POST', url, payload, undefined, config);
}
async put<T = any>(url: string, payload?: any, config?: AxiosRequestConfig): Promise<T> {
return this.request<T>('PUT', url, payload, undefined, config);
}
async delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
return this.request<T>('DELETE', url, undefined, undefined, config);
}
async patch<T = any>(url: string, payload?: any, config?: AxiosRequestConfig): Promise<T> {
return this.request<T>('PATCH', url, payload, undefined, config);
}
}
export default { OauthClient, Client };