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