Skip to content

Commit 6d62c49

Browse files
committed
Step 3: Creating a HarperDB client to make secure requests
1 parent 50df8bb commit 6d62c49

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/lib/harperdb.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* harperClient
3+
*/
4+
5+
export async function harperClient(body) {
6+
const requestOptions = {
7+
method: 'POST',
8+
headers: {
9+
'Content-Type': 'application/json',
10+
'Authorization': `Basic ${process.env.HARPER_API_KEY}`,
11+
},
12+
body: JSON.stringify(body),
13+
redirect: 'follow',
14+
}
15+
16+
const response = await fetch(process.env.HARPER_DATABASE_URL, requestOptions);
17+
const result = await response.text();
18+
19+
return JSON.parse(result, (key, value) => {
20+
if ( !isNaN(Date.parse(value)) ) {
21+
return new Date(value);
22+
}
23+
return value;
24+
});
25+
}

0 commit comments

Comments
 (0)