1 import { HttpStatusCode } from '@shared/core-utils'
2 import { 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 ]: string }
21 abstract class AbstractCommand {
23 private expectedStatus: HttpStatusCode
26 protected server: ServerInfo
31 setServer (server: ServerInfo) {
35 setExpectedStatus (status: HttpStatusCode) {
36 this.expectedStatus = status
39 protected getRequestBody <T> (options: GetCommandOptions) {
40 return unwrapBody<T>(this.getRequest(options))
43 protected getRequestText (options: GetCommandOptions) {
44 return unwrapText(this.getRequest(options))
47 protected putBodyRequest (options: CommonCommandOptions & {
48 fields?: { [ fieldName: string ]: any }
50 const { fields } = options
52 return makePutBodyRequest({
53 ...this.buildCommonRequestOptions(options),
59 protected postBodyRequest (options: CommonCommandOptions & {
60 fields?: { [ fieldName: string ]: any }
62 const { fields } = options
64 return makePostBodyRequest({
65 ...this.buildCommonRequestOptions(options),
71 private buildCommonRequestOptions (options: CommonCommandOptions) {
72 const { token, expectedStatus, defaultExpectedStatus, path } = options
77 token: token ?? this.server.accessToken,
78 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
82 private getRequest (options: GetCommandOptions) {
83 const { query, contentType, accept } = options
85 return makeGetRequest({
86 ...this.buildCommonRequestOptions(options),