PeerTube/shared/server-commands/shared/abstract-command.ts

212 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-07-09 11:21:30 +02:00
import { isAbsolute, join } from 'path'
import { root } from '@shared/core-utils'
import {
makeDeleteRequest,
makeGetRequest,
makePostBodyRequest,
makePutBodyRequest,
makeUploadRequest,
unwrapBody,
unwrapText
} from '../requests/requests'
2021-07-16 09:47:51 +02:00
import { PeerTubeServer } from '../server/server'
2021-07-05 14:57:03 +02:00
2021-07-06 09:55:05 +02:00
export interface OverrideCommandOptions {
2021-07-05 14:57:03 +02:00
token?: string
expectedStatus?: number
}
interface InternalCommonCommandOptions extends OverrideCommandOptions {
2021-07-09 10:21:10 +02:00
// Default to server.url
url?: string
2021-07-06 09:55:05 +02:00
path: string
// If we automatically send the server token if the token is not provided
implicitToken: boolean
2021-07-06 09:55:05 +02:00
defaultExpectedStatus: number
2021-07-16 10:42:24 +02:00
// Common optional request parameters
2021-07-06 10:21:35 +02:00
contentType?: string
accept?: string
2021-07-07 10:33:49 +02:00
redirects?: number
2021-07-09 10:21:10 +02:00
range?: string
2021-07-13 11:05:15 +02:00
host?: string
2021-07-16 10:42:24 +02:00
headers?: { [ name: string ]: string }
requestType?: string
xForwardedFor?: string
}
interface InternalGetCommandOptions extends InternalCommonCommandOptions {
query?: { [ id: string ]: any }
2021-07-06 10:21:35 +02:00
}
2021-09-09 09:31:50 +02:00
interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
query?: { [ id: string ]: any }
rawQuery?: string
}
2021-07-05 14:57:03 +02:00
abstract class AbstractCommand {
constructor (
2021-07-16 09:47:51 +02:00
protected server: PeerTubeServer
2021-07-05 14:57:03 +02:00
) {
}
protected getRequestBody <T> (options: InternalGetCommandOptions) {
2021-07-06 10:21:35 +02:00
return unwrapBody<T>(this.getRequest(options))
}
protected getRequestText (options: InternalGetCommandOptions) {
2021-07-06 10:21:35 +02:00
return unwrapText(this.getRequest(options))
2021-07-06 09:55:05 +02:00
}
2021-07-09 10:21:10 +02:00
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),
2021-07-16 10:42:24 +02:00
defaultExpectedStatus: this.buildExpectedStatus(options),
2021-07-09 10:21:10 +02:00
url: `${protocol}//${host}`,
path: pathname,
range
})
}
protected getRequest (options: InternalGetCommandOptions) {
2021-07-16 10:42:24 +02:00
const { query } = options
2021-07-07 10:33:49 +02:00
return makeGetRequest({
...this.buildCommonRequestOptions(options),
2021-07-16 10:42:24 +02:00
query
2021-07-07 10:33:49 +02:00
})
}
2021-09-09 09:31:50 +02:00
protected deleteRequest (options: InternalDeleteCommandOptions) {
const { query, rawQuery } = options
return makeDeleteRequest({
...this.buildCommonRequestOptions(options),
query,
rawQuery
})
2021-07-06 12:01:59 +02:00
}
protected putBodyRequest (options: InternalCommonCommandOptions & {
2021-07-06 09:55:05 +02:00
fields?: { [ fieldName: string ]: any }
}) {
const { fields } = options
return makePutBodyRequest({
...this.buildCommonRequestOptions(options),
fields
})
}
protected postBodyRequest (options: InternalCommonCommandOptions & {
2021-07-05 14:57:03 +02:00
fields?: { [ fieldName: string ]: any }
}) {
2021-07-16 10:42:24 +02:00
const { fields } = options
2021-07-05 14:57:03 +02:00
return makePostBodyRequest({
2021-07-06 09:55:05 +02:00
...this.buildCommonRequestOptions(options),
2021-07-16 10:42:24 +02:00
fields
2021-07-06 09:55:05 +02:00
})
}
protected postUploadRequest (options: InternalCommonCommandOptions & {
2021-07-08 10:18:40 +02:00
fields?: { [ fieldName: string ]: any }
2021-07-15 10:02:54 +02:00
attaches?: { [ fieldName: string ]: any }
2021-07-08 10:18:40 +02:00
}) {
2021-07-16 10:42:24 +02:00
const { fields, attaches } = options
2021-07-08 10:18:40 +02:00
return makeUploadRequest({
...this.buildCommonRequestOptions(options),
method: 'POST',
fields,
2021-07-16 10:42:24 +02:00
attaches
2021-07-08 10:18:40 +02:00
})
}
protected putUploadRequest (options: InternalCommonCommandOptions & {
2021-07-08 10:18:40 +02:00
fields?: { [ fieldName: string ]: any }
2021-07-15 10:02:54 +02:00
attaches?: { [ fieldName: string ]: any }
2021-07-08 10:18:40 +02:00
}) {
2021-07-16 10:42:24 +02:00
const { fields, attaches } = options
2021-07-08 10:18:40 +02:00
return makeUploadRequest({
...this.buildCommonRequestOptions(options),
method: 'PUT',
fields,
attaches
})
}
2021-07-09 11:21:30 +02:00
protected updateImageRequest (options: InternalCommonCommandOptions & {
fixture: string
fieldname: string
}) {
2021-07-16 10:42:24 +02:00
const filePath = isAbsolute(options.fixture)
? options.fixture
: join(root(), 'server', 'tests', 'fixtures', options.fixture)
2021-07-09 11:21:30 +02:00
return this.postUploadRequest({
...options,
fields: {},
attaches: { [options.fieldname]: filePath }
})
}
2021-07-15 10:02:54 +02:00
protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
2021-07-16 10:42:24 +02:00
const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
2021-07-09 10:21:10 +02:00
return {
2021-07-09 16:23:01 +02:00
url: url ?? this.server.url,
2021-07-09 10:21:10 +02:00
path,
token: this.buildCommonRequestToken(options),
2021-07-16 10:42:24 +02:00
expectedStatus: this.buildExpectedStatus(options),
redirects,
contentType,
range,
host,
accept,
headers,
type: requestType,
xForwardedFor
2021-07-09 10:21:10 +02:00
}
}
2021-07-15 10:02:54 +02:00
protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
2021-07-09 10:21:10 +02:00
const { token } = options
2021-07-06 09:55:05 +02:00
const fallbackToken = options.implicitToken
? this.server.accessToken
: undefined
2021-07-09 10:21:10 +02:00
return token !== undefined ? token : fallbackToken
}
2021-07-06 14:30:20 +02:00
2021-07-16 10:42:24 +02:00
protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
2021-07-09 10:21:10 +02:00
const { expectedStatus, defaultExpectedStatus } = options
2021-07-06 14:30:20 +02:00
2021-07-15 10:02:54 +02:00
return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
2021-07-05 14:57:03 +02:00
}
}
export {
AbstractCommand
}