From a6a79eae0d8564099b6957e76d7a18528d9ef124 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 5 Jul 2021 14:57:03 +0200 Subject: Introduce bulk command --- shared/extra-utils/bulk/bulk.ts | 35 ++++++++++---------- shared/extra-utils/shared/abstract-command.ts | 47 +++++++++++++++++++++++++++ shared/extra-utils/shared/index.ts | 1 + 3 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 shared/extra-utils/shared/abstract-command.ts create mode 100644 shared/extra-utils/shared/index.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.ts index b6f437b8b..c102383e3 100644 --- a/shared/extra-utils/bulk/bulk.ts +++ b/shared/extra-utils/bulk/bulk.ts @@ -1,25 +1,24 @@ -import { BulkRemoveCommentsOfBody } from "@shared/models/bulk/bulk-remove-comments-of-body.model" -import { makePostBodyRequest } from "../requests/requests" + +import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { AbstractCommand, CommonCommandOptions } from '../shared' + +class BulkCommand extends AbstractCommand { -function bulkRemoveCommentsOf (options: { - url: string - token: string - attributes: BulkRemoveCommentsOfBody - expectedStatus?: number -}) { - const { url, token, attributes, expectedStatus } = options - const path = '/api/v1/bulk/remove-comments-of' + removeCommentsOf (options: CommonCommandOptions & { + attributes: BulkRemoveCommentsOfBody + }) { + const { attributes } = options - return makePostBodyRequest({ - url, - path, - token, - fields: attributes, - statusCodeExpected: expectedStatus || HttpStatusCode.NO_CONTENT_204 - }) + return this.postBodyRequest({ + ...options, + path: '/api/v1/bulk/remove-comments-of', + fields: attributes, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } } export { - bulkRemoveCommentsOf + BulkCommand } diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts new file mode 100644 index 000000000..bb06b6bdb --- /dev/null +++ b/shared/extra-utils/shared/abstract-command.ts @@ -0,0 +1,47 @@ +import { HttpStatusCode } from '@shared/core-utils' +import { makePostBodyRequest } from '../requests/requests' +import { ServerInfo } from '../server/servers' + +export interface CommonCommandOptions { + token?: string + expectedStatus?: number +} + +abstract class AbstractCommand { + + private expectedStatus = HttpStatusCode.OK_200 + + constructor ( + protected server: ServerInfo + ) { + + } + + setServer (server: ServerInfo) { + this.server = server + } + + setExpectedStatus (status: HttpStatusCode) { + this.expectedStatus = status + } + + protected postBodyRequest (options: CommonCommandOptions & { + path: string + defaultExpectedStatus: number + fields?: { [ fieldName: string ]: any } + }) { + const { token, fields, expectedStatus, defaultExpectedStatus, path } = options + + return makePostBodyRequest({ + url: this.server.url, + path, + token: token ?? this.server.accessToken, + fields, + statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus + }) + } +} + +export { + AbstractCommand +} diff --git a/shared/extra-utils/shared/index.ts b/shared/extra-utils/shared/index.ts new file mode 100644 index 000000000..e807ab4f7 --- /dev/null +++ b/shared/extra-utils/shared/index.ts @@ -0,0 +1 @@ +export * from './abstract-command' -- cgit v1.2.3