Skip to content

Commit 96bad95

Browse files
authored
feat: add code vault api (#49)
1 parent 8c9b35e commit 96bad95

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

get/src/codes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { getOrcicornShiftCodes } from './source/orcicorn';
22
import { getFextralifeShiftCodes } from './source/fextralife';
3+
import { getCodeVaultShiftCodes } from './source/codevault';
34

45
export async function * getShiftCodes() {
56
yield * getFextralifeShiftCodes();
67
yield * getOrcicornShiftCodes();
8+
yield * getCodeVaultShiftCodes();
79
}

get/src/source/codevault.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ShiftCode } from '../types';
2+
3+
import { parseDate } from '../utils/parseDate';
4+
5+
const BL4_SHIFT_CODES_URL = 'https://code-vault.celo.workers.dev/api/codes';
6+
7+
export async function* getCodeVaultBL4ShiftCodes(): AsyncGenerator<ShiftCode> {
8+
const response = await fetch(BL4_SHIFT_CODES_URL);
9+
const json = (await response.json()) as {
10+
data: { code: string; created_at: string }[];
11+
};
12+
13+
for (const entry of json.data) {
14+
yield {
15+
code: entry.code,
16+
game: 'Borderlands 4',
17+
platform: 'Universal',
18+
reward: 'Unknown',
19+
created: parseDate(entry.created_at),
20+
};
21+
}
22+
}
23+
24+
export async function* getCodeVaultShiftCodes(): AsyncGenerator<ShiftCode> {
25+
yield* getCodeVaultBL4ShiftCodes();
26+
}

get/src/source/orcicorn.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ const Pick = require('stream-json/filters/Pick');
33
const {streamValues} = require('stream-json/streamers/StreamValues');
44

55
import { ShiftCode } from '../types';
6+
import { parseDate } from '../utils/parseDate';
67

78
const SHIFT_CODES_URL = 'https://shift.orcicorn.com/shift-code/index.json';
89

9-
function parseDate(str: string) {
10-
const date = new Date(str);
11-
if (isNaN(date.valueOf())) return undefined;
12-
return date;
13-
}
14-
1510
export async function * getOrcicornShiftCodes(): AsyncGenerator<ShiftCode> {
1611
const response = await fetch(SHIFT_CODES_URL);
1712

get/src/utils/parseDate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function parseDate(str: string) {
2+
const date = new Date(str);
3+
if (isNaN(date.valueOf())) return undefined;
4+
return date;
5+
}

0 commit comments

Comments
 (0)