picosanity
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -5,2 +5,12 @@ # Change Log | ||
## 2.3.0 | ||
### Added | ||
- Support `apiVersion` in config | ||
### Changed | ||
- Improve error handling | ||
## 2.2.0 | ||
@@ -10,3 +20,3 @@ | ||
- Compile for IE11 compatability (if global fetch polyfill is present) | ||
- Compile for IE11 compatibility (if global fetch polyfill is present) | ||
@@ -13,0 +23,0 @@ ## 2.1.0 |
@@ -7,2 +7,3 @@ /// <reference lib="dom" /> | ||
dataset: string | ||
apiVersion?: string | ||
token?: string | ||
@@ -9,0 +10,0 @@ useCdn?: boolean |
@@ -36,2 +36,3 @@ "use strict"; | ||
var host = !cfg.useCdn || cfg.token ? apiHost : cdnHost; | ||
var version = cfg.apiVersion ? "v".concat(cfg.apiVersion.replace(/^v/, '')) : 'v1'; | ||
var opts = { | ||
@@ -42,9 +43,23 @@ credentials: cfg.withCredentials ? 'include' : 'omit', | ||
var qs = getQs(query, params); | ||
return this.fetcher("https://".concat(cfg.projectId, ".").concat(host, "/v1/data/query/").concat(cfg.dataset).concat(qs), opts).then(function (res) { | ||
return res.json(); | ||
}).then(function (res) { | ||
return res.result; | ||
}); | ||
return this.fetcher("https://".concat(cfg.projectId, ".").concat(host, "/").concat(version, "/data/query/").concat(cfg.dataset).concat(qs), opts).then(parse); | ||
}; | ||
function parse(res) { | ||
return res.json().then(function (json) { | ||
if (res.status < 400) { | ||
return json.result; | ||
} | ||
var msg = res.url; | ||
var type = res.statusText; | ||
if (json.error && json.error.description) { | ||
msg = json.error.description; | ||
type = json.error.type || type; | ||
} | ||
throw new Error("HTTP ".concat(res.status, " ").concat(type, ": ").concat(msg)); | ||
}); | ||
} | ||
function getQs(query, params) { | ||
@@ -51,0 +66,0 @@ var baseQs = "?query=".concat(enc(query)); |
{ | ||
"name": "picosanity", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Tiny Sanity client alternative should you only need to do queries", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# picosanity | ||
[![npm version](https://img.shields.io/npm/v/picosanity.svg?style=flat-square)](http://browsenpm.org/package/picosanity)[![Build Status](https://img.shields.io/travis/rexxars/picosanity/main.svg?style=flat-square)](https://travis-ci.org/rexxars/picosanity)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/picosanity?style=flat-square)](https://bundlephobia.com/result?p=picosanity) | ||
[![npm version](https://img.shields.io/npm/v/picosanity.svg?style=flat-square)](http://browsenpm.org/package/picosanity)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/picosanity?style=flat-square)](https://bundlephobia.com/result?p=picosanity) | ||
@@ -26,2 +26,3 @@ Tiny Sanity client alternative, if you only need to do queries and only need to support modern browsers. | ||
dataset: 'myDataset', | ||
apiVersion: '2021-03-25', // use a UTC date string | ||
useCdn: true, | ||
@@ -28,0 +29,0 @@ }) |
@@ -41,9 +41,28 @@ const enc = encodeURIComponent | ||
const host = !cfg.useCdn || cfg.token ? apiHost : cdnHost | ||
const version = cfg.apiVersion ? `v${cfg.apiVersion.replace(/^v/, '')}` : 'v1' | ||
const opts = {credentials: cfg.withCredentials ? 'include' : 'omit', headers} | ||
const qs = getQs(query, params) | ||
return this.fetcher(`https://${cfg.projectId}.${host}/v1/data/query/${cfg.dataset}${qs}`, opts) | ||
.then((res) => res.json()) | ||
.then((res) => res.result) | ||
return this.fetcher( | ||
`https://${cfg.projectId}.${host}/${version}/data/query/${cfg.dataset}${qs}`, | ||
opts | ||
).then(parse) | ||
} | ||
function parse(res) { | ||
return res.json().then((json) => { | ||
if (res.status < 400) { | ||
return json.result | ||
} | ||
let msg = res.url | ||
let type = res.statusText | ||
if (json.error && json.error.description) { | ||
msg = json.error.description | ||
type = json.error.type || type | ||
} | ||
throw new Error(`HTTP ${res.status} ${type}: ${msg}`) | ||
}) | ||
} | ||
function getQs(query, params) { | ||
@@ -50,0 +69,0 @@ const baseQs = `?query=${enc(query)}` |
@@ -81,2 +81,29 @@ const Client = require('../src') | ||
test('can specify api version', (done) => { | ||
const client = new Client({...config, apiVersion: 'v2021-03-25'}) | ||
client | ||
.fetch('*[_id == $id][0] { _id, _type, title, "description": pt::text(nope) }', { | ||
id: '1ba26a25-7f35-4d24-804e-09cc76a0cd73', | ||
}) | ||
.then((res) => { | ||
expect(res).toEqual({...expectedDoc, description: null}) | ||
done() | ||
}) | ||
}) | ||
test('throws on syntax errors', (done) => { | ||
const client = new Client({...config, apiVersion: 'v2021-03-25'}) | ||
client.fetch('*[_id == "nope"][0] { _id, ').then( | ||
() => { | ||
throw new Error('Should not succeed') | ||
}, | ||
(err) => { | ||
expect(err.message).toEqual(`HTTP 400 queryParseError: expected '}' following object body`) | ||
done() | ||
} | ||
) | ||
}) | ||
describe('throws when using unimplemented methods', () => { | ||
@@ -83,0 +110,0 @@ const client = new Client(config) |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.PicoSanity=e():t.PicoSanity=e()}(self,(function(){return(()=>{"use strict";var t={693:(t,e,n)=>{var o=n(26);t.exports=function(t){return new o(t,(function(t,e){return window.fetch(t,e)}))}},26:t=>{var e=encodeURIComponent;function n(t,e){if(!(this instanceof n))return new n(t);this.clientConfig=t,this.fetcher=e}["clone","create","createIfNotExists","createOrReplace","delete","listen","mutate","patch","transaction"].forEach((function(t){n.prototype[t]=function(t){return function(){throw new Error('Method "'.concat(t,'" not implemented, use @sanity/client'))}}(t)})),n.prototype.config=function(t){return t?(this.clientConfig=Object.assign({},this.clientConfig,t),this):this.clientConfig},n.prototype.fetch=function(t,n){var o=this.clientConfig,c=o.token?{Authorization:"Bearer ".concat(o.token)}:void 0,r=!o.useCdn||o.token?"api.sanity.io":"apicdn.sanity.io",i={credentials:o.withCredentials?"include":"omit",headers:c},a=function(t,n){var o="?query=".concat(e(t));return Object.keys(n||{}).reduce((function(t,o){return"".concat(t,"&").concat(e("$".concat(o)),"=").concat(e(JSON.stringify(n[o])))}),o)}(t,n);return this.fetcher("https://".concat(o.projectId,".").concat(r,"/v1/data/query/").concat(o.dataset).concat(a),i).then((function(t){return t.json()})).then((function(t){return t.result}))},t.exports=n}},e={};return function n(o){if(e[o])return e[o].exports;var c=e[o]={exports:{}};return t[o](c,c.exports,n),c.exports}(693)})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.PicoSanity=e():t.PicoSanity=e()}(self,(function(){return(()=>{"use strict";var t={693:(t,e,n)=>{var o=n(26);t.exports=function(t){return new o(t,(function(t,e){return window.fetch(t,e)}))}},26:t=>{var e=encodeURIComponent;function n(t,e){if(!(this instanceof n))return new n(t);this.clientConfig=t,this.fetcher=e}function o(t){return t.json().then((function(e){if(t.status<400)return e.result;var n=t.url,o=t.statusText;throw e.error&&e.error.description&&(n=e.error.description,o=e.error.type||o),new Error("HTTP ".concat(t.status," ").concat(o,": ").concat(n))}))}["clone","create","createIfNotExists","createOrReplace","delete","listen","mutate","patch","transaction"].forEach((function(t){n.prototype[t]=function(t){return function(){throw new Error('Method "'.concat(t,'" not implemented, use @sanity/client'))}}(t)})),n.prototype.config=function(t){return t?(this.clientConfig=Object.assign({},this.clientConfig,t),this):this.clientConfig},n.prototype.fetch=function(t,n){var r=this.clientConfig,c=r.token?{Authorization:"Bearer ".concat(r.token)}:void 0,i=!r.useCdn||r.token?"api.sanity.io":"apicdn.sanity.io",a=r.apiVersion?"v".concat(r.apiVersion.replace(/^v/,"")):"v1",s={credentials:r.withCredentials?"include":"omit",headers:c},u=function(t,n){var o="?query=".concat(e(t));return Object.keys(n||{}).reduce((function(t,o){return"".concat(t,"&").concat(e("$".concat(o)),"=").concat(e(JSON.stringify(n[o])))}),o)}(t,n);return this.fetcher("https://".concat(r.projectId,".").concat(i,"/").concat(a,"/data/query/").concat(r.dataset).concat(u),s).then(o)},t.exports=n}},e={};return function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{}};return t[o](r,r.exports,n),r.exports}(693)})()})); |
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
17006
312
44