picosanity
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -5,2 +5,8 @@ # Change Log | ||
## 2.1.0 | ||
### Added | ||
- Support `token` in constructor | ||
## 2.0.3 | ||
@@ -12,3 +18,2 @@ | ||
## 2.0.0 | ||
@@ -15,0 +20,0 @@ |
@@ -32,5 +32,9 @@ "use strict"; | ||
const cfg = this.clientConfig; | ||
const host = cfg.useCdn ? cdnHost : apiHost; | ||
const headers = cfg.token ? { | ||
Authorization: `Bearer ${cfg.token}` | ||
} : undefined; | ||
const host = !cfg.useCdn || cfg.token ? apiHost : cdnHost; | ||
const opts = { | ||
credentials: cfg.withCredentials ? 'include' : 'omit' | ||
credentials: cfg.withCredentials ? 'include' : 'omit', | ||
headers | ||
}; | ||
@@ -37,0 +41,0 @@ const qs = getQs(query, params); |
{ | ||
"name": "picosanity", | ||
"version": "2.0.3", | ||
"version": "2.1.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/master.svg?style=flat-square)](https://travis-ci.org/rexxars/picosanity)![npm bundle size](https://img.shields.io/bundlephobia/minzip/picosanity?style=flat-square) | ||
[![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) | ||
@@ -9,3 +9,3 @@ Tiny Sanity client alternative, if you only need to do queries and only need to support modern browsers. | ||
- Node.js >= 8 | ||
- Node.js >= 10 | ||
- Modern browsers (Edge >= 14, Chrome, Safari, Firefox etc) | ||
@@ -22,3 +22,3 @@ | ||
```js | ||
const PicoSanity = require('picosanity') | ||
import PicoSanity from 'picosanity' | ||
@@ -28,3 +28,3 @@ const client = new PicoSanity({ | ||
dataset: 'myDataset', | ||
useCdn: true | ||
useCdn: true, | ||
}) | ||
@@ -34,4 +34,4 @@ | ||
.fetch('*[_type == $someType]', {someType: 'article'}) | ||
.then(articles => console.log(articles)) | ||
.catch(err => console.error('Oh noes: %s', err.message)) | ||
.then((articles) => console.log(articles)) | ||
.catch((err) => console.error('Oh noes: %s', err.message)) | ||
``` | ||
@@ -38,0 +38,0 @@ |
const Client = require('./client') | ||
module.exports = function(cfg) { | ||
module.exports = function (cfg) { | ||
return new Client(cfg, (input, init) => window.fetch(input, init)) | ||
} |
@@ -23,8 +23,8 @@ const enc = encodeURIComponent | ||
'patch', | ||
'transaction' | ||
].forEach(method => { | ||
'transaction', | ||
].forEach((method) => { | ||
PicoSanity.prototype[method] = ni(method) | ||
}) | ||
PicoSanity.prototype.config = function(cfg) { | ||
PicoSanity.prototype.config = function (cfg) { | ||
if (cfg) { | ||
@@ -38,10 +38,11 @@ this.clientConfig = Object.assign({}, this.clientConfig, cfg) | ||
PicoSanity.prototype.fetch = function(query, params) { | ||
PicoSanity.prototype.fetch = function (query, params) { | ||
const cfg = this.clientConfig | ||
const host = cfg.useCdn ? cdnHost : apiHost | ||
const opts = {credentials: cfg.withCredentials ? 'include' : 'omit'} | ||
const headers = cfg.token ? {Authorization: `Bearer ${cfg.token}`} : undefined | ||
const host = !cfg.useCdn || cfg.token ? apiHost : cdnHost | ||
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) | ||
.then((res) => res.json()) | ||
.then((res) => res.result) | ||
} | ||
@@ -48,0 +49,0 @@ |
const fetch = require('node-fetch') | ||
const Client = require('./client') | ||
module.exports = function(cfg) { | ||
module.exports = function (cfg) { | ||
return new Client(cfg, fetch) | ||
} |
const Client = require('../src') | ||
const config = {projectId: '89qx0zd4', dataset: 'sweets', useCdn: true} | ||
const token = | ||
'sk6oecvddLqAa9od7KAk90zxCegaNI4jzEPQBZZPfq66BvEfRwRG4KvExnWtKpspQD601VNypC3RQTNySDT8HRtBzqOQ8QTByLmt8dQAIU8kkna9KmnQctri1u7nVDSq0vkkKEvzHgRolNRjJ2sSvddGHx0TKEK2I9gbteKOV50IbRXcWI5c' | ||
@@ -8,5 +10,11 @@ const expectedDoc = { | ||
_type: 'category', | ||
title: 'Japanese' | ||
title: 'Japanese', | ||
} | ||
const expectedDraft = { | ||
...expectedDoc, | ||
_id: `drafts.${expectedDoc._id}`, | ||
title: 'Modified Japanese', | ||
} | ||
const notImplemented = [ | ||
@@ -21,3 +29,3 @@ 'clone', | ||
'patch', | ||
'transaction' | ||
'transaction', | ||
] | ||
@@ -54,2 +62,13 @@ | ||
test('can query with token', async () => { | ||
const client = new Client(config) | ||
const readClient = new Client({...config, token}) | ||
expect( | ||
await client.fetch('*[_id == $id][0]', {id: 'drafts.1ba26a25-7f35-4d24-804e-09cc76a0cd73'}) | ||
).toBe(null) | ||
expect( | ||
await readClient.fetch('*[_id == $id][0]', {id: 'drafts.1ba26a25-7f35-4d24-804e-09cc76a0cd73'}) | ||
).toMatchObject(expectedDraft) | ||
}) | ||
test('can reconfigure with .config(newConfig)', () => { | ||
@@ -65,5 +84,5 @@ const client = new Client(config) | ||
notImplemented.forEach(method => { | ||
notImplemented.forEach((method) => { | ||
test(method, () => expect(client[method]).toThrow(/not implemented/i)) | ||
}) | ||
}) |
@@ -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)=>{const o=n(26);t.exports=function(t){return new o(t,((t,e)=>window.fetch(t,e)))}},26:t=>{const 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((t=>{n.prototype[t]=function(t){return()=>{throw new Error(`Method "${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){const o=this.clientConfig,i=o.useCdn?"apicdn.sanity.io":"api.sanity.io",r={credentials:o.withCredentials?"include":"omit"},c=function(t,n){const o="?query="+e(t);return Object.keys(n||{}).reduce(((t,o)=>`${t}&${e("$"+o)}=${e(JSON.stringify(n[o]))}`),o)}(t,n);return this.fetcher(`https://${o.projectId}.${i}/v1/data/query/${o.dataset}${c}`,r).then((t=>t.json())).then((t=>t.result))},t.exports=n}},e={};return function n(o){if(e[o])return e[o].exports;var i=e[o]={exports:{}};return t[o](i,i.exports,n),i.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)=>{const o=n(26);t.exports=function(t){return new o(t,((t,e)=>window.fetch(t,e)))}},26:t=>{const 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((t=>{n.prototype[t]=function(t){return()=>{throw new Error(`Method "${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){const o=this.clientConfig,i=o.token?{Authorization:"Bearer "+o.token}:void 0,r=!o.useCdn||o.token?"api.sanity.io":"apicdn.sanity.io",c={credentials:o.withCredentials?"include":"omit",headers:i},s=function(t,n){const o="?query="+e(t);return Object.keys(n||{}).reduce(((t,o)=>`${t}&${e("$"+o)}=${e(JSON.stringify(n[o]))}`),o)}(t,n);return this.fetcher(`https://${o.projectId}.${r}/v1/data/query/${o.dataset}${s}`,c).then((t=>t.json())).then((t=>t.result))},t.exports=n}},e={};return function n(o){if(e[o])return e[o].exports;var i=e[o]={exports:{}};return t[o](i,i.exports,n),i.exports}(693)})()})); |
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
13846
243