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 }
36 xForwardedFor?: string
39 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
40 query?: { [ id: string ]: any }
43 interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
44 query?: { [ id: string ]: any }
48 abstract class AbstractCommand {
51 protected server: PeerTubeServer
56 protected getRequestBody <T> (options: InternalGetCommandOptions) {
57 return unwrapBody<T>(this.getRequest(options))
60 protected getRequestText (options: InternalGetCommandOptions) {
61 return unwrapText(this.getRequest(options))
64 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
65 const { url, range } = options
66 const { host, protocol, pathname } = new URL(url)
68 return this.getRequest({
71 token: this.buildCommonRequestToken(options),
72 defaultExpectedStatus: this.buildExpectedStatus(options),
74 url: `${protocol}//${host}`,
80 protected getRequest (options: InternalGetCommandOptions) {
81 const { query } = options
83 return makeGetRequest({
84 ...this.buildCommonRequestOptions(options),
90 protected deleteRequest (options: InternalDeleteCommandOptions) {
91 const { query, rawQuery } = options
93 return makeDeleteRequest({
94 ...this.buildCommonRequestOptions(options),
101 protected putBodyRequest (options: InternalCommonCommandOptions & {
102 fields?: { [ fieldName: string ]: any }
104 const { fields } = options
106 return makePutBodyRequest({
107 ...this.buildCommonRequestOptions(options),
113 protected postBodyRequest (options: InternalCommonCommandOptions & {
114 fields?: { [ fieldName: string ]: any }
116 const { fields } = options
118 return makePostBodyRequest({
119 ...this.buildCommonRequestOptions(options),
125 protected postUploadRequest (options: InternalCommonCommandOptions & {
126 fields?: { [ fieldName: string ]: any }
127 attaches?: { [ fieldName: string ]: any }
129 const { fields, attaches } = options
131 return makeUploadRequest({
132 ...this.buildCommonRequestOptions(options),
140 protected putUploadRequest (options: InternalCommonCommandOptions & {
141 fields?: { [ fieldName: string ]: any }
142 attaches?: { [ fieldName: string ]: any }
144 const { fields, attaches } = options
146 return makeUploadRequest({
147 ...this.buildCommonRequestOptions(options),
155 protected updateImageRequest (options: InternalCommonCommandOptions & {
159 const filePath = isAbsolute(options.fixture)
161 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
163 return this.postUploadRequest({
167 attaches: { [options.fieldname]: filePath }
171 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
172 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
175 url: url ?? this.server.url,
178 token: this.buildCommonRequestToken(options),
179 expectedStatus: this.buildExpectedStatus(options),
192 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
193 const { token } = options
195 const fallbackToken = options.implicitToken
196 ? this.server.accessToken
199 return token !== undefined ? token : fallbackToken
202 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
203 const { expectedStatus, defaultExpectedStatus } = options
205 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus