Skip to content

Commit f6b1d0b

Browse files
committed
Rewrite to TypeScript
1 parent 3b1fa41 commit f6b1d0b

File tree

5 files changed

+134
-52
lines changed

5 files changed

+134
-52
lines changed

functions/kontent_webhook_generic.js

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { DeliveryClient } from '@kontent-ai/delivery-sdk';
2+
import { signatureHelper } from '@kontent-ai/webhook-helper';
3+
import { Handler } from "@netlify/functions";
4+
import {
5+
IWebhookDeliveryData,
6+
IWebhookResponse
7+
} from "@kontent-ai/webhook-helper/dist/cjs/models/webhook-models.class.ts";
8+
9+
const CONFIG_DELIMITER = ",";
10+
11+
export const handler: Handler = async (event) => {
12+
13+
// Only receiving POST requests
14+
if (event.httpMethod !== "POST") {
15+
return { statusCode: 405, body: "Method Not Allowed" };
16+
}
17+
18+
if (!event.body) {
19+
return { statusCode: 400, body: "Missing body" };
20+
}
21+
22+
// Consistency check - make sure your netlify enrionment variable and your webhook secret matches
23+
if (!signatureHelper.isValidSignatureFromString(event.body, process.env.KONTENT_SECRET ?? '', event.headers['x-kc-signature'] ?? '')) {
24+
return { statusCode: 401, body: "Unauthorized" };
25+
}
26+
27+
const jsonBody: IWebhookResponse<IWebhookDeliveryData> = JSON.parse(event.body);
28+
const webhookMessage = jsonBody.message;
29+
const webhookData = jsonBody.data;
30+
31+
// extracting projectId, operation & type from a webhook message
32+
const projectId = webhookMessage.project_id;
33+
const operation = webhookMessage.operation;
34+
const type = webhookMessage.type;
35+
36+
// loading allowed operations and types from config
37+
const allowedOperations = process.env.ALLOWED_OPERATIONS?.split(CONFIG_DELIMITER) ?? [];
38+
const allowedTypes = process.env.ALLOWED_TYPES?.split(CONFIG_DELIMITER) ?? [];
39+
40+
// if the method or type is not allowed abort
41+
if (projectId && allowedOperations.includes(operation) && allowedTypes.includes(type)) {
42+
return { statusCode: 200, body: '[]' };
43+
}
44+
45+
// download the affected items
46+
const deliveryClient = new DeliveryClient({ projectId });
47+
const response = await Promise.all(webhookData.items.map(item => deliveryClient.item(item.codename).languageParameter(item.language).toPromise()));
48+
49+
// print the affected content items into function log
50+
console.log(JSON.stringify(response));
51+
52+
return {
53+
statusCode: 200,
54+
body: `${JSON.stringify(response)}`,
55+
};
56+
};

package-lock.json

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"dependencies": {
66
"@kontent-ai/delivery-sdk": "^12.1.0",
7-
"@kontent-ai/webhook-helper": "^2.0.1"
7+
"@kontent-ai/webhook-helper": "^2.0.1",
8+
"@netlify/functions": "^1.2.0"
89
},
910
"repository": {
1011
"type": "git",
@@ -15,5 +16,8 @@
1516
"bugs": {
1617
"url": "https://github.com/kontent-ai/netlify-webhook-processor/issues"
1718
},
18-
"homepage": "https://github.com/kontent-ai/netlify-webhook-processor#readme"
19+
"homepage": "https://github.com/kontent-ai/netlify-webhook-processor#readme",
20+
"devDependencies": {
21+
"@types/node": "^18.7.17"
22+
}
1923
}

tsconfig.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [
5+
"dom",
6+
"esnext"
7+
],
8+
"types": [
9+
"node"
10+
],
11+
"allowJs": true,
12+
"skipLibCheck": true,
13+
"esModuleInterop": true,
14+
"allowSyntheticDefaultImports": true,
15+
"strict": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"noFallthroughCasesInSwitch": true,
18+
"module": "esnext",
19+
"moduleResolution": "node",
20+
"resolveJsonModule": true,
21+
"isolatedModules": true
22+
},
23+
"include": [
24+
"src"
25+
]
26+
}

0 commit comments

Comments
 (0)