-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fe8eb2
commit a09c2a4
Showing
13 changed files
with
111 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const http = require("http"); | ||
const crypto = require("crypto"); | ||
|
||
const vendorId = process.env.TEST_VENDOR_ID; | ||
const apiKey = process.env.TEST_API_KEY; | ||
const testApiPort = process.env.TEST_API_PORT; | ||
|
||
function generateUUID() { | ||
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => | ||
(c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16), | ||
); | ||
} | ||
|
||
function generateRandomString() { | ||
return crypto.randomBytes(4).toString("hex"); | ||
} | ||
|
||
// Create a server that listens to requests on port 3001 | ||
const server = http.createServer((req, res) => { | ||
// Set CORS headers | ||
res.setHeader("Access-Control-Allow-Origin", "*"); | ||
res.setHeader("Access-Control-Request-Method", "*"); | ||
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, POST"); | ||
res.setHeader("Access-Control-Allow-Headers", "*"); | ||
|
||
if (req.method === "OPTIONS") { | ||
res.writeHead(200); | ||
res.end(); | ||
return; | ||
} | ||
|
||
// Handle POST requests | ||
if (req.method === "POST") { | ||
let data = ""; | ||
|
||
req.on("data", (chunk) => { | ||
data += chunk; | ||
}); | ||
|
||
req.on("end", () => { | ||
console.log(JSON.parse(data)); | ||
res.writeHead(200); | ||
res.end(); | ||
}); | ||
} | ||
}); | ||
|
||
server.listen(testApiPort, () => { | ||
console.log(`Test API server listening on port ${testApiPort}`); | ||
|
||
fetch("http://localhost:3000/api/data/completions", { | ||
method: "POST", | ||
headers: { | ||
host: `localhost:${testApiPort}`, // this is hidden in real world scenarios | ||
vendorId, | ||
apiKey, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
_id: generateUUID(), | ||
property1: generateRandomString(), | ||
property2: generateRandomString(), | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((json) => { | ||
console.log(json); | ||
}); | ||
}); |