Skip to content

Commit

Permalink
feat(int-1031): Refactor fetchOptions in Storyblok and SbFetch classes
Browse files Browse the repository at this point in the history
- Reset property for each call
  • Loading branch information
Thiago Saife Rodrigues committed Jan 9, 2024
1 parent 2beab19 commit 3657f3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class Storyblok {

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return this.cacheResponse(url, query)
Expand All @@ -244,6 +246,8 @@ class Storyblok {

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

const restRes: any = await this.helpers.asyncMap(
Expand All @@ -267,6 +271,8 @@ class Storyblok {

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return Promise.resolve(this.throttle('post', url, params))
Expand All @@ -281,7 +287,10 @@ class Storyblok {

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return Promise.resolve(this.throttle('put', url, params))
}

Expand All @@ -294,6 +303,8 @@ class Storyblok {

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return Promise.resolve(this.throttle('delete', url, params))
Expand All @@ -305,6 +316,8 @@ class Storyblok {
): Promise<ISbStories> {
if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return this.get('cdn/stories', params)
Expand All @@ -317,6 +330,8 @@ class Storyblok {
): Promise<ISbStory> {
if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return this.get(`cdn/stories/${slug}`, params)
Expand Down
12 changes: 6 additions & 6 deletions src/sbFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SbFetch {
}

try {
const response = await fetch(`${url}`, {
const fetchResponse = await fetch(`${url}`, {
method,
headers: this.headers,
body,
Expand All @@ -135,22 +135,22 @@ class SbFetch {
clearTimeout(timeout)
}

const res = (await this._responseHandler(response)) as ISbResponse
const response = (await this._responseHandler(fetchResponse)) as ISbResponse

if (this.responseInterceptor && !this.ejectInterceptor) {
return this._statusHandler(this.responseInterceptor(res))
return this._statusHandler(this.responseInterceptor(response))
} else {
return this._statusHandler(res)
return this._statusHandler(response)
}
} catch (err: TypeError | RangeError | EvalError | any) {
} catch (err: any) {
const error: ISbError = {
message: err,
}
return error
}
}

public setFetchOptions(fetchOptions: RequestInit) {
public setFetchOptions(fetchOptions: RequestInit = {}) {
this.fetchOptions = {...fetchOptions}
}

Expand Down

0 comments on commit 3657f3a

Please sign in to comment.