1 import { isAbsolute, join } from 'path'
2 import { HttpStatusCode } from '@shared/core-utils'
3 import { root } from '../miscs/tests'
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 }
39 abstract class AbstractCommand {
41 private expectedStatus: HttpStatusCode
44 protected server: ServerInfo
49 setServer (server: ServerInfo) {
53 setExpectedStatus (status: HttpStatusCode) {
54 this.expectedStatus = status
57 protected getRequestBody <T> (options: InternalGetCommandOptions) {
58 return unwrapBody<T>(this.getRequest(options))
61 protected getRequestText (options: InternalGetCommandOptions) {
62 return unwrapText(this.getRequest(options))
65 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
66 const { url, range } = options
67 const { host, protocol, pathname } = new URL(url)
69 return this.getRequest({
72 token: this.buildCommonRequestToken(options),
73 defaultExpectedStatus: this.buildStatusCodeExpected(options),
75 url: `${protocol}//${host}`,
81 protected getRequest (options: InternalGetCommandOptions) {
82 const { redirects, query, contentType, accept, range, host } = options
84 return makeGetRequest({
85 ...this.buildCommonRequestOptions(options),
96 protected deleteRequest (options: InternalCommonCommandOptions) {
97 return makeDeleteRequest(this.buildCommonRequestOptions(options))
100 protected putBodyRequest (options: InternalCommonCommandOptions & {
101 fields?: { [ fieldName: string ]: any }
103 const { fields } = options
105 return makePutBodyRequest({
106 ...this.buildCommonRequestOptions(options),
112 protected postBodyRequest (options: InternalCommonCommandOptions & {
113 fields?: { [ fieldName: string ]: any }
116 const { type, fields } = options
118 return makePostBodyRequest({
119 ...this.buildCommonRequestOptions(options),
126 protected postUploadRequest (options: InternalCommonCommandOptions & {
127 fields?: { [ fieldName: string ]: any }
130 const { fields, attaches } = options
132 return makeUploadRequest({
133 ...this.buildCommonRequestOptions(options),
141 protected putUploadRequest (options: InternalCommonCommandOptions & {
142 fields?: { [ fieldName: string ]: any }
145 const { fields, attaches } = options
147 return makeUploadRequest({
148 ...this.buildCommonRequestOptions(options),
156 protected updateImageRequest (options: InternalCommonCommandOptions & {
161 if (isAbsolute(options.fixture)) {
162 filePath = options.fixture
164 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
167 return this.postUploadRequest({
171 attaches: { [options.fieldname]: filePath }
175 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
176 const { url, path } = options
179 url: url ?? this.server.url,
182 token: this.buildCommonRequestToken(options),
183 statusCodeExpected: this.buildStatusCodeExpected(options)
187 private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
188 const { token } = options
190 const fallbackToken = options.implicitToken
191 ? this.server.accessToken
194 return token !== undefined ? token : fallbackToken
197 private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
198 const { expectedStatus, defaultExpectedStatus } = options
200 return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus