-
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
Showing
3 changed files
with
134 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getAccessToken } from "@auth0/nextjs-auth0"; | ||
|
||
function buildUrl(path: string) { | ||
return `${process.env.BACKEND_BASE_URL}${path}`; | ||
} | ||
|
||
async function getAuthHeader() { | ||
const accessTokenResponse = await getAccessToken({ | ||
authorizationParams: { | ||
audience: process.env.AUTH0_AUDIENCE || "", | ||
}, | ||
}); | ||
|
||
return "Bearer " + accessTokenResponse.accessToken || ""; | ||
} | ||
|
||
export const GET = async function getTasks() { | ||
const res = await fetch(buildUrl("/v0/tasks"), { | ||
cache: "no-store", | ||
headers: { | ||
Authorization: await getAuthHeader(), | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
const b = await res.json(); | ||
throw new Error("Failed to fetch data"); | ||
} | ||
|
||
const data = await res.json(); | ||
return Response.json(data); | ||
}; | ||
|
||
export const POST = async function addTask(req: Request) { | ||
const reqData = await req.json(); | ||
|
||
const res = await fetch(buildUrl("/v0/tasks"), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
title: reqData.title, | ||
description: reqData.description, | ||
}), | ||
|
||
headers: { | ||
Authorization: await getAuthHeader(), | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
// This will activate the closest `error.js` Error Boundary | ||
throw new Error("Failed to add task"); | ||
} | ||
|
||
const resData = res.json(); | ||
return Response.json(resData); | ||
}; |
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