-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontentful-js-client.min.js
1 lines (1 loc) · 1.98 KB
/
contentful-js-client.min.js
1
/*! license Contentful JS Client v0.2.2. Copyright 2017 Troy Forster https://github.com/tforster/contentful-js-client */'use strict';class Contentful{constructor(a){this.space=a.space,this.access_token=a.access_token}_nodeRequest(a,b){let c=this;return new Promise((d,f)=>{const e=require('https'),{URL:g}=require('url'),h=new g(a),i={hostname:h.hostname,port:443,path:h.pathname+h.search,method:'GET',headers:{"Content-Type":'application/json'}};let j='';const k=e.request(i,(a)=>{a.setEncoding('utf8'),a.on('data',(a)=>{j+=a}),a.on('end',()=>{b&&(j=c._denormalize(JSON.parse(j))),d(j)})});k.on('error',(a)=>{console.error(`problem with request: ${a.message}`),f(a)}),k.write(''),k.end()})}_windowFetch(a,b){let c=this;return new Promise((d,e)=>{const f=new Request(a,{headers:new Headers({"Content-Type":'application/json'})});return fetch(f).then((a)=>{return a.json()}).then((a)=>{b&&(a=c._denormalize(a)),d(a)}).catch((a)=>{console.error(a),e(a)})})}_denormalize(a){let b=(b)=>{if(typeof('object'===b)&&b.sys&&'Link'===b.sys.type){let d=a.includes[b.sys.linkType].filter((a)=>{return a.sys.id===b.sys.id});return c(d[0])}return b},c=(a)=>{let c={};for(let d in a.fields){let e=a.fields[d];c[d]=Array.isArray(e)?e.map((a)=>{return b(a)}):b(e)}return c};return a&&a.items?a.items.map((a)=>{return c(a)}):(console.warn('obj.items not found?'),[])}_query(a){let b=this;a.include=a.include||10,a.denormalize=a.denormalize||!0,a.raw=a.raw||!1,b.okDenormalize=a.denormalize&&!a.raw;let c=`https://cdn.contentful.com/spaces/${b.space}/entries?access_token=${b.access_token}&content_type=${a.content_type}&include=${a.include}`;if(a.fields)for(let b in a.fields)c+=`&fields.${b}=${a.fields[b]}`;return a.order&&(c+=`&order=${a.order}`),'undefined'==typeof window?b._nodeRequest(c,!0):b._windowFetch(c,!0)}getEntries(a){let b=this;return new Promise((c,d)=>{b._query(a).then((a)=>{return c(a)}).catch((a)=>{return console.error('contentful.getEntries failed:',a),d(a)})})}}'object'==typeof module?module.exports=Contentful:null;