Skip to content

Commit

Permalink
Fixes not working endpoint configuration and 2 level resolve_relation…
Browse files Browse the repository at this point in the history
…ship problem
  • Loading branch information
onefriendaday committed Feb 5, 2024
1 parent 7ea452e commit d97f666
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ class Storyblok {
public constructor(config: ISbConfig, pEndpoint?: string) {
let endpoint = config.endpoint || pEndpoint

const getRegion = new SbHelpers().getRegionURL
const protocol = config.https === false ? 'http' : 'https'

if (!config.oauthToken) {
endpoint = `${protocol}://${getRegion(config.region)}/${'v2' as Version}`
} else {
endpoint = `${protocol}://${getRegion(config.region)}/${'v1' as Version}`
if (!endpoint) {
const getRegion = new SbHelpers().getRegionURL
const protocol = config.https === false ? 'http' : 'https'

if (!config.oauthToken) {
endpoint = `${protocol}://${getRegion(config.region)}/${'v2' as Version}`
} else {
endpoint = `${protocol}://${getRegion(config.region)}/${'v1' as Version}`
}
}

const headers: Headers = new Headers()
Expand Down Expand Up @@ -184,6 +186,10 @@ class Storyblok {
params.resolve_relations = params.resolve_relations.join(',')
}

if (typeof params.resolve_relations !== 'undefined') {
params.resolve_level = 2
}

return params
}

Expand Down Expand Up @@ -296,6 +302,7 @@ class Storyblok {
fetchOptions?: ISbCustomFetch
): Promise<ISbStories> {
this.client.setFetchOptions(fetchOptions)
this._addResolveLevel(params)

return this.get('cdn/stories', params)
}
Expand All @@ -306,6 +313,7 @@ class Storyblok {
fetchOptions?: ISbCustomFetch
): Promise<ISbStory> {
this.client.setFetchOptions(fetchOptions)
this._addResolveLevel(params)

return this.get(`cdn/stories/${slug}`, params)
}
Expand All @@ -318,6 +326,12 @@ class Storyblok {
this.client.eject()
}

private _addResolveLevel(params: ISbStoriesParams | ISbStoryParams): void {
if (typeof params.resolve_relations !== 'undefined') {
params.resolve_level = 2
}
}

private _cleanCopy(value: LinksType): JSON {
return JSON.parse(JSON.stringify(value))
}
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ResponseFn } from './sbFetch'
export interface ISbStoriesParams
extends Partial<ISbStoryData>,
ISbMultipleStoriesData {
resolve_level?: number
_stopResolving?: boolean
by_slugs?: string
by_uuids?: string
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface ISbStoriesParams
}

export interface ISbStoryParams {
resolve_level?: number
token?: string
find_by?: 'uuid'
version?: 'draft' | 'published'
Expand Down

0 comments on commit d97f666

Please sign in to comment.