Skip to content

Commit 0b7dcf3

Browse files
authored
Merging pull request #18768
1 parent 9b6132e commit 0b7dcf3

File tree

4 files changed

+139
-154
lines changed

4 files changed

+139
-154
lines changed

components/aftership/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/aftership",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Pipedream AfterShip Components",
55
"main": "aftership.app.mjs",
66
"keywords": [
@@ -13,7 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^1.5.1"
16+
"@pipedream/platform": "^3.1.0",
17+
"crypto": "^1.0.1"
1718
}
1819
}
19-
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { createHmac } from "crypto";
2+
import { ConfigurationError } from "@pipedream/platform";
3+
import app from "../../aftership.app.mjs";
4+
5+
export default {
6+
props: {
7+
app,
8+
http: {
9+
type: "$.interface.http",
10+
customResponse: true,
11+
},
12+
webhookSecret: {
13+
type: "string",
14+
label: "Secret",
15+
description: "The secret for the webhook. You can find it in `Tracking -> Settings -> Webhooks -> Webhook secret`",
16+
},
17+
},
18+
hooks: {
19+
async deploy() {
20+
if (!this.webhookSecret) {
21+
console.log("No webhook secret was provided, skipping deployment");
22+
throw new ConfigurationError("No webhook secret was provided, skipping deployment");
23+
}
24+
25+
const { data: { trackings } } = await this.app.listTrackings({
26+
params: {
27+
page: 1,
28+
limit: 25,
29+
},
30+
});
31+
32+
trackings
33+
.slice(0, 25)
34+
.reverse()
35+
.forEach((tracking) => {
36+
this.processResource({
37+
event: this.getEventName(),
38+
event_id: tracking.id,
39+
msg: tracking,
40+
ts: Date.parse(tracking.updated_at) / 1000,
41+
});
42+
});
43+
},
44+
},
45+
methods: {
46+
generateMeta() {
47+
throw new ConfigurationError("generateMeta is not implemented");
48+
},
49+
getEventName() {
50+
throw new ConfigurationError("getEventName is not implemented");
51+
},
52+
isSignatureValid(bodyRaw, signature) {
53+
const { webhookSecret } = this;
54+
if (!webhookSecret) {
55+
console.log("No webhook secret found, skipping signature verification");
56+
return true;
57+
}
58+
const hash = createHmac("sha256", webhookSecret)
59+
.update(bodyRaw)
60+
.digest("base64");
61+
return hash === signature;
62+
},
63+
processResource(resource) {
64+
this.$emit(resource, this.generateMeta(resource));
65+
},
66+
},
67+
async run({
68+
body, bodyRaw, headers,
69+
}) {
70+
const signature = headers["aftership-hmac-sha256"];
71+
72+
if (!this.isSignatureValid(bodyRaw, signature)) {
73+
console.log("Could not verify incoming webhook signature");
74+
return this.http.respond({
75+
status: 401,
76+
});
77+
}
78+
79+
this.http.respond({
80+
status: 200,
81+
});
82+
83+
this.processResource(body);
84+
},
85+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import common from "../common/webhook.mjs";
2+
3+
export default {
4+
...common,
5+
key: "aftership-shipment-status-updated",
6+
name: "Shipment Status Updated",
7+
description: "Emit new event when a shipment tracking status is updated. [See the documentation](https://www.aftership.com/docs/shipping/webhook/webhook-overview)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getEventName() {
14+
return "tracking_update";
15+
},
16+
generateMeta(resource) {
17+
const {
18+
event_id: eventId,
19+
msg: tracking,
20+
} = resource;
21+
22+
return {
23+
id: eventId || tracking.id,
24+
summary: `Tracking Updated: ${tracking.tracking_number || tracking.title}`,
25+
ts: Date.parse(tracking.updated_at) || Date.now(),
26+
};
27+
},
28+
},
29+
};

0 commit comments

Comments
 (0)