]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/shared/abstract-command.ts
Introduce videos command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
1 import { isAbsolute, join } from 'path'
2 import { root } from '../miscs/tests'
3 import {
4 makeDeleteRequest,
5 makeGetRequest,
6 makePostBodyRequest,
7 makePutBodyRequest,
8 makeUploadRequest,
9 unwrapBody,
10 unwrapText
11 } from '../requests/requests'
12 import { ServerInfo } from '../server/servers'
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
29 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
30 query?: { [ id: string ]: any }
31 contentType?: string
32 accept?: string
33 redirects?: number
34 range?: string
35 host?: string
36 }
37
38 abstract class AbstractCommand {
39
40 constructor (
41 protected server: ServerInfo
42 ) {
43
44 }
45
46 protected getRequestBody <T> (options: InternalGetCommandOptions) {
47 return unwrapBody<T>(this.getRequest(options))
48 }
49
50 protected getRequestText (options: InternalGetCommandOptions) {
51 return unwrapText(this.getRequest(options))
52 }
53
54 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
55 const { url, range } = options
56 const { host, protocol, pathname } = new URL(url)
57
58 return this.getRequest({
59 ...options,
60
61 token: this.buildCommonRequestToken(options),
62 defaultExpectedStatus: this.buildStatusCodeExpected(options),
63
64 url: `${protocol}//${host}`,
65 path: pathname,
66 range
67 })
68 }
69
70 protected getRequest (options: InternalGetCommandOptions) {
71 const { redirects, query, contentType, accept, range, host } = options
72
73 return makeGetRequest({
74 ...this.buildCommonRequestOptions(options),
75
76 redirects,
77 query,
78 contentType,
79 range,
80 host,
81 accept
82 })
83 }
84
85 protected deleteRequest (options: InternalCommonCommandOptions) {
86 return makeDeleteRequest(this.buildCommonRequestOptions(options))
87 }
88
89 protected putBodyRequest (options: InternalCommonCommandOptions & {
90 fields?: { [ fieldName: string ]: any }
91 }) {
92 const { fields } = options
93
94 return makePutBodyRequest({
95 ...this.buildCommonRequestOptions(options),
96
97 fields
98 })
99 }
100
101 protected postBodyRequest (options: InternalCommonCommandOptions & {
102 fields?: { [ fieldName: string ]: any }
103 headers?: { [ name: string ]: string }
104 type?: string
105 xForwardedFor?: string
106 }) {
107 const { type, fields, xForwardedFor, headers } = options
108
109 return makePostBodyRequest({
110 ...this.buildCommonRequestOptions(options),
111
112 fields,
113 xForwardedFor,
114 type,
115 headers
116 })
117 }
118
119 protected postUploadRequest (options: InternalCommonCommandOptions & {
120 fields?: { [ fieldName: string ]: any }
121 attaches?: { [ fieldName: string ]: any }
122 headers?: { [ name: string ]: string }
123 }) {
124 const { fields, attaches, headers } = options
125
126 return makeUploadRequest({
127 ...this.buildCommonRequestOptions(options),
128
129 method: 'POST',
130 fields,
131 attaches,
132 headers
133 })
134 }
135
136 protected putUploadRequest (options: InternalCommonCommandOptions & {
137 fields?: { [ fieldName: string ]: any }
138 attaches?: { [ fieldName: string ]: any }
139 headers?: { [ name: string ]: string }
140 }) {
141 const { fields, attaches, headers } = options
142
143 return makeUploadRequest({
144 ...this.buildCommonRequestOptions(options),
145
146 method: 'PUT',
147 headers,
148 fields,
149 attaches
150 })
151 }
152
153 protected updateImageRequest (options: InternalCommonCommandOptions & {
154 fixture: string
155 fieldname: string
156 }) {
157 let filePath = ''
158 if (isAbsolute(options.fixture)) {
159 filePath = options.fixture
160 } else {
161 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
162 }
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 } = options
174
175 return {
176 url: url ?? this.server.url,
177 path,
178
179 token: this.buildCommonRequestToken(options),
180 statusCodeExpected: this.buildStatusCodeExpected(options)
181 }
182 }
183
184 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
185 const { token } = options
186
187 const fallbackToken = options.implicitToken
188 ? this.server.accessToken
189 : undefined
190
191 return token !== undefined ? token : fallbackToken
192 }
193
194 protected buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
195 const { expectedStatus, defaultExpectedStatus } = options
196
197 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
198 }
199 }
200
201 export {
202 AbstractCommand
203 }