Skip to content

Commit fc3cfc3

Browse files
Merge pull request #341 from SludgeGirl/add-method-setting-for-webhooks
Add in method setting for webhooks
2 parents 1e786ed + aa7931e commit fc3cfc3

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

docs/webhook.md

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ constructs:
2020
authorizer:
2121
handler: myAuthorizer.main
2222
path: /my-webhook-endpoint
23+
method: POST
2324

2425
plugins:
2526
- serverless-lift
@@ -162,6 +163,23 @@ constructs:
162163

163164
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).
164165

166+
### Method
167+
168+
_Optional_
169+
Defaults to `POST`
170+
171+
This is the HTTP method the webhook will accept. It can be any of the following:
172+
- `POST`
173+
- `PUT`
174+
- `PATCH`
175+
176+
```yaml
177+
constructs:
178+
stripe:
179+
# ...
180+
method: POST
181+
```
182+
165183
## Extensions
166184

167185
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.

src/constructs/aws/Webhook.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ const WEBHOOK_DEFINITION = {
2828
insecure: { type: "boolean" },
2929
path: { type: "string" },
3030
eventType: { type: "string" },
31+
method: { enum: ["POST", "PUT", "PATCH"] },
3132
},
3233
required: ["path"],
3334
additionalProperties: false,
3435
} as const;
3536
const WEBHOOK_DEFAULTS = {
3637
insecure: false,
38+
method: "POST",
3739
};
3840

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

5053
constructor(
5154
scope: CdkConstruct,
@@ -108,9 +111,10 @@ export class Webhook extends AwsConstruct {
108111
EventBusName: this.bus.eventBusName,
109112
},
110113
});
114+
this.method = resolvedConfiguration.method;
111115
const route = new CfnRoute(this, "Route", {
112116
apiId: this.api.apiId,
113-
routeKey: `POST ${resolvedConfiguration.path}`,
117+
routeKey: `${this.method} ${resolvedConfiguration.path}`,
114118
target: Fn.join("/", ["integrations", eventBridgeIntegration.ref]),
115119
authorizationType: "NONE",
116120
});

test/fixtures/webhooks/serverless.yml

+18
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,21 @@ constructs:
2424
bus:
2525
Properties:
2626
Name: myBus
27+
post:
28+
type: webhook
29+
authorizer:
30+
handler: authorizer.main
31+
path: /post
32+
method: POST
33+
put:
34+
type: webhook
35+
authorizer:
36+
handler: authorizer.main
37+
path: /put
38+
method: PUT
39+
patch:
40+
type: webhook
41+
authorizer:
42+
handler: authorizer.main
43+
path: /patch
44+
method: PATCH

test/unit/webhooks.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ describe("webhooks", () => {
2121
});
2222
});
2323

24+
test.each([
25+
["post", "POST"],
26+
["put", "PUT"],
27+
["patch", "PATCH"],
28+
])("%p webhook should have method %p", (useCase, expectedMethod) => {
29+
expect(cfTemplate.Resources[computeLogicalId(useCase, "Route")]["Properties"]).toMatchObject({
30+
RouteKey: expectedMethod + " /" + expectedMethod.toLowerCase(),
31+
});
32+
});
33+
2434
it("allows overriding webhook properties", () => {
2535
expect(cfTemplate.Resources[computeLogicalId("extendedWebhook", "Bus")].Properties).toMatchObject({
2636
Name: "myBus",

0 commit comments

Comments
 (0)