]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/shared/abstract-command.ts
Introduce streaming playlists command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
index d8eba373d7afb2f5ba1a2e2fdcc4935261452b74..be368376f75d305560a9b95d01477d8134065a40 100644 (file)
@@ -1,5 +1,13 @@
 import { HttpStatusCode } from '@shared/core-utils'
-import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests'
+import {
+  makeDeleteRequest,
+  makeGetRequest,
+  makePostBodyRequest,
+  makePutBodyRequest,
+  makeUploadRequest,
+  unwrapBody,
+  unwrapText
+} from '../requests/requests'
 import { ServerInfo } from '../server/servers'
 
 export interface OverrideCommandOptions {
@@ -7,11 +15,24 @@ export interface OverrideCommandOptions {
   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
 }
 
+interface InternalGetCommandOptions extends InternalCommonCommandOptions {
+  query?: { [ id: string ]: any }
+  contentType?: string
+  accept?: string
+  redirects?: number
+  range?: string
+}
+
 abstract class AbstractCommand {
 
   private expectedStatus: HttpStatusCode
@@ -30,11 +51,48 @@ abstract class AbstractCommand {
     this.expectedStatus = status
   }
 
-  protected getRequestBody <T> (options: CommonCommandOptions) {
-    return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options)))
+  protected getRequestBody <T> (options: InternalGetCommandOptions) {
+    return unwrapBody<T>(this.getRequest(options))
+  }
+
+  protected getRequestText (options: InternalGetCommandOptions) {
+    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.buildStatusCodeExpected(options),
+
+      url: `${protocol}//${host}`,
+      path: pathname,
+      range
+    })
+  }
+
+  protected getRequest (options: InternalGetCommandOptions) {
+    const { redirects, query, contentType, accept } = options
+
+    return makeGetRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      redirects,
+      query,
+      contentType,
+      accept
+    })
   }
 
-  protected putBodyRequest (options: CommonCommandOptions & {
+  protected deleteRequest (options: InternalCommonCommandOptions) {
+    return makeDeleteRequest(this.buildCommonRequestOptions(options))
+  }
+
+  protected putBodyRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
   }) {
     const { fields } = options
@@ -46,7 +104,7 @@ abstract class AbstractCommand {
     })
   }
 
-  protected postBodyRequest (options: CommonCommandOptions & {
+  protected postBodyRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
   }) {
     const { fields } = options
@@ -58,16 +116,63 @@ abstract class AbstractCommand {
     })
   }
 
-  private buildCommonRequestOptions (options: CommonCommandOptions) {
-    const { token, expectedStatus, defaultExpectedStatus, path } = options
+  protected postUploadRequest (options: InternalCommonCommandOptions & {
+    fields?: { [ fieldName: string ]: any }
+    attaches?: any
+  }) {
+    const { fields, attaches } = options
+
+    return makeUploadRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      method: 'POST',
+      fields,
+      attaches
+    })
+  }
+
+  protected putUploadRequest (options: InternalCommonCommandOptions & {
+    fields?: { [ fieldName: string ]: any }
+    attaches?: any
+  }) {
+    const { fields, attaches } = options
+
+    return makeUploadRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      method: 'PUT',
+      fields,
+      attaches
+    })
+  }
+
+  private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
+    const { path } = options
 
     return {
       url: this.server.url,
       path,
-      token: token ?? this.server.accessToken,
-      statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+
+      token: this.buildCommonRequestToken(options),
+      statusCodeExpected: this.buildStatusCodeExpected(options)
     }
   }
+
+  private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
+    const { token } = options
+
+    const fallbackToken = options.implicitToken
+      ? this.server.accessToken
+      : undefined
+
+    return token !== undefined ? token : fallbackToken
+  }
+
+  private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
+    const { expectedStatus, defaultExpectedStatus } = options
+
+    return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+  }
 }
 
 export {