storyblok / storyblok-nuxt

Storyblok Nuxt module
https://www.storyblok.com/tp/nuxt-js-multilanguage-website-tutorial
MIT License
275 stars 42 forks source link

feature-request: Use storyblok functions from this package directly on API routes #440

Open chstappert opened 1 year ago

chstappert commented 1 year ago

Description

Can we import storyblok/nuxt directly on a nuxt API route instead of using the client? The setup is already done in the nuxt.config. Since the functions are only in the composable context, it would be nice to use them now on the API routes to get data and combine them with some logic.

Suggested solution or improvement

/* Use storyblok/nuxt package on a nuxt API route like this:  */

import { useStoryblokApi } from '@storyblok/nuxt' // and async functions

export default defineEventHandler(async (event) => {

const story = await useStoryblokApi().getStory('someStory') {...})

return story.data.story 

})

Additional context

I want to get data from storyblok, cache them with Nitro, and use the CMS data for our own (independent) endpoint to have consistent data and a clean setup.

Validations

alvarosabu commented 1 year ago

Hi @chstappert thanks for reaching out.

So Vue composables are meant to work under a setup context, which means, inside components. Since server routes in Nuxt work with Nitro, I'm afraid composables won't work here.

An option would be to create a server utility for Nitro.

steffkes commented 1 year ago

I'd second that!

If you're trying to satisfy the requirement from nuxt's simple-sitemap to provide a list of dynamic urls - obviously based on what we have in storyblok, this little addition would be superb.

Otherwise we're doing the whole initialization again, potentially duplicating our config

zacwebb commented 1 year ago

I'm currently having to setup a separate instance of storyblok-js-client to make calls to the content delivery API from my server/api routes 😕 would be really nice to just use the Nuxt library exclusively