import { isAbsolute , join } from 'path' ; import { root } from '@shared/core-utils' ; import { makeDeleteRequest , makeGetRequest , makePostBodyRequest , makePutBodyRequest , makeUploadRequest , unwrapBody , unwrapText } from '../requests/requests' ; import { PeerTubeServer } from '../server/server' ; export interface OverrideCommandOptions { token ? : string ; expectedStatus ? : number ; } interface InternalCommonCommandOptions extends OverrideCommandOptions { url ? : string ; path : string ; implicitToken : boolean ; defaultExpectedStatus : number ; contentType ? : string ; accept ? : string ; redirects ? : number ; range ? : string ; host ? : string ; headers ? : { [ name ] : string ; } ; requestType ? : string ; xForwardedFor ? : string ; } interface InternalGetCommandOptions extends InternalCommonCommandOptions { query ? : { [ id ] : any ; } ; } interface InternalDeleteCommandOptions extends InternalCommonCommandOptions { query ? : { [ id ] : any ; } ; rawQuery ? : string ; } abstract class AbstractCommand { constructor ( protected server ) { } protected getRequestBody < T > ( options ) { return unwrapBody < T > ( this . getRequest ( options ) ) ; } protected getRequestText ( options ) { return unwrapText ( this . getRequest ( options ) ) ; } protected getRawRequest ( options ) { const { url , range } = options ; const { host , protocol , pathname } = new URL ( url ) ; return this . getRequest ( { ... options , token : this . buildCommonRequestToken ( options ) , defaultExpectedStatus : this . buildExpectedStatus ( options ) , url : `${ protocol }//${ host }` , path : pathname , range } ) ; } protected getRequest ( options ) { const { query } = options ; return makeGetRequest ( { ... this . buildCommonRequestOptions ( options ) , query } ) ; } protected deleteRequest ( options ) { const { query , rawQuery } = options ; return makeDeleteRequest ( { ... this . buildCommonRequestOptions ( options ) , query , rawQuery } ) ; } protected putBodyRequest ( options ) { const { fields } = options ; return makePutBodyRequest ( { ... this . buildCommonRequestOptions ( options ) , fields } ) ; } protected postBodyRequest ( options ) { const { fields } = options ; return makePostBodyRequest ( { ... this . buildCommonRequestOptions ( options ) , fields } ) ; } protected postUploadRequest ( options ) { const { fields , attaches } = options ; return makeUploadRequest ( { ... this . buildCommonRequestOptions ( options ) , method : 'POST' , fields , attaches } ) ; } protected putUploadRequest ( options ) { const { fields , attaches } = options ; return makeUploadRequest ( { ... this . buildCommonRequestOptions ( options ) , method : 'PUT' , fields , attaches } ) ; } protected updateImageRequest ( options ) { const filePath = isAbsolute ( options . fixture ) ? options . fixture : join ( root ( ) , 'server' , 'tests' , 'fixtures' , options . fixture ) ; return this . postUploadRequest ( { ... options , fields : { } , attaches : { [ options . fieldname ] : filePath } } ) ; } protected buildCommonRequestOptions ( options ) { const { url , path , redirects , contentType , accept , range , host , headers , requestType , xForwardedFor } = options ; return { url : url ?? this . server . url , path , token : this . buildCommonRequestToken ( options ) , expectedStatus : this . buildExpectedStatus ( options ) , redirects , contentType , range , host , accept , headers , type : requestType , xForwardedFor } ; } protected buildCommonRequestToken ( options ) { const { token } = options ; const fallbackToken = options . implicitToken ? this . server . accessToken : undefined ; return token !== undefined ? token : fallbackToken ; } protected buildExpectedStatus ( options ) { const { expectedStatus , defaultExpectedStatus } = options ; return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus ; } } export { AbstractCommand } ;