Team Ai
Apppublic

cosmoEagle/leetcode-api

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
fetchDiscussion.ts39 linesDownload Raw Back to Controllers
1import { Response } from 'express';2import { TrendingDiscussionObject } from '../types';3 4const fetchDiscussion = async (5    options: { first: number },6    res: Response,7    formatData: (data: TrendingDiscussionObject) => {},8    query: string9) => {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                    first: options.first || 20, //by default get 20 question21                },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 fetchDiscussion;39