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