Skip to content

Commit

Permalink
feat(apigatewayv2): websocket - callback url (#15227)
Browse files Browse the repository at this point in the history
I added property to extract websocket callback endpoint url to WebSocketStage.
I added it simply like existing `url` property.

It's my first time to submit PR.
Please point out any mistakes.

closes #14836 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
bitoku committed Jul 16, 2021
1 parent ebba618 commit 349de7c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
Expand Up @@ -311,6 +311,15 @@ new WebSocketStage(stack, 'mystage', {
});
```

To retrieve a websocket URL and a callback URL:

```ts
const webSocketURL = webSocketStage.url;
// wss://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}
const callbackURL = webSocketURL.callbackUrl;
// https://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}
```

To add any other route:

```ts
Expand Down
23 changes: 22 additions & 1 deletion packages/@aws-cdk/aws-apigatewayv2/lib/websocket/stage.ts
Expand Up @@ -13,6 +13,14 @@ export interface IWebSocketStage extends IStage {
* The API this stage is associated to.
*/
readonly api: IWebSocketApi;

/**
* The callback URL to this stage.
* You can use the callback URL to send messages to the client from the backend system.
* https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-basic-concept.html
* https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html
*/
readonly callbackUrl: string;
}

/**
Expand Down Expand Up @@ -57,6 +65,10 @@ export class WebSocketStage extends StageBase implements IWebSocketStage {
get url(): string {
throw new Error('url is not available for imported stages.');
}

get callbackUrl(): string {
throw new Error('callback url is not available for imported stages.');
}
}
return new Import(scope, id);
}
Expand Down Expand Up @@ -86,11 +98,20 @@ export class WebSocketStage extends StageBase implements IWebSocketStage {
}

/**
* The URL to this stage.
* The websocket URL to this stage.
*/
public get url(): string {
const s = Stack.of(this);
const urlPath = this.stageName;
return `wss://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}`;
}

/**
* The callback URL to this stage.
*/
public get callbackUrl(): string {
const s = Stack.of(this);
const urlPath = this.stageName;
return `https://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}`;
}
}
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2/test/websocket/stage.test.ts
Expand Up @@ -40,5 +40,23 @@ describe('WebSocketStage', () => {

// THEN
expect(imported.stageName).toEqual(stage.stageName);
expect(() => imported.url).toThrow();
expect(() => imported.callbackUrl).toThrow();
});

test('callback URL', () => {
// GIVEN
const stack = new Stack();
const api = new WebSocketApi(stack, 'Api');

// WHEN
const defaultStage = new WebSocketStage(stack, 'Stage', {
webSocketApi: api,
stageName: 'dev',
});

// THEN
expect(defaultStage.callbackUrl.endsWith('/dev')).toBe(true);
expect(defaultStage.callbackUrl.startsWith('https://')).toBe(true);
});
});

0 comments on commit 349de7c

Please sign in to comment.