(url: string, content: ReactionID)
| 233 | } |
| 234 | |
| 235 | export async function toggleReaction(url: string, content: ReactionID) { |
| 236 | url = url.replace(GITHUB_API, ''); |
| 237 | // We don't know if the reaction exists or not. Attempt to create it. If the GitHub |
| 238 | // API responds that the reaction already exists, delete it. |
| 239 | const body = JSON.stringify({ content }); |
| 240 | const postRequest = githubRequest(url, { method: 'POST', body }); |
| 241 | postRequest.headers.set('Accept', GITHUB_ENCODING__REST_V3); |
| 242 | const response = await githubFetch(postRequest); |
| 243 | const reaction: Reaction = response.ok ? await response.json() : null; |
| 244 | if (response.status === 201) { // reaction created. |
| 245 | return { reaction, deleted: false }; |
| 246 | } |
| 247 | if (response.status !== 200) { |
| 248 | throw new Error('expected "201 reaction created" or "200 reaction already exists"'); |
| 249 | } |
| 250 | // reaction already exists... delete. |
| 251 | const deleteRequest = githubRequest(`${url}/${reaction.id}`, { method: 'DELETE' }); |
| 252 | deleteRequest.headers.set('Accept', GITHUB_ENCODING__REST_V3); |
| 253 | await githubFetch(deleteRequest); |
| 254 | return { reaction, deleted: true }; |
| 255 | } |
| 256 | |
| 257 | export function renderMarkdown(text: string) { |
| 258 | const body = JSON.stringify({ text, mode: 'gfm', context: `${owner}/${repo}` }); |
no test coverage detected