diff --git a/src/index.ts b/src/index.ts index de886491..e0ff484b 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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 = '' diff --git a/src/sbFetch.ts b/src/sbFetch.ts index 1199f3c8..ef8b7a94 100644 --- a/src/sbFetch.ts +++ b/src/sbFetch.ts @@ -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 diff --git a/tests/customFetch.test.js b/tests/customFetch.test.js new file mode 100644 index 00000000..43deb4bf --- /dev/null +++ b/tests/customFetch.test.js @@ -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') + } + }) +})