Created
June 11, 2023 13:58
-
-
Save diramazioni/b97bcdc14a5ff8466e356981bcd7f411 to your computer and use it in GitHub Desktop.
Next.js endpoint to test strapi API requests
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
import { fetchAPI } from "../utils/fetch-api"; | |
//import { NextRequest } from 'next/server'; | |
import { NextResponse } from 'next/server'; | |
export async function GET(request: Request) { | |
const query = { | |
filters: { | |
slug: {$eq:"home",}, | |
}, | |
populate: [ | |
"contentSections.articles", | |
"sections.articles", | |
], | |
locale:"en" | |
// populate: { | |
// contentSections:{ | |
// populate: { | |
// articles: { | |
// populate: '*', | |
// }, | |
// }, | |
// }, | |
// }, | |
}; | |
const page = "/pages" | |
const response = await getApi(page,query); | |
return NextResponse.json(response) | |
} | |
async function getApi(path:string, urlParamsObject:any): Promise<any> { | |
const token = process.env.NEXT_PUBLIC_STRAPI_API_TOKEN; | |
const options = { headers: { Authorization: `Bearer ${token}` } }; | |
try { | |
const response = await fetchAPI(path, urlParamsObject, options); | |
return response; | |
} catch (error) { | |
console.error(error); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment