Artemiy Davydov
03/16/2022, 11:14 AMArtemiy Davydov
03/16/2022, 11:17 AMcors is falseFrank
Frank
Api and the code for this Lambda function?Artemiy Davydov
03/16/2022, 1:43 PMFrank
Artemiy Davydov
03/16/2022, 1:46 PMimport {
APIGatewayProxyEventV2,
APIGatewayProxyStructuredResultV2,
} from "aws-lambda";
import Joi from "joi";
import { User } from "models/user";
import { auth } from "libs/auth";
import cookie from "cookie";
const defaultHeaders = (event: APIGatewayProxyEventV2) => ({
"Access-Control-Request-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
"Access-Control-Request-Method": "OPTIONS, POST, GET, PUT",
"Access-Control-Allow-Origin": event.headers.origin || "",
"Access-Control-Allow-Credentials": "true",
});
/* eslint-disable @typescript-eslint/no-explicit-any */
const logic = async <T>(
event: APIGatewayProxyEventV2,
schema: Joi.ObjectSchema<T>,
handler: (
data: T & { user: User }
) => Promise<APIGatewayProxyStructuredResultV2 | Record<string, unknown>>,
user: User,
plain?: boolean
): Promise<APIGatewayProxyStructuredResultV2> => {
const data =
event.requestContext.http.method.toLowerCase() === "get"
? { ...event.pathParameters, ...event.queryStringParameters }
: { ...event.pathParameters, ...JSON.parse(event.body ?? "") };
const validator = schema.validate(data);
if (validator.error) {
return {
statusCode: 400,
headers: defaultHeaders(event),
};
}
try {
const data = await handler({ ...validator.value, user });
return plain
? {
...data,
...{
headers: {
...(data as APIGatewayProxyStructuredResultV2).headers,
...defaultHeaders(event),
},
},
}
: {
statusCode: 200,
body: JSON.stringify(data, null, 2),
headers: defaultHeaders(event),
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
headers: defaultHeaders(event),
};
}
};
export const wrapper = async <T>(
event: APIGatewayProxyEventV2,
schema: Joi.ObjectSchema<T>,
handler: (
data: T & { user: User }
) => Promise<APIGatewayProxyStructuredResultV2 | { [key: string]: any }>,
isNeedToAuth = false,
plain = false
): Promise<APIGatewayProxyStructuredResultV2> => {
const { token } = cookie.parse(event.headers.cookie || "");
if (isNeedToAuth) {
try {
if (!token) return { statusCode: 401, headers: defaultHeaders(event) };
try {
const { email = "" } = await auth.getTokenInfo(token);
const user = { email, token };
return await logic(event, schema, handler, user, plain);
} catch (error) {
console.error(error);
return { statusCode: 401, headers: defaultHeaders(event) };
}
} catch (error) {
console.error(error);
return { statusCode: 500, headers: defaultHeaders(event) };
}
} else return await logic(event, schema, handler, undefined as any, plain);
};
users/me/handler.ts for example
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
import { wrapper } from "libs/wrapper";
import { schema } from "./schema";
export const main = (
event: APIGatewayProxyEventV2
): Promise<APIGatewayProxyResultV2> => {
return wrapper(event, schema, async ({ user }) => user, true);
};Frank
curl <https://ioe7hbv67f.execute-api.us-east-1.amazonaws.com/123>Frank
Artemiy Davydov
03/16/2022, 1:47 PMArtemiy Davydov
03/16/2022, 1:48 PMFrank
Artemiy Davydov
03/16/2022, 1:49 PMFrank
cors: true on the API, that doesn’t add CORS headers to the response for the API routes u defined.Frank
cors: true, a new OPTION route is added.Frank
OPTION route has the CORS headers in its response.Frank
Artemiy Davydov
03/16/2022, 1:52 PMcors turned off (false), headers that should not be returned (these are old headers)Frank
headers that should not be returned (these are old headers)Are you referring to the headers you are returning inside ur Lambda function? ie.
const defaultHeaders = (event: APIGatewayProxyEventV2) => ({
"Access-Control-Request-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
"Access-Control-Request-Method": "OPTIONS, POST, GET, PUT",
"Access-Control-Allow-Origin": event.headers.origin || "",
"Access-Control-Allow-Credentials": "true",
});Artemiy Davydov
03/16/2022, 1:54 PMexpose-headers. Even with cors is falseArtemiy Davydov
03/16/2022, 1:55 PMFrank
Artemiy Davydov
03/16/2022, 2:02 PMFrank
Frank
Frank
cors: true and see if the these requests get through preflight check?Artemiy Davydov
03/16/2022, 5:15 PMArtemiy Davydov
03/16/2022, 5:15 PMFrank
cors:true right?Frank
Still wrong headers, you mean this field?Artemiy Davydov
03/16/2022, 5:19 PMFrank
access-control-allow-origin is returned by ur Lambda function right?Frank
cors:true, the OPTION request succeeds now. And the GET request is failing with 500. And this access-control-allow-origin is in the response header of the GET request. Correct?Artemiy Davydov
03/17/2022, 10:46 AMheaders.origin instead of * for access-control-allow-origin