Skip to content

Commit

Permalink
feat(typescript): deduplicate Sender type (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Dec 16, 2020
1 parent 4e51a55 commit 87b514e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1,005 deletions.
29 changes: 24 additions & 5 deletions scripts/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ const doNotEditThisFileDisclaimer = `
// make edits in scripts/generate-types.js`;
const eventPayloadsVariable = "EventPayloads";

const generatePayloadType = (typeName) => ({
rootTypeName: typeName,
namedKeyPaths: {
const generatePayloadType = (typeName) => {
const namedKeyPaths = {
[`${typeName}.repository`]: "PayloadRepository",
// This prevents a naming colision between the payload of a `installation_repositories` event
// and the `repositories` attribute of a `installation` event
"WebhookPayloadInstallation.repositories":
"WebhookPayloadInstallation_Repositories",
},
});
};

if (typeName !== "WebhookPayloadMarketplacePurchase") {
namedKeyPaths[`${typeName}.sender`] = "PayloadSender";
}

return {
rootTypeName: typeName,
namedKeyPaths,
};
};

const generateEventNameType = (name, actions) => [
name,
Expand Down Expand Up @@ -57,6 +65,17 @@ webhooks.forEach(({ name, actions, examples }) => {
});
});

tw.add(
webhooks.flatMap(({ examples }) =>
examples
.map((example) => example.sender)
.filter((sender) => sender !== undefined)
),
{
rootTypeName: "PayloadSender",
}
);

const getWebhookPayloadTypeFromEvent = `
${doNotEditThisFileDisclaimer}
Expand Down
Loading

0 comments on commit 87b514e

Please sign in to comment.