]>
Commit | Line | Data |
---|---|---|
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 | responseType?: string | |
37 | xForwardedFor?: string | |
38 | } | |
39 | ||
40 | interface InternalGetCommandOptions extends InternalCommonCommandOptions { | |
41 | query?: { [ id: string ]: any } | |
42 | } | |
43 | ||
44 | interface InternalDeleteCommandOptions extends InternalCommonCommandOptions { | |
45 | query?: { [ id: string ]: any } | |
46 | rawQuery?: string | |
47 | } | |
48 | ||
49 | abstract 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 | }) { | |
105 | const { fields } = options | |
106 | ||
107 | return makePutBodyRequest({ | |
108 | ...this.buildCommonRequestOptions(options), | |
109 | ||
110 | fields | |
111 | }) | |
112 | } | |
113 | ||
114 | protected postBodyRequest (options: InternalCommonCommandOptions & { | |
115 | fields?: { [ fieldName: string ]: any } | |
116 | }) { | |
117 | const { fields } = options | |
118 | ||
119 | return makePostBodyRequest({ | |
120 | ...this.buildCommonRequestOptions(options), | |
121 | ||
122 | fields | |
123 | }) | |
124 | } | |
125 | ||
126 | protected postUploadRequest (options: InternalCommonCommandOptions & { | |
127 | fields?: { [ fieldName: string ]: any } | |
128 | attaches?: { [ fieldName: string ]: any } | |
129 | }) { | |
130 | const { fields, attaches } = options | |
131 | ||
132 | return makeUploadRequest({ | |
133 | ...this.buildCommonRequestOptions(options), | |
134 | ||
135 | method: 'POST', | |
136 | fields, | |
137 | attaches | |
138 | }) | |
139 | } | |
140 | ||
141 | protected putUploadRequest (options: InternalCommonCommandOptions & { | |
142 | fields?: { [ fieldName: string ]: any } | |
143 | attaches?: { [ fieldName: string ]: any } | |
144 | }) { | |
145 | const { fields, attaches } = options | |
146 | ||
147 | return makeUploadRequest({ | |
148 | ...this.buildCommonRequestOptions(options), | |
149 | ||
150 | method: 'PUT', | |
151 | fields, | |
152 | attaches | |
153 | }) | |
154 | } | |
155 | ||
156 | protected updateImageRequest (options: InternalCommonCommandOptions & { | |
157 | fixture: string | |
158 | fieldname: string | |
159 | }) { | |
160 | const filePath = isAbsolute(options.fixture) | |
161 | ? options.fixture | |
162 | : join(root(), 'server', 'tests', 'fixtures', options.fixture) | |
163 | ||
164 | return this.postUploadRequest({ | |
165 | ...options, | |
166 | ||
167 | fields: {}, | |
168 | attaches: { [options.fieldname]: filePath } | |
169 | }) | |
170 | } | |
171 | ||
172 | protected buildCommonRequestOptions (options: InternalCommonCommandOptions) { | |
173 | const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor, responseType } = options | |
174 | ||
175 | return { | |
176 | url: url ?? this.server.url, | |
177 | path, | |
178 | ||
179 | token: this.buildCommonRequestToken(options), | |
180 | expectedStatus: this.buildExpectedStatus(options), | |
181 | ||
182 | redirects, | |
183 | contentType, | |
184 | range, | |
185 | host, | |
186 | accept, | |
187 | headers, | |
188 | type: requestType, | |
189 | responseType, | |
190 | xForwardedFor | |
191 | } | |
192 | } | |
193 | ||
194 | protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) { | |
195 | const { token } = options | |
196 | ||
197 | const fallbackToken = options.implicitToken | |
198 | ? this.server.accessToken | |
199 | : undefined | |
200 | ||
201 | return token !== undefined ? token : fallbackToken | |
202 | } | |
203 | ||
204 | protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) { | |
205 | const { expectedStatus, defaultExpectedStatus } = options | |
206 | ||
207 | return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus | |
208 | } | |
209 | } | |
210 | ||
211 | export { | |
212 | AbstractCommand | |
213 | } |