]>
Commit | Line | Data |
---|---|---|
a6a79eae | 1 | import { HttpStatusCode } from '@shared/core-utils' |
a1637fa1 C |
2 | import { |
3 | makeDeleteRequest, | |
4 | makeGetRequest, | |
5 | makePostBodyRequest, | |
6 | makePutBodyRequest, | |
7 | makeUploadRequest, | |
8 | unwrapBody, | |
9 | unwrapText | |
10 | } from '../requests/requests' | |
a6a79eae C |
11 | import { ServerInfo } from '../server/servers' |
12 | ||
e8bd7ce7 | 13 | export interface OverrideCommandOptions { |
a6a79eae C |
14 | token?: string |
15 | expectedStatus?: number | |
16 | } | |
17 | ||
a1637fa1 | 18 | interface InternalCommonCommandOptions extends OverrideCommandOptions { |
57f879a5 C |
19 | // Default to server.url |
20 | url?: string | |
21 | ||
e8bd7ce7 | 22 | path: string |
a1637fa1 C |
23 | // If we automatically send the server token if the token is not provided |
24 | implicitToken: boolean | |
e8bd7ce7 C |
25 | defaultExpectedStatus: number |
26 | } | |
27 | ||
a1637fa1 | 28 | interface InternalGetCommandOptions extends InternalCommonCommandOptions { |
a92ddacb | 29 | query?: { [ id: string ]: any } |
c1bc8ee4 C |
30 | contentType?: string |
31 | accept?: string | |
ae2abfd3 | 32 | redirects?: number |
57f879a5 | 33 | range?: string |
c1bc8ee4 C |
34 | } |
35 | ||
a6a79eae C |
36 | abstract class AbstractCommand { |
37 | ||
e8bd7ce7 | 38 | private expectedStatus: HttpStatusCode |
a6a79eae C |
39 | |
40 | constructor ( | |
41 | protected server: ServerInfo | |
42 | ) { | |
43 | ||
44 | } | |
45 | ||
46 | setServer (server: ServerInfo) { | |
47 | this.server = server | |
48 | } | |
49 | ||
50 | setExpectedStatus (status: HttpStatusCode) { | |
51 | this.expectedStatus = status | |
52 | } | |
53 | ||
a1637fa1 | 54 | protected getRequestBody <T> (options: InternalGetCommandOptions) { |
c1bc8ee4 C |
55 | return unwrapBody<T>(this.getRequest(options)) |
56 | } | |
57 | ||
a1637fa1 | 58 | protected getRequestText (options: InternalGetCommandOptions) { |
c1bc8ee4 | 59 | return unwrapText(this.getRequest(options)) |
e8bd7ce7 C |
60 | } |
61 | ||
57f879a5 C |
62 | protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) { |
63 | const { url, range } = options | |
64 | const { host, protocol, pathname } = new URL(url) | |
65 | ||
66 | return this.getRequest({ | |
67 | ...options, | |
68 | ||
69 | token: this.buildCommonRequestToken(options), | |
70 | defaultExpectedStatus: this.buildStatusCodeExpected(options), | |
71 | ||
72 | url: `${protocol}//${host}`, | |
73 | path: pathname, | |
74 | range | |
75 | }) | |
76 | } | |
77 | ||
a1637fa1 | 78 | protected getRequest (options: InternalGetCommandOptions) { |
ae2abfd3 C |
79 | const { redirects, query, contentType, accept } = options |
80 | ||
81 | return makeGetRequest({ | |
82 | ...this.buildCommonRequestOptions(options), | |
83 | ||
84 | redirects, | |
85 | query, | |
86 | contentType, | |
87 | accept | |
88 | }) | |
89 | } | |
90 | ||
a1637fa1 | 91 | protected deleteRequest (options: InternalCommonCommandOptions) { |
0c1a77e9 C |
92 | return makeDeleteRequest(this.buildCommonRequestOptions(options)) |
93 | } | |
94 | ||
a1637fa1 | 95 | protected putBodyRequest (options: InternalCommonCommandOptions & { |
e8bd7ce7 C |
96 | fields?: { [ fieldName: string ]: any } |
97 | }) { | |
98 | const { fields } = options | |
99 | ||
100 | return makePutBodyRequest({ | |
101 | ...this.buildCommonRequestOptions(options), | |
102 | ||
103 | fields | |
104 | }) | |
105 | } | |
106 | ||
a1637fa1 | 107 | protected postBodyRequest (options: InternalCommonCommandOptions & { |
a6a79eae C |
108 | fields?: { [ fieldName: string ]: any } |
109 | }) { | |
e8bd7ce7 | 110 | const { fields } = options |
a6a79eae C |
111 | |
112 | return makePostBodyRequest({ | |
e8bd7ce7 C |
113 | ...this.buildCommonRequestOptions(options), |
114 | ||
115 | fields | |
116 | }) | |
117 | } | |
118 | ||
a1637fa1 | 119 | protected postUploadRequest (options: InternalCommonCommandOptions & { |
4f219914 C |
120 | fields?: { [ fieldName: string ]: any } |
121 | attaches?: any | |
122 | }) { | |
123 | const { fields, attaches } = options | |
124 | ||
125 | return makeUploadRequest({ | |
126 | ...this.buildCommonRequestOptions(options), | |
127 | ||
128 | method: 'POST', | |
129 | fields, | |
130 | attaches | |
131 | }) | |
132 | } | |
133 | ||
a1637fa1 | 134 | protected putUploadRequest (options: InternalCommonCommandOptions & { |
4f219914 C |
135 | fields?: { [ fieldName: string ]: any } |
136 | attaches?: any | |
137 | }) { | |
138 | const { fields, attaches } = options | |
139 | ||
140 | return makeUploadRequest({ | |
141 | ...this.buildCommonRequestOptions(options), | |
142 | ||
143 | method: 'PUT', | |
144 | fields, | |
145 | attaches | |
146 | }) | |
147 | } | |
148 | ||
a1637fa1 | 149 | private buildCommonRequestOptions (options: InternalCommonCommandOptions) { |
57f879a5 C |
150 | const { path } = options |
151 | ||
152 | return { | |
153 | url: this.server.url, | |
154 | path, | |
155 | ||
156 | token: this.buildCommonRequestToken(options), | |
157 | statusCodeExpected: this.buildStatusCodeExpected(options) | |
158 | } | |
159 | } | |
160 | ||
161 | private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) { | |
162 | const { token } = options | |
e8bd7ce7 | 163 | |
a1637fa1 C |
164 | const fallbackToken = options.implicitToken |
165 | ? this.server.accessToken | |
166 | : undefined | |
167 | ||
57f879a5 C |
168 | return token !== undefined ? token : fallbackToken |
169 | } | |
23a3a882 | 170 | |
57f879a5 C |
171 | private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) { |
172 | const { expectedStatus, defaultExpectedStatus } = options | |
23a3a882 | 173 | |
57f879a5 | 174 | return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
a6a79eae C |
175 | } |
176 | } | |
177 | ||
178 | export { | |
179 | AbstractCommand | |
180 | } |