]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/shared/abstract-command.ts
Translated using Weblate (Catalan)
[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 26 defaultExpectedStatus: number
e8bd7ce7 27
c0e8b12e 28 // Common optional request parameters
c1bc8ee4
C
29 contentType?: string
30 accept?: string
ae2abfd3 31 redirects?: number
57f879a5 32 range?: string
41d1d075 33 host?: string
c0e8b12e
C
34 headers?: { [ name: string ]: string }
35 requestType?: string
36 xForwardedFor?: string
37}
38
39interface InternalGetCommandOptions extends InternalCommonCommandOptions {
40 query?: { [ id: string ]: any }
c1bc8ee4
C
41}
42
a6a79eae
C
43abstract class AbstractCommand {
44
a6a79eae 45 constructor (
254d3579 46 protected server: PeerTubeServer
a6a79eae
C
47 ) {
48
49 }
50
a1637fa1 51 protected getRequestBody <T> (options: InternalGetCommandOptions) {
c1bc8ee4
C
52 return unwrapBody<T>(this.getRequest(options))
53 }
54
a1637fa1 55 protected getRequestText (options: InternalGetCommandOptions) {
c1bc8ee4 56 return unwrapText(this.getRequest(options))
e8bd7ce7
C
57 }
58
57f879a5
C
59 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
60 const { url, range } = options
61 const { host, protocol, pathname } = new URL(url)
62
63 return this.getRequest({
64 ...options,
65
66 token: this.buildCommonRequestToken(options),
c0e8b12e 67 defaultExpectedStatus: this.buildExpectedStatus(options),
57f879a5
C
68
69 url: `${protocol}//${host}`,
70 path: pathname,
71 range
72 })
73 }
74
a1637fa1 75 protected getRequest (options: InternalGetCommandOptions) {
c0e8b12e 76 const { query } = options
ae2abfd3
C
77
78 return makeGetRequest({
79 ...this.buildCommonRequestOptions(options),
80
c0e8b12e 81 query
ae2abfd3
C
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
C
102 fields?: { [ fieldName: string ]: any }
103 }) {
c0e8b12e 104 const { fields } = options
a6a79eae
C
105
106 return makePostBodyRequest({
e8bd7ce7
C
107 ...this.buildCommonRequestOptions(options),
108
c0e8b12e 109 fields
e8bd7ce7
C
110 })
111 }
112
a1637fa1 113 protected postUploadRequest (options: InternalCommonCommandOptions & {
4f219914 114 fields?: { [ fieldName: string ]: any }
d23dd9fb 115 attaches?: { [ fieldName: string ]: any }
4f219914 116 }) {
c0e8b12e 117 const { fields, attaches } = options
4f219914
C
118
119 return makeUploadRequest({
120 ...this.buildCommonRequestOptions(options),
121
122 method: 'POST',
123 fields,
c0e8b12e 124 attaches
4f219914
C
125 })
126 }
127
a1637fa1 128 protected putUploadRequest (options: InternalCommonCommandOptions & {
4f219914 129 fields?: { [ fieldName: string ]: any }
d23dd9fb 130 attaches?: { [ fieldName: string ]: any }
4f219914 131 }) {
c0e8b12e 132 const { fields, attaches } = options
4f219914
C
133
134 return makeUploadRequest({
135 ...this.buildCommonRequestOptions(options),
136
137 method: 'PUT',
138 fields,
139 attaches
140 })
141 }
142
a5461888
C
143 protected updateImageRequest (options: InternalCommonCommandOptions & {
144 fixture: string
145 fieldname: string
146 }) {
c0e8b12e
C
147 const filePath = isAbsolute(options.fixture)
148 ? options.fixture
149 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
a5461888
C
150
151 return this.postUploadRequest({
152 ...options,
153
154 fields: {},
155 attaches: { [options.fieldname]: filePath }
156 })
157 }
158
d23dd9fb 159 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
c0e8b12e 160 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
57f879a5
C
161
162 return {
dd0ebb71 163 url: url ?? this.server.url,
57f879a5
C
164 path,
165
166 token: this.buildCommonRequestToken(options),
c0e8b12e
C
167 expectedStatus: this.buildExpectedStatus(options),
168
169 redirects,
170 contentType,
171 range,
172 host,
173 accept,
174 headers,
175 type: requestType,
176 xForwardedFor
57f879a5
C
177 }
178 }
179
d23dd9fb 180 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
57f879a5 181 const { token } = options
e8bd7ce7 182
a1637fa1
C
183 const fallbackToken = options.implicitToken
184 ? this.server.accessToken
185 : undefined
186
57f879a5
C
187 return token !== undefined ? token : fallbackToken
188 }
23a3a882 189
c0e8b12e 190 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
57f879a5 191 const { expectedStatus, defaultExpectedStatus } = options
23a3a882 192
d23dd9fb 193 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
a6a79eae
C
194 }
195}
196
197export {
198 AbstractCommand
199}