next-sanity
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -11,2 +11,2 @@ import { CurrentUser } from './types'; | ||
}; | ||
export declare function getCurrentUser(projectId: string, abort: Aborter): Promise<CurrentUser | null>; | ||
export declare function getCurrentUser(projectId: string, abort: Aborter, token?: string): Promise<CurrentUser | null>; |
@@ -82,2 +82,3 @@ 'use strict'; | ||
var projectId = _ref.projectId; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
return function () { | ||
@@ -87,6 +88,10 @@ return useCurrentUser(projectId); | ||
} | ||
function getCurrentUser(projectId, abort) { | ||
function getCurrentUser(projectId, abort, token) { | ||
var headers = token ? { | ||
Authorization: "Bearer " + token | ||
} : undefined; | ||
return fetch("https://" + projectId + ".api.sanity.io/v1/users/me", { | ||
credentials: 'include', | ||
signal: abort.signal | ||
signal: abort.signal, | ||
headers: headers | ||
}).then(function (res) { | ||
@@ -124,6 +129,9 @@ return res.json(); | ||
var EMPTY_PARAMS = {}; | ||
var EMPTY_PARAMS = {}; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function createPreviewSubscriptionHook(_ref) { | ||
var projectId = _ref.projectId, | ||
dataset = _ref.dataset, | ||
token = _ref.token, | ||
EventSource = _ref.EventSource, | ||
_ref$documentLimit = _ref.documentLimit, | ||
@@ -149,3 +157,4 @@ documentLimit = _ref$documentLimit === void 0 ? 3000 : _ref$documentLimit; | ||
initialData: initialData, | ||
enabled: enabled ? typeof window !== 'undefined' : false | ||
enabled: enabled ? typeof window !== 'undefined' : false, | ||
token: token | ||
}); | ||
@@ -171,2 +180,4 @@ }; | ||
documentLimit: documentLimit, | ||
token: token, | ||
EventSource: EventSource, | ||
listen: true, | ||
@@ -189,3 +200,4 @@ overlayDrafts: true, | ||
_options$enabled = options.enabled, | ||
enabled = _options$enabled === void 0 ? false : _options$enabled; | ||
enabled = _options$enabled === void 0 ? false : _options$enabled, | ||
token = options.token; | ||
@@ -215,3 +227,3 @@ var _useState = react.useState(), | ||
var subscription; | ||
getCurrentUser(projectId, aborter).then(function (user) { | ||
getCurrentUser(projectId, aborter, token).then(function (user) { | ||
if (user) { | ||
@@ -247,3 +259,3 @@ return; | ||
}; | ||
}, [getStore, query, params, enabled]); | ||
}, [getStore, query, params, enabled, projectId, token]); | ||
return { | ||
@@ -250,0 +262,0 @@ data: typeof data === 'undefined' ? initialData : data, |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("@sanity/client")),r=require("react"),n=e(require("groq")),o=function(){function e(){this._signal={aborted:!1}}var t,r;return e.prototype.abort=function(){this._signal.aborted=!0},t=e,(r=[{key:"signal",get:function(){return this._signal}}])&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(t.prototype,r),Object.defineProperty(t,"prototype",{writable:!1}),e}();function i(){return"undefined"==typeof AbortController?new o:new AbortController}function u(e,t){return fetch("https://"+e+".api.sanity.io/v1/users/me",{credentials:"include",signal:t.signal}).then((function(e){return e.json()})).then((function(e){return null!=e&&e.id?e:null}))}var a={};exports.groq=n,exports.createClient=function(e){return t(e)},exports.createCurrentUserHook=function(e){var t=e.projectId;return function(){return function(e){var t=r.useState(),n=t[0],o=t[1],a=r.useState(),c=a[0],f=a[1];return r.useEffect((function(){var t=i();return u(e,t).then(o).catch((function(e){return"AbortError"!==e.name&&f(e)})),function(){t.abort()}}),[e]),{data:n,error:c,loading:null!==n||!c}}(t)}},exports.createPreviewSubscriptionHook=function(e){var t,n=e.projectId,o=e.dataset,c=e.documentLimit,f=void 0===c?3e3:c;return function(e,t){void 0===t&&(t={});var o=t.params;return function(e){var t=e.getStore,n=e.projectId,o=e.query,a=e.initialData,c=e.enabled,f=void 0!==c&&c,s=r.useState(),l=s[0],d=s[1],b=r.useState(!1),p=b[0],v=b[1],g=r.useState(),y=g[0],h=g[1],m=function(e){var t=r.useMemo((function(){return JSON.stringify(e)}),[e]);return r.useMemo((function(){return JSON.parse(t)}),[t])}(e.params);return r.useEffect((function(){if(f){v(!0);var e,r=i();return u(n,r).then((function(e){if(!e)throw console.warn("Not authenticated - preview not available"),new Error("Not authenticated - preview not available")})).then((function(){return t(r)})).then((function(t){e=t.subscribe(o,m,(function(e,t){e?d(e):h(t)}))})).catch((function(e){return"AbortError"===e.name?null:d(e)})).finally((function(){return v(!1)})),function(){e&&e.unsubscribe(),r.abort()}}}),[t,o,m,f]),{data:void 0===y?a:y,loading:p,error:l}}({getStore:s,projectId:n,query:e,params:void 0===o?a:o,initialData:t.initialData,enabled:!!t.enabled&&"undefined"!=typeof window})};function s(e){return t||(t=new Promise((function(e){e(function(e){if(e&&e.__esModule)return e;var t={};return e&&Object.keys(e).forEach((function(r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})})),t.default=e,t}(require("@sanity/groq-store")))})).then((function(t){var r=t.groqStore;if(e.signal.aborted){var i=new Error("Cancelling groq store creation");return i.name="AbortError",Promise.reject(i)}return r({projectId:n,dataset:o,documentLimit:f,listen:!0,overlayDrafts:!0,subscriptionThrottleMs:10})}))),t}}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("@sanity/client")),r=require("react"),n=e(require("groq")),o=function(){function e(){this._signal={aborted:!1}}var t,r;return e.prototype.abort=function(){this._signal.aborted=!0},t=e,(r=[{key:"signal",get:function(){return this._signal}}])&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(t.prototype,r),Object.defineProperty(t,"prototype",{writable:!1}),e}();function i(){return"undefined"==typeof AbortController?new o:new AbortController}function u(e,t,r){return fetch("https://"+e+".api.sanity.io/v1/users/me",{credentials:"include",signal:t.signal,headers:r?{Authorization:"Bearer "+r}:void 0}).then((function(e){return e.json()})).then((function(e){return null!=e&&e.id?e:null}))}var a={};exports.groq=n,exports.createClient=function(e){return t(e)},exports.createCurrentUserHook=function(e){var t=e.projectId;return function(){return function(e){var t=r.useState(),n=t[0],o=t[1],a=r.useState(),c=a[0],f=a[1];return r.useEffect((function(){var t=i();return u(e,t).then(o).catch((function(e){return"AbortError"!==e.name&&f(e)})),function(){t.abort()}}),[e]),{data:n,error:c,loading:null!==n||!c}}(t)}},exports.createPreviewSubscriptionHook=function(e){var t,n=e.projectId,o=e.dataset,c=e.token,f=e.EventSource,s=e.documentLimit,l=void 0===s?3e3:s;return function(e,t){void 0===t&&(t={});var o=t.params;return function(e){var t=e.getStore,n=e.projectId,o=e.query,a=e.initialData,c=e.enabled,f=void 0!==c&&c,s=e.token,l=r.useState(),d=l[0],b=l[1],p=r.useState(!1),v=p[0],g=p[1],h=r.useState(),y=h[0],m=h[1],w=function(e){var t=r.useMemo((function(){return JSON.stringify(e)}),[e]);return r.useMemo((function(){return JSON.parse(t)}),[t])}(e.params);return r.useEffect((function(){if(f){g(!0);var e,r=i();return u(n,r,s).then((function(e){if(!e)throw console.warn("Not authenticated - preview not available"),new Error("Not authenticated - preview not available")})).then((function(){return t(r)})).then((function(t){e=t.subscribe(o,w,(function(e,t){e?b(e):m(t)}))})).catch((function(e){return"AbortError"===e.name?null:b(e)})).finally((function(){return g(!1)})),function(){e&&e.unsubscribe(),r.abort()}}}),[t,o,w,f,n,s]),{data:void 0===y?a:y,loading:v,error:d}}({getStore:d,projectId:n,query:e,params:void 0===o?a:o,initialData:t.initialData,enabled:!!t.enabled&&"undefined"!=typeof window,token:c})};function d(e){return t||(t=new Promise((function(e){e(function(e){if(e&&e.__esModule)return e;var t={};return e&&Object.keys(e).forEach((function(r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})})),t.default=e,t}(require("@sanity/groq-store")))})).then((function(t){var r=t.groqStore;if(e.signal.aborted){var i=new Error("Cancelling groq store creation");return i.name="AbortError",Promise.reject(i)}return r({projectId:n,dataset:o,documentLimit:l,token:c,EventSource:f,listen:!0,overlayDrafts:!0,subscriptionThrottleMs:10})}))),t}}; | ||
//# sourceMappingURL=next-sanity.cjs.production.min.js.map |
@@ -57,2 +57,3 @@ import sanityClient from '@sanity/client'; | ||
var projectId = _ref.projectId; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
return function () { | ||
@@ -62,6 +63,10 @@ return useCurrentUser(projectId); | ||
} | ||
function getCurrentUser(projectId, abort) { | ||
function getCurrentUser(projectId, abort, token) { | ||
var headers = token ? { | ||
Authorization: "Bearer " + token | ||
} : undefined; | ||
return fetch("https://" + projectId + ".api.sanity.io/v1/users/me", { | ||
credentials: 'include', | ||
signal: abort.signal | ||
signal: abort.signal, | ||
headers: headers | ||
}).then(function (res) { | ||
@@ -99,6 +104,9 @@ return res.json(); | ||
var EMPTY_PARAMS = {}; | ||
var EMPTY_PARAMS = {}; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function createPreviewSubscriptionHook(_ref) { | ||
var projectId = _ref.projectId, | ||
dataset = _ref.dataset, | ||
token = _ref.token, | ||
EventSource = _ref.EventSource, | ||
_ref$documentLimit = _ref.documentLimit, | ||
@@ -124,3 +132,4 @@ documentLimit = _ref$documentLimit === void 0 ? 3000 : _ref$documentLimit; | ||
initialData: initialData, | ||
enabled: enabled ? typeof window !== 'undefined' : false | ||
enabled: enabled ? typeof window !== 'undefined' : false, | ||
token: token | ||
}); | ||
@@ -146,2 +155,4 @@ }; | ||
documentLimit: documentLimit, | ||
token: token, | ||
EventSource: EventSource, | ||
listen: true, | ||
@@ -164,3 +175,4 @@ overlayDrafts: true, | ||
_options$enabled = options.enabled, | ||
enabled = _options$enabled === void 0 ? false : _options$enabled; | ||
enabled = _options$enabled === void 0 ? false : _options$enabled, | ||
token = options.token; | ||
@@ -190,3 +202,3 @@ var _useState = useState(), | ||
var subscription; | ||
getCurrentUser(projectId, aborter).then(function (user) { | ||
getCurrentUser(projectId, aborter, token).then(function (user) { | ||
if (user) { | ||
@@ -222,3 +234,3 @@ return; | ||
}; | ||
}, [getStore, query, params, enabled]); | ||
}, [getStore, query, params, enabled, projectId, token]); | ||
return { | ||
@@ -225,0 +237,0 @@ data: typeof data === 'undefined' ? initialData : data, |
@@ -0,4 +1,9 @@ | ||
import type { Config } from '@sanity/groq-store/dist/typings/types'; | ||
export declare type GroqStoreEventSource = Config['EventSource']; | ||
export interface ProjectConfig { | ||
projectId: string; | ||
dataset: string; | ||
token?: string; | ||
/** Must be provided when token is used in browser, as native EventSource does not support auth-headers. */ | ||
EventSource?: GroqStoreEventSource; | ||
} | ||
@@ -5,0 +10,0 @@ export interface CurrentUser { |
@@ -8,3 +8,3 @@ import { ProjectConfig } from './types'; | ||
} | ||
export declare function createPreviewSubscriptionHook({ projectId, dataset, documentLimit, }: ProjectConfig & { | ||
export declare function createPreviewSubscriptionHook({ projectId, dataset, token, EventSource, documentLimit, }: ProjectConfig & { | ||
documentLimit?: number; | ||
@@ -11,0 +11,0 @@ }): <R = any>(query: string, options?: SubscriptionOptions<R>) => { |
{ | ||
"name": "next-sanity", | ||
"description": "Sanity.io toolkit for Next.js", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"author": "Sanity.io <hello@sanity.io>", | ||
@@ -22,3 +22,3 @@ "license": "MIT", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint src", | ||
"lint": "eslint .", | ||
"posttest": "npm run lint", | ||
@@ -30,3 +30,3 @@ "prepublishOnly": "tsdx build && npm test", | ||
"@sanity/client": "^3.3.0", | ||
"@sanity/groq-store": "^0.3.1", | ||
"@sanity/groq-store": "^0.4.0", | ||
"groq": "^2.29.3" | ||
@@ -40,15 +40,15 @@ }, | ||
"@types/react-dom": "^17.0.9", | ||
"@typescript-eslint/eslint-plugin": "^4.29.1", | ||
"@typescript-eslint/parser": "^4.29.1", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^7.0.0", | ||
"eslint-config-react-app": "^6.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.30.4", | ||
"@typescript-eslint/parser": "^5.30.4", | ||
"eslint": "^8.19.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-config-react-app": "^7.0.1", | ||
"eslint-config-sanity": "^5.1.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"prettier": "^2.3.2", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"prettier": "^2.7.1", | ||
"react": ">=17.0.2", | ||
"react-dom": "^17.0.2", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.6.3" | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.7.4" | ||
}, | ||
@@ -55,0 +55,0 @@ "peerDependencies": { |
@@ -80,2 +80,9 @@ # next-sanity | ||
useCdn: process.env.NODE_ENV === 'production', | ||
/** | ||
* OPTIONAL config to enable authentication with custom token | ||
* You might need this if you host the preview on a different url than Sanity Studio | ||
*/ | ||
token: '<sanity access token>', | ||
EventSource: /* provide your own event source implementation. Required in browsers to support the above token parameter. */ | ||
} | ||
@@ -82,0 +89,0 @@ ``` |
@@ -6,9 +6,16 @@ import {useEffect, useState} from 'react' | ||
export function createCurrentUserHook({projectId}: {projectId: string; dataset?: string}) { | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
return () => useCurrentUser(projectId) | ||
} | ||
export function getCurrentUser(projectId: string, abort: Aborter): Promise<CurrentUser | null> { | ||
export function getCurrentUser( | ||
projectId: string, | ||
abort: Aborter, | ||
token?: string | ||
): Promise<CurrentUser | null> { | ||
const headers = token ? {Authorization: `Bearer ${token}`} : undefined | ||
return fetch(`https://${projectId}.api.sanity.io/v1/users/me`, { | ||
credentials: 'include', | ||
signal: abort.signal, | ||
headers, | ||
}) | ||
@@ -15,0 +22,0 @@ .then((res) => res.json()) |
@@ -0,4 +1,11 @@ | ||
import type {Config} from '@sanity/groq-store/dist/typings/types' | ||
export type GroqStoreEventSource = Config['EventSource'] | ||
export interface ProjectConfig { | ||
projectId: string | ||
dataset: string | ||
token?: string | ||
/** Must be provided when token is used in browser, as native EventSource does not support auth-headers. */ | ||
EventSource?: GroqStoreEventSource | ||
} | ||
@@ -5,0 +12,0 @@ |
@@ -16,5 +16,8 @@ import {useState, useEffect, useMemo} from 'react' | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export function createPreviewSubscriptionHook({ | ||
projectId, | ||
dataset, | ||
token, | ||
EventSource, | ||
documentLimit = 3000, | ||
@@ -37,2 +40,3 @@ }: ProjectConfig & {documentLimit?: number}) { | ||
enabled: enabled ? typeof window !== 'undefined' : false, | ||
token, | ||
}) | ||
@@ -56,2 +60,4 @@ } | ||
documentLimit, | ||
token, | ||
EventSource, | ||
listen: true, | ||
@@ -74,4 +80,5 @@ overlayDrafts: true, | ||
enabled: boolean | ||
token?: string | ||
}) { | ||
const {getStore, projectId, query, initialData, enabled = false} = options | ||
const {getStore, projectId, query, initialData, enabled = false, token} = options | ||
const [error, setError] = useState<Error>() | ||
@@ -93,3 +100,3 @@ const [loading, setLoading] = useState(false) | ||
let subscription: Subscription | undefined | ||
getCurrentUser(projectId, aborter) | ||
getCurrentUser(projectId, aborter, token) | ||
.then((user) => { | ||
@@ -125,3 +132,3 @@ if (user) { | ||
} | ||
}, [getStore, query, params, enabled]) | ||
}, [getStore, query, params, enabled, projectId, token]) | ||
@@ -128,0 +135,0 @@ return { |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
71920
733
250
4
+ Added@sanity/groq-store@0.4.1(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedgroq-js@1.14.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedsplit2@4.2.0(transitive)
+ Addedthrottle-debounce@5.0.2(transitive)
- Removed@sanity/groq-store@0.3.1(transitive)
- Removedgroq-js@0.4.0-beta.2(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsplit2@3.2.2(transitive)
- Removedthrottle-debounce@3.0.1(transitive)
Updated@sanity/groq-store@^0.4.0