Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/services/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
class APIService {
private baseUrl: string;
private baseUrl: string | undefined;
private config: RequestInit;

constructor() {
this.baseUrl = process.env.BASE_URL;

this.config = {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
};
}

getTests(): any {
this.config = {
...this.config,
method: 'GET',
};

return fetch('tests', this.config).then((response) => response.json());
}

async getUsers(): Promise<any[]> {
this.config = {
...this.config,
method: 'GET',
};

const usersResults = await fetch('users', this.config).then((response) => response.json());

return usersResults;
}

postTestResults(results: any): Promise<Response> {
// Mutate results to match with API body request
let body: any = {
id: results.id,
testName: results.name,
};

const answers: any[] = [];

results.answers.forEach((answer: string) =>
answers.push({
input: answer,
}),
);

body = {
...body,
answers,
};

this.config = {
...this.config,
method: 'POST',
body: JSON.stringify(body),
};

return fetch('tests/results', this.config).catch((error) => error);
}
}

export default APIService;