Using Typescript and Axios implement a function getGithubIssue(owner:string,…
- User
Using Typescript and Axios implement a function
getGithubIssue(owner:string, repo:string, number:number):GithubIssue | null.It only needs to return the fields number, title, body.
The values passed in may be unsanitized
We should return
nullon a 404. A valid value on 200 throw on anything else Assistant
Here's an implementation of the
getGithubIssuefunction using TypeScript, Axios, and Promises:import axios from 'axios'; interface GithubIssue { number: number; title: string; body: string; } async function getGithubIssue(owner: string, repo: string, number: number): Promise<GithubIssue | null> { try { const response = await axios.get(`https://api.github.com/repos/${owner}/${repo}/issues/${number}`); if (response.status === 200) { const { number, title, body } = response.data; return { number, title, body }; } else { return null; } } catch (error) { if (error.response) { if (error.response.status === 404) { return null; } else { throw error; } } else { throw error; } } }Here's a brief explanation of the code:
- We define an interface
GithubIssuefor the issue data we expect to receive from the GitHub API. - We define the
getGithubIssuefunction, which takes in three parameters:owner,repo, andnumber. - We use the
axios.getmethod to make a GET request to the GitHub API and retrieve the issue data. - We use the
awaitkeyword to wait for the promise to resolve before continuing with the function. - We check the
statusproperty of the response object to determine if the request was successful (200) or if the issue was not found (404). - If the request was successful, we extract the
number,title, andbodyproperties from the response data and return them as aGithubIssueobject. - If the request was not successful (i.e., not a 200 or 404), we throw an error.
- We use a
try...catchblock to handle any errors that may occur during the request. If an error occurs and the response object is available, we check thestatusproperty of the response object to determine if it was a 404 error. If it was, we returnnull. Otherwise, we throw the error.
Note that this implementation assumes that the
axiospackage has been installed and imported correctly.- We define an interface
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.