Comparing version 2.0.3 to 2.1.0
@@ -18,4 +18,4 @@ import Butter from "../lib/butter.js"; | ||
const response = await singlePageResponse; | ||
const response = singlePageResponse.data; | ||
await expect(response.data.fields.headline).toEqual("This is an example news page"); | ||
@@ -25,2 +25,11 @@ await expect(response.data.slug).toEqual("example-news-page") | ||
// ensure we can get the page with the real page type attached | ||
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") | ||
await expect(response2.data.page_type).not.toEqual("sport") | ||
return | ||
@@ -48,2 +57,18 @@ } | ||
} | ||
try { | ||
const singlePageResponse2 = await butter.page.retrieve( | ||
"*/about", | ||
"example-news-page", | ||
{ | ||
"locale": "en", | ||
"preview": 1 | ||
} | ||
) | ||
} | ||
catch (error) { | ||
await expect(error).toBeInstanceOf(Error); | ||
await expect(error.message).toEqual("TypeError: Failed to fetch"); | ||
} | ||
@@ -59,7 +84,26 @@ return | ||
const response = await butter.page.list('*') | ||
await expect(response.data.meta.count).toEqual(2); | ||
await expect(response.data.data).toHaveLength(2); | ||
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'); | ||
await expect(firstPage).not.toHaveProperty('date_time'); | ||
return | ||
} | ||
) | ||
test( | ||
"should list pages by single-pages and then retrieve a pageTyped page", | ||
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]; | ||
@@ -70,2 +114,12 @@ await expect(firstPage).toHaveProperty('slug', 'single-page-1'); | ||
await expect(firstPage).not.toHaveProperty('date_time'); | ||
const response2 = await butter.page.retrieve('news', 'example-news-page') | ||
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 | ||
} | ||
@@ -72,0 +126,0 @@ ) |
# Changelog | ||
## [2.1.0](https://github.com/ButterCMS/buttercms-js/compare/v2.0.3...v2.1.0) (2024-07-29) | ||
### Features | ||
* update response structure to echo schema expected from TS defs and v1 ([1a748fa](https://github.com/ButterCMS/buttercms-js/commit/1a748fabbee60fbfaf547584b18ab31a436b6026)) | ||
## [2.0.3](https://github.com/ButterCMS/buttercms-js/compare/v2.0.2...v2.0.3) (2024-06-14) | ||
@@ -4,0 +11,0 @@ |
@@ -88,4 +88,4 @@ import { | ||
headers, | ||
// use either the above timeout o | ||
// r the explicity AbortController cancelRequest to cancel request | ||
// use either the above timeout or | ||
// the explicit AbortController cancelRequest to cancel request | ||
signal: AbortSignal.any([signal, timeoutSignal]) | ||
@@ -105,3 +105,4 @@ } | ||
params, | ||
type | ||
type, | ||
requestHeaders: butterHeaders | ||
} | ||
@@ -108,0 +109,0 @@ ); |
@@ -45,3 +45,20 @@ /** | ||
// The expected structure is { data, [meta, errors] }. | ||
return await response.json() | ||
const data = await response.json() | ||
return { | ||
data: data, | ||
headers: Object.fromEntries(response.headers.entries()), | ||
status: response.status, | ||
statusText: response.statusText, | ||
config: { | ||
url: response.url, | ||
method: "get", | ||
headers: Object.fromEntries(kwargs.requestHeaders.entries()), | ||
transformRequest: onRequest | ||
? [ onRequest ] : [], | ||
transformResponse: onResponse | ||
? [ onResponse ] : [], | ||
timeout: kwargs.config.timeout | ||
} | ||
} | ||
} |
@@ -15,2 +15,7 @@ import { http, HttpResponse } from 'msw' | ||
const failedSinglePostWithGlobHandler = http.get( | ||
"https://api.buttercms.com/v2/pages/*/about/fake-post-slug/", | ||
() => HttpResponse.error("Failed to fetch") | ||
) | ||
const listPageHandlerError = http.get( | ||
@@ -38,4 +43,5 @@ "https://api.buttercms.com/v2/pages/as/", | ||
failedSinglePostHandler, | ||
failedSinglePostWithGlobHandler, | ||
listPageHandlerError, | ||
listPageHandler, | ||
]; |
@@ -34,2 +34,2 @@ { | ||
} | ||
} | ||
} |
{ | ||
"name": "buttercms", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "ButterCMS API Client", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -23,3 +23,3 @@ # ButterCMS JS client | ||
```html | ||
<script src="https://cdnjs.buttercms.com/buttercms-2.0.3.min.js"></script> | ||
<script src="https://cdnjs.buttercms.com/buttercms-2.1.0.min.js"></script> | ||
``` | ||
@@ -26,0 +26,0 @@ <!-- {x-release-please-end} --> |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Butter=t():e.Butter=t()}(this,()=>(()=>{"use strict";var n={d:(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},e={};n.d(e,{default:()=>function(e,t={}){if(e)return this instanceof r?r:new r(e,t);throw"ButterCMS API token not set"}});const C="2.0.0",o={author:"authors",category:"categories",content:"content",feed:"feeds",page:"pages",post:"posts",tag:"tags"};function P(e,t){const{onError:r,onRequest:n,onResponse:o,...a}=t.config,{auth_token:s,test:c,preview:i,...u}=t.params;r&&r(e,{options:a,params:u,type:t.type})}function k(e,t,r){t={auth_token:t};return r.testMode&&(t.test=1,t.preview=1),{...e,...t}}function a(h,v){t=h;const y=`https://api.buttercms.com/v2/${o[t]}/`,e=h.replace(h[0],h[0].toUpperCase()),{applyRequestUrlForErrorMessages:q,cancelRequest:w,cleanup:E,determineFetchError:b,signal:j}=function(t){let o=new AbortController,r="";function a(e){return r+": "+e}function s(){o=null}return{applyRequestUrlForErrorMessages:function(e){r=t+` (${e})`},cancelRequest:async function(){await o.abort(a("Request cancelled"))},cleanup:s,controller:o,determineFetchError:function(e,t){var r=o.signal.reason&&o.signal.aborted,n="TimeoutError"===e.name;return r?o.signal.reason:n?a(`Request timed out after ${t}ms.`):(s(),e)},signal:o.signal}}(e);var t;async function r(e=y,t={}){const{apiToken:r,...n}=v,o=new Headers({"Content-Type":"application/json","X-Butter-Client":"JS/"+C});"undefined"==typeof window&&o.append("Accept-Encoding","gzip"),q(e);var{config:t,headers:a,params:s}=await async function(e,t,r){const{onError:n,onRequest:o,onResponse:a,...s}=r.config;var c,i;return o?({headers:e,options:c,params:i}=await o(e,{cancelRequest:r.cancelRequest,headers:r.headers,options:s,params:r.params,type:r.type}),{config:{...c,onError:n,onRequest:o,onResponse:a},headers:e,params:k(i,t,c)}):{...r,params:k(r.params,t,s)}}(e,r,{cancelRequest:w,config:n,headers:o,params:t,type:h});try{const v=AbortSignal.timeout(t.timeout),C=await fetch(e+"?"+new URLSearchParams(s),{cache:t.cache,method:"GET",headers:a,signal:AbortSignal.any([j,v])});if(E(),200!==C.status)throw{response:C,config:t,params:s};{var c=C;var i={config:t,params:s,type:h};const{onError:u,onRequest:p,onResponse:l,...f}=i.config,{auth_token:d,test:m,preview:g,...R}=i.params;if(l){const u=c.clone();await l(u,{options:f,params:R,type:i.type})}return await c.json();return await void 0}}catch(e){if(e.response){const C=(await e.response.json())["detail"];return P(C,{config:e.config,params:e.params,type:h}),Promise.reject(new Error(C))}{const C=b(e,t.timeout);return P(C,{config:t,params:s,type:h}),E(),Promise.reject(new Error(C))}}}return{cancelRequest:w,list:async function(e={}){return r(y,e)},retrieve:async function(e,t){return r(y+e+"/",t)},search:async function(e="",t={}){return t.query=e,r(y+"search/",t)}}}const u={Author:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=a("author",e);return{cancelRequest:e,list:t,retrieve:r}},Category:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=a("category",e);return{cancelRequest:e,list:t,retrieve:r}},Content:function(e={}){var{cancelRequest:e,retrieve:t}=a("content",e);return{cancelRequest:e,retrieve:t}},Feed:function(e={}){var{cancelRequest:e,retrieve:t}=a("feed",e);return{cancelRequest:e,retrieve:t}},Page:function(e={}){const{cancelRequest:t,retrieve:n,search:r}=a("page",e);return{cancelRequest:t,list:async(e,t)=>n(e,t),retrieve:async(e,t,r)=>n(e+"/"+t,r),search:r}},Post:function(e={}){var{cancelRequest:e,list:t,retrieve:r,search:n}=a("post",e);return{cancelRequest:e,list:t,retrieve:r,search:n}},Tag:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=a("tag",e);return{cancelRequest:e,list:t,retrieve:r}}};function r(e,t){var r,n,{cache:t="default",onError:o=null,onRequest:a=null,onResponse:s=null,testMode:c=!1,timeout:i=3e3}=t;return{version:C,...(r=u,n={apiToken:e,cache:t,onError:o,onRequest:a,onResponse:s,testMode:c,timeout:i},Object.keys(r).reduce((e,t)=>({...e,[t.toLocaleLowerCase()]:r[t](n)}),{}))}}return e.default})()); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Butter=t():e.Butter=t()}(this,()=>(()=>{"use strict";var n={d:(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},e={};n.d(e,{default:()=>function(e,t={}){if(e)return this instanceof r?r:new r(e,t);throw"ButterCMS API token not set"}});const C="2.0.0",o={author:"authors",category:"categories",content:"content",feed:"feeds",page:"pages",post:"posts",tag:"tags"};function P(e,t){const{onError:r,onRequest:n,onResponse:o,...s}=t.config,{auth_token:a,test:c,preview:i,...u}=t.params;r&&r(e,{options:s,params:u,type:t.type})}function T(e,t,r){t={auth_token:t};return r.testMode&&(t.test=1,t.preview=1),{...e,...t}}function s(R,v){t=R;const y=`https://api.buttercms.com/v2/${o[t]}/`,e=R.replace(R[0],R[0].toUpperCase()),{applyRequestUrlForErrorMessages:q,cancelRequest:w,cleanup:E,determineFetchError:b,signal:j}=function(t){let o=new AbortController,r="";function s(e){return r+": "+e}function a(){o=null}return{applyRequestUrlForErrorMessages:function(e){r=t+` (${e})`},cancelRequest:async function(){await o.abort(s("Request cancelled"))},cleanup:a,controller:o,determineFetchError:function(e,t){var r=o&&o.signal.reason&&o.signal.aborted,n="TimeoutError"===e.name;return r?o.signal.reason:n?s(`Request timed out after ${t}ms.`):(a(),e)},signal:o.signal}}(e);var t;async function r(e=y,t={}){const{apiToken:r,...n}=v,o=new Headers({"Content-Type":"application/json","X-Butter-Client":"JS/"+C});"undefined"==typeof window&&o.append("Accept-Encoding","gzip"),q(e);var{config:t,headers:s,params:a}=await async function(e,t,r){const{onError:n,onRequest:o,onResponse:s,...a}=r.config;var c,i;return o?({headers:e,options:c,params:i}=await o(e,{cancelRequest:r.cancelRequest,headers:r.headers,options:a,params:r.params,type:r.type}),{config:{...c,onError:n,onRequest:o,onResponse:s},headers:e,params:T(i,t,c)}):{...r,params:T(r.params,t,a)}}(e,r,{cancelRequest:w,config:n,headers:o,params:t,type:R});try{const v=AbortSignal.timeout(t.timeout),C=await fetch(e+"?"+new URLSearchParams(a),{cache:t.cache,method:"GET",headers:s,signal:AbortSignal.any([j,v])});if(E(),200!==C.status)throw{response:C,config:t,params:a};{var c=C;var i={config:t,params:a,type:R,requestHeaders:o};const{onError:u,onRequest:p,onResponse:l,...f}=i.config,{auth_token:d,test:m,preview:g,...h}=i.params;if(l){const u=c.clone();await l(u,{options:f,params:h,type:i.type})}return await{data:await c.json(),headers:Object.fromEntries(c.headers.entries()),status:c.status,statusText:c.statusText,config:{url:c.url,method:"get",headers:Object.fromEntries(i.requestHeaders.entries()),transformRequest:p?[p]:[],transformResponse:l?[l]:[],timeout:i.config.timeout}};return await void 0}}catch(e){if(e.response){const C=(await e.response.json())["detail"];return P(C,{config:e.config,params:e.params,type:R}),Promise.reject(new Error(C))}{const C=b(e,t.timeout);return P(C,{config:t,params:a,type:R}),E(),Promise.reject(new Error(C))}}}return{cancelRequest:w,list:async function(e={}){return r(y,e)},retrieve:async function(e,t){return r(y+e+"/",t)},search:async function(e="",t={}){return t.query=e,r(y+"search/",t)}}}const u={Author:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=s("author",e);return{cancelRequest:e,list:t,retrieve:r}},Category:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=s("category",e);return{cancelRequest:e,list:t,retrieve:r}},Content:function(e={}){var{cancelRequest:e,retrieve:t}=s("content",e);return{cancelRequest:e,retrieve:t}},Feed:function(e={}){var{cancelRequest:e,retrieve:t}=s("feed",e);return{cancelRequest:e,retrieve:t}},Page:function(e={}){const{cancelRequest:t,retrieve:n,search:r}=s("page",e);return{cancelRequest:t,list:async(e,t)=>n(e,t),retrieve:async(e,t,r)=>n(e+"/"+t,r),search:r}},Post:function(e={}){var{cancelRequest:e,list:t,retrieve:r,search:n}=s("post",e);return{cancelRequest:e,list:t,retrieve:r,search:n}},Tag:function(e={}){var{cancelRequest:e,list:t,retrieve:r}=s("tag",e);return{cancelRequest:e,list:t,retrieve:r}}};function r(e,t){var r,n,{cache:t="default",onError:o=null,onRequest:s=null,onResponse:a=null,testMode:c=!1,timeout:i=3e3}=t;return{version:C,...(r=u,n={apiToken:e,cache:t,onError:o,onRequest:s,onResponse:a,testMode:c,timeout:i},Object.keys(r).reduce((e,t)=>({...e,[t.toLocaleLowerCase()]:r[t](n)}),{}))}}return e.default})()); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82102
1513