@databricks/sdk-alerts 0.0.0-dev → 0.1.0-dev.1

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.
Files changed (54) hide show
  1. package/LICENSE +203 -0
  2. package/dist/v1/client.d.ts +23 -0
  3. package/dist/v1/client.d.ts.map +1 -0
  4. package/dist/v1/client.js +185 -0
  5. package/dist/v1/client.js.map +1 -0
  6. package/dist/v1/index.d.ts +5 -0
  7. package/dist/v1/index.d.ts.map +1 -0
  8. package/dist/v1/index.js +5 -0
  9. package/dist/v1/index.js.map +1 -0
  10. package/dist/v1/model.d.ts +235 -0
  11. package/dist/v1/model.d.ts.map +1 -0
  12. package/dist/v1/model.js +384 -0
  13. package/dist/v1/model.js.map +1 -0
  14. package/dist/v1/transport.d.ts +5 -0
  15. package/dist/v1/transport.d.ts.map +1 -0
  16. package/dist/v1/transport.js +57 -0
  17. package/dist/v1/transport.js.map +1 -0
  18. package/dist/v1/utils.d.ts +22 -0
  19. package/dist/v1/utils.d.ts.map +1 -0
  20. package/dist/v1/utils.js +113 -0
  21. package/dist/v1/utils.js.map +1 -0
  22. package/dist/v2/client.d.ts +23 -0
  23. package/dist/v2/client.d.ts.map +1 -0
  24. package/dist/v2/client.js +197 -0
  25. package/dist/v2/client.js.map +1 -0
  26. package/dist/v2/index.d.ts +5 -0
  27. package/dist/v2/index.d.ts.map +1 -0
  28. package/dist/v2/index.js +5 -0
  29. package/dist/v2/index.js.map +1 -0
  30. package/dist/v2/model.d.ts +233 -0
  31. package/dist/v2/model.d.ts.map +1 -0
  32. package/dist/v2/model.js +459 -0
  33. package/dist/v2/model.js.map +1 -0
  34. package/dist/v2/transport.d.ts +5 -0
  35. package/dist/v2/transport.d.ts.map +1 -0
  36. package/dist/v2/transport.js +57 -0
  37. package/dist/v2/transport.js.map +1 -0
  38. package/dist/v2/utils.d.ts +22 -0
  39. package/dist/v2/utils.d.ts.map +1 -0
  40. package/dist/v2/utils.js +113 -0
  41. package/dist/v2/utils.js.map +1 -0
  42. package/package.json +42 -4
  43. package/src/v1/client.ts +237 -0
  44. package/src/v1/index.ts +25 -0
  45. package/src/v1/model.ts +620 -0
  46. package/src/v1/transport.ts +73 -0
  47. package/src/v1/utils.ts +156 -0
  48. package/src/v2/client.ts +253 -0
  49. package/src/v2/index.ts +32 -0
  50. package/src/v2/model.ts +669 -0
  51. package/src/v2/transport.ts +73 -0
  52. package/src/v2/utils.ts +156 -0
  53. package/README.md +0 -1
  54. package/index.js +0 -1
@@ -0,0 +1,156 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ import type {Call, Options} from '@databricks/sdk-core/api';
4
+ import {execute} from '@databricks/sdk-core/api';
5
+ import {ApiError} from '@databricks/sdk-core/apierror';
6
+ import type {
7
+ HttpClient,
8
+ HttpRequest,
9
+ HttpResponse,
10
+ } from '@databricks/sdk-core/http';
11
+ import type {Logger} from '@databricks/sdk-core/logger';
12
+ import type {CallOptions} from '@databricks/sdk-options/call';
13
+ import JSONBig from 'json-bigint';
14
+ import type {z} from 'zod';
15
+
16
+ // JSON codec that preserves int64 precision. On the way in, large integer
17
+ // literals come back as bigint instead of being rounded to JS Number. On the
18
+ // way out, bigint values are emitted as raw JSON number digits.
19
+ const jsonBigint = JSONBig({useNativeBigInt: true});
20
+
21
+ export interface HttpCallOptions {
22
+ readonly request: HttpRequest;
23
+ readonly httpClient: HttpClient;
24
+ readonly logger: Logger;
25
+ }
26
+
27
+ /**
28
+ * Translates public CallOptions to the internal Options shape accepted by
29
+ * execute(). Even though the shapes match today, this isolates the public
30
+ * API from the executor's internal type so they can diverge.
31
+ */
32
+ export async function executeCall(
33
+ call: Call,
34
+ options?: CallOptions
35
+ ): Promise<void> {
36
+ const opts: Options = {
37
+ ...(options?.retrier !== undefined && {retrier: options.retrier}),
38
+ ...(options?.rateLimiter !== undefined && {
39
+ rateLimiter: options.rateLimiter,
40
+ }),
41
+ ...(options?.timeout !== undefined && {timeout: options.timeout}),
42
+ };
43
+ return execute(options?.signal, call, opts);
44
+ }
45
+
46
+ async function readAll(
47
+ body: ReadableStream<Uint8Array> | null
48
+ ): Promise<Uint8Array> {
49
+ if (body === null) {
50
+ return new Uint8Array(0);
51
+ }
52
+ const reader = body.getReader();
53
+ const chunks: Uint8Array[] = [];
54
+ for (;;) {
55
+ const {done, value} = await reader.read();
56
+ if (done) {
57
+ break;
58
+ }
59
+ chunks.push(value);
60
+ }
61
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
62
+ const result = new Uint8Array(totalLength);
63
+ let offset = 0;
64
+ for (const chunk of chunks) {
65
+ result.set(chunk, offset);
66
+ offset += chunk.length;
67
+ }
68
+ return result;
69
+ }
70
+
71
+ export async function executeHttpCall(
72
+ opts: HttpCallOptions
73
+ ): Promise<Uint8Array> {
74
+ opts.logger.debug('HTTP request', {
75
+ method: opts.request.method,
76
+ url: opts.request.url,
77
+ });
78
+
79
+ let resp: HttpResponse;
80
+ try {
81
+ resp = await opts.httpClient.send(opts.request);
82
+ } catch (e: unknown) {
83
+ opts.logger.debug('HTTP request failed');
84
+ throw e;
85
+ }
86
+
87
+ const body = await readAll(resp.body);
88
+
89
+ opts.logger.debug('HTTP response', {
90
+ statusCode: resp.statusCode,
91
+ body: new TextDecoder().decode(body),
92
+ });
93
+
94
+ const apiErr = ApiError.fromHttpError(resp.statusCode, resp.headers, body);
95
+ if (apiErr !== undefined) {
96
+ throw apiErr;
97
+ }
98
+
99
+ return body;
100
+ }
101
+
102
+ export function buildHttpRequest(
103
+ method: string,
104
+ url: string,
105
+ headers: Headers,
106
+ signal?: AbortSignal,
107
+ body?: string | ReadableStream<Uint8Array>
108
+ ): HttpRequest {
109
+ const req: HttpRequest = {url, method, headers};
110
+ if (body !== undefined) {
111
+ req.body = body;
112
+ }
113
+ if (signal !== undefined) {
114
+ req.signal = signal;
115
+ }
116
+ return req;
117
+ }
118
+
119
+ export function parseResponse<T>(body: Uint8Array, schema: z.ZodType<T>): T {
120
+ const text = new TextDecoder().decode(body);
121
+ const parsed: unknown = jsonBigint.parse(text);
122
+ return schema.parse(parsed);
123
+ }
124
+
125
+ export function marshalRequest(data: unknown, schema: z.ZodType): string {
126
+ return jsonBigint.stringify(schema.parse(data));
127
+ }
128
+
129
+ export function flattenQueryParams(
130
+ prefix: string,
131
+ value: unknown,
132
+ params: URLSearchParams
133
+ ): void {
134
+ if (value === null || value === undefined) {
135
+ return;
136
+ }
137
+ if (Array.isArray(value)) {
138
+ // arrays of objects are not yet supported
139
+ for (const item of value) {
140
+ params.append(prefix, String(item));
141
+ }
142
+ } else if (typeof value === 'object') {
143
+ for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
144
+ flattenQueryParams(`${prefix}.${key}`, val, params);
145
+ }
146
+ } else if (
147
+ typeof value === 'string' ||
148
+ typeof value === 'number' ||
149
+ typeof value === 'boolean' ||
150
+ typeof value === 'bigint'
151
+ ) {
152
+ params.append(prefix, String(value));
153
+ } else {
154
+ throw new Error(`Unsupported query parameter type: ${typeof value}`);
155
+ }
156
+ }
@@ -0,0 +1,253 @@
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 type {Call} from '@databricks/sdk-core/api';
5
+ import {createDefault} from '@databricks/sdk-core/clientinfo';
6
+ import type {Logger} from '@databricks/sdk-core/logger';
7
+ import {NoOpLogger} from '@databricks/sdk-core/logger';
8
+ import type {CallOptions} from '@databricks/sdk-options/call';
9
+ import type {ClientOptions} from '@databricks/sdk-options/client';
10
+ import type {HttpClient} from '@databricks/sdk-core/http';
11
+ import {newHttpClient} from './transport';
12
+ import {
13
+ buildHttpRequest,
14
+ executeCall,
15
+ executeHttpCall,
16
+ marshalRequest,
17
+ parseResponse,
18
+ } from './utils';
19
+ import pkgJson from '../../package.json' with {type: 'json'};
20
+ import type {
21
+ Alert,
22
+ CreateAlertRequest,
23
+ Empty,
24
+ GetAlertRequest,
25
+ ListAlertsRequest,
26
+ ListAlertsResponse,
27
+ TrashAlertRequest,
28
+ UpdateAlertRequest,
29
+ } from './model';
30
+ import {
31
+ marshalAlertSchema,
32
+ unmarshalAlertSchema,
33
+ unmarshalEmptySchema,
34
+ unmarshalListAlertsResponseSchema,
35
+ } from './model';
36
+
37
+ // Package identity segment for this client to be used in the User-Agent header.
38
+ const PACKAGE_SEGMENT = {
39
+ key: 'sdk-js-' + pkgJson.name.replace(/^@[^/]+\/sdk-/, ''),
40
+ value: pkgJson.version,
41
+ };
42
+
43
+ export class AlertsClient {
44
+ private readonly host: string;
45
+ // Workspace ID used to route workspace-level calls on unified hosts (SPOG).
46
+ // When set, workspace-level methods send X-Databricks-Org-Id on every
47
+ // request.
48
+ private readonly workspaceId: string | undefined;
49
+ private readonly httpClient: HttpClient;
50
+ private readonly logger: Logger;
51
+ // User-Agent header value. Composed once at construction from
52
+ // createDefault() merged with this package's identity and the active
53
+ // credential's name.
54
+ private readonly userAgent: string;
55
+
56
+ constructor(options: ClientOptions) {
57
+ if (options.host === undefined) {
58
+ throw new Error('Host is required.');
59
+ }
60
+ this.host = options.host.replace(/\/$/, '');
61
+ this.workspaceId = options.workspaceId;
62
+ this.logger = options.logger ?? new NoOpLogger();
63
+ const info = createDefault()
64
+ .with(PACKAGE_SEGMENT)
65
+ .with({key: 'sdk-js-auth', value: AUTH_VERSION})
66
+ .with({key: 'auth', value: options.credentials?.name() ?? 'default'});
67
+ this.userAgent = info.toString();
68
+ this.httpClient = newHttpClient(options);
69
+ }
70
+
71
+ /** Create Alert */
72
+ async createAlert(
73
+ req: CreateAlertRequest,
74
+ options?: CallOptions
75
+ ): Promise<Alert> {
76
+ const url = `${this.host}/api/2.0/alerts`;
77
+ const body = marshalRequest(req.alert, marshalAlertSchema);
78
+ let resp: Alert | undefined;
79
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
80
+ const headers = new Headers({'Content-Type': 'application/json'});
81
+ if (this.workspaceId !== undefined) {
82
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
83
+ }
84
+ headers.set('User-Agent', this.userAgent);
85
+ const httpReq = buildHttpRequest('POST', url, headers, callSignal, body);
86
+ const respBody = await executeHttpCall({
87
+ request: httpReq,
88
+ httpClient: this.httpClient,
89
+ logger: this.logger,
90
+ });
91
+ resp = parseResponse(respBody, unmarshalAlertSchema);
92
+ };
93
+ await executeCall(call, options);
94
+ if (resp === undefined) {
95
+ throw new Error('API call completed without a result.');
96
+ }
97
+ return resp;
98
+ }
99
+
100
+ /** Gets an alert. */
101
+ async getAlert(req: GetAlertRequest, options?: CallOptions): Promise<Alert> {
102
+ const url = `${this.host}/api/2.0/alerts/${req.id ?? ''}`;
103
+ let resp: Alert | undefined;
104
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
105
+ const headers = new Headers();
106
+ if (this.workspaceId !== undefined) {
107
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
108
+ }
109
+ headers.set('User-Agent', this.userAgent);
110
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
111
+ const respBody = await executeHttpCall({
112
+ request: httpReq,
113
+ httpClient: this.httpClient,
114
+ logger: this.logger,
115
+ });
116
+ resp = parseResponse(respBody, unmarshalAlertSchema);
117
+ };
118
+ await executeCall(call, options);
119
+ if (resp === undefined) {
120
+ throw new Error('API call completed without a result.');
121
+ }
122
+ return resp;
123
+ }
124
+
125
+ /** Gets a list of alerts accessible to the user, ordered by creation time. */
126
+ async listAlerts(
127
+ req: ListAlertsRequest,
128
+ options?: CallOptions
129
+ ): Promise<ListAlertsResponse> {
130
+ const url = `${this.host}/api/2.0/alerts`;
131
+ const params = new URLSearchParams();
132
+ if (req.pageToken !== undefined) {
133
+ params.append('page_token', req.pageToken);
134
+ }
135
+ if (req.pageSize !== undefined) {
136
+ params.append('page_size', String(req.pageSize));
137
+ }
138
+ const query = params.toString();
139
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
140
+ let resp: ListAlertsResponse | undefined;
141
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
142
+ const headers = new Headers();
143
+ if (this.workspaceId !== undefined) {
144
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
145
+ }
146
+ headers.set('User-Agent', this.userAgent);
147
+ const httpReq = buildHttpRequest('GET', fullUrl, headers, callSignal);
148
+ const respBody = await executeHttpCall({
149
+ request: httpReq,
150
+ httpClient: this.httpClient,
151
+ logger: this.logger,
152
+ });
153
+ resp = parseResponse(respBody, unmarshalListAlertsResponseSchema);
154
+ };
155
+ await executeCall(call, options);
156
+ if (resp === undefined) {
157
+ throw new Error('API call completed without a result.');
158
+ }
159
+ return resp;
160
+ }
161
+
162
+ async *listAlertsIter(
163
+ req: ListAlertsRequest,
164
+ options?: CallOptions
165
+ ): AsyncGenerator<Alert> {
166
+ const pageReq: ListAlertsRequest = {...req};
167
+ for (;;) {
168
+ const resp = await this.listAlerts(pageReq, options);
169
+ for (const item of resp.alerts ?? []) {
170
+ yield item;
171
+ }
172
+ if (resp.nextPageToken === undefined || resp.nextPageToken === '') {
173
+ return;
174
+ }
175
+ pageReq.pageToken = resp.nextPageToken;
176
+ }
177
+ }
178
+
179
+ /** Moves an alert to the trash. Trashed alerts immediately disappear from list views, and can no longer trigger. You can restore a trashed alert through the UI. A trashed alert is permanently deleted after 30 days. */
180
+ async trashAlert(
181
+ req: TrashAlertRequest,
182
+ options?: CallOptions
183
+ ): Promise<Empty> {
184
+ const url = `${this.host}/api/2.0/alerts/${req.id ?? ''}`;
185
+ const params = new URLSearchParams();
186
+ if (req.purge !== undefined) {
187
+ params.append('purge', String(req.purge));
188
+ }
189
+ const query = params.toString();
190
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
191
+ let resp: Empty | undefined;
192
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
193
+ const headers = new Headers();
194
+ if (this.workspaceId !== undefined) {
195
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
196
+ }
197
+ headers.set('User-Agent', this.userAgent);
198
+ const httpReq = buildHttpRequest('DELETE', fullUrl, headers, callSignal);
199
+ const respBody = await executeHttpCall({
200
+ request: httpReq,
201
+ httpClient: this.httpClient,
202
+ logger: this.logger,
203
+ });
204
+ resp = parseResponse(respBody, unmarshalEmptySchema);
205
+ };
206
+ await executeCall(call, options);
207
+ if (resp === undefined) {
208
+ throw new Error('API call completed without a result.');
209
+ }
210
+ return resp;
211
+ }
212
+
213
+ /** Update alert */
214
+ async updateAlert(
215
+ req: UpdateAlertRequest,
216
+ options?: CallOptions
217
+ ): Promise<Alert> {
218
+ const url = `${this.host}/api/2.0/alerts/${req.alert?.id ?? ''}`;
219
+ const params = new URLSearchParams();
220
+ if (req.updateMask !== undefined) {
221
+ params.append('update_mask', req.updateMask.toString());
222
+ }
223
+ const query = params.toString();
224
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
225
+ const body = marshalRequest(req.alert, marshalAlertSchema);
226
+ let resp: Alert | undefined;
227
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
228
+ const headers = new Headers({'Content-Type': 'application/json'});
229
+ if (this.workspaceId !== undefined) {
230
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
231
+ }
232
+ headers.set('User-Agent', this.userAgent);
233
+ const httpReq = buildHttpRequest(
234
+ 'PATCH',
235
+ fullUrl,
236
+ headers,
237
+ callSignal,
238
+ body
239
+ );
240
+ const respBody = await executeHttpCall({
241
+ request: httpReq,
242
+ httpClient: this.httpClient,
243
+ logger: this.logger,
244
+ });
245
+ resp = parseResponse(respBody, unmarshalAlertSchema);
246
+ };
247
+ await executeCall(call, options);
248
+ if (resp === undefined) {
249
+ throw new Error('API call completed without a result.');
250
+ }
251
+ return resp;
252
+ }
253
+ }
@@ -0,0 +1,32 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ export {AlertsClient} from './client';
4
+
5
+ export {
6
+ Aggregation,
7
+ AlertEvaluationState,
8
+ AlertLifecycleState,
9
+ ComparisonOperator,
10
+ SchedulePauseStatus,
11
+ } from './model';
12
+
13
+ export type {
14
+ Alert,
15
+ AlertEvaluation,
16
+ AlertNotification,
17
+ AlertOperand,
18
+ AlertOperandColumn,
19
+ AlertOperandValue,
20
+ AlertRunAs,
21
+ AlertSubscription,
22
+ CreateAlertRequest,
23
+ CronSchedule,
24
+ Empty,
25
+ GetAlertRequest,
26
+ ListAlertsRequest,
27
+ ListAlertsResponse,
28
+ TrashAlertRequest,
29
+ UpdateAlertRequest,
30
+ } from './model';
31
+
32
+ export {alertFieldMask} from './model';