]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/shared/abstract-command.ts
Update credits
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
index be368376f75d305560a9b95d01477d8134065a40..a57c857fcf9c2fd37bda3a71f7080f8de6311d61 100644 (file)
@@ -1,4 +1,5 @@
-import { HttpStatusCode } from '@shared/core-utils'
+import { isAbsolute, join } from 'path'
+import { root } from '../miscs/tests'
 import {
   makeDeleteRequest,
   makeGetRequest,
@@ -8,7 +9,7 @@ import {
   unwrapBody,
   unwrapText
 } from '../requests/requests'
-import { ServerInfo } from '../server/servers'
+import { PeerTubeServer } from '../server/server'
 
 export interface OverrideCommandOptions {
   token?: string
@@ -23,34 +24,35 @@ interface InternalCommonCommandOptions extends OverrideCommandOptions {
   // If we automatically send the server token if the token is not provided
   implicitToken: boolean
   defaultExpectedStatus: number
-}
 
-interface InternalGetCommandOptions extends InternalCommonCommandOptions {
-  query?: { [ id: string ]: any }
+  // Common optional request parameters
   contentType?: string
   accept?: string
   redirects?: number
   range?: string
+  host?: string
+  headers?: { [ name: string ]: string }
+  requestType?: string
+  xForwardedFor?: string
 }
 
-abstract class AbstractCommand {
+interface InternalGetCommandOptions extends InternalCommonCommandOptions {
+  query?: { [ id: string ]: any }
+}
 
-  private expectedStatus: HttpStatusCode
+interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
+  query?: { [ id: string ]: any }
+  rawQuery?: string
+}
+
+abstract class AbstractCommand {
 
   constructor (
-    protected server: ServerInfo
+    protected server: PeerTubeServer
   ) {
 
   }
 
-  setServer (server: ServerInfo) {
-    this.server = server
-  }
-
-  setExpectedStatus (status: HttpStatusCode) {
-    this.expectedStatus = status
-  }
-
   protected getRequestBody <T> (options: InternalGetCommandOptions) {
     return unwrapBody<T>(this.getRequest(options))
   }
@@ -67,7 +69,7 @@ abstract class AbstractCommand {
       ...options,
 
       token: this.buildCommonRequestToken(options),
-      defaultExpectedStatus: this.buildStatusCodeExpected(options),
+      defaultExpectedStatus: this.buildExpectedStatus(options),
 
       url: `${protocol}//${host}`,
       path: pathname,
@@ -76,20 +78,24 @@ abstract class AbstractCommand {
   }
 
   protected getRequest (options: InternalGetCommandOptions) {
-    const { redirects, query, contentType, accept } = options
+    const { query } = options
 
     return makeGetRequest({
       ...this.buildCommonRequestOptions(options),
 
-      redirects,
-      query,
-      contentType,
-      accept
+      query
     })
   }
 
-  protected deleteRequest (options: InternalCommonCommandOptions) {
-    return makeDeleteRequest(this.buildCommonRequestOptions(options))
+  protected deleteRequest (options: InternalDeleteCommandOptions) {
+    const { query, rawQuery } = options
+
+    return makeDeleteRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      query,
+      rawQuery
+    })
   }
 
   protected putBodyRequest (options: InternalCommonCommandOptions & {
@@ -118,7 +124,7 @@ abstract class AbstractCommand {
 
   protected postUploadRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
-    attaches?: any
+    attaches?: { [ fieldName: string ]: any }
   }) {
     const { fields, attaches } = options
 
@@ -133,7 +139,7 @@ abstract class AbstractCommand {
 
   protected putUploadRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
-    attaches?: any
+    attaches?: { [ fieldName: string ]: any }
   }) {
     const { fields, attaches } = options
 
@@ -146,19 +152,44 @@ abstract class AbstractCommand {
     })
   }
 
-  private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
-    const { path } = options
+  protected updateImageRequest (options: InternalCommonCommandOptions & {
+    fixture: string
+    fieldname: string
+  }) {
+    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: InternalCommonCommandOptions) {
+    const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
 
     return {
-      url: this.server.url,
+      url: url ?? this.server.url,
       path,
 
       token: this.buildCommonRequestToken(options),
-      statusCodeExpected: this.buildStatusCodeExpected(options)
+      expectedStatus: this.buildExpectedStatus(options),
+
+      redirects,
+      contentType,
+      range,
+      host,
+      accept,
+      headers,
+      type: requestType,
+      xForwardedFor
     }
   }
 
-  private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
+  protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
     const { token } = options
 
     const fallbackToken = options.implicitToken
@@ -168,10 +199,10 @@ abstract class AbstractCommand {
     return token !== undefined ? token : fallbackToken
   }
 
-  private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
+  protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
     const { expectedStatus, defaultExpectedStatus } = options
 
-    return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+    return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
   }
 }