Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat int 1031 custom fetch function #727

Merged
merged 58 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
d2ef2c1
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
9f7d316
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
0701846
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
e67653f
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
3815cde
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
0efa60c
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
e85f071
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
6c36188
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
74ca482
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
4fd8be5
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
4f369e0
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
1d54dee
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
5c372a6
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
213c895
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
3257fd7
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
cee85fa
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
45ee6e1
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
f446ffc
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
f6f9d59
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
9720026
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
6df417e
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
22dea00
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
66278c1
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
7faf284
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
2f89f6e
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
416c1a1
feat(int-1031): Add customFetch method to Storyblok class
Dec 26, 2023
483ef73
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
736f426
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
5dee122
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
3077b4e
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
a3d1abe
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
cc15112
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
6d384c4
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
b1885ce
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
089dbef
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
426a440
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
289fd7e
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
28e4ab1
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
0e09954
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
f8c5c0e
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
2847a2c
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
e9630c2
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
fd587fd
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
ad4e096
feat(int-1031): Add customFetch method to Storyblok class
Dec 27, 2023
99d9c92
feat(int-1031): Add fetchOptions to SbFetch class
Jan 9, 2024
f04408a
feat(int-1031): Refactor fetch options method names
Jan 9, 2024
2beab19
feat(int-1031): Update access modifiers in SbFetch class
Jan 9, 2024
3657f3a
feat(int-1031): Refactor fetchOptions in Storyblok and SbFetch classes
Jan 9, 2024
3e1e5bb
feat(int-1031): Refactor fetchOptions handling in Storyblok class
Jan 9, 2024
d56e7ae
feat(int-1031): Add support for custom Fetch parameter
Jan 9, 2024
fbe03c9
feat(int-1031): Add support for custom Fetch parameter
Jan 9, 2024
b956af7
feat(int-1031): Add ISbCustomFetch interface to handle custom fetch o…
Jan 10, 2024
d0ac17b
feat(int-1031): Add ISbCustomFetch interface to handle custom fetch o…
Jan 10, 2024
b5702d1
feat(int-1031): Add ISbCustomFetch interface to handle custom fetch o…
Jan 10, 2024
791447c
feat(int-1031): Add ISbCustomFetch interface to handle custom fetch o…
Jan 10, 2024
e5bc37a
chore(INT-3031): update nextjs playground
alexjoverm Jan 10, 2024
5f60b6e
feat(int-1031): Update README
Jan 10, 2024
e1805f1
Merge branch 'feat-int-1031-custom-fetch-function' of https://github.…
Jan 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ With this method you can get single or multiple items. The multiple items are pa
- `[return]` Promise, Object `response`
- `path` String, Path (can be `cdn/stories`, `cdn/tags`, `cdn/datasources`, `cdn/links`)
- `options` Object, Options can be found in the [API documentation](https://www.storyblok.com/docs/api/content-delivery?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client).
- `fetchOptions` Object, optional, Fetch options can be found in the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

**Example**

Expand All @@ -291,6 +292,7 @@ With this method you can get all items at once.
- `path` String, Path (can be `cdn/stories`, `cdn/tags`, `cdn/datasources`, `cdn/links`)
- `options` Object, Options can be found in the [API documentation](https://www.storyblok.com/docs/api/content-delivery?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client).
- `entity` String, Storyblok entity like stories, links or datasource. It's optional.
- `fetchOptions` Object, optional, Fetch options can be found in the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

**Example**

Expand All @@ -313,6 +315,7 @@ Storyblok.getAll('cdn/stories', {
- `[return]` Promise, Object `response`
- `path` String, Path (`spaces/*`, ... see more at https://www.storyblok.com/docs/management-api/authentication?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client)
- `payload` Object
- `fetchOptions` Object, optional, Fetch options can be found in the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

**Example**

Expand All @@ -335,6 +338,7 @@ Storyblok.post('spaces/<YOUR_SPACE_ID>/stories', {
- `[return]` Promise, Object `response`
- `path` String, Path (`spaces/*`, ... see more at https://www.storyblok.com/docs/management-api/authentication?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js-client)
- `payload` Object
- `fetchOptions` Object, optional, Fetch options can be found in the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

**Example**

Expand All @@ -357,6 +361,7 @@ Storyblok.put('spaces/<YOUR_SPACE_ID>/stories/1', {
- `[return]` Promise, Object `response`
- `path` String, Path (`spaces/*`, ... see more at https://www.storyblok.com/docs/management-api/authentication)
- `payload` Object
- `fetchOptions` Object, optional, Fetch options can be found in the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

**Example**

Expand Down
107 changes: 85 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,13 @@ class Storyblok {
public constructor(config: ISbConfig, pEndpoint?: string) {
let endpoint = config.endpoint || pEndpoint

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 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 @@ -209,26 +203,37 @@ class Storyblok {
per_page: number,
page: number
): Promise<ISbResult> {
const options = this.factoryParamOptions(
const query = this.factoryParamOptions(
url,
this.helpers.getOptionsPage(params, per_page, page)
)

return this.cacheResponse(url, options)
return this.cacheResponse(url, query)
}

public get(slug: string, params?: ISbStoriesParams): Promise<ISbResult> {
public get(
slug: string,
params?: ISbStoriesParams,
fetchOptions?: RequestInit
): Promise<ISbResult> {
if (!params) params = {} as ISbStoriesParams
const url = `/${slug}`
const query = this.factoryParamOptions(url, params)

if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
thiagosaife marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.client.setFetchOptions()
}

return this.cacheResponse(url, query)
}

public async getAll(
slug: string,
params: ISbStoriesParams,
entity?: string
entity?: string,
fetchOptions?: RequestInit
): Promise<any[]> {
const perPage = params?.per_page || 25
const url = `/${slug}`
Expand All @@ -239,6 +244,12 @@ class Storyblok {
const firstRes = await this.makeRequest(url, params, perPage, firstPage)
const lastPage = firstRes.total ? Math.ceil(firstRes.total / perPage) : 1

if (fetchOptions) {
thiagosaife marked this conversation as resolved.
Show resolved Hide resolved
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

const restRes: any = await this.helpers.asyncMap(
this.helpers.range(firstPage, lastPage),
(i: number) => {
Expand All @@ -253,33 +264,76 @@ class Storyblok {

public post(
slug: string,
params: ISbStoriesParams | ISbContentMangmntAPI
params: ISbStoriesParams | ISbContentMangmntAPI,
fetchOptions?: RequestInit
): Promise<ISbResponseData> {
const url = `/${slug}`

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

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

public put(
slug: string,
params: ISbStoriesParams | ISbContentMangmntAPI
params: ISbStoriesParams | ISbContentMangmntAPI,
fetchOptions?: RequestInit
): Promise<ISbResponseData> {
const url = `/${slug}`

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

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

public delete(
slug: string,
params: ISbStoriesParams | ISbContentMangmntAPI
params: ISbStoriesParams | ISbContentMangmntAPI,
fetchOptions?: RequestInit
): Promise<ISbResponseData> {
const url = `/${slug}`

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

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

public getStories(params: ISbStoriesParams): Promise<ISbStories> {
public getStories(
params: ISbStoriesParams,
fetchOptions?: RequestInit
): Promise<ISbStories> {
if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return this.get('cdn/stories', params)
}

public getStory(slug: string, params: ISbStoryParams): Promise<ISbStory> {
public getStory(
slug: string,
params: ISbStoryParams,
fetchOptions?: RequestInit
): Promise<ISbStory> {
if (fetchOptions) {
this.client.setFetchOptions(fetchOptions)
} else {
this.client.setFetchOptions()
}

return this.get(`cdn/stories/${slug}`, params)
}

Expand Down Expand Up @@ -478,6 +532,15 @@ class Storyblok {
}
}

/**
*
* @param responseData
* @param params
* @param resolveId
* @description Resolves the relations and links of the stories
* @returns Promise<void>
*
*/
private async resolveStories(
responseData: ISbResponseData,
params: ISbStoriesParams,
Expand Down
17 changes: 12 additions & 5 deletions src/sbFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SbFetch {
private ejectInterceptor?: boolean
private url: string
private parameters: ISbStoriesParams
private fetchOptions: RequestInit

public constructor($c: ISbFetch) {
this.baseURL = $c.baseURL
Expand All @@ -35,6 +36,7 @@ class SbFetch {
this.ejectInterceptor = false
this.url = ''
this.parameters = {} as ISbStoriesParams
this.fetchOptions = {}
}

/**
Expand Down Expand Up @@ -121,32 +123,37 @@ class SbFetch {
}

try {
const response = await fetch(`${url}`, {
const fetchResponse = await fetch(`${url}`, {
method,
headers: this.headers,
body,
signal,
...this.fetchOptions,
})

if (this.timeout) {
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 = {}) {
this.fetchOptions = {...fetchOptions}
}

public eject() {
this.ejectInterceptor = true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/createTestContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import StoryblokClient from '../'
const spaceId = 67647

let Storyblok = new StoryblokClient({
oauthToken: process.env.OAUTH_TOKEN,
oauthToken: process.env.VITE_OAUTH_TOKEN,
})

for (var i = 0; i < 26; i++) {
Expand Down
23 changes: 13 additions & 10 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ let Storyblok = new StoryblokClient({
describe('getAll function', () => {
test("getAll('cdn/stories') should return all stories", async () => {
const result = await Storyblok.getAll('cdn/stories')
expect(result.length).toBe(28)
expect(result.length).toBeGreaterThan(0)
})

test("getAll('cdn/stories') should return all stories with filtered results", async () => {
const result = await Storyblok.getAll('cdn/stories', {
starts_with: 'testcontent-0',
})
expect(result.length).toBe(1)
expect(result.length).toBeGreaterThan(0)
})

test("getAll('cdn/stories', filter_query: { __or: [{ category: { any_in_array: 'Category 1' } }, { category: { any_in_array: 'Category 2' } }]}) should return all stories with the specific filter applied", async () => {
Expand All @@ -29,28 +29,31 @@ describe('getAll function', () => {
],
},
})
expect(result.length).toBe(4)
expect(result.length).toBeGreaterThan(0)
})

test("getAll('cdn/stories', {by_slugs: 'folder/*'}) should return all stories with the specific filter applied", async () => {
const result = await Storyblok.getAll('cdn/stories', {
by_slugs: 'folder/*',
})
expect(result.length).toBe(2)
expect(result.length).toBeGreaterThan(0)
})

test("getAll('cdn/links') should return all links", async () => {
const result = await Storyblok.getAll('cdn/links')
expect(result.length).toBe(29)
expect(result.length).toBeGreaterThan(0)
})

if (process.env.OAUTH_TOKEN) {
test("getAll('spaces/67647/stories') should return all spaces", async () => {
if (process.env.VITE_OAUTH_TOKEN) {
const spaceId = process.env.VITE_SPACE_ID
test('should return all spaces', async () => {
let StoryblokManagement = new StoryblokClient({
oauthToken: process.env.OAUTH_TOKEN,
oauthToken: process.env.VITE_OAUTH_TOKEN,
})
const result = await StoryblokManagement.getAll('spaces/67647/stories')
expect(result.length).toBe(29)
const result = await StoryblokManagement.getAll(
`spaces/${spaceId}/stories`
)
expect(result.length).toBeGreaterThan(0)
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion tests/interceptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect, test, describe } from 'vitest'
import StoryblokClient from '../'

const accessToken = 'w0yFvs04aKF2rpz6F8OfIQtt'
const accessToken = process.env.VITE_ACCESS_TOKEN;
const cache = {
type: 'memory',
clear: 'auto',
Expand Down