]> 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 bb06b6bdba43f2fec26900d0eabc7d6653037f29..be368376f75d305560a9b95d01477d8134065a40 100644 (file)
@@ -1,15 +1,41 @@
 import { HttpStatusCode } from '@shared/core-utils'
-import { makePostBodyRequest } from '../requests/requests'
+import {
+  makeDeleteRequest,
+  makeGetRequest,
+  makePostBodyRequest,
+  makePutBodyRequest,
+  makeUploadRequest,
+  unwrapBody,
+  unwrapText
+} from '../requests/requests'
 import { ServerInfo } from '../server/servers'
 
-export interface CommonCommandOptions {
+export interface OverrideCommandOptions {
   token?: string
   expectedStatus?: number
 }
 
+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.OK_200
+  private expectedStatus: HttpStatusCode
 
   constructor (
     protected server: ServerInfo
@@ -25,21 +51,128 @@ abstract class AbstractCommand {
     this.expectedStatus = status
   }
 
-  protected postBodyRequest (options: CommonCommandOptions & {
-    path: string
-    defaultExpectedStatus: number
+  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 deleteRequest (options: InternalCommonCommandOptions) {
+    return makeDeleteRequest(this.buildCommonRequestOptions(options))
+  }
+
+  protected putBodyRequest (options: InternalCommonCommandOptions & {
     fields?: { [ fieldName: string ]: any }
   }) {
-    const { token, fields, expectedStatus, defaultExpectedStatus, path } = options
+    const { fields } = options
+
+    return makePutBodyRequest({
+      ...this.buildCommonRequestOptions(options),
+
+      fields
+    })
+  }
+
+  protected postBodyRequest (options: InternalCommonCommandOptions & {
+    fields?: { [ fieldName: string ]: any }
+  }) {
+    const { fields } = options
 
     return makePostBodyRequest({
-      url: this.server.url,
-      path,
-      token: token ?? this.server.accessToken,
+      ...this.buildCommonRequestOptions(options),
+
+      fields
+    })
+  }
+
+  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,
-      statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+      attaches
     })
   }
+
+  private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
+    const { path } = options
+
+    return {
+      url: this.server.url,
+      path,
+
+      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 {