Nayan Patel
Nayan Patel

Reputation: 1761

Getting an error when using @middy/http-cors middleware having dynamic origin

Getting an error -

"Middleware must be an object containing at least one key among \"before\", \"after\",\"forerror\"

The below is my code -

export function corsMiddleware(): any {
 const corsMiddleWareBefore = async (request: any) => {
 const context = request.context as APIGatewayProxyEventV2;
 const configs: ConfigSettings = (context as any).configs;
 const subDomain = configs.get('BaseUrlPrefix');
 const fullUrls = configs.get('UrlWhitelist').map(domain =>`${subDomain}${domain
 }`);
 const fullUrlsString = fullUrls.join(', ');
 console.log(fullUrlsString);
 const corsFunctions = cors({
   origin: fullUrls,
   headers: 'Content-Type, X-Jic-Token',
   methods: 'GET,OPTIONS',
 });
 console.log(corsFunctions);

 return corsFunctions;
 };
return corsMiddleWareBefore;
}

When I console the corsFunctions, it contains the object with all three properties, before, after and onError. But when we return it gives this error.

Upvotes: 0

Views: 144

Answers (0)

Related Questions