-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.ts
104 lines (93 loc) · 3.38 KB
/
api.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
export * from './api/apis';
export * from './model/models';
import { Interceptor } from './model/models';
import { VERSION } from './version';
import { ActivitiesApi } from './api/activitiesApi';
import { AuthApi } from './api/authApi';
import { CallbacksApi } from './api/callbacksApi';
import { ContactsApi } from './api/contactsApi';
import { CustomListsApi } from './api/customListsApi';
import { DistributionListsApi } from './api/distributionListsApi';
import { EventsApi } from './api/eventsApi';
import { ImportsApi } from './api/importsApi';
import { KeysApi } from './api/keysApi';
import { MessagesApi } from './api/messagesApi';
import { ResourcesApi } from './api/resourcesApi';
import { ResponseRulesApi } from './api/responseRulesApi';
import { ScenariosApi } from './api/scenariosApi';
import { TemplatesApi } from './api/templatesApi';
import { UsersApi } from './api/usersApi';
import { WorkspacesApi } from './api/workspacesApi';
export type ClientConfig = {
host: string;
username?: string;
password?: string;
apiKey?: string;
accessToken?: string;
};
type AuthHeaders = {
'X-Api-Key'?: string;
Authorization?: string;
};
const Client = (config: ClientConfig) => {
let authHeaders: AuthHeaders = {};
if (config.username && config.password && config.apiKey) {
authHeaders['Authorization'] = `Basic ${Buffer.from(`${config.username}:${config.password}`).toString("base64")}`;
authHeaders['X-Api-Key'] = config.apiKey;
} else if (config.accessToken && config.apiKey) {
authHeaders['Authorization'] = `Bearer ${config.accessToken}`;
authHeaders['X-Api-Key'] = config.apiKey;
} else {
throw new Error(`Unsupported authorization:
Please provide either username, password, and apiKey combination
OR accessToken and apiKey combination.`);
}
const defaultHeaders = {
'User-Agent': `whispir-node-${VERSION}`,
...authHeaders,
};
const apiConfig = { defaultHeaders, host: config.host };
const activities = new ActivitiesApi(apiConfig);
const auth = new AuthApi(apiConfig);
const callbacks = new CallbacksApi(apiConfig);
const contacts = new ContactsApi(apiConfig);
const customLists = new CustomListsApi(apiConfig);
const distributionLists = new DistributionListsApi(apiConfig);
const events = new EventsApi(apiConfig);
const imports = new ImportsApi(apiConfig);
const keys = new KeysApi(apiConfig);
const messages = new MessagesApi(apiConfig);
const resources = new ResourcesApi(apiConfig);
const responseRules = new ResponseRulesApi(apiConfig);
const scenarios = new ScenariosApi(apiConfig);
const templates = new TemplatesApi(apiConfig);
const users = new UsersApi(apiConfig);
const workspaces = new WorkspacesApi(apiConfig);
const apis = {
activities,
auth,
callbacks,
contacts,
customLists,
distributionLists,
events,
imports,
keys,
messages,
resources,
responseRules,
scenarios,
templates,
users,
workspaces,
};
const addInterceptor = (interceptor: Interceptor) => {
Object.values(apis).forEach(api => {
api.addInterceptor(interceptor);
});
};
return { addInterceptor, ...apis };
};
export default Client;
module.exports = Client;
module.exports.default = Client;