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