Team Ai
Apppublic

cosmoEagle/leetcode-api

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
fetchSingleProblem.ts39 linesDownload Raw Back to Controllers
1import { Response } from 'express';2import { DailyProblemData, SelectProblemData } from '../types';3 4const fetchSingleProblem = async (5  res: Response,6  formatData: (data: DailyProblemData & SelectProblemData) => void,7  query: string,8  titleSlug: string | null9) => {10  try {11    const response = await fetch('https://leetcode.com/graphql', {12      method: 'POST',13      headers: {14        'Content-Type': 'application/json',15        Referer: 'https://leetcode.com',16      },17      body: JSON.stringify({18        query: query,19        variables: {20          titleSlug, //search question using titleSlug21        },22      }),23    });24 25    const result = await response.json();26 27    if (result.errors) {28      return res.send(result);29    }30 31    return res.json(formatData(result.data));32  } catch (err) {33    console.error('Error: ', err);34    return res.send(err);35  }36};37 38export default fetchSingleProblem;39