Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Allow specifying additional request templates in proxy method #876

Closed
1 of 2 tasks
thchia opened this issue Dec 16, 2022 · 3 comments
Closed
1 of 2 tasks

[core] Allow specifying additional request templates in proxy method #876

thchia opened this issue Dec 16, 2022 · 3 comments
Assignees
Labels
feature-request A feature should be added or improved needs-triage The issue or PR still needs to be triaged

Comments

@thchia
Copy link

thchia commented Dec 16, 2022

I would like to request for the ability to specify additional content-type mappings for request templates in the ApiGatewayToSqs construct. Internally, this is using the addProxyMethodToApiResource method in the core library. The method hardcodes the AWSIntegration props, allowing just a single request template for application/json.

Use Case

I am using the ApiGatewayToSqs construct, but the caller of the endpoint will be making requests with Content-Type of text/plain. Currently this results in a 415 error response, as it is configured with a body passthrough value of None.

Proposed Solution

The method already accepts a parameter called requestTemplate, but:

  • this only lets us change the mapped request, not the content-type for which it applies
  • for this reason, it's a bit of a misnomer, since in my understanding the "Request Template" is both the content-type and the mapped request body.

To avoid breaking changes, I can propose adding a new parameter to the addProxyMethodToApiResource method, something like additionalRequestTemplates, which accepts a map of content-type to mapping.

addProxyMethodToApiResource({
  // ...
  additionalRequestTemplates: {
    'text/plain': '...'
  }
})

A similar thing can be done for integrationResponses, so that we can close the request/response loop.

For my specific issue, these changes would be accompanied by a similar change in the ApiGatewayToSqs construct to allow the additional request template to be passed in.

Workaround

I have worked around this by manipulating the low level objects in CDK, but it's unwieldy to say the least.

// This is the method to which we need to add the request template and responses
// It's the method that creates messages in the queue
const createMessageMethod = api.apiGateway.methods.find(
  (method) => method.httpMethod.toUpperCase() === "POST"
);

// Should never be the case
if (!createMessageMethod) return;

// The property we need to change is marked as readonly by TypeScript
// This fudges it so we can change it
type MutableCfnMethodIntegration = {
  -readonly [K in keyof CfnMethod.IntegrationProperty]: CfnMethod.IntegrationProperty[K];
};

const cfnMethod = createMessageMethod.node.defaultChild as CfnMethod;
const integration = cfnMethod.integration as MutableCfnMethodIntegration;

integration.requestTemplates = Object.assign(
  integration.requestTemplates,
  { "text/plain": "..." }
);

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@thchia thchia added feature-request A feature should be added or improved needs-triage The issue or PR still needs to be triaged labels Dec 16, 2022
@biffgaut
Copy link
Contributor

Thanks, we'll take a look

@georgebearden
Copy link
Contributor

Hi @thchia - Can you please have a look at #888, which should solve this issue. We still need to implement the optional properties across the other ApiGateway-based constructs, but this would be the pattern.

@georgebearden
Copy link
Contributor

#888 is now merged and will be available in the next release. It also added support for custom integration responses. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved needs-triage The issue or PR still needs to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants