Team Ai
Apppublic

cosmoEagle/leetcode-api

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
fetchProblems.ts49 linesDownload Raw Back to Controllers
1import { Response } from 'express';2import { ProblemSetQuestionListData } from '../types';3 4const fetchProblems = async (5  options: { limit?: number; skip?: number; tags?: string; difficulty?: string}, // Mark parameters as optional6  res: Response,7  formatData: (data: ProblemSetQuestionListData) => {},8  query: string9) => {10  try {11    // Set default limit to 1 if only skip is provided12    const limit = options.skip !== undefined && options.limit === undefined ? 1 : options.limit || 20;13    const skip = options.skip || 0; // Default to 0 if not provided14    const tags = options.tags ? options.tags.split(' ') : []; // Split tags or default to empty array15    const difficulty = options.difficulty || undefined; // difficulty has to be 'EASY', 'MEDIUM' or 'HARD'16 17    const response = await fetch('https://leetcode.com/graphql', {18      method: 'POST',19      headers: {20        'Content-Type': 'application/json',21        Referer: 'https://leetcode.com',22      },23      body: JSON.stringify({24        query: query,25        variables: {26          categorySlug: '',27          skip,28          limit,29          filters: { tags,30            difficulty31           },32        },33      }),34    });35    console.log(response)36 37    const result = await response.json();38 39    if (result.errors) {40      return res.status(400).json(result.errors); // Return errors with a 400 status code41    }42    return res.json(formatData(result.data));43  } catch (err) {44    console.error('Error: ', err);45    return res.status(500).json({ error: 'Internal server error' }); // Return a 500 status code for server errors46  }47};48 49export default fetchProblems;