Having a Typescript issue when attempting to `gran...
# help
p
Having a Typescript issue when attempting to
grantDataApiAccess()
to an RDS Serverless Cluster. It looks like the type signature for the return value of
api.getFunction()
(
Fn | undefined
) doesn’t match what
grantDataApiAccess
is looking for (
IGrantable
). Has anybody encountered this issue before? Thanks for the help! Code:
Copy code
cluster.grantDataApiAccess(api.getFunction('GET /test'));
Error:
Copy code
Argument of type 'Function | undefined' is not assignable to parameter of type 'IGrantable'.
  Type 'undefined' is not assignable to type 'IGrantable'.ts(2345)
t
It's because technically it can come back undefined, if you know for sure it can't try this
Copy code
cluster.grantDataApiAccess(api.getFunction('GET /test')!);
note exclamation mark at the end
p
I see - makes sense. My linter will definitely object to this. perhaps wrapping it in an undefined check beforehand
t
The exclamation should assert it's not null
p
right, my linter rules disallow non-null assertion. I’m cool just adding a check before setting even if’s obviously set
Giving this a try since I probably want to eventually grant multiple functions access:
Copy code
// Grant access to the cluster from the Lambda function
const clusterAccessRoutes = ["GET /test"];
clusterAccessRoutes.forEach(route => {
  const fnHandler = api.getFunction(route);
  if (fnHandler) {
    cluster.grantDataApiAccess(fnHandler);
  }
});