picosanity
Advanced tools
Comparing version 1.0.5 to 2.0.0
@@ -1,4 +0,4 @@ | ||
'use strict'; | ||
"use strict"; | ||
var Client = require('./client'); | ||
const Client = require('./client'); | ||
@@ -5,0 +5,0 @@ module.exports = function (cfg) { |
@@ -1,6 +0,6 @@ | ||
'use strict'; | ||
"use strict"; | ||
var enc = encodeURIComponent; | ||
var apiHost = 'api.sanity.io'; | ||
var cdnHost = 'apicdn.sanity.io'; | ||
const enc = encodeURIComponent; | ||
const apiHost = 'api.sanity.io'; | ||
const cdnHost = 'apicdn.sanity.io'; | ||
@@ -12,26 +12,34 @@ function PicoSanity(config, fetcher) { | ||
this.cfg = config; | ||
this.clientConfig = config; | ||
this.fetcher = fetcher; | ||
} | ||
;['clone', 'config', 'create', 'createIfNotExists', 'createOrReplace', 'delete', 'listen', 'mutate', 'patch', 'transaction'].forEach(function (method) { | ||
; | ||
['clone', 'create', 'createIfNotExists', 'createOrReplace', 'delete', 'listen', 'mutate', 'patch', 'transaction'].forEach(method => { | ||
PicoSanity.prototype[method] = ni(method); | ||
}); | ||
PicoSanity.prototype.config = function (cfg) { | ||
if (cfg) { | ||
this.clientConfig = Object.assign({}, this.clientConfig, cfg); | ||
return this; | ||
} | ||
return this.clientConfig; | ||
}; | ||
PicoSanity.prototype.fetch = function (query, params) { | ||
var cfg = this.cfg; | ||
var host = cfg.useCdn ? cdnHost : apiHost; | ||
var opts = { credentials: cfg.withCredentials ? 'include' : 'omit' }; | ||
var qs = getQs(query, params); | ||
return this.fetcher('https://' + cfg.projectId + '.' + host + '/v1/data/query/' + cfg.dataset + qs, opts).then(function (res) { | ||
return res.json(); | ||
}).then(function (res) { | ||
return res.result; | ||
}); | ||
const cfg = this.clientConfig; | ||
const host = cfg.useCdn ? cdnHost : apiHost; | ||
const opts = { | ||
credentials: cfg.withCredentials ? 'include' : 'omit' | ||
}; | ||
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); | ||
}; | ||
function getQs(query, params) { | ||
var baseQs = '?query=' + enc(query); | ||
return Object.keys(params || {}).reduce(function (current, param) { | ||
return current + '&' + enc('$' + param) + '=' + enc(JSON.stringify(params[param])); | ||
const baseQs = `?query=${enc(query)}`; | ||
return Object.keys(params || {}).reduce((current, param) => { | ||
return `${current}&${enc(`$${param}`)}=${enc(JSON.stringify(params[param]))}`; | ||
}, baseQs); | ||
@@ -41,4 +49,4 @@ } | ||
function ni(method) { | ||
return function () { | ||
throw new Error('Method "' + method + '" not implemented, use @sanity/client'); | ||
return () => { | ||
throw new Error(`Method "${method}" not implemented, use @sanity/client`); | ||
}; | ||
@@ -45,0 +53,0 @@ } |
@@ -1,8 +0,9 @@ | ||
'use strict'; | ||
"use strict"; | ||
var fetch = require('node-fetch'); | ||
var Client = require('./client'); | ||
const fetch = require('node-fetch'); | ||
const Client = require('./client'); | ||
module.exports = function (cfg) { | ||
return new Client(cfg, fetch); | ||
}; |
{ | ||
"name": "picosanity", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "Tiny Sanity client alternative should you only need to do queries", | ||
@@ -35,16 +35,16 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"node-fetch": "^2.1.2" | ||
"node-fetch": "^2.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-config-sanity": "^0.132.2", | ||
"jest": "^23.1.0", | ||
"prettier": "^1.13.5", | ||
"rimraf": "^2.6.2", | ||
"webpack": "^4.12.0", | ||
"webpack-command": "^0.2.1" | ||
"@babel/cli": "^7.6.3", | ||
"@babel/preset-env": "^7.6.3", | ||
"eslint": "^6.5.1", | ||
"eslint-config-prettier": "^6.4.0", | ||
"eslint-config-sanity": "^0.142.6", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.18.2", | ||
"rimraf": "^3.0.0", | ||
"webpack": "^4.41.0", | ||
"webpack-command": "^0.5.0" | ||
} | ||
} |
@@ -11,3 +11,2 @@ # picosanity | ||
- Modern browsers (Edge >= 14, Chrome, Safari, Firefox etc) | ||
- IE if [Promise](https://github.com/taylorhakes/promise-polyfill) and [fetch](https://github.com/github/fetch) is polyfilled | ||
@@ -14,0 +13,0 @@ ## Installation |
const Client = require('./client') | ||
module.exports = cfg => new Client(cfg, window.fetch) | ||
module.exports = function(cfg) { | ||
return new Client(cfg, window.fetch) | ||
} |
@@ -10,3 +10,3 @@ const enc = encodeURIComponent | ||
this.cfg = config | ||
this.clientConfig = config | ||
this.fetcher = fetcher | ||
@@ -17,3 +17,2 @@ } | ||
'clone', | ||
'config', | ||
'create', | ||
@@ -31,4 +30,13 @@ 'createIfNotExists', | ||
PicoSanity.prototype.config = function(cfg) { | ||
if (cfg) { | ||
this.clientConfig = Object.assign({}, this.clientConfig, cfg) | ||
return this | ||
} | ||
return this.clientConfig | ||
} | ||
PicoSanity.prototype.fetch = function(query, params) { | ||
const cfg = this.cfg | ||
const cfg = this.clientConfig | ||
const host = cfg.useCdn ? cdnHost : apiHost | ||
@@ -35,0 +43,0 @@ const opts = {credentials: cfg.withCredentials ? 'include' : 'omit'} |
const fetch = require('node-fetch') | ||
const Client = require('./client') | ||
module.exports = cfg => new Client(cfg, fetch) | ||
module.exports = function(cfg) { | ||
return new Client(cfg, fetch) | ||
} |
@@ -1,3 +0,5 @@ | ||
const Client = require('../src') | ||
const Client = require('../lib') | ||
const config = {projectId: '89qx0zd4', dataset: 'sweets', useCdn: true} | ||
const expectedDoc = { | ||
@@ -11,3 +13,2 @@ _id: '1ba26a25-7f35-4d24-804e-09cc76a0cd73', | ||
'clone', | ||
'config', | ||
'create', | ||
@@ -23,4 +24,19 @@ 'createIfNotExists', | ||
test('can construct with `new`', () => { | ||
const client = new Client(config) | ||
expect(client.config()).toMatchObject(config) | ||
}) | ||
test('can construct without `new`', () => { | ||
const client = Client(config) | ||
expect(client.config()).toMatchObject(config) | ||
}) | ||
test('sets config to `clientConfig` for @sanity/client compat', () => { | ||
const client = new Client(config) | ||
expect(client.clientConfig).toMatchObject(config) | ||
}) | ||
test('can query', () => { | ||
const client = new Client({projectId: '89qx0zd4', dataset: 'sweets', useCdn: true}) | ||
const client = new Client(config) | ||
return expect( | ||
@@ -32,3 +48,3 @@ client.fetch('*[_id == "1ba26a25-7f35-4d24-804e-09cc76a0cd73"][0]') | ||
test('can query with params', () => { | ||
const client = new Client({projectId: '89qx0zd4', dataset: 'sweets', useCdn: true}) | ||
const client = new Client(config) | ||
return expect( | ||
@@ -39,11 +55,11 @@ client.fetch('*[_id == $id][0]', {id: '1ba26a25-7f35-4d24-804e-09cc76a0cd73'}) | ||
test('can instantiate client without `new` keyword', () => { | ||
const client = Client({projectId: '89qx0zd4', dataset: 'sweets', useCdn: true}) | ||
return expect( | ||
client.fetch('*[_id == "1ba26a25-7f35-4d24-804e-09cc76a0cd73"][0]') | ||
).resolves.toMatchObject(expectedDoc) | ||
test('can reconfigure with .config(newConfig)', () => { | ||
const client = new Client(config) | ||
expect(client.config()).toMatchObject(config) | ||
expect(client.config({projectId: 'abc123'})).toBe(client) | ||
expect(client.config()).toMatchObject({...config, projectId: 'abc123'}) | ||
}) | ||
describe('throws when using unimplemented methods', () => { | ||
const client = new Client({projectId: '89qx0zd4', dataset: 'sweets', useCdn: true}) | ||
const client = new Client(config) | ||
@@ -50,0 +66,0 @@ notImplemented.forEach(method => { |
@@ -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.PicoSanity=t():e.PicoSanity=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){"use strict";var r=encodeURIComponent;function o(e,t){if(!(this instanceof o))return new o(e);this.cfg=e,this.fetcher=t}["clone","config","create","createIfNotExists","createOrReplace","delete","listen","mutate","patch","transaction"].forEach(function(e){o.prototype[e]=function(e){return function(){throw new Error('Method "'+e+'" not implemented, use @sanity/client')}}(e)}),o.prototype.fetch=function(e,t){var n=this.cfg,o=n.useCdn?"apicdn.sanity.io":"api.sanity.io",i={credentials:n.withCredentials?"include":"omit"},u=function(e,t){var n="?query="+r(e);return Object.keys(t||{}).reduce(function(e,n){return e+"&"+r("$"+n)+"="+r(JSON.stringify(t[n]))},n)}(e,t);return this.fetcher("https://"+n.projectId+"."+o+"/v1/data/query/"+n.dataset+u,i).then(function(e){return e.json()}).then(function(e){return e.result})},e.exports=o},function(e,t,n){"use strict";var r=n(0);e.exports=function(e){return new r(e,window.fetch)}}])}); | ||
!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()}(window,(function(){return function(t){var e={};function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(o,r,function(e){return t[e]}.bind(null,r));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";const o=n(1);t.exports=function(t){return new o(t,window.fetch)}},function(t,e,n){"use strict";const o=encodeURIComponent;function r(t,e){if(!(this instanceof r))return new r(t);this.clientConfig=t,this.fetcher=e}["clone","create","createIfNotExists","createOrReplace","delete","listen","mutate","patch","transaction"].forEach(t=>{r.prototype[t]=function(t){return()=>{throw new Error(`Method "${t}" not implemented, use @sanity/client`)}}(t)}),r.prototype.config=function(t){return t?(this.clientConfig=Object.assign({},this.clientConfig,t),this):this.clientConfig},r.prototype.fetch=function(t,e){const n=this.clientConfig,r=n.useCdn?"apicdn.sanity.io":"api.sanity.io",i={credentials:n.withCredentials?"include":"omit"},c=function(t,e){const n=`?query=${o(t)}`;return Object.keys(e||{}).reduce((t,n)=>`${t}&${o(`$${n}`)}=${o(JSON.stringify(e[n]))}`,n)}(t,e);return this.fetcher(`https://${n.projectId}.${r}/v1/data/query/${n.dataset}${c}`,i).then(t=>t.json()).then(t=>t.result)},t.exports=r}])})); |
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
11982
20
175
43
Updatednode-fetch@^2.6.0