1 import { HttpStatusCode } from '@shared/core-utils'
10 } from '../requests/requests'
11 import { ServerInfo } from '../server/servers'
13 export interface OverrideCommandOptions {
15 expectedStatus?: number
18 interface InternalCommonCommandOptions extends OverrideCommandOptions {
19 // Default to server.url
23 // If we automatically send the server token if the token is not provided
24 implicitToken: boolean
25 defaultExpectedStatus: number
28 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
29 query?: { [ id: string ]: any }
36 abstract class AbstractCommand {
38 private expectedStatus: HttpStatusCode
41 protected server: ServerInfo
46 setServer (server: ServerInfo) {
50 setExpectedStatus (status: HttpStatusCode) {
51 this.expectedStatus = status
54 protected getRequestBody <T> (options: InternalGetCommandOptions) {
55 return unwrapBody<T>(this.getRequest(options))
58 protected getRequestText (options: InternalGetCommandOptions) {
59 return unwrapText(this.getRequest(options))
62 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
63 const { url, range } = options
64 const { host, protocol, pathname } = new URL(url)
66 return this.getRequest({
69 token: this.buildCommonRequestToken(options),
70 defaultExpectedStatus: this.buildStatusCodeExpected(options),
72 url: `${protocol}//${host}`,
78 protected getRequest (options: InternalGetCommandOptions) {
79 const { redirects, query, contentType, accept } = options
81 return makeGetRequest({
82 ...this.buildCommonRequestOptions(options),
91 protected deleteRequest (options: InternalCommonCommandOptions) {
92 return makeDeleteRequest(this.buildCommonRequestOptions(options))
95 protected putBodyRequest (options: InternalCommonCommandOptions & {
96 fields?: { [ fieldName: string ]: any }
98 const { fields } = options
100 return makePutBodyRequest({
101 ...this.buildCommonRequestOptions(options),
107 protected postBodyRequest (options: InternalCommonCommandOptions & {
108 fields?: { [ fieldName: string ]: any }
110 const { fields } = options
112 return makePostBodyRequest({
113 ...this.buildCommonRequestOptions(options),
119 protected postUploadRequest (options: InternalCommonCommandOptions & {
120 fields?: { [ fieldName: string ]: any }
123 const { fields, attaches } = options
125 return makeUploadRequest({
126 ...this.buildCommonRequestOptions(options),
134 protected putUploadRequest (options: InternalCommonCommandOptions & {
135 fields?: { [ fieldName: string ]: any }
138 const { fields, attaches } = options
140 return makeUploadRequest({
141 ...this.buildCommonRequestOptions(options),
149 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
150 const { path } = options
153 url: this.server.url,
156 token: this.buildCommonRequestToken(options),
157 statusCodeExpected: this.buildStatusCodeExpected(options)
161 private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
162 const { token } = options
164 const fallbackToken = options.implicitToken
165 ? this.server.accessToken
168 return token !== undefined ? token : fallbackToken
171 private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
172 const { expectedStatus, defaultExpectedStatus } = options
174 return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus