@databricks/sdk-repos 0.1.0-dev.3 → 0.1.0-dev.5

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.
package/src/v1/client.ts DELETED
@@ -1,253 +0,0 @@
1
- // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
-
3
- import {VERSION as AUTH_VERSION} from '@databricks/sdk-auth';
4
- import {createDefault} from '@databricks/sdk-core/clientinfo';
5
- import type {Logger} from '@databricks/sdk-core/logger';
6
- import {NoOpLogger} from '@databricks/sdk-core/logger';
7
- import type {CallOptions} from '@databricks/sdk-options/call';
8
- import type {ClientOptions} from '@databricks/sdk-options/client';
9
- import type {HttpClient} from '@databricks/sdk-core/http';
10
- import {newHttpClient} from './transport';
11
- import {
12
- buildHttpRequest,
13
- executeCall,
14
- executeHttpCall,
15
- marshalRequest,
16
- parseResponse,
17
- } from './utils';
18
- import pkgJson from '../../package.json' with {type: 'json'};
19
- import type {
20
- CreateRepoRequest,
21
- CreateRepoResponse,
22
- DeleteProjectRequest,
23
- DeleteProjectResponse,
24
- GetRepoRequest,
25
- GetRepoResponse,
26
- ListReposRequest,
27
- ListReposResponse,
28
- RepoInfo,
29
- UpdateRepoRequest,
30
- UpdateRepoResponse,
31
- } from './model';
32
- import {
33
- marshalCreateRepoRequestSchema,
34
- marshalUpdateRepoRequestSchema,
35
- unmarshalCreateRepoResponseSchema,
36
- unmarshalDeleteProjectResponseSchema,
37
- unmarshalGetRepoResponseSchema,
38
- unmarshalListReposResponseSchema,
39
- unmarshalUpdateRepoResponseSchema,
40
- } from './model';
41
-
42
- // Package identity segment for this client to be used in the User-Agent header.
43
- const PACKAGE_SEGMENT = {
44
- key: 'sdk-js-' + pkgJson.name.replace(/^@[^/]+\/sdk-/, ''),
45
- value: pkgJson.version,
46
- };
47
-
48
- export class ReposClient {
49
- private readonly host: string;
50
- // Workspace ID used to route workspace-level calls on unified hosts (SPOG).
51
- // When set, workspace-level methods send X-Databricks-Org-Id on every
52
- // request.
53
- private readonly workspaceId: string | undefined;
54
- private readonly httpClient: HttpClient;
55
- private readonly logger: Logger;
56
- // User-Agent header value. Composed once at construction from
57
- // createDefault() merged with this package's identity and the active
58
- // credential's name.
59
- private readonly userAgent: string;
60
-
61
- constructor(options: ClientOptions) {
62
- if (options.host === undefined) {
63
- throw new Error('Host is required.');
64
- }
65
- this.host = options.host.replace(/\/$/, '');
66
- this.workspaceId = options.workspaceId;
67
- this.logger = options.logger ?? new NoOpLogger();
68
- const info = createDefault()
69
- .with(PACKAGE_SEGMENT)
70
- .with({key: 'sdk-js-auth', value: AUTH_VERSION})
71
- .with({key: 'auth', value: options.credentials?.name() ?? 'default'});
72
- this.userAgent = info.toString();
73
- this.httpClient = newHttpClient(options);
74
- }
75
-
76
- /**
77
- * Creates a repo in the workspace and links it to the remote Git repo specified.
78
- * Note that repos created programmatically must be linked to a remote Git repo, unlike repos
79
- * created in the browser.
80
- */
81
- async createRepo(
82
- req: CreateRepoRequest,
83
- options?: CallOptions
84
- ): Promise<CreateRepoResponse> {
85
- const url = `${this.host}/api/2.0/repos`;
86
- const body = marshalRequest(req, marshalCreateRepoRequestSchema);
87
- let resp: CreateRepoResponse | undefined;
88
- const call = async (callSignal?: AbortSignal): Promise<void> => {
89
- const headers = new Headers({'Content-Type': 'application/json'});
90
- if (this.workspaceId !== undefined) {
91
- headers.set('X-Databricks-Org-Id', this.workspaceId);
92
- }
93
- headers.set('User-Agent', this.userAgent);
94
- const httpReq = buildHttpRequest('POST', url, headers, callSignal, body);
95
- const respBody = await executeHttpCall({
96
- request: httpReq,
97
- httpClient: this.httpClient,
98
- logger: this.logger,
99
- });
100
- resp = parseResponse(respBody, unmarshalCreateRepoResponseSchema);
101
- };
102
- await executeCall(call, options);
103
- if (resp === undefined) {
104
- throw new Error('operation completed without a result.');
105
- }
106
- return resp;
107
- }
108
-
109
- /** Deletes the specified repo. */
110
- async deleteProject(
111
- req: DeleteProjectRequest,
112
- options?: CallOptions
113
- ): Promise<DeleteProjectResponse> {
114
- const url = `${this.host}/api/2.0/repos/${String(req.id ?? '')}`;
115
- let resp: DeleteProjectResponse | undefined;
116
- const call = async (callSignal?: AbortSignal): Promise<void> => {
117
- const headers = new Headers();
118
- if (this.workspaceId !== undefined) {
119
- headers.set('X-Databricks-Org-Id', this.workspaceId);
120
- }
121
- headers.set('User-Agent', this.userAgent);
122
- const httpReq = buildHttpRequest('DELETE', url, headers, callSignal);
123
- const respBody = await executeHttpCall({
124
- request: httpReq,
125
- httpClient: this.httpClient,
126
- logger: this.logger,
127
- });
128
- resp = parseResponse(respBody, unmarshalDeleteProjectResponseSchema);
129
- };
130
- await executeCall(call, options);
131
- if (resp === undefined) {
132
- throw new Error('operation completed without a result.');
133
- }
134
- return resp;
135
- }
136
-
137
- /** Returns the repo with the given repo ID. */
138
- async getRepo(
139
- req: GetRepoRequest,
140
- options?: CallOptions
141
- ): Promise<GetRepoResponse> {
142
- const url = `${this.host}/api/2.0/repos/${String(req.id ?? '')}`;
143
- let resp: GetRepoResponse | undefined;
144
- const call = async (callSignal?: AbortSignal): Promise<void> => {
145
- const headers = new Headers();
146
- if (this.workspaceId !== undefined) {
147
- headers.set('X-Databricks-Org-Id', this.workspaceId);
148
- }
149
- headers.set('User-Agent', this.userAgent);
150
- const httpReq = buildHttpRequest('GET', url, headers, callSignal);
151
- const respBody = await executeHttpCall({
152
- request: httpReq,
153
- httpClient: this.httpClient,
154
- logger: this.logger,
155
- });
156
- resp = parseResponse(respBody, unmarshalGetRepoResponseSchema);
157
- };
158
- await executeCall(call, options);
159
- if (resp === undefined) {
160
- throw new Error('operation completed without a result.');
161
- }
162
- return resp;
163
- }
164
-
165
- /**
166
- * Returns repos that the calling user has Manage permissions on.
167
- * Use `next_page_token` to iterate through additional pages.
168
- */
169
- async listRepos(
170
- req: ListReposRequest,
171
- options?: CallOptions
172
- ): Promise<ListReposResponse> {
173
- const url = `${this.host}/api/2.0/repos`;
174
- const params = new URLSearchParams();
175
- if (req.pathPrefix !== undefined) {
176
- params.append('path_prefix', req.pathPrefix);
177
- }
178
- if (req.nextPageToken !== undefined) {
179
- params.append('next_page_token', req.nextPageToken);
180
- }
181
- const query = params.toString();
182
- const fullUrl = query !== '' ? `${url}?${query}` : url;
183
- let resp: ListReposResponse | undefined;
184
- const call = async (callSignal?: AbortSignal): Promise<void> => {
185
- const headers = new Headers();
186
- if (this.workspaceId !== undefined) {
187
- headers.set('X-Databricks-Org-Id', this.workspaceId);
188
- }
189
- headers.set('User-Agent', this.userAgent);
190
- const httpReq = buildHttpRequest('GET', fullUrl, headers, callSignal);
191
- const respBody = await executeHttpCall({
192
- request: httpReq,
193
- httpClient: this.httpClient,
194
- logger: this.logger,
195
- });
196
- resp = parseResponse(respBody, unmarshalListReposResponseSchema);
197
- };
198
- await executeCall(call, options);
199
- if (resp === undefined) {
200
- throw new Error('operation completed without a result.');
201
- }
202
- return resp;
203
- }
204
-
205
- async *listReposIter(
206
- req: ListReposRequest,
207
- options?: CallOptions
208
- ): AsyncGenerator<RepoInfo> {
209
- const pageReq: ListReposRequest = {...req};
210
- for (;;) {
211
- const resp = await this.listRepos(pageReq, options);
212
- for (const item of resp.repos ?? []) {
213
- yield item;
214
- }
215
- if (resp.nextPageToken === undefined || resp.nextPageToken === '') {
216
- return;
217
- }
218
- pageReq.nextPageToken = resp.nextPageToken;
219
- }
220
- }
221
-
222
- /**
223
- * Updates the repo to a different branch or tag, or updates the repo to the latest commit on
224
- * the same branch.
225
- */
226
- async updateRepo(
227
- req: UpdateRepoRequest,
228
- options?: CallOptions
229
- ): Promise<UpdateRepoResponse> {
230
- const url = `${this.host}/api/2.0/repos/${String(req.id ?? '')}`;
231
- const body = marshalRequest(req, marshalUpdateRepoRequestSchema);
232
- let resp: UpdateRepoResponse | undefined;
233
- const call = async (callSignal?: AbortSignal): Promise<void> => {
234
- const headers = new Headers({'Content-Type': 'application/json'});
235
- if (this.workspaceId !== undefined) {
236
- headers.set('X-Databricks-Org-Id', this.workspaceId);
237
- }
238
- headers.set('User-Agent', this.userAgent);
239
- const httpReq = buildHttpRequest('PATCH', url, headers, callSignal, body);
240
- const respBody = await executeHttpCall({
241
- request: httpReq,
242
- httpClient: this.httpClient,
243
- logger: this.logger,
244
- });
245
- resp = parseResponse(respBody, unmarshalUpdateRepoResponseSchema);
246
- };
247
- await executeCall(call, options);
248
- if (resp === undefined) {
249
- throw new Error('operation completed without a result.');
250
- }
251
- return resp;
252
- }
253
- }
package/src/v1/index.ts DELETED
@@ -1,19 +0,0 @@
1
- // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
-
3
- export {ReposClient} from './client';
4
-
5
- export type {
6
- CreateRepoRequest,
7
- CreateRepoResponse,
8
- DeleteProjectRequest,
9
- DeleteProjectResponse,
10
- GetRepoRequest,
11
- GetRepoResponse,
12
- ListReposRequest,
13
- ListReposResponse,
14
- RepoInfo,
15
- SparseCheckout,
16
- SparseCheckoutUpdate,
17
- UpdateRepoRequest,
18
- UpdateRepoResponse,
19
- } from './model';
package/src/v1/model.ts DELETED
@@ -1,306 +0,0 @@
1
- // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
-
3
- import {z} from 'zod';
4
-
5
- export interface CreateRepoRequest {
6
- /** URL of the Git repository to be linked. */
7
- url?: string | undefined;
8
- /**
9
- * Git provider. This field is case-insensitive. The available Git providers are `gitHub`,
10
- * `bitbucketCloud`, `gitLab`, `azureDevOpsServices` (Azure DevOps Services, including
11
- * Microsoft Entra ID authentication), `gitHubEnterprise`, `bitbucketServer` (Bitbucket
12
- * Data Center), `gitLabEnterpriseEdition` (GitLab Self-Managed), and `awsCodeCommit`
13
- * (deprecated by AWS, not accepting new customers).
14
- */
15
- provider?: string | undefined;
16
- /**
17
- * Desired path for the repo in the workspace. Almost any path in the workspace can be chosen.
18
- * If repo is created in `/Repos`, path must be in the format `/Repos/{folder}/{repo-name}`.
19
- */
20
- path?: string | undefined;
21
- /**
22
- * If specified, the repo will be created with sparse checkout enabled. You cannot enable/disable
23
- * sparse checkout after the repo is created.
24
- */
25
- sparseCheckout?: SparseCheckout | undefined;
26
- }
27
-
28
- export interface CreateRepoResponse {
29
- /** ID of the Git folder (repo) object in the workspace. */
30
- id?: bigint | undefined;
31
- /** Path of the Git folder (repo) in the workspace. */
32
- path?: string | undefined;
33
- /** URL of the linked Git repository. */
34
- url?: string | undefined;
35
- /**
36
- * Git provider of the linked Git repository, e.g. `gitHub`, `azureDevOpsServices`,
37
- * `bitbucketServer` (Bitbucket Data Center), `gitLabEnterpriseEdition` (GitLab
38
- * Self-Managed), or `awsCodeCommit` (deprecated).
39
- */
40
- provider?: string | undefined;
41
- /** Branch that the Git folder (repo) is checked out to. */
42
- branch?: string | undefined;
43
- /** SHA-1 hash representing the commit ID of the current HEAD of the Git folder (repo). */
44
- headCommitId?: string | undefined;
45
- /** Sparse checkout settings for the Git folder (repo). */
46
- sparseCheckout?: SparseCheckout | undefined;
47
- }
48
-
49
- export interface DeleteProjectRequest {
50
- /** The ID for the corresponding repo to delete. */
51
- id?: bigint | undefined;
52
- }
53
-
54
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
55
- export interface DeleteProjectResponse {}
56
-
57
- export interface GetRepoRequest {
58
- /** ID of the Git folder (repo) object in the workspace. */
59
- id?: bigint | undefined;
60
- }
61
-
62
- export interface GetRepoResponse {
63
- /** ID of the Git folder (repo) object in the workspace. */
64
- id?: bigint | undefined;
65
- /** Path of the Git folder (repo) in the workspace. */
66
- path?: string | undefined;
67
- /** URL of the linked Git repository. */
68
- url?: string | undefined;
69
- /**
70
- * Git provider of the linked Git repository, e.g. `gitHub`, `azureDevOpsServices`,
71
- * `bitbucketServer` (Bitbucket Data Center), `gitLabEnterpriseEdition` (GitLab
72
- * Self-Managed), or `awsCodeCommit` (deprecated).
73
- */
74
- provider?: string | undefined;
75
- /** Branch that the local version of the repo is checked out to. */
76
- branch?: string | undefined;
77
- /** SHA-1 hash representing the commit ID of the current HEAD of the repo. */
78
- headCommitId?: string | undefined;
79
- /** Sparse checkout settings for the Git folder (repo). */
80
- sparseCheckout?: SparseCheckout | undefined;
81
- }
82
-
83
- export interface ListReposRequest {
84
- /**
85
- * Filters repos that have paths starting with the given path prefix.
86
- * If not provided or when provided an effectively empty prefix (`/` or `/Workspace`)
87
- * Git folders (repos) from `/Workspace/Repos` will be served.
88
- */
89
- pathPrefix?: string | undefined;
90
- /**
91
- * Token used to get the next page of results. If not specified, returns the first page of
92
- * results as well as a next page token if there are more results.
93
- */
94
- nextPageToken?: string | undefined;
95
- }
96
-
97
- export interface ListReposResponse {
98
- /** List of Git folders (repos). */
99
- repos?: RepoInfo[] | undefined;
100
- /**
101
- * Token that can be specified as a query parameter to the `GET /repos` endpoint to retrieve
102
- * the next page of results.
103
- */
104
- nextPageToken?: string | undefined;
105
- }
106
-
107
- /** Git folder (repo) information. */
108
- export interface RepoInfo {
109
- /** Id of the git folder (repo) in the Workspace. */
110
- id?: bigint | undefined;
111
- /** Root path of the git folder (repo) in the Workspace. */
112
- path?: string | undefined;
113
- /** URL of the remote git repository. */
114
- url?: string | undefined;
115
- /**
116
- * Git provider of the remote git repository, e.g. `gitHub`, `azureDevOpsServices`,
117
- * `bitbucketServer` (Bitbucket Data Center), `gitLabEnterpriseEdition` (GitLab
118
- * Self-Managed), or `awsCodeCommit` (deprecated).
119
- */
120
- provider?: string | undefined;
121
- /** Name of the current git branch of the git folder (repo). */
122
- branch?: string | undefined;
123
- /** Current git commit id of the git folder (repo). */
124
- headCommitId?: string | undefined;
125
- /** Sparse checkout config for the git folder (repo). */
126
- sparseCheckout?: SparseCheckout | undefined;
127
- }
128
-
129
- /** Sparse checkout configuration, it contains options like cone patterns. */
130
- export interface SparseCheckout {
131
- /**
132
- * List of sparse checkout cone patterns, see
133
- * [cone mode handling](https://git-scm.com/docs/git-sparse-checkout#_internalscone_mode_handling)
134
- * for details.
135
- */
136
- patterns?: string[] | undefined;
137
- }
138
-
139
- /** Sparse checkout configuration, it contains options like cone patterns. */
140
- export interface SparseCheckoutUpdate {
141
- /**
142
- * List of sparse checkout cone patterns, see
143
- * [cone mode handling](https://git-scm.com/docs/git-sparse-checkout#_internalscone_mode_handling)
144
- * for details.
145
- */
146
- patterns?: string[] | undefined;
147
- }
148
-
149
- export interface UpdateRepoRequest {
150
- /** ID of the Git folder (repo) object in the workspace. */
151
- id?: bigint | undefined;
152
- /** Branch that the local version of the repo is checked out to. */
153
- branch?: string | undefined;
154
- /**
155
- * Tag that the local version of the repo is checked out to. Updating the repo to a tag puts
156
- * the repo in a detached HEAD state. Before committing new changes, you must update the repo to
157
- * a branch instead of the detached HEAD.
158
- */
159
- tag?: string | undefined;
160
- /**
161
- * If specified, update the sparse checkout settings. The update will fail if sparse checkout is
162
- * not enabled for the repo.
163
- */
164
- sparseCheckout?: SparseCheckoutUpdate | undefined;
165
- }
166
-
167
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
168
- export interface UpdateRepoResponse {}
169
-
170
- export const unmarshalCreateRepoResponseSchema: z.ZodType<CreateRepoResponse> =
171
- z
172
- .object({
173
- id: z
174
- .union([z.number(), z.bigint()])
175
- .transform(v => BigInt(v))
176
- .optional(),
177
- path: z.string().optional(),
178
- url: z.string().optional(),
179
- provider: z.string().optional(),
180
- branch: z.string().optional(),
181
- head_commit_id: z.string().optional(),
182
- sparse_checkout: z.lazy(() => unmarshalSparseCheckoutSchema).optional(),
183
- })
184
- .transform(d => ({
185
- id: d.id,
186
- path: d.path,
187
- url: d.url,
188
- provider: d.provider,
189
- branch: d.branch,
190
- headCommitId: d.head_commit_id,
191
- sparseCheckout: d.sparse_checkout,
192
- }));
193
-
194
- export const unmarshalDeleteProjectResponseSchema: z.ZodType<DeleteProjectResponse> =
195
- z.object({});
196
-
197
- export const unmarshalGetRepoResponseSchema: z.ZodType<GetRepoResponse> = z
198
- .object({
199
- id: z
200
- .union([z.number(), z.bigint()])
201
- .transform(v => BigInt(v))
202
- .optional(),
203
- path: z.string().optional(),
204
- url: z.string().optional(),
205
- provider: z.string().optional(),
206
- branch: z.string().optional(),
207
- head_commit_id: z.string().optional(),
208
- sparse_checkout: z.lazy(() => unmarshalSparseCheckoutSchema).optional(),
209
- })
210
- .transform(d => ({
211
- id: d.id,
212
- path: d.path,
213
- url: d.url,
214
- provider: d.provider,
215
- branch: d.branch,
216
- headCommitId: d.head_commit_id,
217
- sparseCheckout: d.sparse_checkout,
218
- }));
219
-
220
- export const unmarshalListReposResponseSchema: z.ZodType<ListReposResponse> = z
221
- .object({
222
- repos: z.array(z.lazy(() => unmarshalRepoInfoSchema)).optional(),
223
- next_page_token: z.string().optional(),
224
- })
225
- .transform(d => ({
226
- repos: d.repos,
227
- nextPageToken: d.next_page_token,
228
- }));
229
-
230
- export const unmarshalRepoInfoSchema: z.ZodType<RepoInfo> = z
231
- .object({
232
- id: z
233
- .union([z.number(), z.bigint()])
234
- .transform(v => BigInt(v))
235
- .optional(),
236
- path: z.string().optional(),
237
- url: z.string().optional(),
238
- provider: z.string().optional(),
239
- branch: z.string().optional(),
240
- head_commit_id: z.string().optional(),
241
- sparse_checkout: z.lazy(() => unmarshalSparseCheckoutSchema).optional(),
242
- })
243
- .transform(d => ({
244
- id: d.id,
245
- path: d.path,
246
- url: d.url,
247
- provider: d.provider,
248
- branch: d.branch,
249
- headCommitId: d.head_commit_id,
250
- sparseCheckout: d.sparse_checkout,
251
- }));
252
-
253
- export const unmarshalSparseCheckoutSchema: z.ZodType<SparseCheckout> = z
254
- .object({
255
- patterns: z.array(z.string()).optional(),
256
- })
257
- .transform(d => ({
258
- patterns: d.patterns,
259
- }));
260
-
261
- export const unmarshalUpdateRepoResponseSchema: z.ZodType<UpdateRepoResponse> =
262
- z.object({});
263
-
264
- export const marshalCreateRepoRequestSchema: z.ZodType = z
265
- .object({
266
- url: z.string().optional(),
267
- provider: z.string().optional(),
268
- path: z.string().optional(),
269
- sparseCheckout: z.lazy(() => marshalSparseCheckoutSchema).optional(),
270
- })
271
- .transform(d => ({
272
- url: d.url,
273
- provider: d.provider,
274
- path: d.path,
275
- sparse_checkout: d.sparseCheckout,
276
- }));
277
-
278
- export const marshalSparseCheckoutSchema: z.ZodType = z
279
- .object({
280
- patterns: z.array(z.string()).optional(),
281
- })
282
- .transform(d => ({
283
- patterns: d.patterns,
284
- }));
285
-
286
- export const marshalSparseCheckoutUpdateSchema: z.ZodType = z
287
- .object({
288
- patterns: z.array(z.string()).optional(),
289
- })
290
- .transform(d => ({
291
- patterns: d.patterns,
292
- }));
293
-
294
- export const marshalUpdateRepoRequestSchema: z.ZodType = z
295
- .object({
296
- id: z.bigint().optional(),
297
- branch: z.string().optional(),
298
- tag: z.string().optional(),
299
- sparseCheckout: z.lazy(() => marshalSparseCheckoutUpdateSchema).optional(),
300
- })
301
- .transform(d => ({
302
- id: d.id,
303
- branch: d.branch,
304
- tag: d.tag,
305
- sparse_checkout: d.sparseCheckout,
306
- }));
@@ -1,73 +0,0 @@
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
- }