generated from the-pudding/svelte-starter
-
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
c72ef32
commit be4cde4
Showing
3 changed files
with
48 additions
and
2 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
File renamed without changes.
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,37 @@ | ||
import { dev } from "$app/environment"; | ||
import urlParams from "$utils/urlParams.js"; | ||
|
||
export default async function server(endpoint, data) { | ||
const base = "http://localhost:3000"; | ||
// const base = "https://pudding-mowing-server-335031567fcb.herokuapp.com"; | ||
const url = `${base}/${endpoint}`; | ||
|
||
const token = dev ? urlParams.get("token") : ""; | ||
|
||
try { | ||
const start = Date.now(); | ||
const body = JSON.stringify({ data }); | ||
const options = { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
"X-Pudding-Token": token | ||
}, | ||
credentials: "include", | ||
body | ||
}; | ||
|
||
const response = await fetch(url, options); | ||
if (response.status === 404) throw Error(response.statusText); | ||
else { | ||
const result = await response.json(); | ||
const end = Date.now(); | ||
const duration = `${end - start}ms`; | ||
return { ...result, duration }; | ||
} | ||
} catch (err) { | ||
console.log(err?.message); | ||
throw err; | ||
} | ||
} |