]>
Commit | Line | Data |
---|---|---|
a6a79eae | 1 | import { HttpStatusCode } from '@shared/core-utils' |
f59545d9 | 2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests' |
a6a79eae C |
3 | import { ServerInfo } from '../server/servers' |
4 | ||
e8bd7ce7 | 5 | export interface OverrideCommandOptions { |
a6a79eae C |
6 | token?: string |
7 | expectedStatus?: number | |
8 | } | |
9 | ||
e8bd7ce7 C |
10 | interface CommonCommandOptions extends OverrideCommandOptions { |
11 | path: string | |
12 | defaultExpectedStatus: number | |
13 | } | |
14 | ||
c1bc8ee4 C |
15 | interface GetCommandOptions extends CommonCommandOptions { |
16 | query?: { [ id: string ]: string } | |
17 | contentType?: string | |
18 | accept?: string | |
19 | } | |
20 | ||
a6a79eae C |
21 | abstract class AbstractCommand { |
22 | ||
e8bd7ce7 | 23 | private expectedStatus: HttpStatusCode |
a6a79eae C |
24 | |
25 | constructor ( | |
26 | protected server: ServerInfo | |
27 | ) { | |
28 | ||
29 | } | |
30 | ||
31 | setServer (server: ServerInfo) { | |
32 | this.server = server | |
33 | } | |
34 | ||
35 | setExpectedStatus (status: HttpStatusCode) { | |
36 | this.expectedStatus = status | |
37 | } | |
38 | ||
c1bc8ee4 C |
39 | protected getRequestBody <T> (options: GetCommandOptions) { |
40 | return unwrapBody<T>(this.getRequest(options)) | |
41 | } | |
42 | ||
43 | protected getRequestText (options: GetCommandOptions) { | |
44 | return unwrapText(this.getRequest(options)) | |
e8bd7ce7 C |
45 | } |
46 | ||
47 | protected putBodyRequest (options: CommonCommandOptions & { | |
48 | fields?: { [ fieldName: string ]: any } | |
49 | }) { | |
50 | const { fields } = options | |
51 | ||
52 | return makePutBodyRequest({ | |
53 | ...this.buildCommonRequestOptions(options), | |
54 | ||
55 | fields | |
56 | }) | |
57 | } | |
58 | ||
a6a79eae | 59 | protected postBodyRequest (options: CommonCommandOptions & { |
a6a79eae C |
60 | fields?: { [ fieldName: string ]: any } |
61 | }) { | |
e8bd7ce7 | 62 | const { fields } = options |
a6a79eae C |
63 | |
64 | return makePostBodyRequest({ | |
e8bd7ce7 C |
65 | ...this.buildCommonRequestOptions(options), |
66 | ||
67 | fields | |
68 | }) | |
69 | } | |
70 | ||
71 | private buildCommonRequestOptions (options: CommonCommandOptions) { | |
72 | const { token, expectedStatus, defaultExpectedStatus, path } = options | |
73 | ||
74 | return { | |
a6a79eae C |
75 | url: this.server.url, |
76 | path, | |
77 | token: token ?? this.server.accessToken, | |
a6a79eae | 78 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
e8bd7ce7 | 79 | } |
a6a79eae | 80 | } |
c1bc8ee4 C |
81 | |
82 | private getRequest (options: GetCommandOptions) { | |
83 | const { query, contentType, accept } = options | |
84 | ||
85 | return makeGetRequest({ | |
86 | ...this.buildCommonRequestOptions(options), | |
87 | ||
88 | query, | |
89 | contentType, | |
90 | accept | |
91 | }) | |
92 | } | |
a6a79eae C |
93 | } |
94 | ||
95 | export { | |
96 | AbstractCommand | |
97 | } |