Using Typescript implement a function getGithubIssue(owner:string, repo:string,…
- User
Using Typescript 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
import { GithubIssue } from './types'; import axios from 'axios'; const API_URL = 'https://api.github.com'; // Axios request config with headers and base URL const requestConfig = { headers: { 'User-Agent': 'my-app', // Required by GitHub API }, baseURL: API_URL, }; // Define GithubIssue interface interface GithubIssue { number: number; title: string; body: string; } // Function to get a GitHub issue export const getGithubIssue = async ( owner: string, repo: string, number: number ): Promise<GithubIssue | null> => { try { const response = await axios.get<GithubIssue>( `/repos/${owner}/${repo}/issues/${number}`, requestConfig ); if (response.status === 200) { return response.data; } // Handle 404 if (response.status === 404) { return null; } // Throw an error for any other status throw new Error(`Unexpected response: ${response.statusText}`); } catch (error) { console.error('An error occurred while getting GitHub issue:', error); return null; } };This function uses
axiosto make an HTTP request to the GitHub API. It includes aUser-Agentheader as required by the GitHub API. ThegetGithubIssuefunction is asynchronous and returns a Promise that resolves to aGithubIssueobject if the request is successful (status 200) ornullif the issue does not exist (status 404). For any other status, it throws an error.Please note that this implementation does not include error handling for potential network issues or other HTTP errors. It's also important to handle rate limiting and other GitHub API-specific considerations in a production environment.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.