Socket
Socket
Sign inDemoInstall

picosanity

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

picosanity - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

10

CHANGELOG.md

@@ -5,2 +5,12 @@ # Change Log

## 3.0.1
### Added
- Support large queries by automatically switching to non-CDN API and using HTTP POST method.
### Changed
- Improved error handling on non-JSON HTTP responses
## 3.0.0

@@ -7,0 +17,0 @@

67

lib/client.js
"use strict";
var enc = encodeURIComponent;
var has = {}.hasOwnProperty;
var apiHost = 'api.sanity.io';

@@ -32,16 +33,33 @@ var cdnHost = 'apicdn.sanity.io';

var cfg = this.clientConfig;
var headers = cfg.token ? {
var version = cfg.apiVersion ? "v".concat(cfg.apiVersion.replace(/^v/, '')) : 'v1';
var qs = getQs(query, params);
var usePost = qs.length > 11264;
var auth = cfg.token ? {
Authorization: "Bearer ".concat(cfg.token)
} : undefined;
var host = !cfg.useCdn || cfg.token ? apiHost : cdnHost;
var version = cfg.apiVersion ? "v".concat(cfg.apiVersion.replace(/^v/, '')) : 'v1';
var type = usePost ? {
'content-type': 'application/json'
} : undefined;
var headers = Object.assign({}, auth, type);
var host = !cfg.useCdn || cfg.token || usePost ? apiHost : cdnHost;
var opts = {
credentials: cfg.withCredentials ? 'include' : 'omit',
headers: headers
headers: headers,
method: usePost ? 'POST' : 'GET',
body: usePost ? JSON.stringify({
query: query,
params: params
}) : undefined
};
var qs = getQs(query, params);
return this.fetcher("https://".concat(cfg.projectId, ".").concat(host, "/").concat(version, "/data/query/").concat(cfg.dataset).concat(qs), opts).then(parse);
var url = "https://".concat(cfg.projectId, ".").concat(host, "/").concat(version, "/data/query/").concat(cfg.dataset);
return this.fetcher("".concat(url).concat(usePost ? '' : qs), opts).then(parse);
};
function parse(res) {
var contentType = res.headers.get('content-type') || '';
if (!contentType.includes('json')) {
throw getError(res);
}
return res.json().then(function (json) {

@@ -52,19 +70,32 @@ if (res.status < 400) {

var msg = res.url;
var type = res.statusText;
throw getError(res, json);
});
}
if (json.error && json.error.description) {
msg = json.error.description;
type = json.error.type || type;
}
function getError(res, json) {
var msg = res.url;
var type = res.statusText;
throw new Error("HTTP ".concat(res.status, " ").concat(type, ": ").concat(msg));
});
if (json && json.error && json.error.description) {
msg = json.error.description;
type = json.error.type || type;
}
return new Error("HTTP ".concat(res.status, " ").concat(type, ": ").concat(msg));
}
function getQs(query, params) {
var baseQs = "?query=".concat(enc(query));
return Object.keys(params || {}).reduce(function (current, param) {
return "".concat(current, "&").concat(enc("$".concat(param)), "=").concat(enc(JSON.stringify(params[param])));
}, baseQs);
var qs = "?query=".concat(enc(query));
if (!params) {
return qs;
}
for (var param in params) {
if (has.call(params, param)) {
qs += "&".concat(enc("$".concat(param)), "=").concat(enc(JSON.stringify(params[param])));
}
}
return qs;
}

@@ -71,0 +102,0 @@

{
"name": "picosanity",
"version": "3.0.0",
"version": "3.0.1",
"description": "Tiny Sanity client alternative should you only need to do queries",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

const enc = encodeURIComponent
const has = {}.hasOwnProperty
const apiHost = 'api.sanity.io'

@@ -39,14 +40,27 @@ const cdnHost = 'apicdn.sanity.io'

const cfg = this.clientConfig
const headers = cfg.token ? {Authorization: `Bearer ${cfg.token}`} : undefined
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}/${version}/data/query/${cfg.dataset}${qs}`,
opts
).then(parse)
const usePost = qs.length > 11264
const auth = cfg.token ? {Authorization: `Bearer ${cfg.token}`} : undefined
const type = usePost ? {'content-type': 'application/json'} : undefined
const headers = Object.assign({}, auth, type)
const host = !cfg.useCdn || cfg.token || usePost ? apiHost : cdnHost
const opts = {
credentials: cfg.withCredentials ? 'include' : 'omit',
headers,
method: usePost ? 'POST' : 'GET',
body: usePost ? JSON.stringify({query, params}) : undefined,
}
const url = `https://${cfg.projectId}.${host}/${version}/data/query/${cfg.dataset}`
return this.fetcher(`${url}${usePost ? '' : qs}`, opts).then(parse)
}
function parse(res) {
const contentType = res.headers.get('content-type') || ''
if (!contentType.includes('json')) {
throw getError(res)
}
return res.json().then((json) => {

@@ -57,18 +71,30 @@ if (res.status < 400) {

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}`)
throw getError(res, json)
})
}
function getError(res, json) {
let msg = res.url
let type = res.statusText
if (json && json.error && json.error.description) {
msg = json.error.description
type = json.error.type || type
}
return new Error(`HTTP ${res.status} ${type}: ${msg}`)
}
function getQs(query, params) {
const baseQs = `?query=${enc(query)}`
return Object.keys(params || {}).reduce((current, param) => {
return `${current}&${enc(`$${param}`)}=${enc(JSON.stringify(params[param]))}`
}, baseQs)
let qs = `?query=${enc(query)}`
if (!params) {
return qs
}
for (const param in params) {
if (has.call(params, param)) {
qs += `&${enc(`$${param}`)}=${enc(JSON.stringify(params[param]))}`
}
}
return qs
}

@@ -75,0 +101,0 @@

@@ -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}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)})()}));
!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,n={}.hasOwnProperty;function o(t,e){if(!(this instanceof o))return new o(t);this.clientConfig=t,this.fetcher=e}function r(t){if(!(t.headers.get("content-type")||"").includes("json"))throw i(t);return t.json().then((function(e){if(t.status<400)return e.result;throw i(t,e)}))}function i(t,e){var n=t.url,o=t.statusText;return e&&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){o.prototype[t]=function(t){return function(){throw new Error('Method "'.concat(t,'" not implemented, use @sanity/client'))}}(t)})),o.prototype.config=function(t){return t?(this.clientConfig=Object.assign({},this.clientConfig,t),this):this.clientConfig},o.prototype.fetch=function(t,o){var i=this.clientConfig,c=i.apiVersion?"v".concat(i.apiVersion.replace(/^v/,"")):"v1",a=function(t,o){var r="?query=".concat(e(t));if(!o)return r;for(var i in o)n.call(o,i)&&(r+="&".concat(e("$".concat(i)),"=").concat(e(JSON.stringify(o[i]))));return r}(t,o),s=a.length>11264,u=i.token?{Authorization:"Bearer ".concat(i.token)}:void 0,f=s?{"content-type":"application/json"}:void 0,p=Object.assign({},u,f),d=!i.useCdn||i.token||s?"api.sanity.io":"apicdn.sanity.io",h={credentials:i.withCredentials?"include":"omit",headers:p,method:s?"POST":"GET",body:s?JSON.stringify({query:t,params:o}):void 0},l="https://".concat(i.projectId,".").concat(d,"/").concat(c,"/data/query/").concat(i.dataset);return this.fetcher("".concat(l).concat(s?"":a),h).then(r)},t.exports=o}},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)})()}));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc