1 import { isAbsolute, join } from 'path'
2 import { root } from '@shared/core-utils'
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 }
37 xForwardedFor?: string
40 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
41 query?: { [ id: string ]: any }
44 interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
45 query?: { [ id: string ]: any }
49 abstract class AbstractCommand {
52 protected server: PeerTubeServer
57 protected getRequestBody <T> (options: InternalGetCommandOptions) {
58 return unwrapBody<T>(this.getRequest(options))
61 protected getRequestText (options: InternalGetCommandOptions) {
62 return unwrapText(this.getRequest(options))
65 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
66 const { url, range } = options
67 const { host, protocol, pathname } = new URL(url)
69 return this.getRequest({
72 token: this.buildCommonRequestToken(options),
73 defaultExpectedStatus: this.buildExpectedStatus(options),
75 url: `${protocol}//${host}`,
81 protected getRequest (options: InternalGetCommandOptions) {
82 const { query } = options
84 return makeGetRequest({
85 ...this.buildCommonRequestOptions(options),
91 protected deleteRequest (options: InternalDeleteCommandOptions) {
92 const { query, rawQuery } = options
94 return makeDeleteRequest({
95 ...this.buildCommonRequestOptions(options),
102 protected putBodyRequest (options: InternalCommonCommandOptions & {
103 fields?: { [ fieldName: string ]: any }
105 const { fields } = options
107 return makePutBodyRequest({
108 ...this.buildCommonRequestOptions(options),
114 protected postBodyRequest (options: InternalCommonCommandOptions & {
115 fields?: { [ fieldName: string ]: any }
117 const { fields } = options
119 return makePostBodyRequest({
120 ...this.buildCommonRequestOptions(options),
126 protected postUploadRequest (options: InternalCommonCommandOptions & {
127 fields?: { [ fieldName: string ]: any }
128 attaches?: { [ fieldName: string ]: any }
130 const { fields, attaches } = options
132 return makeUploadRequest({
133 ...this.buildCommonRequestOptions(options),
141 protected putUploadRequest (options: InternalCommonCommandOptions & {
142 fields?: { [ fieldName: string ]: any }
143 attaches?: { [ fieldName: string ]: any }
145 const { fields, attaches } = options
147 return makeUploadRequest({
148 ...this.buildCommonRequestOptions(options),
156 protected updateImageRequest (options: InternalCommonCommandOptions & {
160 const filePath = isAbsolute(options.fixture)
162 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
164 return this.postUploadRequest({
168 attaches: { [options.fieldname]: filePath }
172 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
173 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor, responseType } = options
176 url: url ?? this.server.url,
179 token: this.buildCommonRequestToken(options),
180 expectedStatus: this.buildExpectedStatus(options),
194 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
195 const { token } = options
197 const fallbackToken = options.implicitToken
198 ? this.server.accessToken
201 return token !== undefined ? token : fallbackToken
204 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
205 const { expectedStatus, defaultExpectedStatus } = options
207 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus