| 177 | } |
| 178 | |
| 179 | const v3uncached = async ( |
| 180 | query: string, |
| 181 | options: GhRestApiOptions = v3defaults, |
| 182 | ): Promise<RestResponse> => { |
| 183 | const {ignoreHttpStatus, method, body, headers, responseFormat} = {...v3defaults, ...options}; |
| 184 | // Block write operations (POST, PUT, PATCH, DELETE) when token user doesn't match |
| 185 | if (method !== 'GET') { |
| 186 | await assertCurrentUser(); |
| 187 | } |
| 188 | |
| 189 | const personalToken = await getToken(); |
| 190 | |
| 191 | if (!query.startsWith('https')) { |
| 192 | query = query.startsWith('/') ? query.slice(1) : ['repos', getRepo()!.nameWithOwner, query].filter(Boolean).join('/'); |
| 193 | } |
| 194 | |
| 195 | const url = new URL(query, api3); |
| 196 | log.http(url.href); |
| 197 | const response = await fetch(url.href, { |
| 198 | method, |
| 199 | body: body && JSON.stringify(body), |
| 200 | headers: { |
| 201 | 'user-agent': 'Refined GitHub', |
| 202 | accept: 'application/vnd.github.v3+json', |
| 203 | ...headers, |
| 204 | ...personalToken && {Authorization: `token ${personalToken}`}, |
| 205 | }, |
| 206 | }); |
| 207 | let apiResponse: AnyObject; |
| 208 | if (responseFormat === 'base64') { |
| 209 | const arrayBuffer = await response.arrayBuffer(); |
| 210 | const content = uint8ArrayToBase64(new Uint8Array(arrayBuffer)); |
| 211 | apiResponse = {content}; |
| 212 | } else { |
| 213 | const content = await response.text(); |
| 214 | apiResponse = responseFormat === 'json' ? JSON.parse(content) : {content}; |
| 215 | } |
| 216 | |
| 217 | if ( |
| 218 | response.ok |
| 219 | || ignoreHttpStatus === true |
| 220 | || ignoreHttpStatus === response.status |
| 221 | ) { |
| 222 | return Object.assign(apiResponse, { |
| 223 | httpStatus: response.status, |
| 224 | headers: response.headers, |
| 225 | ok: response.ok, |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | throw await getError(apiResponse); |
| 230 | }; |
| 231 | |
| 232 | const v3 = mem(v3uncached, { |
| 233 | cacheKey: JSON.stringify, |