File size: 1,065 Bytes
c2df9c2
607207a
c2df9c2
 
 
 
 
 
 
 
 
a73e8b4
c2df9c2
a73e8b4
607207a
a73e8b4
607207a
 
45dbf34
78354fc
1c1c1be
f16de12
1c1c1be
f16de12
1c1c1be
45dbf34
 
 
 
 
 
 
c2df9c2
45dbf34
 
c2df9c2
 
 
 
 
45dbf34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { NextApiRequest, NextApiResponse } from 'next';
import { odds, serp, surfer, sports } from './embed';

export const config = {
    api: {
      bodyParser: {
        sizeLimit: '1mb',
      },
    },
};

type FunctionHandler = any;

const handlers: FunctionHandler = {
  'search': serp,
  'surfer': surfer,
  'sports_odds': odds,
  'sports_results': sports
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const args = req.body.args as string;
  const functionName = req.body.name as string;
  const functionInput = JSON.parse(args);
  
  const functionHandler = handlers[functionName];
  
  if (!functionHandler) {
    console.error(`Function "${functionName}" is not supported.`);
    return res.status(500).json({ error: `Function "${functionName}" is not supported.` });
  }
  
  try {
    const result = await functionHandler(functionInput);
    return res.status(200).send(result);
  } catch (error) {
    console.error(error);
    // @ts-ignore
    return res.status(500).json({ error: error.message });
  }
}