Skip to content

Commit

Permalink
Merge pull request #776 from storyblok/hotfix/fixes-not-working-endpo…
Browse files Browse the repository at this point in the history
…int-config

Fixes not working endpoint configuration and 2 level resolve_relation…
  • Loading branch information
ademarCardoso authored Feb 6, 2024
2 parents 7ea452e + 79c6e1e commit 6e0f350
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const STORYBLOK_AGENT = 'SB-Agent'
export const STORYBLOK_JS_CLIENT_AGENT = {
defaultAgentName: 'SB-JS-CLIENT',
defaultAgentVersion: 'SB-Agent-Version',
packageVersion: process.env.npm_package_version || '5.0.0',
packageVersion: '6.0.0',
}
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 6e0f350

Please sign in to comment.