]>
Commit | Line | Data |
---|---|---|
1 | import { HttpStatusCode } from '@shared/core-utils' | |
2 | import { | |
3 | makeDeleteRequest, | |
4 | makeGetRequest, | |
5 | makePostBodyRequest, | |
6 | makePutBodyRequest, | |
7 | makeUploadRequest, | |
8 | unwrapBody, | |
9 | unwrapText | |
10 | } from '../requests/requests' | |
11 | import { ServerInfo } from '../server/servers' | |
12 | ||
13 | export interface OverrideCommandOptions { | |
14 | token?: string | |
15 | expectedStatus?: number | |
16 | } | |
17 | ||
18 | interface InternalCommonCommandOptions extends OverrideCommandOptions { | |
19 | // Default to server.url | |
20 | url?: string | |
21 | ||
22 | path: string | |
23 | // If we automatically send the server token if the token is not provided | |
24 | implicitToken: boolean | |
25 | defaultExpectedStatus: number | |
26 | } | |
27 | ||
28 | interface InternalGetCommandOptions extends InternalCommonCommandOptions { | |
29 | query?: { [ id: string ]: any } | |
30 | contentType?: string | |
31 | accept?: string | |
32 | redirects?: number | |
33 | range?: string | |
34 | } | |
35 | ||
36 | abstract class AbstractCommand { | |
37 | ||
38 | private expectedStatus: HttpStatusCode | |
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 | ||
54 | protected getRequestBody <T> (options: InternalGetCommandOptions) { | |
55 | return unwrapBody<T>(this.getRequest(options)) | |
56 | } | |
57 | ||
58 | protected getRequestText (options: InternalGetCommandOptions) { | |
59 | return unwrapText(this.getRequest(options)) | |
60 | } | |
61 | ||
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 | ||
78 | protected getRequest (options: InternalGetCommandOptions) { | |
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 | ||
91 | protected deleteRequest (options: InternalCommonCommandOptions) { | |
92 | return makeDeleteRequest(this.buildCommonRequestOptions(options)) | |
93 | } | |
94 | ||
95 | protected putBodyRequest (options: InternalCommonCommandOptions & { | |
96 | fields?: { [ fieldName: string ]: any } | |
97 | }) { | |
98 | const { fields } = options | |
99 | ||
100 | return makePutBodyRequest({ | |
101 | ...this.buildCommonRequestOptions(options), | |
102 | ||
103 | fields | |
104 | }) | |
105 | } | |
106 | ||
107 | protected postBodyRequest (options: InternalCommonCommandOptions & { | |
108 | fields?: { [ fieldName: string ]: any } | |
109 | }) { | |
110 | const { fields } = options | |
111 | ||
112 | return makePostBodyRequest({ | |
113 | ...this.buildCommonRequestOptions(options), | |
114 | ||
115 | fields | |
116 | }) | |
117 | } | |
118 | ||
119 | protected postUploadRequest (options: InternalCommonCommandOptions & { | |
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 | ||
134 | protected putUploadRequest (options: InternalCommonCommandOptions & { | |
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 | ||
149 | private buildCommonRequestOptions (options: InternalCommonCommandOptions) { | |
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 | |
163 | ||
164 | const fallbackToken = options.implicitToken | |
165 | ? this.server.accessToken | |
166 | : undefined | |
167 | ||
168 | return token !== undefined ? token : fallbackToken | |
169 | } | |
170 | ||
171 | private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) { | |
172 | const { expectedStatus, defaultExpectedStatus } = options | |
173 | ||
174 | return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus | |
175 | } | |
176 | } | |
177 | ||
178 | export { | |
179 | AbstractCommand | |
180 | } |