]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/shared/abstract-command.ts
Introduce live command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
1 import { HttpStatusCode } from '@shared/core-utils'
2 import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, makeUploadRequest, unwrapBody, unwrapText } from '../requests/requests'
3 import { ServerInfo } from '../server/servers'
4
5 export interface OverrideCommandOptions {
6 token?: string
7 expectedStatus?: number
8 }
9
10 interface CommonCommandOptions extends OverrideCommandOptions {
11 path: string
12 defaultExpectedStatus: number
13 }
14
15 interface GetCommandOptions extends CommonCommandOptions {
16 query?: { [ id: string ]: any }
17 contentType?: string
18 accept?: string
19 redirects?: number
20 }
21
22 abstract class AbstractCommand {
23
24 private expectedStatus: HttpStatusCode
25
26 constructor (
27 protected server: ServerInfo
28 ) {
29
30 }
31
32 setServer (server: ServerInfo) {
33 this.server = server
34 }
35
36 setExpectedStatus (status: HttpStatusCode) {
37 this.expectedStatus = status
38 }
39
40 protected getRequestBody <T> (options: GetCommandOptions) {
41 return unwrapBody<T>(this.getRequest(options))
42 }
43
44 protected getRequestText (options: GetCommandOptions) {
45 return unwrapText(this.getRequest(options))
46 }
47
48 protected getRequest (options: GetCommandOptions) {
49 const { redirects, query, contentType, accept } = options
50
51 return makeGetRequest({
52 ...this.buildCommonRequestOptions(options),
53
54 redirects,
55 query,
56 contentType,
57 accept
58 })
59 }
60
61 protected deleteRequest (options: CommonCommandOptions) {
62 return makeDeleteRequest(this.buildCommonRequestOptions(options))
63 }
64
65 protected putBodyRequest (options: CommonCommandOptions & {
66 fields?: { [ fieldName: string ]: any }
67 }) {
68 const { fields } = options
69
70 return makePutBodyRequest({
71 ...this.buildCommonRequestOptions(options),
72
73 fields
74 })
75 }
76
77 protected postBodyRequest (options: CommonCommandOptions & {
78 fields?: { [ fieldName: string ]: any }
79 }) {
80 const { fields } = options
81
82 return makePostBodyRequest({
83 ...this.buildCommonRequestOptions(options),
84
85 fields
86 })
87 }
88
89 protected postUploadRequest (options: CommonCommandOptions & {
90 fields?: { [ fieldName: string ]: any }
91 attaches?: any
92 }) {
93 const { fields, attaches } = options
94
95 return makeUploadRequest({
96 ...this.buildCommonRequestOptions(options),
97
98 method: 'POST',
99 fields,
100 attaches
101 })
102 }
103
104 protected putUploadRequest (options: CommonCommandOptions & {
105 fields?: { [ fieldName: string ]: any }
106 attaches?: any
107 }) {
108 const { fields, attaches } = options
109
110 return makeUploadRequest({
111 ...this.buildCommonRequestOptions(options),
112
113 method: 'PUT',
114 fields,
115 attaches
116 })
117 }
118
119 private buildCommonRequestOptions (options: CommonCommandOptions) {
120 const { token, expectedStatus, defaultExpectedStatus, path } = options
121
122 return {
123 url: this.server.url,
124 path,
125
126 // Token can be null if we don't want to add it
127 token: token !== undefined ? token : this.server.accessToken,
128
129 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
130 }
131 }
132 }
133
134 export {
135 AbstractCommand
136 }