From 1a748fabbee60fbfaf547584b18ab31a436b6026 Mon Sep 17 00:00:00 2001 From: Cobey Potter Date: Fri, 26 Jul 2024 09:14:56 -0400 Subject: [PATCH] feat: update response structure to echo schema expected from TS defs and v1 This updates the response structure to echo schema expected from TypeScript definitions and the same data schema/structure as return in buttercms-js version 1. The response no longer returns the `xsrfCookieName`, `xsrfHeaderName`, and `maxContentLength` in `response.config` as they are not in use by native fetch. Additionally, `response.config` returns only the fully formed `url` (`response.config.url`) rather than breaking it down in discrete parts. If needed, please use the native `URL` API in your code to get similar datapoints. This PR fixes the issues raised in https://github.com/ButterCMS/buttercms-js/issues/89. bump package version to 2.1.0 --- __tests__/butter.test.js | 30 +++++++++++++++++------------- examples/basic.html | 2 +- examples/hooks.html | 4 ++-- lib/useButter.js | 7 ++++--- lib/utils/useOnResponse.js | 19 ++++++++++++++++++- mocks/responses/page/page.json | 2 +- package.json | 2 +- tests/basic.html | 4 ++-- tests/hooks.html | 2 +- tests/lib/butter.min.js | 2 +- 10 files changed, 48 insertions(+), 26 deletions(-) diff --git a/__tests__/butter.test.js b/__tests__/butter.test.js index b5b8386..9018295 100644 --- a/__tests__/butter.test.js +++ b/__tests__/butter.test.js @@ -15,14 +15,16 @@ test( } ) - const response = await singlePageResponse; - + const response = singlePageResponse.data; + await expect(response.data.fields.headline).toEqual("This is an example news page"); await expect(response.data.slug).toEqual("example-news-page") await expect(response.data.page_type).not.toEqual("sport") // ensure we can get the page with the real page type attached - const response2 = await butter.page.retrieve('news', 'example-news-page') + const singlePageResponse2 = await butter.page.retrieve('news', 'example-news-page') + + const response2 = singlePageResponse2.data; await expect(response2.data.fields.headline).toEqual("This is an example news page"); await expect(response2.data.slug).toEqual("example-news-page") @@ -76,11 +78,11 @@ test( "should list pages by single-pages", async () => { const response = await butter.page.list('*') - - await expect(response.meta.count).toEqual(2); - await expect(response.data).toHaveLength(2); + + await expect(response.data.meta.count).toEqual(2); + await expect(response.data.data).toHaveLength(2); - const firstPage = response.data[0]; + const firstPage = response.data.data[0]; await expect(firstPage).toHaveProperty('slug', 'single-page-1'); await expect(firstPage).toHaveProperty('fields.title', 'This is a single page'); @@ -96,10 +98,10 @@ test( async () => { const response = await butter.page.list('*') - await expect(response.meta.count).toEqual(2); - await expect(response.data).toHaveLength(2); + await expect(response.data.meta.count).toEqual(2); + await expect(response.data.data).toHaveLength(2); - const firstPage = response.data[0]; + const firstPage = response.data.data[0]; await expect(firstPage).toHaveProperty('slug', 'single-page-1'); await expect(firstPage).toHaveProperty('fields.title', 'This is a single page'); @@ -108,9 +110,11 @@ test( const response2 = await butter.page.retrieve('news', 'example-news-page') - await expect(response2.data.fields.headline).toEqual("This is an example news page"); - await expect(response2.data.slug).toEqual("example-news-page") - await expect(response2.data.page_type).not.toEqual("sport") + const exactPage = response2.data + + await expect(exactPage.data.fields.headline).toEqual("This is an example news page"); + await expect(exactPage.data.slug).toEqual("example-news-page") + await expect(exactPage.data.page_type).not.toEqual("sport") return } diff --git a/examples/basic.html b/examples/basic.html index efbc972..1eccfcf 100644 --- a/examples/basic.html +++ b/examples/basic.html @@ -23,7 +23,7 @@ const { meta, data: posts - } = resp + } = resp.data for(i = 0; i < posts.length; i++) { var title = posts[i].title; diff --git a/examples/hooks.html b/examples/hooks.html index a412d0a..69c60fa 100644 --- a/examples/hooks.html +++ b/examples/hooks.html @@ -7,7 +7,7 @@ - +