]>
Commit | Line | Data |
---|---|---|
a6a79eae | 1 | import { HttpStatusCode } from '@shared/core-utils' |
0c1a77e9 | 2 | import { makeDeleteRequest, 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 | 15 | interface GetCommandOptions extends CommonCommandOptions { |
a92ddacb | 16 | query?: { [ id: string ]: any } |
c1bc8ee4 C |
17 | contentType?: string |
18 | accept?: string | |
ae2abfd3 | 19 | redirects?: number |
c1bc8ee4 C |
20 | } |
21 | ||
a6a79eae C |
22 | abstract class AbstractCommand { |
23 | ||
e8bd7ce7 | 24 | private expectedStatus: HttpStatusCode |
a6a79eae C |
25 | |
26 | constructor ( | |
27 | protected server: ServerInfo | |
28 | ) { | |
29 | ||
30 | } | |
31 | ||
32 | setServer (server: ServerInfo) { | |
33 | this.server = server | |
34 | } | |
35 | ||
36 | setExpectedStatus (status: HttpStatusCode) { | |
37 | this.expectedStatus = status | |
38 | } | |
39 | ||
c1bc8ee4 C |
40 | protected getRequestBody <T> (options: GetCommandOptions) { |
41 | return unwrapBody<T>(this.getRequest(options)) | |
42 | } | |
43 | ||
44 | protected getRequestText (options: GetCommandOptions) { | |
45 | return unwrapText(this.getRequest(options)) | |
e8bd7ce7 C |
46 | } |
47 | ||
ae2abfd3 C |
48 | protected getRequest (options: GetCommandOptions) { |
49 | const { redirects, query, contentType, accept } = options | |
50 | ||
51 | return makeGetRequest({ | |
52 | ...this.buildCommonRequestOptions(options), | |
53 | ||
54 | redirects, | |
55 | query, | |
56 | contentType, | |
57 | accept | |
58 | }) | |
59 | } | |
60 | ||
0c1a77e9 C |
61 | protected deleteRequest (options: CommonCommandOptions) { |
62 | return makeDeleteRequest(this.buildCommonRequestOptions(options)) | |
63 | } | |
64 | ||
e8bd7ce7 C |
65 | protected putBodyRequest (options: CommonCommandOptions & { |
66 | fields?: { [ fieldName: string ]: any } | |
67 | }) { | |
68 | const { fields } = options | |
69 | ||
70 | return makePutBodyRequest({ | |
71 | ...this.buildCommonRequestOptions(options), | |
72 | ||
73 | fields | |
74 | }) | |
75 | } | |
76 | ||
a6a79eae | 77 | protected postBodyRequest (options: CommonCommandOptions & { |
a6a79eae C |
78 | fields?: { [ fieldName: string ]: any } |
79 | }) { | |
e8bd7ce7 | 80 | const { fields } = options |
a6a79eae C |
81 | |
82 | return makePostBodyRequest({ | |
e8bd7ce7 C |
83 | ...this.buildCommonRequestOptions(options), |
84 | ||
85 | fields | |
86 | }) | |
87 | } | |
88 | ||
89 | private buildCommonRequestOptions (options: CommonCommandOptions) { | |
90 | const { token, expectedStatus, defaultExpectedStatus, path } = options | |
91 | ||
92 | return { | |
a6a79eae C |
93 | url: this.server.url, |
94 | path, | |
23a3a882 C |
95 | |
96 | // Token can be null if we don't want to add it | |
97 | token: token !== undefined ? token : this.server.accessToken, | |
98 | ||
a6a79eae | 99 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
e8bd7ce7 | 100 | } |
a6a79eae C |
101 | } |
102 | } | |
103 | ||
104 | export { | |
105 | AbstractCommand | |
106 | } |