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