Loading src/DrupalState.ts +1 −0 Original line number Diff line number Diff line Loading @@ -388,6 +388,7 @@ class DrupalState { res, params, refresh, anon, }); return object; } Loading src/__tests__/drupalState.test.ts +1 −1 Original line number Diff line number Diff line Loading @@ -458,7 +458,7 @@ describe('drupalState', () => { expect(fetchMock).toBeCalledTimes(2); }); test('should fetch resource anonymously', async () => { test('should fetch resource anonymously when using anon:true', async () => { const store: DrupalState = new DrupalState({ apiBase: 'https://dev-ds-demo.pantheonsite.io', apiPrefix: 'jsonapi', Loading src/__tests__/getObjectByPath.test.ts +32 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ const dsConfig = { apiBase: 'https://dev-ds-demo.pantheonsite.io', apiPrefix: 'jsonapi', defaultLocale: 'en', debug: true, }; describe('getObjectByPath', () => { Loading Loading @@ -162,4 +163,35 @@ describe('getObjectByPath', () => { Server responded with status code: 404`); }); }); test('should fetch a resource anonymously when anon: true', async () => { const store: DrupalState = new DrupalState({ ...dsConfig, clientId: '9adc9c69-fa3b-4c21-9cef-fbd345d1a269', clientSecret: 'mysecret', }); const getAuthHeaderSpy = jest.spyOn(store, 'getAuthHeader'); store.setState({ dsApiIndex: demoApiIndex.links }); fetchMock.mock( 'https://dev-ds-demo.pantheonsite.io/router/translate-path?path=/recipes/fiery-chili-sauce&_format=json', { status: 200, body: translatePathResponse, } ); fetchMock.mock( 'https://dev-ds-demo.pantheonsite.io/en/jsonapi/node/recipe/da1359f4-2e60-462c-8909-47c3bce11fdf', { status: 200, body: recipesResourceData, } ); expect( await store.getObjectByPath({ objectName: 'node--recipe', path: '/recipes/fiery-chili-sauce', anon: true, }) ).toEqual(recipesResourceByPath); expect(getAuthHeaderSpy).toBeCalledTimes(0); }); }); src/main.ts +16 −0 Original line number Diff line number Diff line Loading @@ -187,6 +187,22 @@ async function main(): Promise<void> { // }) // ); // console.log('-- Fetch an object anonymously --'); // console.log( // await authStore.getObject({ // objectName: 'node-recipe', // anon: true, // }) // ); // console.log('-- Also works with getObjectByPath --'); // console.log( // await authStore.getObjectByPath({ // objectName: 'node-recipe', // path: '/recipes/fiery-chili-sauce', // anon: true, // }) // ); // You also have direct access to the Zustand store if necessary store.setState({ custom: 'custom state' }); } Loading Loading
src/DrupalState.ts +1 −0 Original line number Diff line number Diff line Loading @@ -388,6 +388,7 @@ class DrupalState { res, params, refresh, anon, }); return object; } Loading
src/__tests__/drupalState.test.ts +1 −1 Original line number Diff line number Diff line Loading @@ -458,7 +458,7 @@ describe('drupalState', () => { expect(fetchMock).toBeCalledTimes(2); }); test('should fetch resource anonymously', async () => { test('should fetch resource anonymously when using anon:true', async () => { const store: DrupalState = new DrupalState({ apiBase: 'https://dev-ds-demo.pantheonsite.io', apiPrefix: 'jsonapi', Loading
src/__tests__/getObjectByPath.test.ts +32 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ const dsConfig = { apiBase: 'https://dev-ds-demo.pantheonsite.io', apiPrefix: 'jsonapi', defaultLocale: 'en', debug: true, }; describe('getObjectByPath', () => { Loading Loading @@ -162,4 +163,35 @@ describe('getObjectByPath', () => { Server responded with status code: 404`); }); }); test('should fetch a resource anonymously when anon: true', async () => { const store: DrupalState = new DrupalState({ ...dsConfig, clientId: '9adc9c69-fa3b-4c21-9cef-fbd345d1a269', clientSecret: 'mysecret', }); const getAuthHeaderSpy = jest.spyOn(store, 'getAuthHeader'); store.setState({ dsApiIndex: demoApiIndex.links }); fetchMock.mock( 'https://dev-ds-demo.pantheonsite.io/router/translate-path?path=/recipes/fiery-chili-sauce&_format=json', { status: 200, body: translatePathResponse, } ); fetchMock.mock( 'https://dev-ds-demo.pantheonsite.io/en/jsonapi/node/recipe/da1359f4-2e60-462c-8909-47c3bce11fdf', { status: 200, body: recipesResourceData, } ); expect( await store.getObjectByPath({ objectName: 'node--recipe', path: '/recipes/fiery-chili-sauce', anon: true, }) ).toEqual(recipesResourceByPath); expect(getAuthHeaderSpy).toBeCalledTimes(0); }); });
src/main.ts +16 −0 Original line number Diff line number Diff line Loading @@ -187,6 +187,22 @@ async function main(): Promise<void> { // }) // ); // console.log('-- Fetch an object anonymously --'); // console.log( // await authStore.getObject({ // objectName: 'node-recipe', // anon: true, // }) // ); // console.log('-- Also works with getObjectByPath --'); // console.log( // await authStore.getObjectByPath({ // objectName: 'node-recipe', // path: '/recipes/fiery-chili-sauce', // anon: true, // }) // ); // You also have direct access to the Zustand store if necessary store.setState({ custom: 'custom state' }); } Loading