(config: OpenAPIConfig, options: ApiRequestOptions)
| 137 | }; |
| 138 | |
| 139 | export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => { |
| 140 | const [token, username, password, additionalHeaders] = await Promise.all([ |
| 141 | resolve(options, config.TOKEN), |
| 142 | resolve(options, config.USERNAME), |
| 143 | resolve(options, config.PASSWORD), |
| 144 | resolve(options, config.HEADERS), |
| 145 | ]); |
| 146 | |
| 147 | const headers = Object.entries({ |
| 148 | Accept: 'application/json', |
| 149 | ...additionalHeaders, |
| 150 | ...options.headers, |
| 151 | }) |
| 152 | .filter(([_, value]) => isDefined(value)) |
| 153 | .reduce((headers, [key, value]) => ({ |
| 154 | ...headers, |
| 155 | [key]: String(value), |
| 156 | }), {} as Record<string, string>); |
| 157 | |
| 158 | if (isStringWithValue(token)) { |
| 159 | headers['Authorization'] = `Bearer ${token}`; |
| 160 | } |
| 161 | |
| 162 | if (isStringWithValue(username) && isStringWithValue(password)) { |
| 163 | const credentials = base64(`${username}:${password}`); |
| 164 | headers['Authorization'] = `Basic ${credentials}`; |
| 165 | } |
| 166 | |
| 167 | if (options.body !== undefined) { |
| 168 | if (options.mediaType) { |
| 169 | headers['Content-Type'] = options.mediaType; |
| 170 | } else if (isBlob(options.body)) { |
| 171 | headers['Content-Type'] = options.body.type || 'application/octet-stream'; |
| 172 | } else if (isString(options.body)) { |
| 173 | headers['Content-Type'] = 'text/plain'; |
| 174 | } else if (!isFormData(options.body)) { |
| 175 | headers['Content-Type'] = 'application/json'; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return new Headers(headers); |
| 180 | }; |
| 181 | |
| 182 | export const getRequestBody = (options: ApiRequestOptions): any => { |
| 183 | if (options.body !== undefined) { |
no test coverage detected