Move to eslint
[oweals/peertube.git] / shared / extra-utils / server / redundancy.ts
1 import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
2 import { VideoRedundanciesTarget } from '@shared/models'
3
4 function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) {
5   const path = '/api/v1/server/redundancy/' + host
6
7   return makePutBodyRequest({
8     url,
9     path,
10     token: accessToken,
11     fields: { redundancyAllowed },
12     statusCodeExpected: expectedStatus
13   })
14 }
15
16 function listVideoRedundancies (options: {
17   url: string
18   accessToken: string
19   target: VideoRedundanciesTarget
20   start?: number
21   count?: number
22   sort?: string
23   statusCodeExpected?: number
24 }) {
25   const path = '/api/v1/server/redundancy/videos'
26
27   const { url, accessToken, target, statusCodeExpected, start, count, sort } = options
28
29   return makeGetRequest({
30     url,
31     token: accessToken,
32     path,
33     query: {
34       start: start ?? 0,
35       count: count ?? 5,
36       sort: sort ?? 'name',
37       target
38     },
39     statusCodeExpected: statusCodeExpected || 200
40   })
41 }
42
43 function addVideoRedundancy (options: {
44   url: string
45   accessToken: string
46   videoId: number
47 }) {
48   const path = '/api/v1/server/redundancy/videos'
49   const { url, accessToken, videoId } = options
50
51   return makePostBodyRequest({
52     url,
53     token: accessToken,
54     path,
55     fields: { videoId },
56     statusCodeExpected: 204
57   })
58 }
59
60 function removeVideoRedundancy (options: {
61   url: string
62   accessToken: string
63   redundancyId: number
64 }) {
65   const { url, accessToken, redundancyId } = options
66   const path = '/api/v1/server/redundancy/videos/' + redundancyId
67
68   return makeDeleteRequest({
69     url,
70     token: accessToken,
71     path,
72     statusCodeExpected: 204
73   })
74 }
75
76 export {
77   updateRedundancy,
78   listVideoRedundancies,
79   addVideoRedundancy,
80   removeVideoRedundancy
81 }