-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfake_backend.js
More file actions
57 lines (49 loc) · 1.87 KB
/
fake_backend.js
File metadata and controls
57 lines (49 loc) · 1.87 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const apiurl = import.meta.env.VITE_FAKE_BACKEND_APIURL;
const flowid = import.meta.env.VITE_FAKE_BACKEND_FLOWID;
const apikey = import.meta.env.VITE_FAKE_BACKEND_APIKEY
const defaultHeader = {
'Content-Type': "application/json",
'x-api-key': apikey,
'api-version': '1.0'
};
// Call Incode's `omni/start` API to create an Incode session which will include a
// token in the response.
const fakeBackendStart = async function() {
const url = `${apiurl}/omni/start`;
const params = {
configurationId: flowid,
// language: "en-US",
// redirectionUrl: "https://example.com?custom_parameter=some+value",
// externalCustomerId: "the id of the customer in your system",
};
let response;
try {
response = await fetch(url, { method: 'POST', body: JSON.stringify(params), headers: defaultHeader});
if (!response.ok) {
throw new Error('Request failed with code ' + response.status)
}
} catch(e) {
throw new Error('HTTP Post Error: ' + e.message)
}
// The session response has many values, but you should only pass the token to the frontend.
const {token} = await response.json();
return {token};
}
// Finishes the session started at /start
const fakeBackendFinish = async function(token) {
const url = `${apiurl}/omni/finish-status`;
let sessionHeaders = {...defaultHeader};
sessionHeaders['X-Incode-Hardware-Id'] = token;
let response;
try {
response = await fetch(url, {method: 'GET', headers: sessionHeaders});
if (!response.ok) {
throw new Error('Request failed with code ' + response.status)
}
} catch(e) {
throw new Error('HTTP Post Error: ' + e.message)
}
const {redirectionUrl, action} = await response.json();
return {redirectionUrl, action};
}
export {fakeBackendStart, fakeBackendFinish};