Skip to content

Commit 87911e4

Browse files
committed
Add JSON structures, API file
1 parent 511a5df commit 87911e4

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

peerprep/api/gateway.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Question, ErrorBody } from "./structs";
2+
3+
const questions: { [key: string]: Question } = {
4+
"0" : {
5+
"id": 0,
6+
"difficulty": 2,
7+
"title": "Two Sum",
8+
"description": "Given an array of integers, return indices of the two numbers such that they add up to a specific target.",
9+
"test_cases": {
10+
"[2, 7, 11, 15], 9" : "[0, 1]",
11+
"[3, 2, 4], 6" : "[1, 2]",
12+
"[3, 3], 6" : "[0, 1]"
13+
}
14+
},
15+
"1" : {
16+
"id": 1,
17+
"difficulty": 1,
18+
"title": "Reverse Integer",
19+
"description": "Given a 32-bit signed integer, reverse digits of an integer.",
20+
"test_cases": {
21+
"123" : "321",
22+
"1" : "1",
23+
"22" : "22"
24+
}
25+
}
26+
};
27+
28+
export async function fetchQuestion(questionId: string): Promise<Question|ErrorBody> {
29+
// remove this when services are up
30+
if (process.env.DEV_ENV === "dev") {
31+
return questions[questionId] === undefined
32+
? {error: "Question not found", status: 404}
33+
: questions[questionId];
34+
}
35+
try {
36+
const response = await fetch(`${process.env.QUESTION_SERVICE}/questions/solve/${questionId}`);
37+
if (!response.ok) {
38+
return {
39+
...(await response.json()),
40+
status: response.status
41+
};
42+
}
43+
return await response.json() as Question;
44+
} catch (err: any) {
45+
return { error: err.message, status: 0};
46+
}
47+
}

peerprep/api/structs.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export enum Difficulty {
2+
Easy = 1,
3+
Medium,
4+
Hard
5+
}
6+
7+
export interface Question {
8+
id: number;
9+
difficulty: Difficulty;
10+
title: string;
11+
description: string;
12+
test_cases: {
13+
[key: string]: string;
14+
};
15+
}
16+
17+
export interface ErrorBody {
18+
error: string;
19+
status: number;
20+
}
21+
22+
export function isError(obj: Question | ErrorBody): obj is ErrorBody {
23+
return (obj as ErrorBody).error !== undefined;
24+
}

0 commit comments

Comments
 (0)