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
- Added unit tests to GET
  • Loading branch information
Thiago Saife Rodrigues committed Dec 26, 2023
1 parent a938d74 commit d2ef2c1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ class Storyblok {
})
}

public customFetch(url: string, options: RequestInit) {
// Create a new SbFetch instance with the custom fetch function
const customClient = new SbFetch({
baseURL: this.client.baseURL,
headers: this.client.headers,
fetch: (...args: [any]) => fetch(...args),
});

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}`);
}

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

public setComponentResolver(resolver: ComponentResolverFn): void {
this.richTextResolver.addNode('blok', (node: ISbNode) => {
let html = ''
Expand Down
4 changes: 2 additions & 2 deletions src/sbFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ interface ISbFetch {
}

class SbFetch {
private baseURL: string
public baseURL: string
private timeout?: number
private headers: Headers
public headers: Headers
private responseInterceptor?: ResponseFn
private fetch: typeof fetch
private ejectInterceptor?: boolean
Expand Down
43 changes: 43 additions & 0 deletions tests/customFetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, test, describe, beforeEach } from 'vitest'
import StoryblokClient from 'src/index.ts'

describe('customFetch', () => {
let client

beforeEach(() => {
client = new StoryblokClient({
accessToken: process.env.VITE_ACCESS_TOKEN,
oauthToken: process.env.VITE_OAUTH_TOKEN,
})
})

test('should call GET method', async () => {
const response = await client.customFetch(
`spaces/${process.env.VITE_SPACE_ID}/stories`,
{
method: 'GET',
body: {},
}
)
expect(response).toHaveProperty('data')
})

// Uncomment and adjust the following test if your API supports POST method for the endpoint
// test('should call POST method', async () => {
// const response = await client.customFetch(`spaces/${process.env.VITE_SPACE_ID}/stories`, {
// method: 'POST',
// body: JSON.stringify({ key: 'value' }),
// })
// expect(response).toHaveProperty('data')
// })

test('should return an error for invalid method', async () => {
try {
await client.customFetch(`spaces/${process.env.VITE_SPACE_ID}/stories`, {
method: 'INVALID',
})
} catch (error) {
expect(error).toHaveProperty('message')
}
})
})

0 comments on commit d2ef2c1

Please sign in to comment.