@devite/nuxt-sanity 2.18.0 → 2.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/module.mjs +2 -3
- package/dist/runtime/client/MinimalSanityClient.d.ts +1 -1
- package/dist/runtime/client/MinimalSanityClient.js +7 -7
- package/dist/runtime/composables/query.d.ts +3 -8
- package/dist/runtime/composables/query.js +26 -17
- package/dist/runtime/composables/visual_editing_state.d.ts +1 -1
- package/dist/runtime/server/routes/cache/query.js +27 -17
- package/package.json +36 -21
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import defu from 'defu';
|
|
|
3
3
|
import { randomBytes } from 'node:crypto';
|
|
4
4
|
|
|
5
5
|
const name = "@devite/nuxt-sanity";
|
|
6
|
-
const version = "2.
|
|
6
|
+
const version = "2.19.1";
|
|
7
7
|
|
|
8
8
|
const CONFIG_KEY = "sanity";
|
|
9
9
|
const module$1 = defineNuxtModule({
|
|
@@ -106,7 +106,6 @@ const module$1 = defineNuxtModule({
|
|
|
106
106
|
handler: resolve("runtime/server/routes/cache/asset")
|
|
107
107
|
});
|
|
108
108
|
addServerHandler({
|
|
109
|
-
method: "get",
|
|
110
109
|
route: moduleConfig.minimalClient.queryEndpoint,
|
|
111
110
|
handler: resolve("runtime/server/routes/cache/query")
|
|
112
111
|
});
|
|
@@ -134,7 +133,7 @@ const module$1 = defineNuxtModule({
|
|
|
134
133
|
minimalClient: moduleConfig.minimalClient,
|
|
135
134
|
useCdn: moduleConfig.useCdn,
|
|
136
135
|
apiVersion: moduleConfig.apiVersion,
|
|
137
|
-
perspective: "raw",
|
|
136
|
+
perspective: options.perspective || "raw",
|
|
138
137
|
token: options.token || "",
|
|
139
138
|
withCredentials: options.withCredentials || false,
|
|
140
139
|
/* Visual Editing */
|
|
@@ -7,7 +7,7 @@ declare class MinimalSanityClient extends SanityClient {
|
|
|
7
7
|
constructor(config: ModuleOptions);
|
|
8
8
|
private getByteLength;
|
|
9
9
|
private toQueryString;
|
|
10
|
-
fetch<T>(query: string, params: QueryParams,
|
|
10
|
+
fetch<T>(query: string, params: QueryParams, options?: {
|
|
11
11
|
perspective?: ClientPerspective;
|
|
12
12
|
}): Promise<T | null>;
|
|
13
13
|
clone(): MinimalSanityClient;
|
|
@@ -32,17 +32,17 @@ class MinimalSanityClient extends SanityClient {
|
|
|
32
32
|
return acc + `&${encodeURIComponent(`$${paramName}`)}=${encodeURIComponent(JSON.stringify(params[paramName]))}`;
|
|
33
33
|
}, baseQueryStr);
|
|
34
34
|
}
|
|
35
|
-
async fetch(query, params,
|
|
36
|
-
const perspectiveQueryString =
|
|
37
|
-
const queryString = this.toQueryString(query, params || {})
|
|
38
|
-
const byteLength = this.getByteLength(queryString);
|
|
35
|
+
async fetch(query, params, options) {
|
|
36
|
+
const perspectiveQueryString = this.config.useCdn ? "" : `perspective=${options?.perspective || this.config.perspective}`;
|
|
37
|
+
const queryString = this.toQueryString(query, params || {});
|
|
38
|
+
const byteLength = this.getByteLength(queryString + (perspectiveQueryString.length > 0 ? perspectiveQueryString.length + 1 : 0));
|
|
39
39
|
const isEligibleForGetRequest = byteLength <= 9e3;
|
|
40
40
|
const minimalClientConfig = typeof this.config.minimalClient === "object" ? this.config.minimalClient : {};
|
|
41
|
-
const useCache = minimalClientConfig.cachingEnabled && isEligibleForGetRequest;
|
|
42
41
|
const cacheBaseUrl = import.meta.client ? minimalClientConfig.cacheClientBaseUrl : minimalClientConfig.cacheServerBaseUrl;
|
|
43
|
-
const requestUrl =
|
|
42
|
+
const requestUrl = minimalClientConfig.cachingEnabled && cacheBaseUrl ? cacheBaseUrl + minimalClientConfig.queryEndpoint : `https://${this.config.projectId}.${this.config.useCdn && isEligibleForGetRequest ? API_CDN_HOST : API_HOST}${this.queryPath}`;
|
|
43
|
+
const requestSearchParams = isEligibleForGetRequest ? queryString + (perspectiveQueryString.length > 0 ? `&${perspectiveQueryString}` : "") : "?" + perspectiveQueryString;
|
|
44
44
|
try {
|
|
45
|
-
return (await $fetch(requestUrl +
|
|
45
|
+
return (await $fetch(requestUrl + requestSearchParams, {
|
|
46
46
|
...this.fetchOptions,
|
|
47
47
|
method: isEligibleForGetRequest ? "GET" : "POST",
|
|
48
48
|
body: !isEligibleForGetRequest ? { query, params } : void 0
|
|
@@ -4,12 +4,7 @@ import type { EncodeDataAttributeFunction } from '@sanity/core-loader/encode-dat
|
|
|
4
4
|
import type { Ref } from 'vue';
|
|
5
5
|
export interface UseSanityQueryOptions<T> extends AsyncDataOptions<T> {
|
|
6
6
|
client?: 'default' | 'minimal';
|
|
7
|
-
perspective?: 'drafts' | 'published' | 'raw';
|
|
8
|
-
}
|
|
9
|
-
export interface SanityQueryResponse<T> {
|
|
10
|
-
data: T;
|
|
11
|
-
sourceMap?: ContentSourceMap;
|
|
12
|
-
encodeDataAttribute?: EncodeDataAttributeFunction;
|
|
7
|
+
perspective?: 'drafts' | 'preview-drafts' | 'published' | 'raw';
|
|
13
8
|
}
|
|
14
9
|
export type AsyncSanityData<T, E> = _AsyncSanityData<T, E> & Promise<_AsyncSanityData<T, E>>;
|
|
15
10
|
interface AsyncDataExecuteOptions {
|
|
@@ -26,6 +21,6 @@ export interface _AsyncSanityData<T, E> {
|
|
|
26
21
|
error: Ref<E | null>;
|
|
27
22
|
status: Ref<AsyncDataRequestStatus>;
|
|
28
23
|
}
|
|
29
|
-
export declare function useLazySanityQuery<T = unknown, E = Error>(query: string, params?: QueryParams, options?: UseSanityQueryOptions<
|
|
30
|
-
export declare function useSanityQuery<T = unknown, E = Error>(query: string, _params?: QueryParams, options?: UseSanityQueryOptions<
|
|
24
|
+
export declare function useLazySanityQuery<T = unknown, E = Error>(query: string, params?: QueryParams, options?: UseSanityQueryOptions<T | null>): AsyncSanityData<T | null, E>;
|
|
25
|
+
export declare function useSanityQuery<T = unknown, E = Error>(query: string, _params?: QueryParams, options?: UseSanityQueryOptions<T | null>, lazy?: boolean): AsyncSanityData<T | null, E>;
|
|
31
26
|
export {};
|
|
@@ -4,6 +4,7 @@ import { reactive, ref } from "vue";
|
|
|
4
4
|
import defu from "defu";
|
|
5
5
|
import useSanityClient from "../utils/useSanityClient.js";
|
|
6
6
|
import { useSanityVisualEditingState } from "./visual_editing_state.js";
|
|
7
|
+
import { useNuxtApp } from "#imports";
|
|
7
8
|
import { useRuntimeConfig } from "#imports";
|
|
8
9
|
export function useLazySanityQuery(query, params = {}, options = {}) {
|
|
9
10
|
return useSanityQuery(query, params, options, true);
|
|
@@ -19,37 +20,45 @@ export function useSanityQuery(query, _params = {}, options = {}, lazy = false)
|
|
|
19
20
|
options.watch ||= [];
|
|
20
21
|
options.watch.push(reactiveParams);
|
|
21
22
|
}
|
|
22
|
-
const data = ref(null);
|
|
23
23
|
const sourceMap = ref(null);
|
|
24
24
|
const encodeDataAttribute = ref(() => {
|
|
25
25
|
});
|
|
26
26
|
function updateRefs(resultData, resultSourceMap, resultEncodeDataAttribute) {
|
|
27
|
-
data.value = resultData;
|
|
27
|
+
pendingData.data.value = resultData;
|
|
28
28
|
sourceMap.value = resultSourceMap || null;
|
|
29
29
|
if (resultEncodeDataAttribute) encodeDataAttribute.value = resultEncodeDataAttribute;
|
|
30
30
|
}
|
|
31
|
-
const fetchFunc = () =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
const fetchFunc = () => {
|
|
32
|
+
const sanityFetchPromises = useNuxtApp()._sanityFetchPromises ||= /* @__PURE__ */ new Map();
|
|
33
|
+
if (sanityFetchPromises.has(key)) {
|
|
34
|
+
return sanityFetchPromises.get(key);
|
|
35
|
+
}
|
|
36
|
+
const fetchPromise = new Promise((resolve) => {
|
|
37
|
+
(visualEditingEnabled ? import("../utils/visualEditing/fetchSanityData") : import("../utils/default/fetchSanityData")).then(async ({ fetchSanityData }) => {
|
|
38
|
+
const client = import.meta.server ? (await import("../server/utils/useSanityClient.js")).default(clientType, sanityConfig) : await useSanityClient(visualEditingEnabled, clientType, sanityConfig);
|
|
39
|
+
function onDataUpdate(resultData, resultSourceMap, resultEncodeDataAttribute) {
|
|
40
|
+
updateRefs(resultData, resultSourceMap, resultEncodeDataAttribute);
|
|
41
|
+
resolve(resultData);
|
|
42
|
+
sanityFetchPromises.delete(key);
|
|
43
|
+
}
|
|
44
|
+
const fetchSanityDataFunc = fetchSanityData;
|
|
45
|
+
fetchSanityDataFunc(query, reactiveParams, client, perspective, onDataUpdate);
|
|
46
|
+
});
|
|
41
47
|
});
|
|
42
|
-
|
|
48
|
+
sanityFetchPromises.set(key, fetchPromise);
|
|
49
|
+
return fetchPromise;
|
|
50
|
+
};
|
|
43
51
|
const key = "sanity-" + hash(query + (reactiveParams ? JSON.stringify(reactiveParams) : ""));
|
|
44
52
|
const pendingData = lazy ? useLazyAsyncData(key, fetchFunc, options) : useAsyncData(key, fetchFunc, options);
|
|
53
|
+
Object.assign(
|
|
54
|
+
pendingData,
|
|
55
|
+
{ sourceMap, encodeDataAttribute }
|
|
56
|
+
);
|
|
45
57
|
if (visualEditingEnabled && import.meta.client) {
|
|
46
58
|
import("../utils/visualEditing/subscribeToChanges.js").then(async ({ subscribeToChanges }) => {
|
|
47
59
|
const client = await useSanityClient(visualEditingEnabled, clientType, sanityConfig);
|
|
48
60
|
subscribeToChanges(query, reactiveParams, client, updateRefs);
|
|
49
61
|
});
|
|
50
62
|
}
|
|
51
|
-
return
|
|
52
|
-
updateRefs(value.data, value.sourceMap);
|
|
53
|
-
resolve({ ...pendingData, data, sourceMap, encodeDataAttribute });
|
|
54
|
-
})), { ...pendingData, data, sourceMap, encodeDataAttribute });
|
|
63
|
+
return pendingData;
|
|
55
64
|
}
|
|
@@ -1,23 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
defineEventHandler,
|
|
4
|
-
getRequestURL,
|
|
5
|
-
setResponseHeader
|
|
6
|
-
} from "h3";
|
|
7
|
-
import { hash } from "ohash";
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
import { createError, defineEventHandler, getQuery, getRequestURL, readBody, setResponseHeader } from "h3";
|
|
8
3
|
import { useStorage } from "nitropack/runtime/internal/storage";
|
|
4
|
+
import { hash } from "ohash";
|
|
9
5
|
import useSanityClient from "../../utils/useSanityClient.js";
|
|
10
|
-
import { useRuntimeConfig } from "#imports";
|
|
11
6
|
const TTL = 60 * 60 * 8;
|
|
12
7
|
export default defineEventHandler(async (event) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
let query, params;
|
|
9
|
+
switch (event.method) {
|
|
10
|
+
case "GET": {
|
|
11
|
+
const queryParams = getRequestURL(event).searchParams;
|
|
12
|
+
query = queryParams.get("query");
|
|
13
|
+
params = Object.fromEntries(Array.from(queryParams).filter(([key]) => key.startsWith("$")).map(([key, value]) => [key.slice(1), JSON.parse(value)]));
|
|
14
|
+
if (!query)
|
|
15
|
+
throw createError({ statusCode: 400, statusMessage: 'Missing query parameter "query"' });
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
case "POST": {
|
|
19
|
+
const body = await readBody(event);
|
|
20
|
+
query = body.query;
|
|
21
|
+
params = body.params || {};
|
|
22
|
+
if (!query)
|
|
23
|
+
throw createError({ statusCode: 400, statusMessage: 'Missing field "query"' });
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
default:
|
|
27
|
+
throw createError({ statusCode: 405, statusMessage: "Method Not Allowed" });
|
|
28
|
+
}
|
|
29
|
+
const perspective = getQuery(event).perspective || useRuntimeConfig().public.sanity.perspective;
|
|
30
|
+
if (typeof perspective === "string" && !["drafts", "preview-drafts", "published", "raw"].includes(perspective))
|
|
21
31
|
throw createError({ statusCode: 400, statusMessage: "Invalid perspective" });
|
|
22
32
|
const hashedQuery = hash(query + JSON.stringify(params) + (perspective || ""));
|
|
23
33
|
const dataCache = useStorage("sanityData");
|
|
@@ -35,7 +45,7 @@ export default defineEventHandler(async (event) => {
|
|
|
35
45
|
client.config.useCdn = false;
|
|
36
46
|
const result = await client.fetch(
|
|
37
47
|
query,
|
|
38
|
-
|
|
48
|
+
params,
|
|
39
49
|
{ perspective }
|
|
40
50
|
);
|
|
41
51
|
if (!result)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devite/nuxt-sanity",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.1",
|
|
4
4
|
"description": "Advanced Sanity integration for Nuxt.js.",
|
|
5
5
|
"repository": "devite-io/nuxt-sanity",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,37 +26,63 @@
|
|
|
26
26
|
"files": [
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"prepack": "nuxt-module-build build",
|
|
31
|
+
"dev": "nuxi dev playground",
|
|
32
|
+
"dev:cms": "cd playground/cms && pnpm dev",
|
|
33
|
+
"dev:build": "nuxi build playground",
|
|
34
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
35
|
+
"release": "pnpm lint && pnpm test && pnpm prepack && git push && changelogen --release && pnpm publish --access=public && git push --follow-tags",
|
|
36
|
+
"lint": "eslint --fix .",
|
|
37
|
+
"test": "vitest run --passWithNoTests",
|
|
38
|
+
"test:watch": "vitest watch",
|
|
39
|
+
"test:types": "vue-tsc --noEmit"
|
|
40
|
+
},
|
|
29
41
|
"dependencies": {
|
|
30
42
|
"@nuxt/image": "^2.0.0",
|
|
31
43
|
"@portabletext/vue": "^1.0.14",
|
|
32
|
-
"@sanity/client": "^7.14.
|
|
44
|
+
"@sanity/client": "^7.14.1",
|
|
33
45
|
"@sanity/core-loader": "^2.0.5",
|
|
34
46
|
"@sanity/preview-url-secret": "^4.0.2",
|
|
35
|
-
"@sanity/types": "^5.
|
|
36
|
-
"@sanity/visual-editing": "^5.1.
|
|
47
|
+
"@sanity/types": "^5.7.0",
|
|
48
|
+
"@sanity/visual-editing": "^5.1.1",
|
|
37
49
|
"defu": "^6.1.4",
|
|
38
50
|
"ofetch": "^1.5.1",
|
|
39
51
|
"ohash": "^2.0.11",
|
|
40
52
|
"unstorage": "^1.17.4"
|
|
41
53
|
},
|
|
42
54
|
"devDependencies": {
|
|
43
|
-
"@nuxt/eslint-config": "^1.
|
|
44
|
-
"@nuxt/kit": "^4.
|
|
55
|
+
"@nuxt/eslint-config": "^1.13.0",
|
|
56
|
+
"@nuxt/kit": "^4.3.0",
|
|
45
57
|
"@nuxt/module-builder": "^1.0.2",
|
|
46
|
-
"@nuxt/schema": "^4.
|
|
58
|
+
"@nuxt/schema": "^4.3.0",
|
|
47
59
|
"@nuxt/test-utils": "^3.23.0",
|
|
48
60
|
"@types/node": "latest",
|
|
49
61
|
"changelogen": "^0.6.2",
|
|
50
62
|
"eslint": "^9.39.2",
|
|
51
63
|
"h3": "^1.15.5",
|
|
52
|
-
"nuxt": "^4.
|
|
64
|
+
"nuxt": "^4.3.0",
|
|
53
65
|
"typescript": "^5.9.3",
|
|
54
66
|
"vite": "^7.3.1",
|
|
55
67
|
"vitest": "^3.2.4",
|
|
56
68
|
"vitest-environment-nuxt": "^1.0.1",
|
|
57
69
|
"vue": "^3.5.27",
|
|
58
70
|
"vue-router": "^4.6.4",
|
|
59
|
-
"vue-tsc": "^3.2.
|
|
71
|
+
"vue-tsc": "^3.2.4"
|
|
72
|
+
},
|
|
73
|
+
"pnpm": {
|
|
74
|
+
"overrides": {
|
|
75
|
+
"sharp": "0.34.4",
|
|
76
|
+
"css-tree": "2.2.1"
|
|
77
|
+
},
|
|
78
|
+
"onlyBuiltDependencies": [
|
|
79
|
+
"@parcel/watcher",
|
|
80
|
+
"@swc/core",
|
|
81
|
+
"esbuild",
|
|
82
|
+
"scrollmirror",
|
|
83
|
+
"sharp",
|
|
84
|
+
"yarn"
|
|
85
|
+
]
|
|
60
86
|
},
|
|
61
87
|
"resolutions": {
|
|
62
88
|
"@devite/nuxt-sanity": "link:."
|
|
@@ -65,16 +91,5 @@
|
|
|
65
91
|
"externals": [
|
|
66
92
|
"@sanity/client"
|
|
67
93
|
]
|
|
68
|
-
},
|
|
69
|
-
"scripts": {
|
|
70
|
-
"dev": "nuxi dev playground",
|
|
71
|
-
"dev:cms": "cd playground/cms && pnpm dev",
|
|
72
|
-
"dev:build": "nuxi build playground",
|
|
73
|
-
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
74
|
-
"release": "pnpm lint && pnpm test && pnpm prepack && git push && changelogen --release && pnpm publish --access=public && git push --follow-tags",
|
|
75
|
-
"lint": "eslint --fix .",
|
|
76
|
-
"test": "vitest run --passWithNoTests",
|
|
77
|
-
"test:watch": "vitest watch",
|
|
78
|
-
"test:types": "vue-tsc --noEmit"
|
|
79
94
|
}
|
|
80
|
-
}
|
|
95
|
+
}
|