next-sanity 5.1.7 → 5.2.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/webhook.cjs +17 -13
- package/dist/webhook.cjs.js +1 -0
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.ts +21 -0
- package/dist/webhook.js +15 -6
- package/dist/webhook.js.map +1 -1
- package/package.json +2 -2
- package/src/webhook/parseBody.ts +35 -4
package/dist/webhook.cjs
CHANGED
|
@@ -3,13 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var
|
|
7
|
-
function _interopDefaultCompat(e) {
|
|
8
|
-
return e && typeof e === 'object' && 'default' in e ? e : {
|
|
9
|
-
default: e
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
var sanityWebhook__default = /*#__PURE__*/_interopDefaultCompat(sanityWebhook);
|
|
6
|
+
var webhook = require('@sanity/webhook');
|
|
13
7
|
const config = {
|
|
14
8
|
api: {
|
|
15
9
|
/**
|
|
@@ -29,18 +23,27 @@ async function _readBody(readable) {
|
|
|
29
23
|
}
|
|
30
24
|
return Buffer.concat(chunks).toString("utf8");
|
|
31
25
|
}
|
|
32
|
-
const {
|
|
33
|
-
isValidSignature,
|
|
34
|
-
SIGNATURE_HEADER_NAME
|
|
35
|
-
} = sanityWebhook__default.default;
|
|
36
26
|
async function parseBody(req, secret) {
|
|
37
27
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
38
|
-
let signature = req.headers[SIGNATURE_HEADER_NAME];
|
|
28
|
+
let signature = req.headers[webhook.SIGNATURE_HEADER_NAME];
|
|
39
29
|
if (Array.isArray(signature)) {
|
|
40
30
|
signature = signature[0];
|
|
41
31
|
}
|
|
42
32
|
const body = await _readBody(req);
|
|
43
|
-
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null;
|
|
33
|
+
const validSignature = secret ? webhook.isValidSignature(body, signature, secret.trim()) : null;
|
|
34
|
+
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
35
|
+
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
body: body.trim() && JSON.parse(body),
|
|
39
|
+
isValidSignature: validSignature
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function parseAppBody(req, secret) {
|
|
43
|
+
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
44
|
+
const signature = req.headers.get(webhook.SIGNATURE_HEADER_NAME);
|
|
45
|
+
const body = await req.text();
|
|
46
|
+
const validSignature = secret ? webhook.isValidSignature(body, signature, secret.trim()) : null;
|
|
44
47
|
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
45
48
|
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
46
49
|
}
|
|
@@ -50,5 +53,6 @@ async function parseBody(req, secret) {
|
|
|
50
53
|
};
|
|
51
54
|
}
|
|
52
55
|
exports.config = config;
|
|
56
|
+
exports.parseAppBody = parseAppBody;
|
|
53
57
|
exports.parseBody = parseBody;
|
|
54
58
|
//# sourceMappingURL=webhook.cjs.map
|
package/dist/webhook.cjs.js
CHANGED
package/dist/webhook.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhook.cjs","sources":["../src/webhook/config.ts","../src/webhook/readBody.ts","../src/webhook/parseBody.ts"],"sourcesContent":["import type {PageConfig} from 'next/types'\n\n/**\n * Configurates the API function with the right runtime and body parsing to handle Sanity Webhook events.\n * @public\n */\nexport const config: PageConfig = {\n api: {\n /**\n * Next.js will by default parse the body, which can lead to invalid signatures.\n */\n bodyParser: false,\n },\n /**\n * `@sanity/webhook` isn't updated to support the edge runtime yet, and currently requires Node.js APIs such as Buffer.\n */\n runtime: 'nodejs',\n}\n","import type {NextApiRequest} from 'next'\n\n/** @internal */\nexport async function _readBody(readable: NextApiRequest): Promise<string> {\n const chunks = []\n for await (const chunk of readable) {\n chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)\n }\n return Buffer.concat(chunks).toString('utf8')\n}\n","import type {SanityDocument} from '@sanity/types'\nimport
|
|
1
|
+
{"version":3,"file":"webhook.cjs","sources":["../src/webhook/config.ts","../src/webhook/readBody.ts","../src/webhook/parseBody.ts"],"sourcesContent":["import type {PageConfig} from 'next/types'\n\n/**\n * Configurates the API function with the right runtime and body parsing to handle Sanity Webhook events.\n * @public\n */\nexport const config: PageConfig = {\n api: {\n /**\n * Next.js will by default parse the body, which can lead to invalid signatures.\n */\n bodyParser: false,\n },\n /**\n * `@sanity/webhook` isn't updated to support the edge runtime yet, and currently requires Node.js APIs such as Buffer.\n */\n runtime: 'nodejs',\n}\n","import type {NextApiRequest} from 'next'\n\n/** @internal */\nexport async function _readBody(readable: NextApiRequest): Promise<string> {\n const chunks = []\n for await (const chunk of readable) {\n chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)\n }\n return Buffer.concat(chunks).toString('utf8')\n}\n","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 ParseBody = {\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: SanityDocument\n}\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(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParseBody> {\n let signature = req.headers[SIGNATURE_HEADER_NAME]!\n if (Array.isArray(signature)) {\n signature = signature[0]\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),\n isValidSignature: validSignature,\n }\n}\n\n/** @public */\nexport type ParseAppBody = {\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: SanityDocument\n}\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 parseAppBody(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParseAppBody> {\n const signature = req.headers.get(SIGNATURE_HEADER_NAME)!\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),\n isValidSignature: validSignature,\n }\n}\n"],"names":["config","api","bodyParser","runtime","_readBody","readable","chunks","chunk","push","Buffer","from","concat","toString","parseBody","req","secret","waitForContentLakeEventualConsistency","arguments","length","undefined","signature","headers","SIGNATURE_HEADER_NAME","Array","isArray","body","readBody","validSignature","isValidSignature","trim","Promise","resolve","setTimeout","JSON","parse","parseAppBody","get","text"],"mappings":";;;;;;AAMO,MAAMA,MAAqB,GAAA;EAChCC,GAAK,EAAA;IAAA;AAAA;AAAA;IAIHC,UAAY,EAAA;EACd,CAAA;EAAA;AAAA;AAAA;EAIAC,OAAS,EAAA;AACX,CAAA;ACdA,eAAsBC,UAAUC,QAA2C,EAAA;EACzE,MAAMC,SAAS,EAAC;EAChB,WAAA,MAAiBC,SAASF,QAAU,EAAA;IAC3BC,MAAA,CAAAE,IAAA,CAAK,OAAOD,KAAU,KAAA,QAAA,GAAWE,OAAOC,IAAK,CAAAH,KAAK,IAAIA,KAAK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,SAASa,OAAAA,CAAAA,gBAAiB,CAAAH,IAAA,EAAML,WAAWL,MAAO,CAAAc,IAAA,EAAM,CAAI,GAAA,IAAA;EAE/E,IAAAF,cAAA,KAAmB,SAASX,qCAAuC,EAAA;IACrE,MAAM,IAAIc,OAAQ,CAACC,WAAYC,UAAW,CAAAD,OAAA,EAAS,GAAI,CAAC,CAAA;EAC1D;EAEO,OAAA;IACLN,MAAMA,IAAK,CAAAI,IAAA,CAAA,CAAU,IAAAI,IAAA,CAAKC,MAAMT,IAAI,CAAA;IACpCG,gBAAkB,EAAAD;EAAA,CACpB;AACF;;;"}
|
package/dist/webhook.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {NextApiRequest} from 'next'
|
|
2
|
+
import type {NextRequest} from 'next/server'
|
|
2
3
|
import type {PageConfig} from 'next/types'
|
|
3
4
|
import type {SanityDocument} from '@sanity/types'
|
|
4
5
|
|
|
@@ -8,6 +9,26 @@ import type {SanityDocument} from '@sanity/types'
|
|
|
8
9
|
*/
|
|
9
10
|
export declare const config: PageConfig
|
|
10
11
|
|
|
12
|
+
/** @public */
|
|
13
|
+
export declare type ParseAppBody = {
|
|
14
|
+
/**
|
|
15
|
+
* 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`
|
|
16
|
+
*/
|
|
17
|
+
isValidSignature: boolean | null
|
|
18
|
+
body: SanityDocument
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
23
|
+
* without worrying about getting stale data.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseAppBody(
|
|
27
|
+
req: NextRequest,
|
|
28
|
+
secret?: string,
|
|
29
|
+
waitForContentLakeEventualConsistency?: boolean,
|
|
30
|
+
): Promise<ParseAppBody>
|
|
31
|
+
|
|
11
32
|
/** @public */
|
|
12
33
|
export declare type ParseBody = {
|
|
13
34
|
/**
|
package/dist/webhook.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
|
|
2
2
|
const config = {
|
|
3
3
|
api: {
|
|
4
4
|
/**
|
|
@@ -18,10 +18,6 @@ async function _readBody(readable) {
|
|
|
18
18
|
}
|
|
19
19
|
return Buffer.concat(chunks).toString("utf8");
|
|
20
20
|
}
|
|
21
|
-
const {
|
|
22
|
-
isValidSignature,
|
|
23
|
-
SIGNATURE_HEADER_NAME
|
|
24
|
-
} = sanityWebhook;
|
|
25
21
|
async function parseBody(req, secret) {
|
|
26
22
|
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
27
23
|
let signature = req.headers[SIGNATURE_HEADER_NAME];
|
|
@@ -38,5 +34,18 @@ async function parseBody(req, secret) {
|
|
|
38
34
|
isValidSignature: validSignature
|
|
39
35
|
};
|
|
40
36
|
}
|
|
41
|
-
|
|
37
|
+
async function parseAppBody(req, secret) {
|
|
38
|
+
let waitForContentLakeEventualConsistency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
39
|
+
const signature = req.headers.get(SIGNATURE_HEADER_NAME);
|
|
40
|
+
const body = await req.text();
|
|
41
|
+
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null;
|
|
42
|
+
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
43
|
+
await new Promise(resolve => setTimeout(resolve, 1e3));
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
body: body.trim() && JSON.parse(body),
|
|
47
|
+
isValidSignature: validSignature
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export { config, parseAppBody, parseBody };
|
|
42
51
|
//# sourceMappingURL=webhook.js.map
|
package/dist/webhook.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhook.js","sources":["../src/webhook/config.ts","../src/webhook/readBody.ts","../src/webhook/parseBody.ts"],"sourcesContent":["import type {PageConfig} from 'next/types'\n\n/**\n * Configurates the API function with the right runtime and body parsing to handle Sanity Webhook events.\n * @public\n */\nexport const config: PageConfig = {\n api: {\n /**\n * Next.js will by default parse the body, which can lead to invalid signatures.\n */\n bodyParser: false,\n },\n /**\n * `@sanity/webhook` isn't updated to support the edge runtime yet, and currently requires Node.js APIs such as Buffer.\n */\n runtime: 'nodejs',\n}\n","import type {NextApiRequest} from 'next'\n\n/** @internal */\nexport async function _readBody(readable: NextApiRequest): Promise<string> {\n const chunks = []\n for await (const chunk of readable) {\n chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)\n }\n return Buffer.concat(chunks).toString('utf8')\n}\n","import type {SanityDocument} from '@sanity/types'\nimport
|
|
1
|
+
{"version":3,"file":"webhook.js","sources":["../src/webhook/config.ts","../src/webhook/readBody.ts","../src/webhook/parseBody.ts"],"sourcesContent":["import type {PageConfig} from 'next/types'\n\n/**\n * Configurates the API function with the right runtime and body parsing to handle Sanity Webhook events.\n * @public\n */\nexport const config: PageConfig = {\n api: {\n /**\n * Next.js will by default parse the body, which can lead to invalid signatures.\n */\n bodyParser: false,\n },\n /**\n * `@sanity/webhook` isn't updated to support the edge runtime yet, and currently requires Node.js APIs such as Buffer.\n */\n runtime: 'nodejs',\n}\n","import type {NextApiRequest} from 'next'\n\n/** @internal */\nexport async function _readBody(readable: NextApiRequest): Promise<string> {\n const chunks = []\n for await (const chunk of readable) {\n chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)\n }\n return Buffer.concat(chunks).toString('utf8')\n}\n","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 ParseBody = {\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: SanityDocument\n}\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(\n req: NextApiRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParseBody> {\n let signature = req.headers[SIGNATURE_HEADER_NAME]!\n if (Array.isArray(signature)) {\n signature = signature[0]\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),\n isValidSignature: validSignature,\n }\n}\n\n/** @public */\nexport type ParseAppBody = {\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: SanityDocument\n}\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 parseAppBody(\n req: NextRequest,\n secret?: string,\n waitForContentLakeEventualConsistency: boolean = true,\n): Promise<ParseAppBody> {\n const signature = req.headers.get(SIGNATURE_HEADER_NAME)!\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),\n isValidSignature: validSignature,\n }\n}\n"],"names":["config","api","bodyParser","runtime","_readBody","readable","chunks","chunk","push","Buffer","from","concat","toString","parseBody","req","secret","waitForContentLakeEventualConsistency","arguments","length","undefined","signature","headers","SIGNATURE_HEADER_NAME","Array","isArray","body","readBody","validSignature","isValidSignature","trim","Promise","resolve","setTimeout","JSON","parse","parseAppBody","get","text"],"mappings":";AAMO,MAAMA,MAAqB,GAAA;E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,EACuB;EAAA,IADvBC,qCAAA,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAiD,IAC1B;EACvB,MAAMG,SAAY,GAAAN,GAAA,CAAIO,OAAQ,CAAAe,GAAA,CAAId,qBAAqB,CAAA;EAEjD,MAAAG,IAAA,GAAO,MAAMX,GAAA,CAAIuB,IAAK,EAAA;EACtB,MAAAV,cAAA,GAAiBZ,SAASa,gBAAiB,CAAAH,IAAA,EAAML,WAAWL,MAAO,CAAAc,IAAA,EAAM,CAAI,GAAA,IAAA;EAE/E,IAAAF,cAAA,KAAmB,SAASX,qCAAuC,EAAA;IACrE,MAAM,IAAIc,OAAQ,CAACC,WAAYC,UAAW,CAAAD,OAAA,EAAS,GAAI,CAAC,CAAA;EAC1D;EAEO,OAAA;IACLN,MAAMA,IAAK,CAAAI,IAAA,CAAA,CAAU,IAAAI,IAAA,CAAKC,MAAMT,IAAI,CAAA;IACpCG,gBAAkB,EAAAD;EAAA,CACpB;AACF;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-sanity",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Sanity.io toolkit for Next.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -191,7 +191,7 @@
|
|
|
191
191
|
"jest": "^29.6.2",
|
|
192
192
|
"jest-environment-jsdom": "^29.6.2",
|
|
193
193
|
"ls-engines": "^0.9.0",
|
|
194
|
-
"next": "13.4.
|
|
194
|
+
"next": "13.4.13-canary.16",
|
|
195
195
|
"postcss": "^8.4.27",
|
|
196
196
|
"prettier": "^3.0.1",
|
|
197
197
|
"prettier-plugin-packagejson": "^2.4.5",
|
package/src/webhook/parseBody.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type {SanityDocument} from '@sanity/types'
|
|
2
|
-
import
|
|
2
|
+
import {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook'
|
|
3
3
|
import type {NextApiRequest} from 'next'
|
|
4
|
-
|
|
5
|
-
// As `@sanity/webhook` isn't shipping ESM, extracting from the default export have the best ecosystem support
|
|
6
|
-
const {isValidSignature, SIGNATURE_HEADER_NAME} = sanityWebhook
|
|
4
|
+
import type {NextRequest} from 'next/server'
|
|
7
5
|
|
|
8
6
|
import {_readBody as readBody} from './readBody'
|
|
9
7
|
|
|
@@ -42,3 +40,36 @@ export async function parseBody(
|
|
|
42
40
|
isValidSignature: validSignature,
|
|
43
41
|
}
|
|
44
42
|
}
|
|
43
|
+
|
|
44
|
+
/** @public */
|
|
45
|
+
export type ParseAppBody = {
|
|
46
|
+
/**
|
|
47
|
+
* 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`
|
|
48
|
+
*/
|
|
49
|
+
isValidSignature: boolean | null
|
|
50
|
+
body: SanityDocument
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Handles parsing the body JSON, and validating its signature. Also waits for Content Lake eventual consistency so you can run your queries
|
|
54
|
+
* without worrying about getting stale data.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export async function parseAppBody(
|
|
58
|
+
req: NextRequest,
|
|
59
|
+
secret?: string,
|
|
60
|
+
waitForContentLakeEventualConsistency: boolean = true,
|
|
61
|
+
): Promise<ParseAppBody> {
|
|
62
|
+
const signature = req.headers.get(SIGNATURE_HEADER_NAME)!
|
|
63
|
+
|
|
64
|
+
const body = await req.text()
|
|
65
|
+
const validSignature = secret ? isValidSignature(body, signature, secret.trim()) : null
|
|
66
|
+
|
|
67
|
+
if (validSignature !== false && waitForContentLakeEventualConsistency) {
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
body: body.trim() && JSON.parse(body),
|
|
73
|
+
isValidSignature: validSignature,
|
|
74
|
+
}
|
|
75
|
+
}
|