@nuxtjs/sanity
Advanced tools
Comparing version 0.3.9 to 0.3.10
@@ -0,1 +1,13 @@ | ||
### [0.3.10](https://github.com/nuxt-community/sanity-module/compare/0.3.9...0.3.10) (2020-10-27) | ||
### Features | ||
* make POST requests for large queries ([12c7bb1](https://github.com/nuxt-community/sanity-module/commit/12c7bb14ef6a1121eea1775ff1beb55ad288ae0e)), closes [/www.sanity.io/docs/http-query#the-post-form-J0Wj7](https://github.com/nuxt-community//www.sanity.io/docs/http-query/issues/the-post-form-J0Wj7) | ||
### Performance Improvements | ||
* use built-in `String.raw` if available ([0da5573](https://github.com/nuxt-community/sanity-module/commit/0da55733ba6bc3a61fc892961980f51ebdbe5bb7)) | ||
### [0.3.9](https://github.com/nuxt-community/sanity-module/compare/0.3.8...0.3.9) (2020-10-11) | ||
@@ -2,0 +14,0 @@ |
@@ -14,3 +14,4 @@ import { Module } from '@nuxt/types'; | ||
} | ||
declare function getQs(query: string, params?: Record<string, any>): string; | ||
declare function getQuery(query: string, params?: Record<string, any>): string; | ||
declare const getByteSize: (query: string) => number; | ||
declare function createClient(config: SanityConfiguration): { | ||
@@ -25,5 +26,8 @@ clone: () => any; | ||
/** | ||
* From https://github.com/sanity-io/sanity/tree/next/packages/groq | ||
* For use with https://github.com/asbjornh/eslint-plugin-groq | ||
* and https://github.com/sanity-io/vscode-sanity | ||
* | ||
* based on https://github.com/sanity-io/sanity/tree/next/packages/groq | ||
*/ | ||
declare const groq: (strings: TemplateStringsArray, ...keys: any[]) => string; | ||
declare const groq: (template: TemplateStringsArray, ...substitutions: any[]) => string; | ||
@@ -84,2 +88,2 @@ interface SanityModuleOptions extends Partial<SanityConfiguration> { | ||
export default nuxtModule; | ||
export { SanityConfiguration, SanityModuleOptions, createClient, getQs, groq }; | ||
export { SanityConfiguration, SanityModuleOptions, createClient, getByteSize, getQuery, groq }; |
@@ -7,3 +7,3 @@ import { resolve } from 'path'; | ||
const enc = encodeURIComponent; | ||
function getQs(query, params = {}) { | ||
function getQuery(query, params = {}) { | ||
const baseQs = `?query=${enc(query)}`; | ||
@@ -14,19 +14,26 @@ return Object.keys(params).reduce((current, param) => { | ||
} | ||
const getByteSize = (query) => encodeURI(query).split(/%..|./).length; | ||
function createClient(config) { | ||
const {projectId, dataset, useCdn, withCredentials, token} = config; | ||
const fetchOptions = { | ||
credentials: withCredentials ? "include" : "omit", | ||
headers: { | ||
...token ? { | ||
Authorization: `Bearer ${token}` | ||
} : {}, | ||
Accept: "application/json", | ||
...process.server ? {"accept-encoding": "gzip, deflate"} : {} | ||
} | ||
}; | ||
return { | ||
clone: () => createClient({projectId, dataset, useCdn, withCredentials, token}), | ||
async fetch(query, params) { | ||
const host = useCdn ? cdnHost : apiHost; | ||
const qs = getQs(query, params); | ||
const response = await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}${qs}`, { | ||
credentials: withCredentials ? "include" : "omit", | ||
headers: { | ||
...token ? { | ||
Authorization: `Bearer ${token}` | ||
} : {}, | ||
Accept: "application/json", | ||
...process.server ? {"accept-encoding": "gzip, deflate"} : {} | ||
} | ||
}); | ||
const qs = getQuery(query, params); | ||
const usePostRequest = getByteSize(qs) > 9e3; | ||
const host = useCdn && !usePostRequest ? cdnHost : apiHost; | ||
const response = usePostRequest ? await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}`, { | ||
method: "post", | ||
body: JSON.stringify({query, params}), | ||
...fetchOptions | ||
}) : await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}${qs}`, fetchOptions); | ||
const {result} = await response.json(); | ||
@@ -38,6 +45,6 @@ return result; | ||
const groq = (strings, ...keys) => { | ||
const groq = String.raw || ((strings, ...keys) => { | ||
const lastIndex = strings.length - 1; | ||
return strings.slice(0, lastIndex).reduce((query, currentString, index) => query + currentString + keys[index], "") + strings[lastIndex]; | ||
}; | ||
}); | ||
@@ -128,2 +135,2 @@ const isNuxtBuild = process.client || process.server; | ||
export default nuxtModule; | ||
export { createClient, getQs, groq }; | ||
export { createClient, getByteSize, getQuery, groq }; |
@@ -15,3 +15,3 @@ 'use strict'; | ||
const enc = encodeURIComponent; | ||
function getQs(query, params = {}) { | ||
function getQuery(query, params = {}) { | ||
const baseQs = `?query=${enc(query)}`; | ||
@@ -22,19 +22,26 @@ return Object.keys(params).reduce((current, param) => { | ||
} | ||
const getByteSize = (query) => encodeURI(query).split(/%..|./).length; | ||
function createClient(config) { | ||
const {projectId, dataset, useCdn, withCredentials, token} = config; | ||
const fetchOptions = { | ||
credentials: withCredentials ? "include" : "omit", | ||
headers: { | ||
...token ? { | ||
Authorization: `Bearer ${token}` | ||
} : {}, | ||
Accept: "application/json", | ||
...process.server ? {"accept-encoding": "gzip, deflate"} : {} | ||
} | ||
}; | ||
return { | ||
clone: () => createClient({projectId, dataset, useCdn, withCredentials, token}), | ||
async fetch(query, params) { | ||
const host = useCdn ? cdnHost : apiHost; | ||
const qs = getQs(query, params); | ||
const response = await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}${qs}`, { | ||
credentials: withCredentials ? "include" : "omit", | ||
headers: { | ||
...token ? { | ||
Authorization: `Bearer ${token}` | ||
} : {}, | ||
Accept: "application/json", | ||
...process.server ? {"accept-encoding": "gzip, deflate"} : {} | ||
} | ||
}); | ||
const qs = getQuery(query, params); | ||
const usePostRequest = getByteSize(qs) > 9e3; | ||
const host = useCdn && !usePostRequest ? cdnHost : apiHost; | ||
const response = usePostRequest ? await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}`, { | ||
method: "post", | ||
body: JSON.stringify({query, params}), | ||
...fetchOptions | ||
}) : await fetch(`https://${projectId}.${host}/v1/data/query/${dataset}${qs}`, fetchOptions); | ||
const {result} = await response.json(); | ||
@@ -46,6 +53,6 @@ return result; | ||
const groq = (strings, ...keys) => { | ||
const groq = String.raw || ((strings, ...keys) => { | ||
const lastIndex = strings.length - 1; | ||
return strings.slice(0, lastIndex).reduce((query, currentString, index) => query + currentString + keys[index], "") + strings[lastIndex]; | ||
}; | ||
}); | ||
@@ -137,3 +144,4 @@ const isNuxtBuild = process.client || process.server; | ||
exports.default = nuxtModule; | ||
exports.getQs = getQs; | ||
exports.getByteSize = getByteSize; | ||
exports.getQuery = getQuery; | ||
exports.groq = groq; |
{ | ||
"name": "@nuxtjs/sanity", | ||
"version": "0.3.9", | ||
"version": "0.3.10", | ||
"description": "Sanity integration for Nuxt.js", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"release": "yarn build && yarn test && release-it", | ||
"test": "yarn lint && yarn build && jest --runInBand" | ||
"test": "yarn lint && yarn build && jest" | ||
}, | ||
@@ -43,30 +43,30 @@ "husky": { | ||
"dependencies": { | ||
"defu": "^3.1.0" | ||
"defu": "3.1.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/plugin-transform-runtime": "^7.11.5", | ||
"@babel/preset-env": "^7.11.5", | ||
"@babel/preset-typescript": "latest", | ||
"@nuxt/types": "^2.14.6", | ||
"@nuxt/typescript-build": "^2.0.3", | ||
"@nuxt/typescript-runtime": "^2.0.0", | ||
"@nuxtjs/eslint-config-typescript": "latest", | ||
"@babel/plugin-transform-runtime": "7.12.1", | ||
"@babel/preset-env": "7.12.1", | ||
"@babel/preset-typescript": "7.12.1", | ||
"@nuxt/types": "2.14.7", | ||
"@nuxt/typescript-build": "2.0.3", | ||
"@nuxt/typescript-runtime": "2.0.0", | ||
"@nuxtjs/eslint-config-typescript": "4.0.0", | ||
"@nuxtjs/module-test-utils": "2.0.0-3", | ||
"@nuxtjs/tailwindcss": "^3.1.0", | ||
"@release-it/conventional-changelog": "^2.0.0", | ||
"@sanity/client": "^2.0.1", | ||
"@types/fs-extra": "^9.0.1", | ||
"@types/jest": "^26.0.14", | ||
"@types/webpack-env": "^1.15.3", | ||
"@vue/test-utils": "^1.1.0", | ||
"babel-jest": "26.5.2", | ||
"eslint": "^7.10.0", | ||
"husky": "^4.3.0", | ||
"jest": "26.5.2", | ||
"jest-serializer-vue-tjw": "^3.14.0", | ||
"lint-staged": "^10.4.0", | ||
"nuxt-edge": "latest", | ||
"playwright": "^1.4.2", | ||
"release-it": "^14.0.4", | ||
"siroc": "^0.3.3" | ||
"@nuxtjs/tailwindcss": "3.2.0", | ||
"@release-it/conventional-changelog": "2.0.0", | ||
"@sanity/client": "2.0.1", | ||
"@types/fs-extra": "9.0.2", | ||
"@types/jest": "26.0.15", | ||
"@types/webpack-env": "1.15.3", | ||
"@vue/test-utils": "1.1.0", | ||
"babel-jest": "26.6.1", | ||
"eslint": "7.12.1", | ||
"husky": "4.3.0", | ||
"jest": "26.6.1", | ||
"jest-serializer-vue-tjw": "3.15.0", | ||
"lint-staged": "10.5.0", | ||
"nuxt-edge": "2.14.8-26729321.046a0a4b", | ||
"playwright": "1.5.1", | ||
"release-it": "14.2.0", | ||
"siroc": "0.4.0" | ||
}, | ||
@@ -73,0 +73,0 @@ "peerDependencies": { |
40093
887
4
+ Addeddefu@3.1.0(transitive)
- Removeddefu@3.2.2(transitive)
Updateddefu@3.1.0