-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.ts
More file actions
104 lines (90 loc) · 3.42 KB
/
Copy pathsubscriptions.ts
File metadata and controls
104 lines (90 loc) · 3.42 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
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
import client from './client'
import { withParsedEnabledProviderIds } from '@/domain/subscription/enabledProviderIds'
import type { Subscription, AccessRestriction, AccessLog } from '@/types'
interface SubscriptionPayload {
name?: string
enabled_provider_ids?: number[]
custom_config_id?: number | null
config_template_id?: number | null
rule_insert_mode?: 'prepend' | 'append' | 'replace'
proxy_prefix_enabled?: boolean
token_expired_at?: string | null
}
interface AccessLogParams {
page?: number
page_size?: number
}
interface AccessLogResponse {
items: AccessLog[]
total: number
page: number
page_size: number
}
export const subscriptionsApi = {
list: async (): Promise<Subscription[]> => {
const res = await client.get<{ code: number; data: Subscription[] }>('/subscriptions')
return res.data.data.map((s) => withParsedEnabledProviderIds(s))
},
get: async (id: number): Promise<Subscription> => {
const res = await client.get<{
code: number
data: { subscription: Subscription; access_restrictions: AccessRestriction[] }
}>(`/subscriptions/${id}`)
return withParsedEnabledProviderIds(res.data.data.subscription)
},
getWithRestrictions: async (
id: number
): Promise<{ subscription: Subscription; access_restrictions: AccessRestriction[] }> => {
const res = await client.get<{
code: number
data: { subscription: Subscription; access_restrictions: AccessRestriction[] }
}>(`/subscriptions/${id}`)
return {
subscription: withParsedEnabledProviderIds(res.data.data.subscription),
access_restrictions: res.data.data.access_restrictions,
}
},
create: async (data: SubscriptionPayload): Promise<Subscription> => {
const res = await client.post<{ code: number; data: Subscription }>('/subscriptions', data)
return withParsedEnabledProviderIds(res.data.data)
},
update: async (id: number, data: SubscriptionPayload): Promise<Subscription> => {
const res = await client.put<{ code: number; data: Subscription }>(`/subscriptions/${id}`, data)
return withParsedEnabledProviderIds(res.data.data)
},
delete: async (id: number): Promise<void> => {
await client.delete(`/subscriptions/${id}`)
},
regenerateToken: async (id: number): Promise<{ token: string }> => {
const res = await client.post<{ code: number; data: { token: string } }>(
`/subscriptions/${id}/regenerate-token`
)
return res.data.data
},
getAccessLogs: async (id: number, params?: AccessLogParams): Promise<AccessLogResponse> => {
const res = await client.get<{ code: number; data: AccessLogResponse }>(
`/subscriptions/${id}/access-logs`,
{ params }
)
return res.data.data
},
getRestrictions: async (id: number): Promise<AccessRestriction[]> => {
const res = await client.get<{ code: number; data: AccessRestriction[] }>(
`/subscriptions/${id}/restrictions`
)
return res.data.data
},
addRestriction: async (
id: number,
data: { type: 'ip' | 'cidr' | 'country'; value: string; mode: 'allow' | 'deny' }
): Promise<AccessRestriction> => {
const res = await client.post<{ code: number; data: AccessRestriction }>(
`/subscriptions/${id}/restrictions`,
data
)
return res.data.data
},
deleteRestriction: async (subscriptionId: number, restrictionId: number): Promise<void> => {
await client.delete(`/subscriptions/${subscriptionId}/restrictions/${restrictionId}`)
},
}