]> 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 1e07e946920ba34b1ac3c9a26b860c155fcdca9f..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
@@ -16,37 +17,42 @@ export interface OverrideCommandOptions {
 }
 
 interface InternalCommonCommandOptions extends OverrideCommandOptions {
+  // Default to server.url
+  url?: string
+
   path: string
   // 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 }
+}
+
+interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
+  query?: { [ id: string ]: any }
+  rawQuery?: string
+}
 
-  private expectedStatus: HttpStatusCode
+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))
   }
@@ -55,21 +61,41 @@ abstract class AbstractCommand {
     return unwrapText(this.getRequest(options))
   }
 
+  protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
+    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: 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 & {
@@ -98,7 +124,7 @@ abstract class AbstractCommand {
 
   protected postUploadRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
-    attaches?: any
+    attaches?: { [ fieldName: string ]: any }
   }) {
     const { fields, attaches } = options
 
@@ -113,7 +139,7 @@ abstract class AbstractCommand {
 
   protected putUploadRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
-    attaches?: any
+    attaches?: { [ fieldName: string ]: any }
   }) {
     const { fields, attaches } = options
 
@@ -126,22 +152,58 @@ abstract class AbstractCommand {
     })
   }
 
-  private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
-    const { token, expectedStatus, defaultExpectedStatus, path } = options
+  protected updateImageRequest (options: InternalCommonCommandOptions & {
+    fixture: string
+    fieldname: string
+  }) {
+    const filePath = isAbsolute(options.fixture)
+      ? options.fixture
+      : join(root(), 'server', 'tests', 'fixtures', options.fixture)
 
-    const fallbackToken = options.implicitToken
-      ? this.server.accessToken
-      : undefined
+    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: token !== undefined ? token : fallbackToken,
+      token: this.buildCommonRequestToken(options),
+      expectedStatus: this.buildExpectedStatus(options),
 
-      statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+      redirects,
+      contentType,
+      range,
+      host,
+      accept,
+      headers,
+      type: requestType,
+      xForwardedFor
     }
   }
+
+  protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
+    const { token } = options
+
+    const fallbackToken = options.implicitToken
+      ? this.server.accessToken
+      : undefined
+
+    return token !== undefined ? token : fallbackToken
+  }
+
+  protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
+    const { expectedStatus, defaultExpectedStatus } = options
+
+    return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
+  }
 }
 
 export {