Hi!
I mostly work on sveltekit what comes to metaframeworks such as nuxt. Our client has a fairly large contentful based nuxt 2 site which i'm now porting to nuxt 4
I'm fetching data from Contentful graphql using server/api/ routes, because i'm afraid of leaking the Contentful private api key. Is this really the way to go?
Here are some examples of my code
// server/utils/contentful-client.ts
import { GraphQLClient } from "graphql-request";
export const getContentfulClient = () => {
const config = useRuntimeConfig();
return new GraphQLClient(
`https://graphql.contentful.com/content/v1/spaces/${config.contentful.spaceId}`,
{
headers: {
Authorization: `Bearer ${config.contentful.token}`,
},
},
);
};
// server/api/sectors.ts
import { getSdk } from "~~/shared/types/contentful.generated";
import { getContentfulClient } from "../utils/contentful-client";
export default defineEventHandler(async (event) => {
const client = getContentfulClient();
const query = getQuery(event);
if (!query.locale) {
throw Error("Request needs locale param");
}
const locale = getContentfulLocale(String(query.locale));
if (!locale) {
throw new Error("Error getting locale with param");
}
const res = await client.request<SectorsQuery>(
SectorsDocument,
{
locale,
},
);
if (!res) {
throw createError({
statusCode: 404,
statusMessage: "Article not found",
});
}
return res;
});
// app/pages/index.vue
const { locale } = useI18n();
const { data } = useFetch<SectorsQuery>('/api/sectors', {
query: {
locale: locale.value,
},
});
Is this really the way to go without exposing secret api keys? Also i need to type the responses again in the "client" code, yet in sveltekit it inherits the return type from page load.