1 import { isAbsolute, join } from 'path'
2 import { root } from '../miscs/tests'
11 } from '../requests/requests'
12 import { PeerTubeServer } from '../server/server'
14 export interface OverrideCommandOptions {
16 expectedStatus?: number
19 interface InternalCommonCommandOptions extends OverrideCommandOptions {
20 // Default to server.url
24 // If we automatically send the server token if the token is not provided
25 implicitToken: boolean
26 defaultExpectedStatus: number
28 // Common optional request parameters
34 headers?: { [ name: string ]: string }
36 xForwardedFor?: string
39 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
40 query?: { [ id: string ]: any }
43 abstract class AbstractCommand {
46 protected server: PeerTubeServer
51 protected getRequestBody <T> (options: InternalGetCommandOptions) {
52 return unwrapBody<T>(this.getRequest(options))
55 protected getRequestText (options: InternalGetCommandOptions) {
56 return unwrapText(this.getRequest(options))
59 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
60 const { url, range } = options
61 const { host, protocol, pathname } = new URL(url)
63 return this.getRequest({
66 token: this.buildCommonRequestToken(options),
67 defaultExpectedStatus: this.buildExpectedStatus(options),
69 url: `${protocol}//${host}`,
75 protected getRequest (options: InternalGetCommandOptions) {
76 const { query } = options
78 return makeGetRequest({
79 ...this.buildCommonRequestOptions(options),
85 protected deleteRequest (options: InternalCommonCommandOptions) {
86 return makeDeleteRequest(this.buildCommonRequestOptions(options))
89 protected putBodyRequest (options: InternalCommonCommandOptions & {
90 fields?: { [ fieldName: string ]: any }
92 const { fields } = options
94 return makePutBodyRequest({
95 ...this.buildCommonRequestOptions(options),
101 protected postBodyRequest (options: InternalCommonCommandOptions & {
102 fields?: { [ fieldName: string ]: any }
104 const { fields } = options
106 return makePostBodyRequest({
107 ...this.buildCommonRequestOptions(options),
113 protected postUploadRequest (options: InternalCommonCommandOptions & {
114 fields?: { [ fieldName: string ]: any }
115 attaches?: { [ fieldName: string ]: any }
117 const { fields, attaches } = options
119 return makeUploadRequest({
120 ...this.buildCommonRequestOptions(options),
128 protected putUploadRequest (options: InternalCommonCommandOptions & {
129 fields?: { [ fieldName: string ]: any }
130 attaches?: { [ fieldName: string ]: any }
132 const { fields, attaches } = options
134 return makeUploadRequest({
135 ...this.buildCommonRequestOptions(options),
143 protected updateImageRequest (options: InternalCommonCommandOptions & {
147 const filePath = isAbsolute(options.fixture)
149 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
151 return this.postUploadRequest({
155 attaches: { [options.fieldname]: filePath }
159 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
160 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
163 url: url ?? this.server.url,
166 token: this.buildCommonRequestToken(options),
167 expectedStatus: this.buildExpectedStatus(options),
180 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
181 const { token } = options
183 const fallbackToken = options.implicitToken
184 ? this.server.accessToken
187 return token !== undefined ? token : fallbackToken
190 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
191 const { expectedStatus, defaultExpectedStatus } = options
193 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus