1 import { isAbsolute, join } from 'path'
2 import { HttpStatusCode } from '@shared/core-utils'
3 import { root } from '../miscs/miscs'
12 } from '../requests/requests'
13 import { ServerInfo } from '../server/servers'
15 export interface OverrideCommandOptions {
17 expectedStatus?: number
20 interface InternalCommonCommandOptions extends OverrideCommandOptions {
21 // Default to server.url
25 // If we automatically send the server token if the token is not provided
26 implicitToken: boolean
27 defaultExpectedStatus: number
30 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
31 query?: { [ id: string ]: any }
38 abstract class AbstractCommand {
40 private expectedStatus: HttpStatusCode
43 protected server: ServerInfo
48 setServer (server: ServerInfo) {
52 setExpectedStatus (status: HttpStatusCode) {
53 this.expectedStatus = status
56 protected getRequestBody <T> (options: InternalGetCommandOptions) {
57 return unwrapBody<T>(this.getRequest(options))
60 protected getRequestText (options: InternalGetCommandOptions) {
61 return unwrapText(this.getRequest(options))
64 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
65 const { url, range } = options
66 const { host, protocol, pathname } = new URL(url)
68 return this.getRequest({
71 token: this.buildCommonRequestToken(options),
72 defaultExpectedStatus: this.buildStatusCodeExpected(options),
74 url: `${protocol}//${host}`,
80 protected getRequest (options: InternalGetCommandOptions) {
81 const { redirects, query, contentType, accept } = options
83 return makeGetRequest({
84 ...this.buildCommonRequestOptions(options),
93 protected deleteRequest (options: InternalCommonCommandOptions) {
94 return makeDeleteRequest(this.buildCommonRequestOptions(options))
97 protected putBodyRequest (options: InternalCommonCommandOptions & {
98 fields?: { [ fieldName: string ]: any }
100 const { fields } = options
102 return makePutBodyRequest({
103 ...this.buildCommonRequestOptions(options),
109 protected postBodyRequest (options: InternalCommonCommandOptions & {
110 fields?: { [ fieldName: string ]: any }
112 const { fields } = options
114 return makePostBodyRequest({
115 ...this.buildCommonRequestOptions(options),
121 protected postUploadRequest (options: InternalCommonCommandOptions & {
122 fields?: { [ fieldName: string ]: any }
125 const { fields, attaches } = options
127 return makeUploadRequest({
128 ...this.buildCommonRequestOptions(options),
136 protected putUploadRequest (options: InternalCommonCommandOptions & {
137 fields?: { [ fieldName: string ]: any }
140 const { fields, attaches } = options
142 return makeUploadRequest({
143 ...this.buildCommonRequestOptions(options),
151 protected updateImageRequest (options: InternalCommonCommandOptions & {
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
162 return this.postUploadRequest({
166 attaches: { [options.fieldname]: filePath }
170 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
171 const { path } = options
174 url: this.server.url,
177 token: this.buildCommonRequestToken(options),
178 statusCodeExpected: this.buildStatusCodeExpected(options)
182 private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
183 const { token } = options
185 const fallbackToken = options.implicitToken
186 ? this.server.accessToken
189 return token !== undefined ? token : fallbackToken
192 private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
193 const { expectedStatus, defaultExpectedStatus } = options
195 return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus