Skip to content

Commit

Permalink
feat(int-1031): Add customFetch method to Storyblok class
Browse files Browse the repository at this point in the history
- Add customFetch function with function signature and examples
  • Loading branch information
Thiago Saife Rodrigues committed Dec 27, 2023
1 parent 6d384c4 commit b1885ce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ window.storyblok.on('input', (event) => {

The `customFetch` function is a custom implementation of the fetch API to be used alongside the client's calls. It is designed to handle different types of `BodyInit` in the `SbFetch` methods (`get`, `post`, `put`, `delete`).

### Function Signature

```typescript
customFetch(
path: string,
fetchOptions?: RequestInit,
config?: ISbConfig,
): Promise<ISbResponse | ISbError>
```

### Examples

Here's an example of how to use the `customFetch` function:
Expand All @@ -273,16 +283,27 @@ const client = new StoryblokClient({

// GET request
client
.customFetch(`spaces/${SPACE_ID}/stories`, { method: 'GET' })
.customFetch(
`spaces/${SPACE_ID}/stories`,
{ method: 'GET',
// ...other fetch options
},
{
timeout: 5000,
}
)
.then((response) => console.log(response))
.catch((error) => console.error(error))

// POST request with JSON body
client
.customFetch(`spaces/${SPACE_ID}/stories`, {
method: 'POST',
body: JSON.stringify({ key: 'value' }),
})
.customFetch(
`spaces/${SPACE_ID}/stories`,
{
method: 'POST',
body: JSON.stringify({ key: 'value' }),
}
)
.then((response) => console.log(response))
.catch((error) => console.error(error))
```
Expand Down
60 changes: 35 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
ThrottleFn,
IMemoryType,
ICacheProvider,
ISbResponse,
ISbError,
} from './interfaces'

let memory: Partial<IMemoryType> = {}
Expand Down Expand Up @@ -154,37 +156,45 @@ class Storyblok {
})
}

public customFetch(url: string, options: RequestInit) {
/**
*
* @param url string
* @param options RequestInit
* @param config ISbConfig
* @returns Promise<ISbResponse | ISbError>
*
*/
public customFetch(
url: string,
options: RequestInit,
config: ISbConfig
): Promise<ISbResponse | ISbError> {
// Create a new SbFetch instance with the custom fetch function
const customClient = new SbFetch({
const mergedConfig = {
baseURL: this.client.baseURL,
headers: this.client.headers,
fetch: (...args: [any]) => fetch(...args),
})
...config,
}

let method
let params
switch ((options.method || 'get').toLowerCase()) {
case 'get':
method = customClient.get.bind(customClient)
params = {} // GET requests typically don't have a body
break
case 'post':
method = customClient.post.bind(customClient)
params = JSON.parse(options.body as string)
break
case 'put':
method = customClient.put.bind(customClient)
params = JSON.parse(options.body as string)
break
case 'delete':
method = customClient.delete.bind(customClient)
params = JSON.parse(options.body as string)
break
default:
throw new Error(`Invalid method: ${options.method}`)
const customClient = new SbFetch(mergedConfig)

const methodType = (options.method || 'get').toLowerCase()

const methods: { [key: string]: (url: string, params: ISbStoriesParams) => Promise<ISbResponse | ISbError> } = {
get: customClient.get.bind(customClient),
post: customClient.post.bind(customClient),
put: customClient.put.bind(customClient),
delete: customClient.delete.bind(customClient),
};

if (!(methodType in methods)) {
throw new Error(`Invalid method: ${options.method}`)
}

const method = methods[methodType]
const params =
methodType === 'get' ? {} : JSON.parse(options.body as string)

return method(`/${url}`, params)
}

Expand Down

0 comments on commit b1885ce

Please sign in to comment.