Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/aftership/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/aftership",
"version": "0.2.0",
"version": "0.3.0",
"description": "Pipedream AfterShip Components",
"main": "aftership.app.mjs",
"keywords": [
Expand All @@ -13,7 +13,7 @@
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.5.1"
"@pipedream/platform": "^3.1.0",
"crypto": "^1.0.1"
}
}

85 changes: 85 additions & 0 deletions components/aftership/sources/common/webhook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { createHmac } from "crypto";
import { ConfigurationError } from "@pipedream/platform";
import app from "../../aftership.app.mjs";

export default {
props: {
app,
http: {
type: "$.interface.http",
customResponse: true,
},
webhookSecret: {
type: "string",
label: "Secret",
description: "The secret for the webhook. You can find it in `Tracking -> Settings -> Webhooks -> Webhook secret`",
},
},
hooks: {
async deploy() {
if (!this.webhookSecret) {
console.log("No webhook secret was provided, skipping deployment");
throw new ConfigurationError("No webhook secret was provided, skipping deployment");
}

const { data: { trackings } } = await this.app.listTrackings({
params: {
page: 1,
limit: 25,
},
});

trackings
.slice(0, 25)
.reverse()
.forEach((tracking) => {
this.processResource({
event: this.getEventName(),
event_id: tracking.id,
msg: tracking,
ts: Date.parse(tracking.updated_at) / 1000,
});
});
},
},
methods: {
generateMeta() {
throw new ConfigurationError("generateMeta is not implemented");
},
getEventName() {
throw new ConfigurationError("getEventName is not implemented");
},
isSignatureValid(bodyRaw, signature) {
const { webhookSecret } = this;
if (!webhookSecret) {
console.log("No webhook secret found, skipping signature verification");
return true;
}
const hash = createHmac("sha256", webhookSecret)
.update(bodyRaw)
.digest("base64");
return hash === signature;
},
processResource(resource) {
this.$emit(resource, this.generateMeta(resource));
},
},
async run({
body, bodyRaw, headers,
}) {
const signature = headers["aftership-hmac-sha256"];

if (!this.isSignatureValid(bodyRaw, signature)) {
console.log("Could not verify incoming webhook signature");
return this.http.respond({
status: 401,
});
}

this.http.respond({
status: 200,
});

this.processResource(body);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import common from "../common/webhook.mjs";

export default {
...common,
key: "aftership-shipment-status-updated",
name: "Shipment Status Updated",

Check warning on line 6 in components/aftership/sources/shipment-status-updated/shipment-status-updated.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source names should start with "New". See https://pipedream.com/docs/components/guidelines/#source-name
description: "Emit new event when a shipment tracking status is updated. [See the documentation](https://www.aftership.com/docs/shipping/webhook/webhook-overview)",
version: "0.0.1",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
getEventName() {
return "tracking_update";
},
generateMeta(resource) {
const {
event_id: eventId,
msg: tracking,
} = resource;

return {
id: eventId || tracking.id,
summary: `Tracking Updated: ${tracking.tracking_number || tracking.title}`,
ts: Date.parse(tracking.updated_at) || Date.now(),
};
},
},
};
Loading
Loading