@devite/nuxt-sanity 2.17.1 → 2.19.0

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.d.mts CHANGED
@@ -65,6 +65,7 @@ interface LinkExternal {
65
65
  interface LinkInternal {
66
66
  _type: 'linkInternal';
67
67
  slug: string;
68
+ queryString?: string;
68
69
  linkTitle?: string;
69
70
  relationship?: string;
70
71
  }
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devite/nuxt-sanity",
3
- "version": "2.17.1",
3
+ "version": "2.19.0",
4
4
  "configKey": "sanity",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
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.17.1";
6
+ const version = "2.19.0";
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, _options?: {
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, _options) {
36
- const perspectiveQueryString = `&perspective=${_options?.perspective || this.config.perspective}`;
37
- const queryString = this.toQueryString(query, params || {}) + (this.config.useCdn ? "" : perspectiveQueryString);
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 = useCache && cacheBaseUrl ? cacheBaseUrl + minimalClientConfig.queryEndpoint : `https://${this.config.projectId}.${this.config.useCdn && isEligibleForGetRequest ? API_CDN_HOST : API_HOST}${this.queryPath}`;
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 + queryString, {
45
+ return (await $fetch(requestUrl + requestSearchParams, {
46
46
  ...this.fetchOptions,
47
47
  method: isEligibleForGetRequest ? "GET" : "POST",
48
48
  body: !isEligibleForGetRequest ? { query, params } : void 0
@@ -1,13 +1,13 @@
1
1
  <template>
2
- <main v-if="sanityData?.modules?.length">
3
- <SanityComponent
4
- v-for="(module, index) in sanityData.modules"
5
- :key="module._key"
6
- :index="index"
7
- :data="module"
8
- />
9
- <slot />
10
- </main>
2
+ <main v-if="sanityData?.modules?.length">
3
+ <SanityComponent
4
+ v-for="(module, index) in sanityData.modules"
5
+ :key="module._key"
6
+ :index="index"
7
+ :data="module"
8
+ />
9
+ <slot />
10
+ </main>
11
11
  </template>
12
12
 
13
13
  <script setup>
@@ -4,7 +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';
7
+ perspective?: 'drafts' | 'preview-drafts' | 'published' | 'raw';
8
8
  }
9
9
  export interface SanityQueryResponse<T> {
10
10
  data: T;
@@ -1,23 +1,33 @@
1
- import {
2
- createError,
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
- const url = getRequestURL(event);
14
- const queryParams = url.searchParams;
15
- const query = queryParams.get("query");
16
- if (!query)
17
- throw createError({ statusCode: 400, statusMessage: "Missing query parameter" });
18
- const params = Array.from(queryParams).filter(([key]) => key.startsWith("$"));
19
- const perspective = queryParams.get("perspective") || useRuntimeConfig().public.sanity.perspective;
20
- if (typeof perspective === "string" && !["drafts", "published", "raw"].includes(perspective))
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
- Object.fromEntries(params.map(([key, value]) => [key.slice(1), JSON.parse(value)])),
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.17.1",
3
+ "version": "2.19.0",
4
4
  "description": "Advanced Sanity integration for Nuxt.js.",
5
5
  "repository": "devite-io/nuxt-sanity",
6
6
  "license": "MIT",
@@ -29,34 +29,34 @@
29
29
  "dependencies": {
30
30
  "@nuxt/image": "^2.0.0",
31
31
  "@portabletext/vue": "^1.0.14",
32
- "@sanity/client": "^7.13.2",
32
+ "@sanity/client": "^7.14.1",
33
33
  "@sanity/core-loader": "^2.0.5",
34
34
  "@sanity/preview-url-secret": "^4.0.2",
35
- "@sanity/types": "^5.1.0",
36
- "@sanity/visual-editing": "^5.0.4",
35
+ "@sanity/types": "^5.7.0",
36
+ "@sanity/visual-editing": "^5.1.1",
37
37
  "defu": "^6.1.4",
38
38
  "ofetch": "^1.5.1",
39
39
  "ohash": "^2.0.11",
40
- "unstorage": "^1.17.3"
40
+ "unstorage": "^1.17.4"
41
41
  },
42
42
  "devDependencies": {
43
- "@nuxt/eslint-config": "^1.12.1",
44
- "@nuxt/kit": "^4.2.2",
43
+ "@nuxt/eslint-config": "^1.13.0",
44
+ "@nuxt/kit": "^4.3.0",
45
45
  "@nuxt/module-builder": "^1.0.2",
46
- "@nuxt/schema": "^4.2.2",
47
- "@nuxt/test-utils": "^3.22.0",
46
+ "@nuxt/schema": "^4.3.0",
47
+ "@nuxt/test-utils": "^3.23.0",
48
48
  "@types/node": "latest",
49
49
  "changelogen": "^0.6.2",
50
50
  "eslint": "^9.39.2",
51
- "h3": "^1.15.4",
52
- "nuxt": "^4.2.2",
51
+ "h3": "^1.15.5",
52
+ "nuxt": "^4.3.0",
53
53
  "typescript": "^5.9.3",
54
- "vite": "^7.3.0",
54
+ "vite": "^7.3.1",
55
55
  "vitest": "^3.2.4",
56
56
  "vitest-environment-nuxt": "^1.0.1",
57
- "vue": "^3.5.26",
57
+ "vue": "^3.5.27",
58
58
  "vue-router": "^4.6.4",
59
- "vue-tsc": "^3.2.1"
59
+ "vue-tsc": "^3.2.4"
60
60
  },
61
61
  "resolutions": {
62
62
  "@devite/nuxt-sanity": "link:."