X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fextra-utils%2Fshared%2Fabstract-command.ts;h=d8eba373d7afb2f5ba1a2e2fdcc4935261452b74;hb=e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6;hp=bb06b6bdba43f2fec26900d0eabc7d6653037f29;hpb=329619b3453479f76c049816b7403b86e9d45cb5;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index bb06b6bdb..d8eba373d 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -1,15 +1,20 @@ import { HttpStatusCode } from '@shared/core-utils' -import { makePostBodyRequest } from '../requests/requests' +import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' import { ServerInfo } from '../server/servers' -export interface CommonCommandOptions { +export interface OverrideCommandOptions { token?: string expectedStatus?: number } +interface CommonCommandOptions extends OverrideCommandOptions { + path: string + defaultExpectedStatus: number +} + abstract class AbstractCommand { - private expectedStatus = HttpStatusCode.OK_200 + private expectedStatus: HttpStatusCode constructor ( protected server: ServerInfo @@ -25,20 +30,43 @@ abstract class AbstractCommand { this.expectedStatus = status } + protected getRequestBody (options: CommonCommandOptions) { + return unwrap(makeGetRequest(this.buildCommonRequestOptions(options))) + } + + protected putBodyRequest (options: CommonCommandOptions & { + fields?: { [ fieldName: string ]: any } + }) { + const { fields } = options + + return makePutBodyRequest({ + ...this.buildCommonRequestOptions(options), + + fields + }) + } + protected postBodyRequest (options: CommonCommandOptions & { - path: string - defaultExpectedStatus: number fields?: { [ fieldName: string ]: any } }) { - const { token, fields, expectedStatus, defaultExpectedStatus, path } = options + const { fields } = options return makePostBodyRequest({ + ...this.buildCommonRequestOptions(options), + + fields + }) + } + + private buildCommonRequestOptions (options: CommonCommandOptions) { + const { token, expectedStatus, defaultExpectedStatus, path } = options + + return { url: this.server.url, path, token: token ?? this.server.accessToken, - fields, statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus - }) + } } }