-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: new typescript and error support
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useStoryblokApi, useStoryblokBridge } from "@storyblok/vue"; | ||
import type { ISbStoryData, ISbError, ISbResult } from '@storyblok/vue'; | ||
import { useAsyncData, useState, onMounted, createError } from "#imports"; | ||
|
||
const useAsyncStoryblok = async ( | ||
url: string, | ||
apiOptions = {}, | ||
bridgeOptions = {}, | ||
) => { | ||
const uniqueKey = `${JSON.stringify(apiOptions)}${url}`; | ||
const story = useState<ISbStoryData>(`${uniqueKey}-state`, () => ({} as ISbStoryData)); | ||
const storyblokApiInstance = useStoryblokApi(); | ||
|
||
onMounted(() => { | ||
if (story.value && story.value.id) { | ||
useStoryblokBridge( | ||
story.value.id, | ||
evStory => (story.value = evStory), | ||
bridgeOptions, | ||
); | ||
} | ||
}); | ||
|
||
const { data, error } = await useAsyncData<ISbResult, ISbError>( | ||
`${uniqueKey}-asyncdata`, | ||
async () => await storyblokApiInstance.get(`cdn/stories/${url}`, apiOptions), | ||
); | ||
|
||
// ToDo: Wait JS sdk 500 to 404 error | ||
if (error.value?.response.status >= 400 && error.value?.response.status < 600) { | ||
throw createError({ statusCode: error.value?.response.status, statusMessage: error.value?.message.message }); | ||
} | ||
|
||
story.value = data.value?.data.story; | ||
|
||
return story; | ||
}; | ||
|
||
export default useAsyncStoryblok; | ||
|