Skip to content

Commit

Permalink
Merge pull request #341 from SludgeGirl/add-method-setting-for-webhooks
Browse files Browse the repository at this point in the history
Add in method setting for webhooks
  • Loading branch information
fredericbarthelet committed Sep 19, 2023
2 parents 1e786ed + aa7931e commit fc3cfc3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ constructs:
authorizer:
handler: myAuthorizer.main
path: /my-webhook-endpoint
method: POST

plugins:
- serverless-lift
Expand Down Expand Up @@ -162,6 +163,23 @@ constructs:

Always favor dynamic path selector to ensure the minimum amount of compute is executed downstream. The list of available dynamic selector is available in [API Gateway documentation](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-aws-services.html#http-api-develop-integrations-aws-services-parameter-mapping).

### Method

_Optional_
Defaults to `POST`

This is the HTTP method the webhook will accept. It can be any of the following:
- `POST`
- `PUT`
- `PATCH`

```yaml
constructs:
stripe:
# ...
method: POST
```

## Extensions

You can specify an `extensions` property on the webhook construct to extend the underlying CloudFormation resources. In the exemple below, the EventBridge Bus CloudFormation resource generated by the `stripe` webhook construct will be extended with the new `Name: StripeBus` CloudFormation property.
Expand Down
6 changes: 5 additions & 1 deletion src/constructs/aws/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ const WEBHOOK_DEFINITION = {
insecure: { type: "boolean" },
path: { type: "string" },
eventType: { type: "string" },
method: { enum: ["POST", "PUT", "PATCH"] },
},
required: ["path"],
additionalProperties: false,
} as const;
const WEBHOOK_DEFAULTS = {
insecure: false,
method: "POST",
};

type Configuration = FromSchema<typeof WEBHOOK_DEFINITION>;
Expand All @@ -46,6 +48,7 @@ export class Webhook extends AwsConstruct {
private readonly bus: EventBus;
private readonly apiEndpointOutput: CfnOutput;
private readonly endpointPathOutput: CfnOutput;
private readonly method: string;

constructor(
scope: CdkConstruct,
Expand Down Expand Up @@ -108,9 +111,10 @@ export class Webhook extends AwsConstruct {
EventBusName: this.bus.eventBusName,
},
});
this.method = resolvedConfiguration.method;
const route = new CfnRoute(this, "Route", {
apiId: this.api.apiId,
routeKey: `POST ${resolvedConfiguration.path}`,
routeKey: `${this.method} ${resolvedConfiguration.path}`,
target: Fn.join("/", ["integrations", eventBridgeIntegration.ref]),
authorizationType: "NONE",
});
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/webhooks/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ constructs:
bus:
Properties:
Name: myBus
post:
type: webhook
authorizer:
handler: authorizer.main
path: /post
method: POST
put:
type: webhook
authorizer:
handler: authorizer.main
path: /put
method: PUT
patch:
type: webhook
authorizer:
handler: authorizer.main
path: /patch
method: PATCH
10 changes: 10 additions & 0 deletions test/unit/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ describe("webhooks", () => {
});
});

test.each([
["post", "POST"],
["put", "PUT"],
["patch", "PATCH"],
])("%p webhook should have method %p", (useCase, expectedMethod) => {
expect(cfTemplate.Resources[computeLogicalId(useCase, "Route")]["Properties"]).toMatchObject({
RouteKey: expectedMethod + " /" + expectedMethod.toLowerCase(),
});
});

it("allows overriding webhook properties", () => {
expect(cfTemplate.Resources[computeLogicalId("extendedWebhook", "Bus")].Properties).toMatchObject({
Name: "myBus",
Expand Down

0 comments on commit fc3cfc3

Please sign in to comment.