next-sanity 5.4.2 → 5.4.4
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.
|
@@ -6,31 +6,55 @@ Object.defineProperty(exports, '__esModule', {
|
|
|
6
6
|
var webhook = require('@sanity/webhook');
|
|
7
7
|
var readBody = require('./readBody.cjs');
|
|
8
8
|
async function parseBody(req, secret) {
|
|
9
|
+
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
10
|
+
if (typeof EdgeRuntime !== "undefined") {
|
|
11
|
+
throw new TypeError("The edge runtime isn't supported. You'll have to use the 'nodejs' runtime until the underlying `@sanity/webhook` package is updated to support it.");
|
|
12
|
+
}
|
|
13
|
+
return "text" in req ? parseAppBody(req, secret, waitForContentLakeEventualConsistency) : parsePageBody(req, secret, waitForContentLakeEventualConsistency);
|
|
14
|
+
}
|
|
15
|
+
async function parsePageBody(req, secret) {
|
|
9
16
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
10
17
|
let signature = req.headers[webhook.SIGNATURE_HEADER_NAME];
|
|
11
18
|
if (Array.isArray(signature)) {
|
|
12
19
|
signature = signature[0];
|
|
13
20
|
}
|
|
21
|
+
if (!signature) {
|
|
22
|
+
console.error("Missing signature header");
|
|
23
|
+
return {
|
|
24
|
+
body: null,
|
|
25
|
+
isValidSignature: null
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (req.readableEnded) {
|
|
29
|
+
throw new Error("Request already ended and the POST body can't be read. Have you setup `export {config} from 'next-sanity/webhook' in your webhook API handler?`");
|
|
30
|
+
}
|
|
14
31
|
const body = await readBody._readBody(req);
|
|
15
32
|
const validSignature = secret ? webhook.isValidSignature(body, signature, secret.trim()) : null;
|
|
16
33
|
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
17
34
|
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
18
35
|
}
|
|
19
36
|
return {
|
|
20
|
-
body: body.trim()
|
|
37
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
21
38
|
isValidSignature: validSignature
|
|
22
39
|
};
|
|
23
40
|
}
|
|
24
41
|
async function parseAppBody(req, secret) {
|
|
25
42
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
26
43
|
const signature = req.headers.get(webhook.SIGNATURE_HEADER_NAME);
|
|
44
|
+
if (!signature) {
|
|
45
|
+
console.error("Missing signature header");
|
|
46
|
+
return {
|
|
47
|
+
body: null,
|
|
48
|
+
isValidSignature: null
|
|
49
|
+
};
|
|
50
|
+
}
|
|
27
51
|
const body = await req.text();
|
|
28
52
|
const validSignature = secret ? webhook.isValidSignature(body, signature, secret.trim()) : null;
|
|
29
53
|
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
30
54
|
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
31
55
|
}
|
|
32
56
|
return {
|
|
33
|
-
body: body.trim()
|
|
57
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
34
58
|
isValidSignature: validSignature
|
|
35
59
|
};
|
|
36
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseBody.cjs","sources":["../../src/webhook/parseBody.ts"],"sourcesContent":["import type {SanityDocument} from '@sanity/types'\nimport {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook'\nimport type {NextApiRequest} from 'next'\nimport type {NextRequest} from 'next/server'\n\nimport {_readBody as readBody} from './readBody'\n\n/** @public */\nexport type ParsedBody<T> = {\n /**\n * If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`\n */\n isValidSignature: boolean | null\n body: T\n}\n\n
|
|
1
|
+
{"version":3,"file":"parseBody.cjs","sources":["../../src/webhook/parseBody.ts"],"sourcesContent":["import type {SanityDocument} from '@sanity/types'\nimport {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook'\nimport type {NextApiRequest} from 'next'\nimport type {NextRequest} from 'next/server'\n\nimport {_readBody as readBody} from './readBody'\n\n/** @public */\nexport type ParsedBody<T> = {\n /**\n * If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`\n */\n isValidSignature: boolean | null\n body: T | null\n}\n\n/**\n * @deprecated Use `ParsedBody` instead\n * @public\n */\nexport type ParseBody<Body = SanityDocument> = ParsedBody<Body>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\nexport async function parseBody<Body = SanityDocument>(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency?: boolean,\n): Promise<ParsedBody<Body>>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\nexport async function parseBody<Body = SanityDocument>(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency?: boolean,\n): Promise<ParsedBody<Body>>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\n// eslint-disable-next-line require-await\nexport async function parseBody<Body = SanityDocument>(\n req: NextApiRequest | NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n // @ts-expect-error -- add global typings for EdgeRuntime\n if (typeof EdgeRuntime !== 'undefined') {\n throw new TypeError(\n `The edge runtime isn't supported. You'll have to use the 'nodejs' runtime until the underlying \\`@sanity/webhook\\` package is updated to support it.`,\n )\n }\n return 'text' in req\n ? parseAppBody(req, secret, waitForContentLakeEventualConsistency)\n : parsePageBody(req, secret, waitForContentLakeEventualConsistency)\n}\n\nasync function parsePageBody<Body = SanityDocument>(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n let signature = req.headers[SIGNATURE_HEADER_NAME]\n if (Array.isArray(signature)) {\n signature = signature[0]\n }\n if (!signature) {\n console.error('Missing signature header')\n return {body: null, isValidSignature: null}\n }\n\n if (req.readableEnded) {\n throw new Error(\n `Request already ended and the POST body can't be read. Have you setup \\`export {config} from 'next-sanity/webhook' in your webhook API handler?\\``,\n )\n }\n\n const body = await readBody(req)\n const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null\n\n if (validSignature !== false && waitForContentLakeEventualConsistency) {\n await new Promise((resolve) => setTimeout(resolve, 1000))\n }\n\n return {\n body: body.trim() ? JSON.parse(body) : null,\n isValidSignature: validSignature,\n }\n}\n\n/**\n * @deprecated Use `ParsedBody` instead\n * @public\n */\nexport type ParseAppBody<Body = SanityDocument> = ParsedBody<Body>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @deprecated Use `parseBody` instead\n * @public\n */\nexport async function parseAppBody<Body = SanityDocument>(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n const signature = req.headers.get(SIGNATURE_HEADER_NAME)!\n if (!signature) {\n console.error('Missing signature header')\n return {body: null, isValidSignature: null}\n }\n\n const body = await req.text()\n const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null\n\n if (validSignature !== false && waitForContentLakeEventualConsistency) {\n await new Promise((resolve) => setTimeout(resolve, 1000))\n }\n\n return {\n body: body.trim() ? JSON.parse(body) : null,\n isValidSignature: validSignature,\n }\n}\n"],"names":["parseBody","req","secret","waitForContentLakeEventualConsistency","arguments","length","undefined","EdgeRuntime","TypeError","parseAppBody","parsePageBody","signature","headers","SIGNATURE_HEADER_NAME","Array","isArray","console","error","body","isValidSignature","readableEnded","Error","readBody","validSignature","trim","Promise","resolve","setTimeout","JSON","parse","get","text"],"mappings":";;;;;;;AA+CA,eAAsBA,SACpBA,CAAAC,GAAA,EACAC,MACA,EAC2B;EAAA,IAD3BC,qCAA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,OAAA;IACLR,MAAMA,IAAK,CAAAM,IAAA,KAASI,IAAK,CAAAC,KAAA,CAAMX,IAAI,CAAI,GAAA,IAAA;IACvCC,gBAAkB,EAAAI;EAAA,CACpB;AACF;AAaA,eAAsBd,YACpBA,CAAAR,GAAA,EACAC,MACA,EAC2B;EAAA,IAD3BC,qCAAA,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAiD,IACtB;EAC3B,MAAMO,SAAY,GAAAV,GAAA,CAAIW,OAAQ,CAAAkB,GAAA,CAAIjB,OAAqB,CAAAA,qBAAA,CAAA;EACvD,IAAI,CAACF,SAAW,EAAA;IACdK,OAAA,CAAQC,MAAM,0BAA0B,CAAA;IACxC,OAAO;MAACC,IAAA,EAAM,IAAM;MAAAC,gBAAA,EAAkB;IAAI,CAAA;EAC5C;EAEM,MAAAD,IAAA,GAAO,MAAMjB,GAAA,CAAI8B,IAAK,EAAA;EACtB,MAAAR,cAAA,GAAiBrB,SAASiB,OAAAA,CAAAA,gBAAiB,CAAAD,IAAA,EAAMP,WAAWT,MAAO,CAAAsB,IAAA,EAAM,CAAI,GAAA,IAAA;EAE/E,IAAAD,cAAA,KAAmB,SAASpB,qCAAuC,EAAA;IACrE,MAAM,IAAIsB,OAAQ,CAACC,WAAYC,UAAW,CAAAD,OAAA,EAAS,GAAI,CAAC,CAAA;EAC1D;EAEO,OAAA;IACLR,MAAMA,IAAK,CAAAM,IAAA,KAASI,IAAK,CAAAC,KAAA,CAAMX,IAAI,CAAI,GAAA,IAAA;IACvCC,gBAAkB,EAAAI;EAAA,CACpB;AACF;;"}
|
|
@@ -1,31 +1,55 @@
|
|
|
1
1
|
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
|
|
2
2
|
import { _readBody } from './readBody.js';
|
|
3
3
|
async function parseBody(req, secret) {
|
|
4
|
+
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
5
|
+
if (typeof EdgeRuntime !== "undefined") {
|
|
6
|
+
throw new TypeError("The edge runtime isn't supported. You'll have to use the 'nodejs' runtime until the underlying `@sanity/webhook` package is updated to support it.");
|
|
7
|
+
}
|
|
8
|
+
return "text" in req ? parseAppBody(req, secret, waitForContentLakeEventualConsistency) : parsePageBody(req, secret, waitForContentLakeEventualConsistency);
|
|
9
|
+
}
|
|
10
|
+
async function parsePageBody(req, secret) {
|
|
4
11
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
5
12
|
let signature = req.headers[SIGNATURE_HEADER_NAME];
|
|
6
13
|
if (Array.isArray(signature)) {
|
|
7
14
|
signature = signature[0];
|
|
8
15
|
}
|
|
16
|
+
if (!signature) {
|
|
17
|
+
console.error("Missing signature header");
|
|
18
|
+
return {
|
|
19
|
+
body: null,
|
|
20
|
+
isValidSignature: null
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (req.readableEnded) {
|
|
24
|
+
throw new Error("Request already ended and the POST body can't be read. Have you setup `export {config} from 'next-sanity/webhook' in your webhook API handler?`");
|
|
25
|
+
}
|
|
9
26
|
const body = await _readBody(req);
|
|
10
27
|
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null;
|
|
11
28
|
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
12
29
|
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
13
30
|
}
|
|
14
31
|
return {
|
|
15
|
-
body: body.trim()
|
|
32
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
16
33
|
isValidSignature: validSignature
|
|
17
34
|
};
|
|
18
35
|
}
|
|
19
36
|
async function parseAppBody(req, secret) {
|
|
20
37
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
21
38
|
const signature = req.headers.get(SIGNATURE_HEADER_NAME);
|
|
39
|
+
if (!signature) {
|
|
40
|
+
console.error("Missing signature header");
|
|
41
|
+
return {
|
|
42
|
+
body: null,
|
|
43
|
+
isValidSignature: null
|
|
44
|
+
};
|
|
45
|
+
}
|
|
22
46
|
const body = await req.text();
|
|
23
47
|
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null;
|
|
24
48
|
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
25
49
|
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
26
50
|
}
|
|
27
51
|
return {
|
|
28
|
-
body: body.trim()
|
|
52
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
29
53
|
isValidSignature: validSignature
|
|
30
54
|
};
|
|
31
55
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseBody.js","sources":["../../src/webhook/parseBody.ts"],"sourcesContent":["import type {SanityDocument} from '@sanity/types'\nimport {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook'\nimport type {NextApiRequest} from 'next'\nimport type {NextRequest} from 'next/server'\n\nimport {_readBody as readBody} from './readBody'\n\n/** @public */\nexport type ParsedBody<T> = {\n /**\n * If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`\n */\n isValidSignature: boolean | null\n body: T\n}\n\n
|
|
1
|
+
{"version":3,"file":"parseBody.js","sources":["../../src/webhook/parseBody.ts"],"sourcesContent":["import type {SanityDocument} from '@sanity/types'\nimport {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook'\nimport type {NextApiRequest} from 'next'\nimport type {NextRequest} from 'next/server'\n\nimport {_readBody as readBody} from './readBody'\n\n/** @public */\nexport type ParsedBody<T> = {\n /**\n * If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`\n */\n isValidSignature: boolean | null\n body: T | null\n}\n\n/**\n * @deprecated Use `ParsedBody` instead\n * @public\n */\nexport type ParseBody<Body = SanityDocument> = ParsedBody<Body>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\nexport async function parseBody<Body = SanityDocument>(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency?: boolean,\n): Promise<ParsedBody<Body>>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\nexport async function parseBody<Body = SanityDocument>(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency?: boolean,\n): Promise<ParsedBody<Body>>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @public\n */\n// eslint-disable-next-line require-await\nexport async function parseBody<Body = SanityDocument>(\n req: NextApiRequest | NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n // @ts-expect-error -- add global typings for EdgeRuntime\n if (typeof EdgeRuntime !== 'undefined') {\n throw new TypeError(\n `The edge runtime isn't supported. You'll have to use the 'nodejs' runtime until the underlying \\`@sanity/webhook\\` package is updated to support it.`,\n )\n }\n return 'text' in req\n ? parseAppBody(req, secret, waitForContentLakeEventualConsistency)\n : parsePageBody(req, secret, waitForContentLakeEventualConsistency)\n}\n\nasync function parsePageBody<Body = SanityDocument>(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n let signature = req.headers[SIGNATURE_HEADER_NAME]\n if (Array.isArray(signature)) {\n signature = signature[0]\n }\n if (!signature) {\n console.error('Missing signature header')\n return {body: null, isValidSignature: null}\n }\n\n if (req.readableEnded) {\n throw new Error(\n `Request already ended and the POST body can't be read. Have you setup \\`export {config} from 'next-sanity/webhook' in your webhook API handler?\\``,\n )\n }\n\n const body = await readBody(req)\n const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null\n\n if (validSignature !== false && waitForContentLakeEventualConsistency) {\n await new Promise((resolve) => setTimeout(resolve, 1000))\n }\n\n return {\n body: body.trim() ? JSON.parse(body) : null,\n isValidSignature: validSignature,\n }\n}\n\n/**\n * @deprecated Use `ParsedBody` instead\n * @public\n */\nexport type ParseAppBody<Body = SanityDocument> = ParsedBody<Body>\n/**\n * Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries\n * without worrying about getting stale data.\n * @deprecated Use `parseBody` instead\n * @public\n */\nexport async function parseAppBody<Body = SanityDocument>(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParsedBody<Body>> {\n const signature = req.headers.get(SIGNATURE_HEADER_NAME)!\n if (!signature) {\n console.error('Missing signature header')\n return {body: null, isValidSignature: null}\n }\n\n const body = await req.text()\n const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null\n\n if (validSignature !== false && waitForContentLakeEventualConsistency) {\n await new Promise((resolve) => setTimeout(resolve, 1000))\n }\n\n return {\n body: body.trim() ? JSON.parse(body) : null,\n isValidSignature: validSignature,\n }\n}\n"],"names":["parseBody","req","secret","waitForContentLakeEventualConsistency","arguments","length","undefined","EdgeRuntime","TypeError","parseAppBody","parsePageBody","signature","headers","SIGNATURE_HEADER_NAME","Array","isArray","console","error","body","isValidSignature","readableEnded","Error","readBody","validSignature","trim","Promise","resolve","setTimeout","JSON","parse","get","text"],"mappings":";;AA+CA,eAAsBA,SACpBA,CAAAC,GAAA,EACAC,MACA,EAC2B;EAAA,IAD3BC,qCAAA,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAiD,IACtB;EAEvB,IAAA,OAAOG,gBAAgB,WAAa,EAAA;IACtC,MAAM,IAAIC,SAAA,CACR,oJAAA,CACF;EACF;EACO,OAAA,MAAA,IAAUP,GACb,GAAAQ,YAAA,CAAaR,GAAK,EAAAC,MAAA,EAAQC,qCAAqC,CAC/D,GAAAO,aAAA,CAAcT,GAAK,EAAAC,MAAA,EAAQC,qCAAqC,CAAA;AACtE;AAEA,eAAeO,aACbA,CAAAT,G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,CAACF,SAAW,EAAA;IACdK,OAAA,CAAQC,MAAM,0BAA0B,CAAA;IACxC,OAAO;MAACC,IAAA,EAAM,IAAM;MAAAC,gBAAA,EAAkB;IAAI,CAAA;EAC5C;EAEM,MAAAD,IAAA,GAAO,MAAMjB,GAAA,CAAI8B,IAAK,EAAA;EACtB,MAAAR,cAAA,GAAiBrB,SAASiB,gBAAiB,CAAAD,IAAA,EAAMP,WAAWT,MAAO,CAAAsB,IAAA,EAAM,CAAI,GAAA,IAAA;EAE/E,IAAAD,cAAA,KAAmB,SAASpB,qCAAuC,EAAA;IACrE,MAAM,IAAIsB,OAAQ,CAACC,WAAYC,UAAW,CAAAD,OAAA,EAAS,GAAI,CAAC,CAAA;EAC1D;EAEO,OAAA;IACLR,MAAMA,IAAK,CAAAM,IAAA,KAASI,IAAK,CAAAC,KAAA,CAAMX,IAAI,CAAI,GAAA,IAAA;IACvCC,gBAAkB,EAAAI;EAAA,CACpB;AACF;"}
|
package/dist/webhook.d.ts
CHANGED
|
@@ -9,21 +9,28 @@ import type {SanityDocument} from '@sanity/types'
|
|
|
9
9
|
*/
|
|
10
10
|
export declare const config: PageConfig
|
|
11
11
|
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `ParsedBody` instead
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
13
16
|
export declare type ParseAppBody<Body = SanityDocument> = ParsedBody<Body>
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
17
20
|
* without worrying about getting stale data.
|
|
21
|
+
* @deprecated Use `parseBody` instead
|
|
18
22
|
* @public
|
|
19
23
|
*/
|
|
20
24
|
export declare function parseAppBody<Body = SanityDocument>(
|
|
21
25
|
req: NextRequest,
|
|
22
26
|
secret?: string,
|
|
23
27
|
waitForContentLakeEventualConsistency?: boolean,
|
|
24
|
-
): Promise<
|
|
28
|
+
): Promise<ParsedBody<Body>>
|
|
25
29
|
|
|
26
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated Use `ParsedBody` instead
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
27
34
|
export declare type ParseBody<Body = SanityDocument> = ParsedBody<Body>
|
|
28
35
|
|
|
29
36
|
/**
|
|
@@ -35,7 +42,18 @@ export declare function parseBody<Body = SanityDocument>(
|
|
|
35
42
|
req: NextApiRequest,
|
|
36
43
|
secret?: string,
|
|
37
44
|
waitForContentLakeEventualConsistency?: boolean,
|
|
38
|
-
): Promise<
|
|
45
|
+
): Promise<ParsedBody<Body>>
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
49
|
+
* without worrying about getting stale data.
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare function parseBody<Body = SanityDocument>(
|
|
53
|
+
req: NextRequest,
|
|
54
|
+
secret?: string,
|
|
55
|
+
waitForContentLakeEventualConsistency?: boolean,
|
|
56
|
+
): Promise<ParsedBody<Body>>
|
|
39
57
|
|
|
40
58
|
/** @public */
|
|
41
59
|
export declare type ParsedBody<T> = {
|
|
@@ -43,7 +61,7 @@ export declare type ParsedBody<T> = {
|
|
|
43
61
|
* If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`
|
|
44
62
|
*/
|
|
45
63
|
isValidSignature: boolean | null
|
|
46
|
-
body: T
|
|
64
|
+
body: T | null
|
|
47
65
|
}
|
|
48
66
|
|
|
49
67
|
export {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-sanity",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.4",
|
|
4
4
|
"description": "Sanity.io toolkit for Next.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -174,15 +174,15 @@
|
|
|
174
174
|
"singleQuote": true
|
|
175
175
|
},
|
|
176
176
|
"dependencies": {
|
|
177
|
-
"@sanity/preview-kit": "3.1.
|
|
178
|
-
"@sanity/webhook": "
|
|
177
|
+
"@sanity/preview-kit": "3.1.4",
|
|
178
|
+
"@sanity/webhook": "3.0.1",
|
|
179
179
|
"groq": "^3.0.0"
|
|
180
180
|
},
|
|
181
181
|
"devDependencies": {
|
|
182
|
-
"@next/bundle-analyzer": "^13.4.
|
|
183
|
-
"@next/eslint-plugin-next": "13.4.
|
|
182
|
+
"@next/bundle-analyzer": "^13.4.19",
|
|
183
|
+
"@next/eslint-plugin-next": "13.4.19",
|
|
184
184
|
"@rollup/plugin-url": "^8.0.1",
|
|
185
|
-
"@sanity/client": "6.4.
|
|
185
|
+
"@sanity/client": "6.4.9",
|
|
186
186
|
"@sanity/eslint-config-studio": "^3.0.0",
|
|
187
187
|
"@sanity/image-url": "^1.0.2",
|
|
188
188
|
"@sanity/pkg-utils": "^2.4.6",
|
|
@@ -206,7 +206,7 @@
|
|
|
206
206
|
"jest": "^29.6.2",
|
|
207
207
|
"jest-environment-jsdom": "^29.6.2",
|
|
208
208
|
"ls-engines": "^0.9.0",
|
|
209
|
-
"next": "13.4.
|
|
209
|
+
"next": "13.4.19",
|
|
210
210
|
"postcss": "^8.4.28",
|
|
211
211
|
"prettier": "^3.0.2",
|
|
212
212
|
"prettier-plugin-packagejson": "^2.4.5",
|
|
@@ -224,7 +224,7 @@
|
|
|
224
224
|
"url-loader": "^4.1.1"
|
|
225
225
|
},
|
|
226
226
|
"peerDependencies": {
|
|
227
|
-
"@sanity/client": "^6.4.
|
|
227
|
+
"@sanity/client": "^6.4.9",
|
|
228
228
|
"@sanity/icons": "^2.0.0",
|
|
229
229
|
"@sanity/types": "^3.0.0",
|
|
230
230
|
"@sanity/ui": "^1.0.0",
|
package/src/webhook/parseBody.ts
CHANGED
|
@@ -11,10 +11,13 @@ export type ParsedBody<T> = {
|
|
|
11
11
|
* If a secret is given then it returns a boolean. If no secret is provided then no validation is done on the signature, and it'll return `null`
|
|
12
12
|
*/
|
|
13
13
|
isValidSignature: boolean | null
|
|
14
|
-
body: T
|
|
14
|
+
body: T | null
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Use `ParsedBody` instead
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
18
21
|
export type ParseBody<Body = SanityDocument> = ParsedBody<Body>
|
|
19
22
|
/**
|
|
20
23
|
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
@@ -22,14 +25,61 @@ export type ParseBody<Body = SanityDocument> = ParsedBody<Body>
|
|
|
22
25
|
* @public
|
|
23
26
|
*/
|
|
24
27
|
export async function parseBody<Body = SanityDocument>(
|
|
28
|
+
req: NextApiRequest,
|
|
29
|
+
secret?: string,
|
|
30
|
+
waitForContentLakeEventualConsistency?: boolean,
|
|
31
|
+
): Promise<ParsedBody<Body>>
|
|
32
|
+
/**
|
|
33
|
+
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
34
|
+
* without worrying about getting stale data.
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export async function parseBody<Body = SanityDocument>(
|
|
38
|
+
req: NextRequest,
|
|
39
|
+
secret?: string,
|
|
40
|
+
waitForContentLakeEventualConsistency?: boolean,
|
|
41
|
+
): Promise<ParsedBody<Body>>
|
|
42
|
+
/**
|
|
43
|
+
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
44
|
+
* without worrying about getting stale data.
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
// eslint-disable-next-line require-await
|
|
48
|
+
export async function parseBody<Body = SanityDocument>(
|
|
49
|
+
req: NextApiRequest | NextRequest,
|
|
50
|
+
secret?: string,
|
|
51
|
+
waitForContentLakeEventualConsistency: boolean = true,
|
|
52
|
+
): Promise<ParsedBody<Body>> {
|
|
53
|
+
// @ts-expect-error -- add global typings for EdgeRuntime
|
|
54
|
+
if (typeof EdgeRuntime !== 'undefined') {
|
|
55
|
+
throw new TypeError(
|
|
56
|
+
`The edge runtime isn't supported. You'll have to use the 'nodejs' runtime until the underlying \`@sanity/webhook\` package is updated to support it.`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
return 'text' in req
|
|
60
|
+
? parseAppBody(req, secret, waitForContentLakeEventualConsistency)
|
|
61
|
+
: parsePageBody(req, secret, waitForContentLakeEventualConsistency)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function parsePageBody<Body = SanityDocument>(
|
|
25
65
|
req: NextApiRequest,
|
|
26
66
|
secret?: string,
|
|
27
67
|
waitForContentLakeEventualConsistency: boolean = true,
|
|
28
|
-
): Promise<
|
|
29
|
-
let signature = req.headers[SIGNATURE_HEADER_NAME]
|
|
68
|
+
): Promise<ParsedBody<Body>> {
|
|
69
|
+
let signature = req.headers[SIGNATURE_HEADER_NAME]
|
|
30
70
|
if (Array.isArray(signature)) {
|
|
31
71
|
signature = signature[0]
|
|
32
72
|
}
|
|
73
|
+
if (!signature) {
|
|
74
|
+
console.error('Missing signature header')
|
|
75
|
+
return {body: null, isValidSignature: null}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (req.readableEnded) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Request already ended and the POST body can't be read. Have you setup \`export {config} from 'next-sanity/webhook' in your webhook API handler?\``,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
33
83
|
|
|
34
84
|
const body = await readBody(req)
|
|
35
85
|
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null
|
|
@@ -39,24 +89,32 @@ export async function parseBody<Body = SanityDocument>(
|
|
|
39
89
|
}
|
|
40
90
|
|
|
41
91
|
return {
|
|
42
|
-
body: body.trim()
|
|
92
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
43
93
|
isValidSignature: validSignature,
|
|
44
94
|
}
|
|
45
95
|
}
|
|
46
96
|
|
|
47
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* @deprecated Use `ParsedBody` instead
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
48
101
|
export type ParseAppBody<Body = SanityDocument> = ParsedBody<Body>
|
|
49
102
|
/**
|
|
50
103
|
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
51
104
|
* without worrying about getting stale data.
|
|
105
|
+
* @deprecated Use `parseBody` instead
|
|
52
106
|
* @public
|
|
53
107
|
*/
|
|
54
108
|
export async function parseAppBody<Body = SanityDocument>(
|
|
55
109
|
req: NextRequest,
|
|
56
110
|
secret?: string,
|
|
57
111
|
waitForContentLakeEventualConsistency: boolean = true,
|
|
58
|
-
): Promise<
|
|
112
|
+
): Promise<ParsedBody<Body>> {
|
|
59
113
|
const signature = req.headers.get(SIGNATURE_HEADER_NAME)!
|
|
114
|
+
if (!signature) {
|
|
115
|
+
console.error('Missing signature header')
|
|
116
|
+
return {body: null, isValidSignature: null}
|
|
117
|
+
}
|
|
60
118
|
|
|
61
119
|
const body = await req.text()
|
|
62
120
|
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null
|
|
@@ -66,7 +124,7 @@ export async function parseAppBody<Body = SanityDocument>(
|
|
|
66
124
|
}
|
|
67
125
|
|
|
68
126
|
return {
|
|
69
|
-
body: body.trim()
|
|
127
|
+
body: body.trim() ? JSON.parse(body) : null,
|
|
70
128
|
isValidSignature: validSignature,
|
|
71
129
|
}
|
|
72
130
|
}
|