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