]> 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 d8eba373d7afb2f5ba1a2e2fdcc4935261452b74..a57c857fcf9c2fd37bda3a71f7080f8de6311d61 100644 (file)
-import { HttpStatusCode } from '@shared/core-utils'
-import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests'
-import { ServerInfo } from '../server/servers'
+import { isAbsolute, join } from 'path'
+import { root } from '../miscs/tests'
+import {
+  makeDeleteRequest,
+  makeGetRequest,
+  makePostBodyRequest,
+  makePutBodyRequest,
+  makeUploadRequest,
+  unwrapBody,
+  unwrapText
+} from '../requests/requests'
+import { PeerTubeServer } from '../server/server'
 
 export interface OverrideCommandOptions {
   token?: string
   expectedStatus?: number
 }
 
-interface CommonCommandOptions extends 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
+
+  // 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
+  protected getRequestBody <T> (options: InternalGetCommandOptions) {
+    return unwrapBody<T>(this.getRequest(options))
   }
 
-  setExpectedStatus (status: HttpStatusCode) {
-    this.expectedStatus = status
+  protected getRequestText (options: InternalGetCommandOptions) {
+    return unwrapText(this.getRequest(options))
   }
 
-  protected getRequestBody <T> (options: CommonCommandOptions) {
-    return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(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 putBodyRequest (options: CommonCommandOptions & {
+  protected getRequest (options: InternalGetCommandOptions) {
+    const { query } = options
+
+    return makeGetRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      query
+    })
+  }
+
+  protected deleteRequest (options: InternalDeleteCommandOptions) {
+    const { query, rawQuery } = options
+
+    return makeDeleteRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      query,
+      rawQuery
+    })
+  }
+
+  protected putBodyRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
   }) {
     const { fields } = options
@@ -46,7 +110,7 @@ abstract class AbstractCommand {
     })
   }
 
-  protected postBodyRequest (options: CommonCommandOptions & {
+  protected postBodyRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
   }) {
     const { fields } = options
@@ -58,16 +122,88 @@ abstract class AbstractCommand {
     })
   }
 
-  private buildCommonRequestOptions (options: CommonCommandOptions) {
-    const { token, expectedStatus, defaultExpectedStatus, path } = options
+  protected postUploadRequest (options: InternalCommonCommandOptions & {
+    fields?: { [ fieldName: string ]: any }
+    attaches?: { [ fieldName: string ]: any }
+  }) {
+    const { fields, attaches } = options
+
+    return makeUploadRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      method: 'POST',
+      fields,
+      attaches
+    })
+  }
+
+  protected putUploadRequest (options: InternalCommonCommandOptions & {
+    fields?: { [ fieldName: string ]: any }
+    attaches?: { [ fieldName: string ]: any }
+  }) {
+    const { fields, attaches } = options
+
+    return makeUploadRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      method: 'PUT',
+      fields,
+      attaches
+    })
+  }
+
+  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: token ?? this.server.accessToken,
-      statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+
+      token: this.buildCommonRequestToken(options),
+      expectedStatus: this.buildExpectedStatus(options),
+
+      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 {