feat: CORS enable button...ish still now understanding this

This commit is contained in:
Zakaria
2026-09-27 14:46:04 -04:00
parent 8470b8d17a
commit b474e823ce
17 changed files with 943 additions and 31 deletions
@@ -11,7 +11,9 @@ import {
GetResourcesCommand,
GetStagesCommand,
PutIntegrationCommand,
PutIntegrationResponseCommand,
PutMethodCommand,
PutMethodResponseCommand,
type APIGatewayClient,
} from '@aws-sdk/client-api-gateway'
import {AddPermissionCommand, GetFunctionCommand, RemovePermissionCommand, type LambdaClient} from '@aws-sdk/client-lambda'
@@ -280,8 +282,8 @@ describe('AwsApiGatewayAdapter', () => {
await expect(adapter.listApiGatewayRoutes('a1b2c3')).resolves.toEqual({
routes: [
{resourceId: 'resource456', path: '/hello', method: 'GET', functionName: 'hello'},
{resourceId: 'resource456', path: '/hello', method: 'POST', functionName: 'hello'},
{resourceId: 'resource456', path: '/hello', method: 'GET', functionName: 'hello', integrationType: null},
{resourceId: 'resource456', path: '/hello', method: 'POST', functionName: 'hello', integrationType: null},
],
stages: ['dev', 'prod'],
})
@@ -308,6 +310,73 @@ describe('AwsApiGatewayAdapter', () => {
expect((lambdaCommands[0] as RemovePermissionCommand).input).toMatchObject({FunctionName: 'hello', StatementId: 'apigateway-a1b2c3-dev-POST-hello'})
})
test('enables CORS for non-proxy methods, creates OPTIONS, and reports proxy methods', async () => {
const commands: unknown[] = []
const adapter = new AwsApiGatewayAdapter(fakeClient((command) => {
commands.push(command)
if (command instanceof GetMethodCommand && command.input.httpMethod === 'GET') {
return {
methodResponses: {200: {
responseParameters: {'method.response.header.X-Existing': true},
responseModels: {'application/json': 'ExistingModel'},
}},
methodIntegration: {type: 'HTTP', integrationResponses: {200: {
responseParameters: {'method.response.header.X-Existing': "'existing'"},
responseTemplates: {'application/json': '$input.body'},
}}},
}
}
if (command instanceof GetMethodCommand && command.input.httpMethod === 'POST') {
return {methodIntegration: {type: 'AWS_PROXY'}}
}
if (command instanceof GetMethodCommand && command.input.httpMethod === 'OPTIONS') {
return {
methodResponses: {200: {
responseParameters: {'method.response.header.Access-Control-Allow-Origin': true},
responseModels: {'application/json': 'OptionsModel'},
}},
methodIntegration: {timeoutInMillis: 29000, integrationResponses: {200: {
responseTemplates: {'text/plain': 'existing template'},
}}},
}
}
return {}
}))
await expect(adapter.enableApiGatewayCors('a1b2c3', {
resourceId: 'resource456', stage: 'dev', origin: '*', headers: 'Content-Type', methods: ['GET', 'POST'],
})).resolves.toEqual({configuredMethods: ['GET'], proxyMethods: ['POST']})
expect(commands.map((command) => (command as {constructor: {name: string}}).constructor.name)).toEqual([
'GetMethodCommand', 'PutMethodResponseCommand', 'PutIntegrationResponseCommand', 'GetMethodCommand', 'GetMethodCommand',
'PutMethodCommand', 'PutMethodResponseCommand', 'PutIntegrationCommand', 'PutIntegrationResponseCommand', 'CreateDeploymentCommand',
])
expect((commands[1] as PutMethodResponseCommand).input.responseParameters).toEqual({
'method.response.header.X-Existing': true,
'method.response.header.Access-Control-Allow-Origin': false,
'method.response.header.Access-Control-Allow-Headers': false,
'method.response.header.Access-Control-Allow-Methods': false,
})
expect((commands[1] as PutMethodResponseCommand).input.responseModels).toEqual({'application/json': 'ExistingModel'})
expect((commands[2] as PutIntegrationResponseCommand).input.responseParameters).toMatchObject({
'method.response.header.X-Existing': "'existing'",
'method.response.header.Access-Control-Allow-Origin': "'*'",
'method.response.header.Access-Control-Allow-Headers': "'Content-Type'",
'method.response.header.Access-Control-Allow-Methods': "'GET,POST,OPTIONS'",
})
expect((commands[2] as PutIntegrationResponseCommand).input.responseTemplates).toEqual({'application/json': '$input.body'})
expect((commands[5] as PutMethodCommand).input).toMatchObject({httpMethod: 'OPTIONS', authorizationType: 'NONE'})
expect((commands[6] as PutMethodResponseCommand).input).toMatchObject({
responseParameters: {'method.response.header.Access-Control-Allow-Origin': true},
responseModels: {'application/json': 'OptionsModel'},
})
expect((commands[7] as PutIntegrationCommand).input).toMatchObject({
type: 'MOCK', requestTemplates: {'application/json': '{"statusCode": 200}'}, timeoutInMillis: 29000,
})
expect((commands[8] as PutIntegrationResponseCommand).input.responseTemplates).toEqual({'text/plain': 'existing template'})
expect((commands[9] as CreateDeploymentCommand).input).toMatchObject({stageName: 'dev'})
})
test('returns the AWS API Gateway schema', () => {
const adapter = new AwsApiGatewayAdapter(fakeClient(() => ({})))