@databricks/sdk-budgets 0.0.0-dev → 0.1.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,521 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ import {z} from 'zod';
4
+
5
+ /** Type of action that a budget alert executes when its threshold is crossed. */
6
+ export enum ActionConfigurationType {
7
+ EMAIL_NOTIFICATION = 'EMAIL_NOTIFICATION',
8
+ }
9
+
10
+ export enum AlertConfigurationQuantityType {
11
+ LIST_PRICE_DOLLARS_USD = 'LIST_PRICE_DOLLARS_USD',
12
+ }
13
+
14
+ export enum AlertConfigurationTimePeriod {
15
+ MONTH = 'MONTH',
16
+ }
17
+
18
+ export enum AlertConfigurationTriggerType {
19
+ CUMULATIVE_SPENDING_EXCEEDED = 'CUMULATIVE_SPENDING_EXCEEDED',
20
+ }
21
+
22
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested enum name.
23
+ export enum BudgetConfigurationFilter_Operator {
24
+ IN = 'IN',
25
+ }
26
+
27
+ export interface ActionConfiguration {
28
+ /** <Databricks> action configuration ID. */
29
+ actionConfigurationId?: string | undefined;
30
+ /** The type of the action. */
31
+ actionType?: ActionConfigurationType | undefined;
32
+ /** Target for the action. For example, an email address. */
33
+ target?: string | undefined;
34
+ }
35
+
36
+ export interface AlertConfiguration {
37
+ /** <Databricks> alert configuration ID. */
38
+ alertConfigurationId?: string | undefined;
39
+ /** The time window of usage data for the budget. */
40
+ timePeriod?: AlertConfigurationTimePeriod | undefined;
41
+ /** The evaluation method to determine when this budget alert is in a triggered state. */
42
+ triggerType?: AlertConfigurationTriggerType | undefined;
43
+ /** The way to calculate cost for this budget alert. This is what `quantity_threshold` is measured in. */
44
+ quantityType?: AlertConfigurationQuantityType | undefined;
45
+ /** The threshold for the budget alert to determine if it is in a triggered state. The number is evaluated based on `quantity_type`. */
46
+ quantityThreshold?: string | undefined;
47
+ /** Configured actions for this alert. These define what happens when an alert enters a triggered state. */
48
+ actionConfigurations?: ActionConfiguration[] | undefined;
49
+ }
50
+
51
+ export interface BudgetConfiguration {
52
+ /** <Databricks> budget configuration ID. */
53
+ budgetConfigurationId?: string | undefined;
54
+ /** <Databricks> account ID. */
55
+ accountId?: string | undefined;
56
+ /** Creation time of this budget configuration. */
57
+ createTime?: bigint | undefined;
58
+ /** Update time of this budget configuration. */
59
+ updateTime?: bigint | undefined;
60
+ /** Alerts to configure when this budget is in a triggered state. Budgets must have exactly one alert configuration. */
61
+ alertConfigurations?: AlertConfiguration[] | undefined;
62
+ /**
63
+ * Configured filters for this budget. These are applied to your account's usage to limit the scope of what is considered for this budget.
64
+ * Leave empty to include all usage for this account. All provided filters must be matched for usage to be included.
65
+ */
66
+ filter?: BudgetConfigurationFilter | undefined;
67
+ /** Human-readable name of budget configuration. Max Length: 128 */
68
+ displayName?: string | undefined;
69
+ }
70
+
71
+ export interface BudgetConfigurationFilter {
72
+ /** If provided, usage must match with the provided <Databricks> workspace IDs. */
73
+ workspaceId?: BudgetConfigurationFilter_WorkspaceIdClause | undefined;
74
+ /**
75
+ * A list of tag keys and values that will limit the budget to usage that includes those specific custom tags.
76
+ * Tags are case-sensitive and should be entered exactly as they appear in your usage data.
77
+ */
78
+ tags?: BudgetConfigurationFilter_TagClause[] | undefined;
79
+ }
80
+
81
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
82
+ export interface BudgetConfigurationFilter_Clause {
83
+ operator?: BudgetConfigurationFilter_Operator | undefined;
84
+ values?: string[] | undefined;
85
+ }
86
+
87
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
88
+ export interface BudgetConfigurationFilter_TagClause {
89
+ key?: string | undefined;
90
+ value?: BudgetConfigurationFilter_Clause | undefined;
91
+ }
92
+
93
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
94
+ export interface BudgetConfigurationFilter_WorkspaceIdClause {
95
+ operator?: BudgetConfigurationFilter_Operator | undefined;
96
+ values?: bigint[] | undefined;
97
+ }
98
+
99
+ export interface CreateBudgetConfigurationBudget {
100
+ /** <Databricks> budget configuration ID. */
101
+ budgetConfigurationId?: string | undefined;
102
+ /** <Databricks> account ID. */
103
+ accountId?: string | undefined;
104
+ /** Creation time of this budget configuration. */
105
+ createTime?: bigint | undefined;
106
+ /** Update time of this budget configuration. */
107
+ updateTime?: bigint | undefined;
108
+ /** Alerts to configure when this budget is in a triggered state. Budgets must have exactly one alert configuration. */
109
+ alertConfigurations?: AlertConfiguration[] | undefined;
110
+ /**
111
+ * Configured filters for this budget. These are applied to your account's usage to limit the scope of what is considered for this budget.
112
+ * Leave empty to include all usage for this account. All provided filters must be matched for usage to be included.
113
+ */
114
+ filter?: BudgetConfigurationFilter | undefined;
115
+ /** Human-readable name of budget configuration. Max Length: 128 */
116
+ displayName?: string | undefined;
117
+ }
118
+
119
+ export interface CreateBudgetConfigurationRequest {
120
+ /** Properties of the new budget configuration. */
121
+ budget?: CreateBudgetConfigurationBudget | undefined;
122
+ }
123
+
124
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
125
+ export interface CreateBudgetConfigurationRequest_Response {
126
+ /** The created budget configuration. */
127
+ budget?: BudgetConfiguration | undefined;
128
+ }
129
+
130
+ /**
131
+ * *
132
+ * Delete budget
133
+ */
134
+ export interface DeleteBudgetConfigurationRequest {
135
+ /** The <Databricks> budget configuration ID. */
136
+ budgetId?: string | undefined;
137
+ /** <Databricks> account ID. */
138
+ accountId?: string | undefined;
139
+ }
140
+
141
+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-object-type -- Proto-style nested message name.
142
+ export interface DeleteBudgetConfigurationRequest_Response {}
143
+
144
+ export interface GetBudgetConfigurationRequest {
145
+ /** The budget configuration ID */
146
+ budgetId?: string | undefined;
147
+ /** <Databricks> account ID. */
148
+ accountId?: string | undefined;
149
+ includeSpendStatus?: boolean | undefined;
150
+ }
151
+
152
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
153
+ export interface GetBudgetConfigurationRequest_Response {
154
+ budget?: BudgetConfiguration | undefined;
155
+ }
156
+
157
+ export interface ListBudgetConfigurationsRequest {
158
+ /** <Databricks> account ID. */
159
+ accountId?: string | undefined;
160
+ /**
161
+ * A page token received from a previous get all budget configurations call. This token can be used to retrieve the subsequent page.
162
+ * Requests first page if absent.
163
+ */
164
+ pageToken?: string | undefined;
165
+ includeSpendStatus?: boolean | undefined;
166
+ includeWorkspaceBudgets?: boolean | undefined;
167
+ }
168
+
169
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
170
+ export interface ListBudgetConfigurationsRequest_Response {
171
+ budgets?: BudgetConfiguration[] | undefined;
172
+ /** Token which can be sent as `page_token` to retrieve the next page of results. If this field is omitted, there are no subsequent budgets. */
173
+ nextPageToken?: string | undefined;
174
+ }
175
+
176
+ export interface UpdateBudgetConfigurationBudget {
177
+ /** <Databricks> budget configuration ID. */
178
+ budgetConfigurationId?: string | undefined;
179
+ /** <Databricks> account ID. */
180
+ accountId?: string | undefined;
181
+ /** Creation time of this budget configuration. */
182
+ createTime?: bigint | undefined;
183
+ /** Update time of this budget configuration. */
184
+ updateTime?: bigint | undefined;
185
+ /** Alerts to configure when this budget is in a triggered state. Budgets must have exactly one alert configuration. */
186
+ alertConfigurations?: AlertConfiguration[] | undefined;
187
+ /**
188
+ * Configured filters for this budget. These are applied to your account's usage to limit the scope of what is considered for this budget.
189
+ * Leave empty to include all usage for this account. All provided filters must be matched for usage to be included.
190
+ */
191
+ filter?: BudgetConfigurationFilter | undefined;
192
+ /** Human-readable name of budget configuration. Max Length: 128 */
193
+ displayName?: string | undefined;
194
+ }
195
+
196
+ export interface UpdateBudgetConfigurationRequest {
197
+ /** The <Databricks> budget configuration ID. */
198
+ budgetId?: string | undefined;
199
+ /** The updated budget. This will overwrite the budget specified by the budget ID. */
200
+ budget?: UpdateBudgetConfigurationBudget | undefined;
201
+ }
202
+
203
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
204
+ export interface UpdateBudgetConfigurationRequest_Response {
205
+ /** The updated budget. */
206
+ budget?: BudgetConfiguration | undefined;
207
+ }
208
+
209
+ export const unmarshalActionConfigurationSchema: z.ZodType<ActionConfiguration> =
210
+ z
211
+ .object({
212
+ action_configuration_id: z.string().optional(),
213
+ action_type: z.enum(ActionConfigurationType).optional(),
214
+ target: z.string().optional(),
215
+ })
216
+ .transform(d => ({
217
+ actionConfigurationId: d.action_configuration_id,
218
+ actionType: d.action_type,
219
+ target: d.target,
220
+ }));
221
+
222
+ export const unmarshalAlertConfigurationSchema: z.ZodType<AlertConfiguration> =
223
+ z
224
+ .object({
225
+ alert_configuration_id: z.string().optional(),
226
+ time_period: z.enum(AlertConfigurationTimePeriod).optional(),
227
+ trigger_type: z.enum(AlertConfigurationTriggerType).optional(),
228
+ quantity_type: z.enum(AlertConfigurationQuantityType).optional(),
229
+ quantity_threshold: z.string().optional(),
230
+ action_configurations: z
231
+ .array(z.lazy(() => unmarshalActionConfigurationSchema))
232
+ .optional(),
233
+ })
234
+ .transform(d => ({
235
+ alertConfigurationId: d.alert_configuration_id,
236
+ timePeriod: d.time_period,
237
+ triggerType: d.trigger_type,
238
+ quantityType: d.quantity_type,
239
+ quantityThreshold: d.quantity_threshold,
240
+ actionConfigurations: d.action_configurations,
241
+ }));
242
+
243
+ export const unmarshalBudgetConfigurationSchema: z.ZodType<BudgetConfiguration> =
244
+ z
245
+ .object({
246
+ budget_configuration_id: z.string().optional(),
247
+ account_id: z.string().optional(),
248
+ create_time: z
249
+ .union([z.number(), z.bigint()])
250
+ .transform(v => BigInt(v))
251
+ .optional(),
252
+ update_time: z
253
+ .union([z.number(), z.bigint()])
254
+ .transform(v => BigInt(v))
255
+ .optional(),
256
+ alert_configurations: z
257
+ .array(z.lazy(() => unmarshalAlertConfigurationSchema))
258
+ .optional(),
259
+ filter: z.lazy(() => unmarshalBudgetConfigurationFilterSchema).optional(),
260
+ display_name: z.string().optional(),
261
+ })
262
+ .transform(d => ({
263
+ budgetConfigurationId: d.budget_configuration_id,
264
+ accountId: d.account_id,
265
+ createTime: d.create_time,
266
+ updateTime: d.update_time,
267
+ alertConfigurations: d.alert_configurations,
268
+ filter: d.filter,
269
+ displayName: d.display_name,
270
+ }));
271
+
272
+ export const unmarshalBudgetConfigurationFilterSchema: z.ZodType<BudgetConfigurationFilter> =
273
+ z
274
+ .object({
275
+ workspace_id: z
276
+ .lazy(() => unmarshalBudgetConfigurationFilter_WorkspaceIdClauseSchema)
277
+ .optional(),
278
+ tags: z
279
+ .array(z.lazy(() => unmarshalBudgetConfigurationFilter_TagClauseSchema))
280
+ .optional(),
281
+ })
282
+ .transform(d => ({
283
+ workspaceId: d.workspace_id,
284
+ tags: d.tags,
285
+ }));
286
+
287
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
288
+ export const unmarshalBudgetConfigurationFilter_ClauseSchema: z.ZodType<BudgetConfigurationFilter_Clause> =
289
+ z
290
+ .object({
291
+ operator: z.enum(BudgetConfigurationFilter_Operator).optional(),
292
+ values: z.array(z.string()).optional(),
293
+ })
294
+ .transform(d => ({
295
+ operator: d.operator,
296
+ values: d.values,
297
+ }));
298
+
299
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
300
+ export const unmarshalBudgetConfigurationFilter_TagClauseSchema: z.ZodType<BudgetConfigurationFilter_TagClause> =
301
+ z
302
+ .object({
303
+ key: z.string().optional(),
304
+ value: z
305
+ .lazy(() => unmarshalBudgetConfigurationFilter_ClauseSchema)
306
+ .optional(),
307
+ })
308
+ .transform(d => ({
309
+ key: d.key,
310
+ value: d.value,
311
+ }));
312
+
313
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
314
+ export const unmarshalBudgetConfigurationFilter_WorkspaceIdClauseSchema: z.ZodType<BudgetConfigurationFilter_WorkspaceIdClause> =
315
+ z
316
+ .object({
317
+ operator: z.enum(BudgetConfigurationFilter_Operator).optional(),
318
+ values: z
319
+ .array(z.union([z.number(), z.bigint()]).transform(v => BigInt(v)))
320
+ .optional(),
321
+ })
322
+ .transform(d => ({
323
+ operator: d.operator,
324
+ values: d.values,
325
+ }));
326
+
327
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
328
+ export const unmarshalCreateBudgetConfigurationRequest_ResponseSchema: z.ZodType<CreateBudgetConfigurationRequest_Response> =
329
+ z
330
+ .object({
331
+ budget: z.lazy(() => unmarshalBudgetConfigurationSchema).optional(),
332
+ })
333
+ .transform(d => ({
334
+ budget: d.budget,
335
+ }));
336
+
337
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
338
+ export const unmarshalDeleteBudgetConfigurationRequest_ResponseSchema: z.ZodType<DeleteBudgetConfigurationRequest_Response> =
339
+ z.object({});
340
+
341
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
342
+ export const unmarshalGetBudgetConfigurationRequest_ResponseSchema: z.ZodType<GetBudgetConfigurationRequest_Response> =
343
+ z
344
+ .object({
345
+ budget: z.lazy(() => unmarshalBudgetConfigurationSchema).optional(),
346
+ })
347
+ .transform(d => ({
348
+ budget: d.budget,
349
+ }));
350
+
351
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
352
+ export const unmarshalListBudgetConfigurationsRequest_ResponseSchema: z.ZodType<ListBudgetConfigurationsRequest_Response> =
353
+ z
354
+ .object({
355
+ budgets: z
356
+ .array(z.lazy(() => unmarshalBudgetConfigurationSchema))
357
+ .optional(),
358
+ next_page_token: z.string().optional(),
359
+ })
360
+ .transform(d => ({
361
+ budgets: d.budgets,
362
+ nextPageToken: d.next_page_token,
363
+ }));
364
+
365
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
366
+ export const unmarshalUpdateBudgetConfigurationRequest_ResponseSchema: z.ZodType<UpdateBudgetConfigurationRequest_Response> =
367
+ z
368
+ .object({
369
+ budget: z.lazy(() => unmarshalBudgetConfigurationSchema).optional(),
370
+ })
371
+ .transform(d => ({
372
+ budget: d.budget,
373
+ }));
374
+
375
+ export const marshalActionConfigurationSchema: z.ZodType = z
376
+ .object({
377
+ actionConfigurationId: z.string().optional(),
378
+ actionType: z.enum(ActionConfigurationType).optional(),
379
+ target: z.string().optional(),
380
+ })
381
+ .transform(d => ({
382
+ action_configuration_id: d.actionConfigurationId,
383
+ action_type: d.actionType,
384
+ target: d.target,
385
+ }));
386
+
387
+ export const marshalAlertConfigurationSchema: z.ZodType = z
388
+ .object({
389
+ alertConfigurationId: z.string().optional(),
390
+ timePeriod: z.enum(AlertConfigurationTimePeriod).optional(),
391
+ triggerType: z.enum(AlertConfigurationTriggerType).optional(),
392
+ quantityType: z.enum(AlertConfigurationQuantityType).optional(),
393
+ quantityThreshold: z.string().optional(),
394
+ actionConfigurations: z
395
+ .array(z.lazy(() => marshalActionConfigurationSchema))
396
+ .optional(),
397
+ })
398
+ .transform(d => ({
399
+ alert_configuration_id: d.alertConfigurationId,
400
+ time_period: d.timePeriod,
401
+ trigger_type: d.triggerType,
402
+ quantity_type: d.quantityType,
403
+ quantity_threshold: d.quantityThreshold,
404
+ action_configurations: d.actionConfigurations,
405
+ }));
406
+
407
+ export const marshalBudgetConfigurationFilterSchema: z.ZodType = z
408
+ .object({
409
+ workspaceId: z
410
+ .lazy(() => marshalBudgetConfigurationFilter_WorkspaceIdClauseSchema)
411
+ .optional(),
412
+ tags: z
413
+ .array(z.lazy(() => marshalBudgetConfigurationFilter_TagClauseSchema))
414
+ .optional(),
415
+ })
416
+ .transform(d => ({
417
+ workspace_id: d.workspaceId,
418
+ tags: d.tags,
419
+ }));
420
+
421
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
422
+ export const marshalBudgetConfigurationFilter_ClauseSchema: z.ZodType = z
423
+ .object({
424
+ operator: z.enum(BudgetConfigurationFilter_Operator).optional(),
425
+ values: z.array(z.string()).optional(),
426
+ })
427
+ .transform(d => ({
428
+ operator: d.operator,
429
+ values: d.values,
430
+ }));
431
+
432
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
433
+ export const marshalBudgetConfigurationFilter_TagClauseSchema: z.ZodType = z
434
+ .object({
435
+ key: z.string().optional(),
436
+ value: z
437
+ .lazy(() => marshalBudgetConfigurationFilter_ClauseSchema)
438
+ .optional(),
439
+ })
440
+ .transform(d => ({
441
+ key: d.key,
442
+ value: d.value,
443
+ }));
444
+
445
+ // eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
446
+ export const marshalBudgetConfigurationFilter_WorkspaceIdClauseSchema: z.ZodType =
447
+ z
448
+ .object({
449
+ operator: z.enum(BudgetConfigurationFilter_Operator).optional(),
450
+ values: z.array(z.bigint()).optional(),
451
+ })
452
+ .transform(d => ({
453
+ operator: d.operator,
454
+ values: d.values,
455
+ }));
456
+
457
+ export const marshalCreateBudgetConfigurationBudgetSchema: z.ZodType = z
458
+ .object({
459
+ budgetConfigurationId: z.string().optional(),
460
+ accountId: z.string().optional(),
461
+ createTime: z.bigint().optional(),
462
+ updateTime: z.bigint().optional(),
463
+ alertConfigurations: z
464
+ .array(z.lazy(() => marshalAlertConfigurationSchema))
465
+ .optional(),
466
+ filter: z.lazy(() => marshalBudgetConfigurationFilterSchema).optional(),
467
+ displayName: z.string().optional(),
468
+ })
469
+ .transform(d => ({
470
+ budget_configuration_id: d.budgetConfigurationId,
471
+ account_id: d.accountId,
472
+ create_time: d.createTime,
473
+ update_time: d.updateTime,
474
+ alert_configurations: d.alertConfigurations,
475
+ filter: d.filter,
476
+ display_name: d.displayName,
477
+ }));
478
+
479
+ export const marshalCreateBudgetConfigurationRequestSchema: z.ZodType = z
480
+ .object({
481
+ budget: z
482
+ .lazy(() => marshalCreateBudgetConfigurationBudgetSchema)
483
+ .optional(),
484
+ })
485
+ .transform(d => ({
486
+ budget: d.budget,
487
+ }));
488
+
489
+ export const marshalUpdateBudgetConfigurationBudgetSchema: z.ZodType = z
490
+ .object({
491
+ budgetConfigurationId: z.string().optional(),
492
+ accountId: z.string().optional(),
493
+ createTime: z.bigint().optional(),
494
+ updateTime: z.bigint().optional(),
495
+ alertConfigurations: z
496
+ .array(z.lazy(() => marshalAlertConfigurationSchema))
497
+ .optional(),
498
+ filter: z.lazy(() => marshalBudgetConfigurationFilterSchema).optional(),
499
+ displayName: z.string().optional(),
500
+ })
501
+ .transform(d => ({
502
+ budget_configuration_id: d.budgetConfigurationId,
503
+ account_id: d.accountId,
504
+ create_time: d.createTime,
505
+ update_time: d.updateTime,
506
+ alert_configurations: d.alertConfigurations,
507
+ filter: d.filter,
508
+ display_name: d.displayName,
509
+ }));
510
+
511
+ export const marshalUpdateBudgetConfigurationRequestSchema: z.ZodType = z
512
+ .object({
513
+ budgetId: z.string().optional(),
514
+ budget: z
515
+ .lazy(() => marshalUpdateBudgetConfigurationBudgetSchema)
516
+ .optional(),
517
+ })
518
+ .transform(d => ({
519
+ budget_id: d.budgetId,
520
+ budget: d.budget,
521
+ }));
@@ -0,0 +1,73 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ import type {Credentials} from '@databricks/sdk-auth';
4
+ import {defaultCredentials} from '@databricks/sdk-auth/credentials';
5
+ import type {
6
+ HttpClient,
7
+ HttpRequest,
8
+ HttpResponse,
9
+ } from '@databricks/sdk-core/http';
10
+ import {newFetchHttpClient} from '@databricks/sdk-core/http';
11
+ import type {ClientOptions} from '@databricks/sdk-options/client';
12
+
13
+ /** Creates a new HTTP client with the given options. */
14
+ export function newHttpClient(options?: ClientOptions): HttpClient {
15
+ const opts = options ?? {};
16
+
17
+ // If an HTTP client is provided, use it as-is. Throw if other options are
18
+ // also set, since they would be silently ignored.
19
+ if (opts.httpClient !== undefined) {
20
+ if (opts.credentials !== undefined || opts.timeout !== undefined) {
21
+ throw new Error(
22
+ 'httpClient cannot be combined with credentials or timeout'
23
+ );
24
+ }
25
+ return opts.httpClient;
26
+ }
27
+
28
+ const credentials = opts.credentials ?? defaultCredentials();
29
+
30
+ const base = newFetchHttpClient();
31
+ let client: HttpClient = new AuthHttpClient(base, credentials);
32
+
33
+ if (opts.timeout !== undefined) {
34
+ client = new TimeoutHttpClient(client, opts.timeout);
35
+ }
36
+
37
+ return client;
38
+ }
39
+
40
+ /** Wraps an HttpClient and adds authentication headers to requests. */
41
+ class AuthHttpClient implements HttpClient {
42
+ constructor(
43
+ private readonly base: HttpClient,
44
+ private readonly credentials: Credentials
45
+ ) {}
46
+
47
+ async send(request: HttpRequest): Promise<HttpResponse> {
48
+ const authHeaders = await this.credentials.authHeaders();
49
+ // Do not modify the original request.
50
+ const headers = new Headers(request.headers);
51
+ for (const h of authHeaders) {
52
+ headers.set(h.key, h.value);
53
+ }
54
+ return this.base.send({...request, headers});
55
+ }
56
+ }
57
+
58
+ /** Wraps an HttpClient and applies a default timeout to requests. */
59
+ class TimeoutHttpClient implements HttpClient {
60
+ constructor(
61
+ private readonly base: HttpClient,
62
+ private readonly timeout: number
63
+ ) {}
64
+
65
+ async send(request: HttpRequest): Promise<HttpResponse> {
66
+ const timeoutSignal = AbortSignal.timeout(this.timeout);
67
+ const signal =
68
+ request.signal !== undefined
69
+ ? AbortSignal.any([request.signal, timeoutSignal])
70
+ : timeoutSignal;
71
+ return this.base.send({...request, signal});
72
+ }
73
+ }