]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/shared/abstract-command.ts
Introduce CustomPage command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
1 import { HttpStatusCode } from '@shared/core-utils'
2 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests'
3 import { ServerInfo } from '../server/servers'
4
5 export interface OverrideCommandOptions {
6 token?: string
7 expectedStatus?: number
8 }
9
10 interface CommonCommandOptions extends OverrideCommandOptions {
11 path: string
12 defaultExpectedStatus: number
13 }
14
15 abstract class AbstractCommand {
16
17 private expectedStatus: HttpStatusCode
18
19 constructor (
20 protected server: ServerInfo
21 ) {
22
23 }
24
25 setServer (server: ServerInfo) {
26 this.server = server
27 }
28
29 setExpectedStatus (status: HttpStatusCode) {
30 this.expectedStatus = status
31 }
32
33 protected getRequestBody <T> (options: CommonCommandOptions) {
34 return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options)))
35 }
36
37 protected putBodyRequest (options: CommonCommandOptions & {
38 fields?: { [ fieldName: string ]: any }
39 }) {
40 const { fields } = options
41
42 return makePutBodyRequest({
43 ...this.buildCommonRequestOptions(options),
44
45 fields
46 })
47 }
48
49 protected postBodyRequest (options: CommonCommandOptions & {
50 fields?: { [ fieldName: string ]: any }
51 }) {
52 const { fields } = options
53
54 return makePostBodyRequest({
55 ...this.buildCommonRequestOptions(options),
56
57 fields
58 })
59 }
60
61 private buildCommonRequestOptions (options: CommonCommandOptions) {
62 const { token, expectedStatus, defaultExpectedStatus, path } = options
63
64 return {
65 url: this.server.url,
66 path,
67 token: token ?? this.server.accessToken,
68 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
69 }
70 }
71 }
72
73 export {
74 AbstractCommand
75 }