1 import { HttpStatusCode } from '@shared/core-utils'
2 import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests'
3 import { ServerInfo } from '../server/servers'
5 export interface OverrideCommandOptions {
7 expectedStatus?: number
10 interface CommonCommandOptions extends OverrideCommandOptions {
12 defaultExpectedStatus: number
15 interface GetCommandOptions extends CommonCommandOptions {
16 query?: { [ id: string ]: any }
22 abstract class AbstractCommand {
24 private expectedStatus: HttpStatusCode
27 protected server: ServerInfo
32 setServer (server: ServerInfo) {
36 setExpectedStatus (status: HttpStatusCode) {
37 this.expectedStatus = status
40 protected getRequestBody <T> (options: GetCommandOptions) {
41 return unwrapBody<T>(this.getRequest(options))
44 protected getRequestText (options: GetCommandOptions) {
45 return unwrapText(this.getRequest(options))
48 protected getRequest (options: GetCommandOptions) {
49 const { redirects, query, contentType, accept } = options
51 return makeGetRequest({
52 ...this.buildCommonRequestOptions(options),
61 protected deleteRequest (options: CommonCommandOptions) {
62 return makeDeleteRequest(this.buildCommonRequestOptions(options))
65 protected putBodyRequest (options: CommonCommandOptions & {
66 fields?: { [ fieldName: string ]: any }
68 const { fields } = options
70 return makePutBodyRequest({
71 ...this.buildCommonRequestOptions(options),
77 protected postBodyRequest (options: CommonCommandOptions & {
78 fields?: { [ fieldName: string ]: any }
80 const { fields } = options
82 return makePostBodyRequest({
83 ...this.buildCommonRequestOptions(options),
89 private buildCommonRequestOptions (options: CommonCommandOptions) {
90 const { token, expectedStatus, defaultExpectedStatus, path } = options
96 // Token can be null if we don't want to add it
97 token: token !== undefined ? token : this.server.accessToken,
99 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus