]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/shared/abstract-command.ts
Introduce login command
[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/tests'
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 host?: string
37 }
38
39 abstract class AbstractCommand {
40
41 private expectedStatus: HttpStatusCode
42
43 constructor (
44 protected server: ServerInfo
45 ) {
46
47 }
48
49 setServer (server: ServerInfo) {
50 this.server = server
51 }
52
53 setExpectedStatus (status: HttpStatusCode) {
54 this.expectedStatus = status
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.buildStatusCodeExpected(options),
74
75 url: `${protocol}//${host}`,
76 path: pathname,
77 range
78 })
79 }
80
81 protected getRequest (options: InternalGetCommandOptions) {
82 const { redirects, query, contentType, accept, range, host } = options
83
84 return makeGetRequest({
85 ...this.buildCommonRequestOptions(options),
86
87 redirects,
88 query,
89 contentType,
90 range,
91 host,
92 accept
93 })
94 }
95
96 protected deleteRequest (options: InternalCommonCommandOptions) {
97 return makeDeleteRequest(this.buildCommonRequestOptions(options))
98 }
99
100 protected putBodyRequest (options: InternalCommonCommandOptions & {
101 fields?: { [ fieldName: string ]: any }
102 }) {
103 const { fields } = options
104
105 return makePutBodyRequest({
106 ...this.buildCommonRequestOptions(options),
107
108 fields
109 })
110 }
111
112 protected postBodyRequest (options: InternalCommonCommandOptions & {
113 fields?: { [ fieldName: string ]: any }
114 type?: string
115 }) {
116 const { type, fields } = options
117
118 return makePostBodyRequest({
119 ...this.buildCommonRequestOptions(options),
120
121 fields,
122 type
123 })
124 }
125
126 protected postUploadRequest (options: InternalCommonCommandOptions & {
127 fields?: { [ fieldName: string ]: any }
128 attaches?: 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?: 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 let filePath = ''
161 if (isAbsolute(options.fixture)) {
162 filePath = options.fixture
163 } else {
164 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
165 }
166
167 return this.postUploadRequest({
168 ...options,
169
170 fields: {},
171 attaches: { [options.fieldname]: filePath }
172 })
173 }
174
175 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
176 const { url, path } = options
177
178 return {
179 url: url ?? this.server.url,
180 path,
181
182 token: this.buildCommonRequestToken(options),
183 statusCodeExpected: this.buildStatusCodeExpected(options)
184 }
185 }
186
187 private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
188 const { token } = options
189
190 const fallbackToken = options.implicitToken
191 ? this.server.accessToken
192 : undefined
193
194 return token !== undefined ? token : fallbackToken
195 }
196
197 private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
198 const { expectedStatus, defaultExpectedStatus } = options
199
200 return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
201 }
202 }
203
204 export {
205 AbstractCommand
206 }