aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/shared/abstract-command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/server-commands/shared/abstract-command.ts')
-rw-r--r--shared/server-commands/shared/abstract-command.ts223
1 files changed, 0 insertions, 223 deletions
diff --git a/shared/server-commands/shared/abstract-command.ts b/shared/server-commands/shared/abstract-command.ts
deleted file mode 100644
index 463acc26b..000000000
--- a/shared/server-commands/shared/abstract-command.ts
+++ /dev/null
@@ -1,223 +0,0 @@
1import { isAbsolute, join } from 'path'
2import { root } from '@shared/core-utils'
3import {
4 makeDeleteRequest,
5 makeGetRequest,
6 makePostBodyRequest,
7 makePutBodyRequest,
8 makeUploadRequest,
9 unwrapBody,
10 unwrapText
11} from '../requests/requests'
12import { PeerTubeServer } from '../server/server'
13
14export interface OverrideCommandOptions {
15 token?: string
16 expectedStatus?: number
17}
18
19interface 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 responseType?: string
37 xForwardedFor?: string
38}
39
40interface InternalGetCommandOptions extends InternalCommonCommandOptions {
41 query?: { [ id: string ]: any }
42}
43
44interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
45 query?: { [ id: string ]: any }
46 rawQuery?: string
47}
48
49abstract class AbstractCommand {
50
51 constructor (
52 protected server: PeerTubeServer
53 ) {
54
55 }
56
57 protected getRequestBody <T> (options: InternalGetCommandOptions) {
58 return unwrapBody<T>(this.getRequest(options))
59 }
60
61 protected getRequestText (options: InternalGetCommandOptions) {
62 return unwrapText(this.getRequest(options))
63 }
64
65 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
66 const { url, range } = options
67 const { host, protocol, pathname } = new URL(url)
68
69 return this.getRequest({
70 ...options,
71
72 token: this.buildCommonRequestToken(options),
73 defaultExpectedStatus: this.buildExpectedStatus(options),
74
75 url: `${protocol}//${host}`,
76 path: pathname,
77 range
78 })
79 }
80
81 protected getRequest (options: InternalGetCommandOptions) {
82 const { query } = options
83
84 return makeGetRequest({
85 ...this.buildCommonRequestOptions(options),
86
87 query
88 })
89 }
90
91 protected deleteRequest (options: InternalDeleteCommandOptions) {
92 const { query, rawQuery } = options
93
94 return makeDeleteRequest({
95 ...this.buildCommonRequestOptions(options),
96
97 query,
98 rawQuery
99 })
100 }
101
102 protected putBodyRequest (options: InternalCommonCommandOptions & {
103 fields?: { [ fieldName: string ]: any }
104 headers?: { [name: string]: string }
105 }) {
106 const { fields, headers } = options
107
108 return makePutBodyRequest({
109 ...this.buildCommonRequestOptions(options),
110
111 fields,
112 headers
113 })
114 }
115
116 protected postBodyRequest (options: InternalCommonCommandOptions & {
117 fields?: { [ fieldName: string ]: any }
118 headers?: { [name: string]: string }
119 }) {
120 const { fields, headers } = options
121
122 return makePostBodyRequest({
123 ...this.buildCommonRequestOptions(options),
124
125 fields,
126 headers
127 })
128 }
129
130 protected postUploadRequest (options: InternalCommonCommandOptions & {
131 fields?: { [ fieldName: string ]: any }
132 attaches?: { [ fieldName: string ]: any }
133 }) {
134 const { fields, attaches } = options
135
136 return makeUploadRequest({
137 ...this.buildCommonRequestOptions(options),
138
139 method: 'POST',
140 fields,
141 attaches
142 })
143 }
144
145 protected putUploadRequest (options: InternalCommonCommandOptions & {
146 fields?: { [ fieldName: string ]: any }
147 attaches?: { [ fieldName: string ]: any }
148 }) {
149 const { fields, attaches } = options
150
151 return makeUploadRequest({
152 ...this.buildCommonRequestOptions(options),
153
154 method: 'PUT',
155 fields,
156 attaches
157 })
158 }
159
160 protected updateImageRequest (options: InternalCommonCommandOptions & {
161 fixture: string
162 fieldname: string
163 }) {
164 const filePath = isAbsolute(options.fixture)
165 ? options.fixture
166 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
167
168 return this.postUploadRequest({
169 ...options,
170
171 fields: {},
172 attaches: { [options.fieldname]: filePath }
173 })
174 }
175
176 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
177 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor, responseType } = options
178
179 return {
180 url: url ?? this.server.url,
181 path,
182
183 token: this.buildCommonRequestToken(options),
184 expectedStatus: this.buildExpectedStatus(options),
185
186 redirects,
187 contentType,
188 range,
189 host,
190 accept,
191 headers,
192 type: requestType,
193 responseType,
194 xForwardedFor
195 }
196 }
197
198 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
199 const { token } = options
200
201 const fallbackToken = options.implicitToken
202 ? this.server.accessToken
203 : undefined
204
205 return token !== undefined ? token : fallbackToken
206 }
207
208 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
209 const { expectedStatus, defaultExpectedStatus } = options
210
211 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
212 }
213
214 protected buildVideoPasswordHeader (videoPassword: string) {
215 return videoPassword !== undefined && videoPassword !== null
216 ? { 'x-peertube-video-password': videoPassword }
217 : undefined
218 }
219}
220
221export {
222 AbstractCommand
223}