picosanity
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -5,2 +5,8 @@ # Change Log | ||
## 4.1.0 | ||
### Added | ||
- Support for [perspectives](https://www.sanity.io/blog/introducing-perspectives-sanity-previews) on either a client level or query level. Thanks @jjjjjx! | ||
## 4.0.0 | ||
@@ -7,0 +13,0 @@ |
/// <reference lib="dom" /> | ||
type QueryParams = {[key: string]: unknown} | ||
interface ClientConfig { | ||
export interface ClientConfig { | ||
projectId: string | ||
@@ -11,4 +11,9 @@ dataset: string | ||
withCredentials?: boolean | ||
perspective?: string | ||
} | ||
export interface FetchOptions { | ||
perspective?: string | ||
} | ||
export interface PicoSanity { | ||
@@ -51,2 +56,11 @@ // Client/configuration | ||
/** | ||
* Perform a GROQ-query against the configured dataset. | ||
* | ||
* @param query GROQ-query to perform | ||
* @param params Optional query parameters | ||
* @param options Optional fetch options | ||
*/ | ||
fetch<R = any>(query: string, params: QueryParams, options: FetchOptions): Promise<R> | ||
/** | ||
* @alpha | ||
@@ -53,0 +67,0 @@ * @deprecated Don't rely on this existing, unless you: |
@@ -32,6 +32,9 @@ "use strict"; | ||
PicoSanity.prototype.fetch = function (query, params) { | ||
PicoSanity.prototype.fetch = function (query, params, options) { | ||
var cfg = this.clientConfig; | ||
var version = cfg.apiVersion ? "v".concat(cfg.apiVersion.replace(/^v/, '')) : 'v1'; | ||
var qs = getQs(query, params); | ||
var perspective = options ? options.perspective : cfg.perspective; | ||
var qs = getQs(query, params, { | ||
perspective: perspective | ||
}); | ||
var usePost = qs.length > 11264; | ||
@@ -65,4 +68,4 @@ var auth = cfg.token ? { | ||
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); | ||
var url = "https://".concat(cfg.projectId, ".").concat(host, "/").concat(version, "/data/query/").concat(cfg.dataset).concat(usePost ? '' : qs); | ||
return this.fetcher(url, opts).then(parse); | ||
}; | ||
@@ -98,5 +101,9 @@ | ||
function getQs(query, params) { | ||
function getQs(query, params, opts) { | ||
var qs = "?query=".concat(enc(query)); | ||
if (opts.perspective) { | ||
qs += "&perspective=".concat(enc(opts.perspective)); | ||
} | ||
if (!params) { | ||
@@ -103,0 +110,0 @@ return qs; |
{ | ||
"name": "picosanity", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Tiny Sanity client alternative should you only need to do queries", | ||
@@ -39,13 +39,13 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.17.3", | ||
"@babel/preset-env": "^7.16.11", | ||
"eslint": "^8.9.0", | ||
"@babel/cli": "^7.22.10", | ||
"@babel/preset-env": "^7.22.10", | ||
"eslint": "^8.47.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-sanity": "^5.1.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.5.1", | ||
"rimraf": "^3.0.0", | ||
"webpack": "^5.69.0", | ||
"webpack-cli": "^4.9.2" | ||
"jest": "^29.6.2", | ||
"prettier": "^3.0.1", | ||
"rimraf": "^5.0.1", | ||
"webpack": "^5.88.2", | ||
"webpack-cli": "^5.1.4" | ||
} | ||
} |
@@ -39,6 +39,7 @@ const enc = encodeURIComponent | ||
PicoSanity.prototype.fetch = function (query, params) { | ||
PicoSanity.prototype.fetch = function (query, params, options) { | ||
const cfg = this.clientConfig | ||
const version = cfg.apiVersion ? `v${cfg.apiVersion.replace(/^v/, '')}` : 'v1' | ||
const qs = getQs(query, params) | ||
const perspective = options ? options.perspective : cfg.perspective | ||
const qs = getQs(query, params, {perspective}) | ||
const usePost = qs.length > 11264 | ||
@@ -66,4 +67,7 @@ const auth = cfg.token ? {Authorization: `Bearer ${cfg.token}`} : undefined | ||
const url = `https://${cfg.projectId}.${host}/${version}/data/query/${cfg.dataset}` | ||
return this.fetcher(`${url}${usePost ? '' : qs}`, opts).then(parse) | ||
const url = `https://${cfg.projectId}.${host}/${version}/data/query/${cfg.dataset}${ | ||
usePost ? '' : qs | ||
}` | ||
return this.fetcher(url, opts).then(parse) | ||
} | ||
@@ -97,4 +101,9 @@ | ||
function getQs(query, params) { | ||
function getQs(query, params, opts) { | ||
let qs = `?query=${enc(query)}` | ||
if (opts.perspective) { | ||
qs += `&perspective=${enc(opts.perspective)}` | ||
} | ||
if (!params) { | ||
@@ -101,0 +110,0 @@ return qs |
@@ -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,{fetch:function(t,e){return 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.fetch,this.headers=e.headers||{}}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,f=i.token?{Authorization:"Bearer ".concat(i.token)}:void 0,u=s?{"content-type":"application/json"}:void 0,p=Object.assign({},this.headers,f,u),h=i.useCdn?"apicdn.sanity.io":"api.sanity.io",d={headers:p,method:s?"POST":"GET"};s&&(d.body=JSON.stringify({query:t,params:o})),"undefined"!=typeof window&&(d.credentials=i.withCredentials?"include":"omit");var l="https://".concat(i.projectId,".").concat(h,"/").concat(c,"/data/query/").concat(i.dataset);return this.fetcher("".concat(l).concat(s?"":a),d).then(r)},t.exports=o}},e={};return function n(o){var r=e[o];if(void 0!==r)return r.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,(()=>(()=>{"use strict";var t={693:(t,e,n)=>{var r=n(26);t.exports=function(t){return new r(t,{fetch:function(t,e){return fetch(t,e)}})}},26:t=>{var e=encodeURIComponent,n={}.hasOwnProperty;function r(t,e){if(!(this instanceof r))return new r(t);this.clientConfig=t,this.fetcher=e.fetch,this.headers=e.headers||{}}function o(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,r=t.statusText;return e&&e.error&&e.error.description&&(n=e.error.description,r=e.error.type||r),new Error("HTTP ".concat(t.status," ").concat(r,": ").concat(n))}["clone","create","createIfNotExists","createOrReplace","delete","listen","mutate","patch","transaction"].forEach((function(t){r.prototype[t]=function(t){return function(){throw new Error('Method "'.concat(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,r,i){var c=this.clientConfig,s=c.apiVersion?"v".concat(c.apiVersion.replace(/^v/,"")):"v1",a=function(t,r,o){var i="?query=".concat(e(t));if(o.perspective&&(i+="&perspective=".concat(e(o.perspective))),!r)return i;for(var c in r)n.call(r,c)&&(i+="&".concat(e("$".concat(c)),"=").concat(e(JSON.stringify(r[c]))));return i}(t,r,{perspective:i?i.perspective:c.perspective}),p=a.length>11264,f=c.token?{Authorization:"Bearer ".concat(c.token)}:void 0,u=p?{"content-type":"application/json"}:void 0,h=Object.assign({},this.headers,f,u),d=c.useCdn?"apicdn.sanity.io":"api.sanity.io",l={headers:h,method:p?"POST":"GET"};p&&(l.body=JSON.stringify({query:t,params:r})),"undefined"!=typeof window&&(l.credentials=c.withCredentials?"include":"omit");var y="https://".concat(c.projectId,".").concat(d,"/").concat(s,"/data/query/").concat(c.dataset).concat(p?"":a);return this.fetcher(y,l).then(o)},t.exports=r}},e={};return function n(r){var o=e[r];if(void 0!==o)return o.exports;var i=e[r]={exports:{}};return t[r](i,i.exports,n),i.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
16541
302