]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/shared/abstract-command.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
CommitLineData
a5461888 1import { isAbsolute, join } from 'path'
a6a79eae 2import { HttpStatusCode } from '@shared/core-utils'
6c5065a0 3import { root } from '../miscs/tests'
a1637fa1
C
4import {
5 makeDeleteRequest,
6 makeGetRequest,
7 makePostBodyRequest,
8 makePutBodyRequest,
9 makeUploadRequest,
10 unwrapBody,
11 unwrapText
12} from '../requests/requests'
a6a79eae
C
13import { ServerInfo } from '../server/servers'
14
e8bd7ce7 15export interface OverrideCommandOptions {
a6a79eae
C
16 token?: string
17 expectedStatus?: number
18}
19
a1637fa1 20interface InternalCommonCommandOptions extends OverrideCommandOptions {
57f879a5
C
21 // Default to server.url
22 url?: string
23
e8bd7ce7 24 path: string
a1637fa1
C
25 // If we automatically send the server token if the token is not provided
26 implicitToken: boolean
e8bd7ce7
C
27 defaultExpectedStatus: number
28}
29
a1637fa1 30interface InternalGetCommandOptions extends InternalCommonCommandOptions {
a92ddacb 31 query?: { [ id: string ]: any }
c1bc8ee4
C
32 contentType?: string
33 accept?: string
ae2abfd3 34 redirects?: number
57f879a5 35 range?: string
c1bc8ee4
C
36}
37
a6a79eae
C
38abstract class AbstractCommand {
39
e8bd7ce7 40 private expectedStatus: HttpStatusCode
a6a79eae
C
41
42 constructor (
43 protected server: ServerInfo
44 ) {
45
46 }
47
48 setServer (server: ServerInfo) {
49 this.server = server
50 }
51
52 setExpectedStatus (status: HttpStatusCode) {
53 this.expectedStatus = status
54 }
55
a1637fa1 56 protected getRequestBody <T> (options: InternalGetCommandOptions) {
c1bc8ee4
C
57 return unwrapBody<T>(this.getRequest(options))
58 }
59
a1637fa1 60 protected getRequestText (options: InternalGetCommandOptions) {
c1bc8ee4 61 return unwrapText(this.getRequest(options))
e8bd7ce7
C
62 }
63
57f879a5
C
64 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
65 const { url, range } = options
66 const { host, protocol, pathname } = new URL(url)
67
68 return this.getRequest({
69 ...options,
70
71 token: this.buildCommonRequestToken(options),
72 defaultExpectedStatus: this.buildStatusCodeExpected(options),
73
74 url: `${protocol}//${host}`,
75 path: pathname,
76 range
77 })
78 }
79
a1637fa1 80 protected getRequest (options: InternalGetCommandOptions) {
dd0ebb71 81 const { redirects, query, contentType, accept, range } = options
ae2abfd3
C
82
83 return makeGetRequest({
84 ...this.buildCommonRequestOptions(options),
85
86 redirects,
87 query,
88 contentType,
dd0ebb71 89 range,
ae2abfd3
C
90 accept
91 })
92 }
93
a1637fa1 94 protected deleteRequest (options: InternalCommonCommandOptions) {
0c1a77e9
C
95 return makeDeleteRequest(this.buildCommonRequestOptions(options))
96 }
97
a1637fa1 98 protected putBodyRequest (options: InternalCommonCommandOptions & {
e8bd7ce7
C
99 fields?: { [ fieldName: string ]: any }
100 }) {
101 const { fields } = options
102
103 return makePutBodyRequest({
104 ...this.buildCommonRequestOptions(options),
105
106 fields
107 })
108 }
109
a1637fa1 110 protected postBodyRequest (options: InternalCommonCommandOptions & {
a6a79eae
C
111 fields?: { [ fieldName: string ]: any }
112 }) {
e8bd7ce7 113 const { fields } = options
a6a79eae
C
114
115 return makePostBodyRequest({
e8bd7ce7
C
116 ...this.buildCommonRequestOptions(options),
117
118 fields
119 })
120 }
121
a1637fa1 122 protected postUploadRequest (options: InternalCommonCommandOptions & {
4f219914
C
123 fields?: { [ fieldName: string ]: any }
124 attaches?: any
125 }) {
126 const { fields, attaches } = options
127
128 return makeUploadRequest({
129 ...this.buildCommonRequestOptions(options),
130
131 method: 'POST',
132 fields,
133 attaches
134 })
135 }
136
a1637fa1 137 protected putUploadRequest (options: InternalCommonCommandOptions & {
4f219914
C
138 fields?: { [ fieldName: string ]: any }
139 attaches?: any
140 }) {
141 const { fields, attaches } = options
142
143 return makeUploadRequest({
144 ...this.buildCommonRequestOptions(options),
145
146 method: 'PUT',
147 fields,
148 attaches
149 })
150 }
151
a5461888
C
152 protected updateImageRequest (options: InternalCommonCommandOptions & {
153 fixture: string
154 fieldname: string
155 }) {
156 let filePath = ''
157 if (isAbsolute(options.fixture)) {
158 filePath = options.fixture
159 } else {
160 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
161 }
162
163 return this.postUploadRequest({
164 ...options,
165
166 fields: {},
167 attaches: { [options.fieldname]: filePath }
168 })
169 }
170
a1637fa1 171 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
dd0ebb71 172 const { url, path } = options
57f879a5
C
173
174 return {
dd0ebb71 175 url: url ?? this.server.url,
57f879a5
C
176 path,
177
178 token: this.buildCommonRequestToken(options),
179 statusCodeExpected: this.buildStatusCodeExpected(options)
180 }
181 }
182
183 private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
184 const { token } = options
e8bd7ce7 185
a1637fa1
C
186 const fallbackToken = options.implicitToken
187 ? this.server.accessToken
188 : undefined
189
57f879a5
C
190 return token !== undefined ? token : fallbackToken
191 }
23a3a882 192
57f879a5
C
193 private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
194 const { expectedStatus, defaultExpectedStatus } = options
23a3a882 195
57f879a5 196 return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
a6a79eae
C
197 }
198}
199
200export {
201 AbstractCommand
202}