-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapis.ts
244 lines (230 loc) · 7.81 KB
/
apis.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
export * from './aPICallsApi';
import { APICallsApi } from './aPICallsApi';
export * from './audiencesApi';
import { AudiencesApi } from './audiencesApi';
export * from './auditTrailApi';
import { AuditTrailApi } from './auditTrailApi';
export * from './catalogApi';
import { CatalogApi } from './catalogApi';
export * from './computedTraitsApi';
import { ComputedTraitsApi } from './computedTraitsApi';
export * from './deletionAndSuppressionApi';
import { DeletionAndSuppressionApi } from './deletionAndSuppressionApi';
export * from './deliveryOverviewApi';
import { DeliveryOverviewApi } from './deliveryOverviewApi';
export * from './destinationFiltersApi';
import { DestinationFiltersApi } from './destinationFiltersApi';
export * from './destinationsApi';
import { DestinationsApi } from './destinationsApi';
export * from './edgeFunctionsApi';
import { EdgeFunctionsApi } from './edgeFunctionsApi';
export * from './eventsApi';
import { EventsApi } from './eventsApi';
export * from './functionsApi';
import { FunctionsApi } from './functionsApi';
export * from './iAMGroupsApi';
import { IAMGroupsApi } from './iAMGroupsApi';
export * from './iAMRolesApi';
import { IAMRolesApi } from './iAMRolesApi';
export * from './iAMUsersApi';
import { IAMUsersApi } from './iAMUsersApi';
export * from './labelsApi';
import { LabelsApi } from './labelsApi';
export * from './monthlyTrackedUsersApi';
import { MonthlyTrackedUsersApi } from './monthlyTrackedUsersApi';
export * from './profilesSyncApi';
import { ProfilesSyncApi } from './profilesSyncApi';
export * from './reverseETLApi';
import { ReverseETLApi } from './reverseETLApi';
export * from './selectiveSyncApi';
import { SelectiveSyncApi } from './selectiveSyncApi';
export * from './sourcesApi';
import { SourcesApi } from './sourcesApi';
export * from './spacesApi';
import { SpacesApi } from './spacesApi';
export * from './testingApi';
import { TestingApi } from './testingApi';
export * from './trackingPlansApi';
import { TrackingPlansApi } from './trackingPlansApi';
export * from './transformationsApi';
import { TransformationsApi } from './transformationsApi';
export * from './warehousesApi';
import { WarehousesApi } from './warehousesApi';
export * from './workspacesApi';
import { WorkspacesApi } from './workspacesApi';
import * as http from 'http';
export class HttpError extends Error {
constructor(
public response: http.IncomingMessage,
public body: any,
public statusCode?: number
) {
super('HTTP request failed');
this.name = 'HttpError';
}
}
export { RequestFile } from '../model/models';
export const APIS = [
APICallsApi,
AudiencesApi,
AuditTrailApi,
CatalogApi,
ComputedTraitsApi,
DeletionAndSuppressionApi,
DeliveryOverviewApi,
DestinationFiltersApi,
DestinationsApi,
EdgeFunctionsApi,
EventsApi,
FunctionsApi,
IAMGroupsApi,
IAMRolesApi,
IAMUsersApi,
LabelsApi,
MonthlyTrackedUsersApi,
ProfilesSyncApi,
ReverseETLApi,
SelectiveSyncApi,
SourcesApi,
SpacesApi,
TestingApi,
TrackingPlansApi,
TransformationsApi,
WarehousesApi,
WorkspacesApi,
];
export interface Apis {
apiCalls: APICallsApi;
auditTrail: AuditTrailApi;
catalog: CatalogApi;
deletionAndSuppresion: DeletionAndSuppressionApi;
destinationFilters: DestinationFiltersApi;
destinations: DestinationsApi;
edgeFunctions: EdgeFunctionsApi;
events: EventsApi;
functions: FunctionsApi;
iamGroups: IAMGroupsApi;
iamRoles: IAMRolesApi;
iamUsers: IAMUsersApi;
labels: LabelsApi;
monthlyTrackedUsers: MonthlyTrackedUsersApi;
profilesSync: ProfilesSyncApi;
selectiveSync: SelectiveSyncApi;
sources: SourcesApi;
spaces: SpacesApi;
testing: TestingApi;
trackingPlans: TrackingPlansApi;
transformations: TransformationsApi;
warehouses: WarehousesApi;
workspaces: WorkspacesApi;
}
/**
* Configures all the APIs and consolidates them into a single client.
*
* @param token the authorization token
* @returns The consolidated client
*/
export function configureApis(
token: string,
headers: Record<string, string> = {}
): Apis {
const apis = {
apiCalls: new APICallsApi(),
auditTrail: new AuditTrailApi(),
catalog: new CatalogApi(),
deletionAndSuppresion: new DeletionAndSuppressionApi(),
destinationFilters: new DestinationFiltersApi(),
destinations: new DestinationsApi(),
edgeFunctions: new EdgeFunctionsApi(),
events: new EventsApi(),
functions: new FunctionsApi(),
iamGroups: new IAMGroupsApi(),
iamRoles: new IAMRolesApi(),
iamUsers: new IAMUsersApi(),
labels: new LabelsApi(),
monthlyTrackedUsers: new MonthlyTrackedUsersApi(),
profilesSync: new ProfilesSyncApi(),
selectiveSync: new SelectiveSyncApi(),
sources: new SourcesApi(),
spaces: new SpacesApi(),
testing: new TestingApi(),
trackingPlans: new TrackingPlansApi(),
transformations: new TransformationsApi(),
warehouses: new WarehousesApi(),
workspaces: new WorkspacesApi(),
};
for (const k of Object.keys(apis)) {
const key = k as keyof typeof apis;
headers['User-Agent'] = 'Public API SDK 52.0.0 (TypeScript)';
apis[key].accessToken = token;
apis[key].defaultHeaders = headers;
}
return apis;
}
/**
* Configures all the APIs and consolidates them into a single client for EU region.
*
* @param token the authorization token
* @returns The consolidated client
*/
export function configureEuApis(
token: string,
headers: Record<string, string> = {}
): Apis {
const basePath = 'https://eu1.api.segmentapis.com';
const apis = {
apiCalls: new APICallsApi(basePath),
auditTrail: new AuditTrailApi(basePath),
catalog: new CatalogApi(basePath),
deletionAndSuppresion: new DeletionAndSuppressionApi(basePath),
destinationFilters: new DestinationFiltersApi(basePath),
destinations: new DestinationsApi(basePath),
edgeFunctions: new EdgeFunctionsApi(basePath),
events: new EventsApi(basePath),
functions: new FunctionsApi(basePath),
iamGroups: new IAMGroupsApi(basePath),
iamRoles: new IAMRolesApi(basePath),
iamUsers: new IAMUsersApi(basePath),
labels: new LabelsApi(basePath),
monthlyTrackedUsers: new MonthlyTrackedUsersApi(basePath),
profilesSync: new ProfilesSyncApi(basePath),
selectiveSync: new SelectiveSyncApi(basePath),
sources: new SourcesApi(basePath),
spaces: new SpacesApi(basePath),
testing: new TestingApi(basePath),
trackingPlans: new TrackingPlansApi(basePath),
transformations: new TransformationsApi(basePath),
warehouses: new WarehousesApi(basePath),
workspaces: new WorkspacesApi(basePath),
};
for (const k of Object.keys(apis)) {
const key = k as keyof typeof apis;
headers['User-Agent'] = 'Public API SDK 52.0.0 (TypeScript)';
apis[key].accessToken = token;
apis[key].defaultHeaders = headers;
}
return apis;
}
/**
* Unwraps the data from a Public API request, and re-throws client errors
*
* @param promise An async Public API request
* @param fallback A fallback string in case data is unavailable
*
* @returns the unwrapped data
*/
export async function unwrap<T>(
promise: Promise<{
body: { data?: T };
}>,
fallback?: string
): Promise<T> {
const response = await promise.catch((e: HttpError) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
throw e.body.errors[0];
});
if (response.body === undefined || response.body.data === undefined) {
throw new Error(fallback ?? 'Could not load data');
}
return response.body.data;
}