diff options
Diffstat (limited to 'shared/extra-utils')
-rw-r--r-- | shared/extra-utils/bulk/bulk.ts | 10 | ||||
-rw-r--r-- | shared/extra-utils/cli/cli.ts | 6 | ||||
-rw-r--r-- | shared/extra-utils/custom-pages/custom-pages.ts | 47 | ||||
-rw-r--r-- | shared/extra-utils/custom-pages/index.ts | 1 | ||||
-rw-r--r-- | shared/extra-utils/index.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/requests/requests.ts | 5 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/shared/abstract-command.ts | 44 |
8 files changed, 73 insertions, 45 deletions
diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.ts index c102383e3..a58fb3fbb 100644 --- a/shared/extra-utils/bulk/bulk.ts +++ b/shared/extra-utils/bulk/bulk.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | 1 | ||
2 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' | 2 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
4 | import { AbstractCommand, CommonCommandOptions } from '../shared' | 4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
5 | 5 | ||
6 | class BulkCommand extends AbstractCommand { | 6 | export class BulkCommand extends AbstractCommand { |
7 | 7 | ||
8 | removeCommentsOf (options: CommonCommandOptions & { | 8 | removeCommentsOf (options: OverrideCommandOptions & { |
9 | attributes: BulkRemoveCommentsOfBody | 9 | attributes: BulkRemoveCommentsOfBody |
10 | }) { | 10 | }) { |
11 | const { attributes } = options | 11 | const { attributes } = options |
@@ -18,7 +18,3 @@ class BulkCommand extends AbstractCommand { | |||
18 | }) | 18 | }) |
19 | } | 19 | } |
20 | } | 20 | } |
21 | |||
22 | export { | ||
23 | BulkCommand | ||
24 | } | ||
diff --git a/shared/extra-utils/cli/cli.ts b/shared/extra-utils/cli/cli.ts index 1bf100869..bc1dddc68 100644 --- a/shared/extra-utils/cli/cli.ts +++ b/shared/extra-utils/cli/cli.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { exec } from 'child_process' | 1 | import { exec } from 'child_process' |
2 | import { AbstractCommand } from '../shared' | 2 | import { AbstractCommand } from '../shared' |
3 | 3 | ||
4 | class CLICommand extends AbstractCommand { | 4 | export class CLICommand extends AbstractCommand { |
5 | 5 | ||
6 | static exec (command: string) { | 6 | static exec (command: string) { |
7 | return new Promise<string>((res, rej) => { | 7 | return new Promise<string>((res, rej) => { |
@@ -21,7 +21,3 @@ class CLICommand extends AbstractCommand { | |||
21 | return CLICommand.exec(`${this.getEnv()} ${command}`) | 21 | return CLICommand.exec(`${this.getEnv()} ${command}`) |
22 | } | 22 | } |
23 | } | 23 | } |
24 | |||
25 | export { | ||
26 | CLICommand | ||
27 | } | ||
diff --git a/shared/extra-utils/custom-pages/custom-pages.ts b/shared/extra-utils/custom-pages/custom-pages.ts index bf2d16c70..56dabdc0f 100644 --- a/shared/extra-utils/custom-pages/custom-pages.ts +++ b/shared/extra-utils/custom-pages/custom-pages.ts | |||
@@ -1,31 +1,30 @@ | |||
1 | import { CustomPage } from '@shared/models' | ||
1 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
2 | import { makeGetRequest, makePutBodyRequest } from '../requests/requests' | 3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
3 | 4 | ||
4 | function getInstanceHomepage (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | 5 | export class CustomPagesCommand extends AbstractCommand { |
5 | const path = '/api/v1/custom-pages/homepage/instance' | ||
6 | 6 | ||
7 | return makeGetRequest({ | 7 | getInstanceHomepage (options: OverrideCommandOptions = {}) { |
8 | url, | 8 | const path = '/api/v1/custom-pages/homepage/instance' |
9 | path, | ||
10 | statusCodeExpected | ||
11 | }) | ||
12 | } | ||
13 | |||
14 | function updateInstanceHomepage (url: string, token: string, content: string) { | ||
15 | const path = '/api/v1/custom-pages/homepage/instance' | ||
16 | 9 | ||
17 | return makePutBodyRequest({ | 10 | return this.getRequestBody<CustomPage>({ |
18 | url, | 11 | ...options, |
19 | path, | 12 | path, |
20 | token, | 13 | defaultExpectedStatus: HttpStatusCode.OK_200 |
21 | fields: { content }, | 14 | }) |
22 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | 15 | } |
23 | }) | ||
24 | } | ||
25 | 16 | ||
26 | // --------------------------------------------------------------------------- | 17 | updateInstanceHomepage (options: OverrideCommandOptions & { |
18 | content: string | ||
19 | }) { | ||
20 | const { content } = options | ||
21 | const path = '/api/v1/custom-pages/homepage/instance' | ||
27 | 22 | ||
28 | export { | 23 | return this.putBodyRequest({ |
29 | getInstanceHomepage, | 24 | ...options, |
30 | updateInstanceHomepage | 25 | path, |
26 | fields: { content }, | ||
27 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
28 | }) | ||
29 | } | ||
31 | } | 30 | } |
diff --git a/shared/extra-utils/custom-pages/index.ts b/shared/extra-utils/custom-pages/index.ts new file mode 100644 index 000000000..5e70778f8 --- /dev/null +++ b/shared/extra-utils/custom-pages/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './custom-pages' | |||
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 4bf057492..10781d049 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts | |||
@@ -2,7 +2,7 @@ export * from './bulk' | |||
2 | 2 | ||
3 | export * from './cli' | 3 | export * from './cli' |
4 | 4 | ||
5 | export * from './custom-pages/custom-pages' | 5 | export * from './custom-pages' |
6 | 6 | ||
7 | export * from './feeds/feeds' | 7 | export * from './feeds/feeds' |
8 | 8 | ||
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 38e24d897..eb59ca600 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts | |||
@@ -182,6 +182,10 @@ function decodeQueryString (path: string) { | |||
182 | return decode(path.split('?')[1]) | 182 | return decode(path.split('?')[1]) |
183 | } | 183 | } |
184 | 184 | ||
185 | function unwrap <T> (test: request.Test): Promise<T> { | ||
186 | return test.then(res => res.body) | ||
187 | } | ||
188 | |||
185 | // --------------------------------------------------------------------------- | 189 | // --------------------------------------------------------------------------- |
186 | 190 | ||
187 | export { | 191 | export { |
@@ -194,5 +198,6 @@ export { | |||
194 | makePutBodyRequest, | 198 | makePutBodyRequest, |
195 | makeDeleteRequest, | 199 | makeDeleteRequest, |
196 | makeRawRequest, | 200 | makeRawRequest, |
201 | unwrap, | ||
197 | updateImageRequest | 202 | updateImageRequest |
198 | } | 203 | } |
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 1b0775421..e07926065 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -8,6 +8,7 @@ import { randomInt } from '../../core-utils/miscs/miscs' | |||
8 | import { VideoChannel } from '../../models/videos' | 8 | import { VideoChannel } from '../../models/videos' |
9 | import { BulkCommand } from '../bulk' | 9 | import { BulkCommand } from '../bulk' |
10 | import { CLICommand } from '../cli' | 10 | import { CLICommand } from '../cli' |
11 | import { CustomPagesCommand } from '../custom-pages' | ||
11 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' | 12 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' |
12 | import { makeGetRequest } from '../requests/requests' | 13 | import { makeGetRequest } from '../requests/requests' |
13 | 14 | ||
@@ -65,6 +66,7 @@ interface ServerInfo { | |||
65 | 66 | ||
66 | bulkCommand?: BulkCommand | 67 | bulkCommand?: BulkCommand |
67 | cliCommand?: CLICommand | 68 | cliCommand?: CLICommand |
69 | customPageCommand?: CustomPagesCommand | ||
68 | } | 70 | } |
69 | 71 | ||
70 | function parallelTests () { | 72 | function parallelTests () { |
@@ -272,6 +274,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
272 | 274 | ||
273 | server.bulkCommand = new BulkCommand(server) | 275 | server.bulkCommand = new BulkCommand(server) |
274 | server.cliCommand = new CLICommand(server) | 276 | server.cliCommand = new CLICommand(server) |
277 | server.customPageCommand = new CustomPagesCommand(server) | ||
275 | 278 | ||
276 | res(server) | 279 | res(server) |
277 | }) | 280 | }) |
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index bb06b6bdb..d8eba373d 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts | |||
@@ -1,15 +1,20 @@ | |||
1 | import { HttpStatusCode } from '@shared/core-utils' | 1 | import { HttpStatusCode } from '@shared/core-utils' |
2 | import { makePostBodyRequest } from '../requests/requests' | 2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' |
3 | import { ServerInfo } from '../server/servers' | 3 | import { ServerInfo } from '../server/servers' |
4 | 4 | ||
5 | export interface CommonCommandOptions { | 5 | export interface OverrideCommandOptions { |
6 | token?: string | 6 | token?: string |
7 | expectedStatus?: number | 7 | expectedStatus?: number |
8 | } | 8 | } |
9 | 9 | ||
10 | interface CommonCommandOptions extends OverrideCommandOptions { | ||
11 | path: string | ||
12 | defaultExpectedStatus: number | ||
13 | } | ||
14 | |||
10 | abstract class AbstractCommand { | 15 | abstract class AbstractCommand { |
11 | 16 | ||
12 | private expectedStatus = HttpStatusCode.OK_200 | 17 | private expectedStatus: HttpStatusCode |
13 | 18 | ||
14 | constructor ( | 19 | constructor ( |
15 | protected server: ServerInfo | 20 | protected server: ServerInfo |
@@ -25,20 +30,43 @@ abstract class AbstractCommand { | |||
25 | this.expectedStatus = status | 30 | this.expectedStatus = status |
26 | } | 31 | } |
27 | 32 | ||
33 | protected getRequestBody <T> (options: CommonCommandOptions) { | ||
34 | return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options))) | ||
35 | } | ||
36 | |||
37 | protected putBodyRequest (options: CommonCommandOptions & { | ||
38 | fields?: { [ fieldName: string ]: any } | ||
39 | }) { | ||
40 | const { fields } = options | ||
41 | |||
42 | return makePutBodyRequest({ | ||
43 | ...this.buildCommonRequestOptions(options), | ||
44 | |||
45 | fields | ||
46 | }) | ||
47 | } | ||
48 | |||
28 | protected postBodyRequest (options: CommonCommandOptions & { | 49 | protected postBodyRequest (options: CommonCommandOptions & { |
29 | path: string | ||
30 | defaultExpectedStatus: number | ||
31 | fields?: { [ fieldName: string ]: any } | 50 | fields?: { [ fieldName: string ]: any } |
32 | }) { | 51 | }) { |
33 | const { token, fields, expectedStatus, defaultExpectedStatus, path } = options | 52 | const { fields } = options |
34 | 53 | ||
35 | return makePostBodyRequest({ | 54 | return makePostBodyRequest({ |
55 | ...this.buildCommonRequestOptions(options), | ||
56 | |||
57 | fields | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | private buildCommonRequestOptions (options: CommonCommandOptions) { | ||
62 | const { token, expectedStatus, defaultExpectedStatus, path } = options | ||
63 | |||
64 | return { | ||
36 | url: this.server.url, | 65 | url: this.server.url, |
37 | path, | 66 | path, |
38 | token: token ?? this.server.accessToken, | 67 | token: token ?? this.server.accessToken, |
39 | fields, | ||
40 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus | 68 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
41 | }) | 69 | } |
42 | } | 70 | } |
43 | } | 71 | } |
44 | 72 | ||