]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - shared/extra-utils/shared/abstract-command.ts
Specify if we want to fallback to the server token
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
... / ...
CommitLineData
1import { HttpStatusCode } from '@shared/core-utils'
2import {
3 makeDeleteRequest,
4 makeGetRequest,
5 makePostBodyRequest,
6 makePutBodyRequest,
7 makeUploadRequest,
8 unwrapBody,
9 unwrapText
10} from '../requests/requests'
11import { ServerInfo } from '../server/servers'
12
13export interface OverrideCommandOptions {
14 token?: string
15 expectedStatus?: number
16}
17
18interface InternalCommonCommandOptions extends OverrideCommandOptions {
19 path: string
20 // If we automatically send the server token if the token is not provided
21 implicitToken: boolean
22 defaultExpectedStatus: number
23}
24
25interface InternalGetCommandOptions extends InternalCommonCommandOptions {
26 query?: { [ id: string ]: any }
27 contentType?: string
28 accept?: string
29 redirects?: number
30}
31
32abstract class AbstractCommand {
33
34 private expectedStatus: HttpStatusCode
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
50 protected getRequestBody <T> (options: InternalGetCommandOptions) {
51 return unwrapBody<T>(this.getRequest(options))
52 }
53
54 protected getRequestText (options: InternalGetCommandOptions) {
55 return unwrapText(this.getRequest(options))
56 }
57
58 protected getRequest (options: InternalGetCommandOptions) {
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
71 protected deleteRequest (options: InternalCommonCommandOptions) {
72 return makeDeleteRequest(this.buildCommonRequestOptions(options))
73 }
74
75 protected putBodyRequest (options: InternalCommonCommandOptions & {
76 fields?: { [ fieldName: string ]: any }
77 }) {
78 const { fields } = options
79
80 return makePutBodyRequest({
81 ...this.buildCommonRequestOptions(options),
82
83 fields
84 })
85 }
86
87 protected postBodyRequest (options: InternalCommonCommandOptions & {
88 fields?: { [ fieldName: string ]: any }
89 }) {
90 const { fields } = options
91
92 return makePostBodyRequest({
93 ...this.buildCommonRequestOptions(options),
94
95 fields
96 })
97 }
98
99 protected postUploadRequest (options: InternalCommonCommandOptions & {
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
114 protected putUploadRequest (options: InternalCommonCommandOptions & {
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
129 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
130 const { token, expectedStatus, defaultExpectedStatus, path } = options
131
132 const fallbackToken = options.implicitToken
133 ? this.server.accessToken
134 : undefined
135
136 return {
137 url: this.server.url,
138 path,
139
140 token: token !== undefined ? token : fallbackToken,
141
142 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
143 }
144 }
145}
146
147export {
148 AbstractCommand
149}