]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/shared/abstract-command.ts
Introduce import command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
CommitLineData
a6a79eae 1import { HttpStatusCode } from '@shared/core-utils'
a1637fa1
C
2import {
3 makeDeleteRequest,
4 makeGetRequest,
5 makePostBodyRequest,
6 makePutBodyRequest,
7 makeUploadRequest,
8 unwrapBody,
9 unwrapText
10} from '../requests/requests'
a6a79eae
C
11import { ServerInfo } from '../server/servers'
12
e8bd7ce7 13export interface OverrideCommandOptions {
a6a79eae
C
14 token?: string
15 expectedStatus?: number
16}
17
a1637fa1 18interface InternalCommonCommandOptions extends OverrideCommandOptions {
e8bd7ce7 19 path: string
a1637fa1
C
20 // If we automatically send the server token if the token is not provided
21 implicitToken: boolean
e8bd7ce7
C
22 defaultExpectedStatus: number
23}
24
a1637fa1 25interface InternalGetCommandOptions extends InternalCommonCommandOptions {
a92ddacb 26 query?: { [ id: string ]: any }
c1bc8ee4
C
27 contentType?: string
28 accept?: string
ae2abfd3 29 redirects?: number
c1bc8ee4
C
30}
31
a6a79eae
C
32abstract class AbstractCommand {
33
e8bd7ce7 34 private expectedStatus: HttpStatusCode
a6a79eae
C
35
36 constructor (
37 protected server: ServerInfo
38 ) {
39
40 }
41
42 setServer (server: ServerInfo) {
43 this.server = server
44 }
45
46 setExpectedStatus (status: HttpStatusCode) {
47 this.expectedStatus = status
48 }
49
a1637fa1 50 protected getRequestBody <T> (options: InternalGetCommandOptions) {
c1bc8ee4
C
51 return unwrapBody<T>(this.getRequest(options))
52 }
53
a1637fa1 54 protected getRequestText (options: InternalGetCommandOptions) {
c1bc8ee4 55 return unwrapText(this.getRequest(options))
e8bd7ce7
C
56 }
57
a1637fa1 58 protected getRequest (options: InternalGetCommandOptions) {
ae2abfd3
C
59 const { redirects, query, contentType, accept } = options
60
61 return makeGetRequest({
62 ...this.buildCommonRequestOptions(options),
63
64 redirects,
65 query,
66 contentType,
67 accept
68 })
69 }
70
a1637fa1 71 protected deleteRequest (options: InternalCommonCommandOptions) {
0c1a77e9
C
72 return makeDeleteRequest(this.buildCommonRequestOptions(options))
73 }
74
a1637fa1 75 protected putBodyRequest (options: InternalCommonCommandOptions & {
e8bd7ce7
C
76 fields?: { [ fieldName: string ]: any }
77 }) {
78 const { fields } = options
79
80 return makePutBodyRequest({
81 ...this.buildCommonRequestOptions(options),
82
83 fields
84 })
85 }
86
a1637fa1 87 protected postBodyRequest (options: InternalCommonCommandOptions & {
a6a79eae
C
88 fields?: { [ fieldName: string ]: any }
89 }) {
e8bd7ce7 90 const { fields } = options
a6a79eae
C
91
92 return makePostBodyRequest({
e8bd7ce7
C
93 ...this.buildCommonRequestOptions(options),
94
95 fields
96 })
97 }
98
a1637fa1 99 protected postUploadRequest (options: InternalCommonCommandOptions & {
4f219914
C
100 fields?: { [ fieldName: string ]: any }
101 attaches?: any
102 }) {
103 const { fields, attaches } = options
104
105 return makeUploadRequest({
106 ...this.buildCommonRequestOptions(options),
107
108 method: 'POST',
109 fields,
110 attaches
111 })
112 }
113
a1637fa1 114 protected putUploadRequest (options: InternalCommonCommandOptions & {
4f219914
C
115 fields?: { [ fieldName: string ]: any }
116 attaches?: any
117 }) {
118 const { fields, attaches } = options
119
120 return makeUploadRequest({
121 ...this.buildCommonRequestOptions(options),
122
123 method: 'PUT',
124 fields,
125 attaches
126 })
127 }
128
a1637fa1 129 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
e8bd7ce7
C
130 const { token, expectedStatus, defaultExpectedStatus, path } = options
131
a1637fa1
C
132 const fallbackToken = options.implicitToken
133 ? this.server.accessToken
134 : undefined
135
e8bd7ce7 136 return {
a6a79eae
C
137 url: this.server.url,
138 path,
23a3a882 139
a1637fa1 140 token: token !== undefined ? token : fallbackToken,
23a3a882 141
a6a79eae 142 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
e8bd7ce7 143 }
a6a79eae
C
144 }
145}
146
147export {
148 AbstractCommand
149}