forked from opendatahub-io/odh-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.ts
More file actions
71 lines (64 loc) · 2.66 KB
/
subscriptions.ts
File metadata and controls
71 lines (64 loc) · 2.66 KB
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
import {
handleRestFailures,
isModArchResponse,
APIOptions,
restGET,
restDELETE,
} from 'mod-arch-core';
import { BFF_API_VERSION, URL_PREFIX } from '../utilities/const';
import { MaaSSubscription, ModelSubscriptionRef, TokenRateLimit } from '../types/subscriptions';
const isRecord = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object';
const isMaaSSubscriptionRef = (v: unknown): v is ModelSubscriptionRef =>
isRecord(v) &&
typeof v.name === 'string' &&
typeof v.namespace === 'string' &&
(v.tokenRateLimits === undefined ||
(Array.isArray(v.tokenRateLimits) && v.tokenRateLimits.every(isTokenRateLimit))) &&
(v.tokenRateLimitRef === undefined || typeof v.tokenRateLimitRef === 'string') &&
(v.billingRate === undefined || typeof v.billingRate === 'object');
const isMaaSSubscription = (v: unknown): v is MaaSSubscription =>
isRecord(v) &&
typeof v.name === 'string' &&
typeof v.namespace === 'string' &&
(v.phase === undefined || typeof v.phase === 'string') &&
(v.priority === undefined || typeof v.priority === 'number') &&
typeof v.owner === 'object' &&
Array.isArray(v.modelRefs) &&
v.modelRefs.every(isMaaSSubscriptionRef) &&
(v.tokenMetadata === undefined || typeof v.tokenMetadata === 'object') &&
(v.creationTimestamp === undefined || typeof v.creationTimestamp === 'string');
const isTokenRateLimit = (v: unknown): v is TokenRateLimit =>
isRecord(v) && typeof v.limit === 'number' && typeof v.window === 'string';
const isMaaSSubscriptionArray = (v: unknown): v is MaaSSubscription[] =>
Array.isArray(v) && v.every(isMaaSSubscription);
/** GET /api/v1/all-subscriptions - List all subscriptions */
export const listSubscriptions =
(hostPath = '') =>
(opts: APIOptions): Promise<MaaSSubscription[]> =>
handleRestFailures(
restGET(hostPath, `${URL_PREFIX}/api/${BFF_API_VERSION}/all-subscriptions`, {}, opts),
).then((response) => {
if (isModArchResponse<unknown>(response) && isMaaSSubscriptionArray(response.data)) {
return response.data;
}
throw new Error('Invalid response format');
});
const isDeleteSubscriptionResponse = (v: unknown): v is { message: string } =>
isRecord(v) && typeof v.message === 'string';
export const deleteSubscription =
(hostPath = '') =>
(opts: APIOptions, name: string): Promise<void> =>
handleRestFailures(
restDELETE(
hostPath,
`${URL_PREFIX}/api/${BFF_API_VERSION}/subscription/${encodeURIComponent(name)}`,
{},
{},
opts,
),
).then((response) => {
if (isDeleteSubscriptionResponse(response)) {
return;
}
throw new Error('Invalid response format');
});