import { HttpStatusCode } from '@shared/core-utils'
-import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests'
+import {
+ makeDeleteRequest,
+ makeGetRequest,
+ makePostBodyRequest,
+ makePutBodyRequest,
+ makeUploadRequest,
+ unwrapBody,
+ unwrapText
+} from '../requests/requests'
import { ServerInfo } from '../server/servers'
export interface OverrideCommandOptions {
expectedStatus?: number
}
-interface CommonCommandOptions extends OverrideCommandOptions {
+interface InternalCommonCommandOptions extends OverrideCommandOptions {
+ // Default to server.url
+ url?: string
+
path: string
+ // If we automatically send the server token if the token is not provided
+ implicitToken: boolean
defaultExpectedStatus: number
}
-interface GetCommandOptions extends CommonCommandOptions {
+interface InternalGetCommandOptions extends InternalCommonCommandOptions {
query?: { [ id: string ]: any }
contentType?: string
accept?: string
+ redirects?: number
+ range?: string
}
abstract class AbstractCommand {
this.expectedStatus = status
}
- protected getRequestBody <T> (options: GetCommandOptions) {
+ protected getRequestBody <T> (options: InternalGetCommandOptions) {
return unwrapBody<T>(this.getRequest(options))
}
- protected getRequestText (options: GetCommandOptions) {
+ protected getRequestText (options: InternalGetCommandOptions) {
return unwrapText(this.getRequest(options))
}
- protected putBodyRequest (options: CommonCommandOptions & {
+ protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
+ const { url, range } = options
+ const { host, protocol, pathname } = new URL(url)
+
+ return this.getRequest({
+ ...options,
+
+ token: this.buildCommonRequestToken(options),
+ defaultExpectedStatus: this.buildStatusCodeExpected(options),
+
+ url: `${protocol}//${host}`,
+ path: pathname,
+ range
+ })
+ }
+
+ protected getRequest (options: InternalGetCommandOptions) {
+ const { redirects, query, contentType, accept } = options
+
+ return makeGetRequest({
+ ...this.buildCommonRequestOptions(options),
+
+ redirects,
+ query,
+ contentType,
+ accept
+ })
+ }
+
+ protected deleteRequest (options: InternalCommonCommandOptions) {
+ return makeDeleteRequest(this.buildCommonRequestOptions(options))
+ }
+
+ protected putBodyRequest (options: InternalCommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
}) {
const { fields } = options
})
}
- protected postBodyRequest (options: CommonCommandOptions & {
+ protected postBodyRequest (options: InternalCommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
}) {
const { fields } = options
})
}
- private buildCommonRequestOptions (options: CommonCommandOptions) {
- const { token, expectedStatus, defaultExpectedStatus, path } = options
+ protected postUploadRequest (options: InternalCommonCommandOptions & {
+ fields?: { [ fieldName: string ]: any }
+ attaches?: any
+ }) {
+ const { fields, attaches } = options
+
+ return makeUploadRequest({
+ ...this.buildCommonRequestOptions(options),
+
+ method: 'POST',
+ fields,
+ attaches
+ })
+ }
+
+ protected putUploadRequest (options: InternalCommonCommandOptions & {
+ fields?: { [ fieldName: string ]: any }
+ attaches?: any
+ }) {
+ const { fields, attaches } = options
+
+ return makeUploadRequest({
+ ...this.buildCommonRequestOptions(options),
+
+ method: 'PUT',
+ fields,
+ attaches
+ })
+ }
+
+ private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
+ const { path } = options
return {
url: this.server.url,
path,
- token: token ?? this.server.accessToken,
- statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+
+ token: this.buildCommonRequestToken(options),
+ statusCodeExpected: this.buildStatusCodeExpected(options)
}
}
- private getRequest (options: GetCommandOptions) {
- const { query, contentType, accept } = options
+ private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
+ const { token } = options
- return makeGetRequest({
- ...this.buildCommonRequestOptions(options),
+ const fallbackToken = options.implicitToken
+ ? this.server.accessToken
+ : undefined
- query,
- contentType,
- accept
- })
+ return token !== undefined ? token : fallbackToken
+ }
+
+ private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
+ const { expectedStatus, defaultExpectedStatus } = options
+
+ return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
}
}