-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
28 lines (24 loc) · 1010 Bytes
/
index.ts
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
import { parse } from 'muninn';
import * as cheerio from 'cheerio';
import { Env, Contribution, Contributions } from './types/base';
import { createSchema } from './utils';
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext){
const getContributions = async () => {
try {
const response = await fetch(`https://github.com/users/${env.GITHUB_USERNAME}/contributions`);
const responseData = cheerio.load(await response.text())
const parsedData = parse(responseData, { schema: createSchema(responseData) }) as unknown as Contributions;
return parsedData.contributions.sort((a: Contribution, b: Contribution) => new Date(a.date).getTime() - new Date(b.date).getTime());
} catch (error: any) {
throw new Error(error);
}
};
const results = JSON.stringify(await getContributions());
return new Response(results, {
headers: {
'content-type': 'application/json;charset=UTF-8'
}
});
}
};