Skip to content

Instantly share code, notes, and snippets.

@diramazioni
Created June 11, 2023 13:58
Show Gist options
  • Save diramazioni/b97bcdc14a5ff8466e356981bcd7f411 to your computer and use it in GitHub Desktop.
Save diramazioni/b97bcdc14a5ff8466e356981bcd7f411 to your computer and use it in GitHub Desktop.
Next.js endpoint to test strapi API requests
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