I have question specific to sst stack w.r.t typesc...
# sst
m
I have question specific to sst stack w.r.t typescript (first time coding in typescript so yea) I am trying to create helper methods which can create api-stacks for me. The idea is that i will pass in
<http://sst.App|sst.App>, name, props
and rest would be generic. I am planning to use
props
to pass in which methods to create (
get
,
post
etc) since the name would be same and then use
props.tables
to attach permissions to specific tables (dynamodb). Right now my props interface looksl ike this
Copy code
interface MyApiProps extends sst.StackProps {
  readonly authorizer: apigAuthorizers.HttpUserPoolAuthorizer;
  readonly table?: sst.Table;
  readonly get: boolean;
}
The problem i am having is on
table?
property. given that its "optional" it can exist or not. Now when i try to make a custom prop by this code
Copy code
new MyApiStack(app, "heartbeat", {authType: sst.ApiAuthorizationType.NONE, get: true});
I get an error on
_this_.api.attachPermissions([props?.table as sst.Table]);
in my
MyApiStack
class which is generalizing all this. The error that i get is "The specified permissions are not supported". This error is easily fixed if i pass a table object in
MyApiStack(app, "myapi", {authType: sst.ApiAuthorizationType.NONE, get: true, table: myTable});
But I want to find a way where i could skip passing a table object and based on object type (null etc) i avoid attaching permissions. I have already tried this code but it didn;t work
Copy code
if (props?.table !== null) {
      this.api.attachPermissions([props?.table as sst.Table]);
    }
t
Your last snippet should work without the
as
You might need to write it like this
Copy code
if (props.table)
  this.api.attachPermissions([props.table])
Typescript doesn't know if it's undefined until you're inside the
if
statement
remembered in js undefined is not the same thing as null
Also I know in our docs we've been encouraging extending the sst.StackProps but you probably don't need to be doing that unless you're using stuff in it. Even if you are I'd just make that another argument
Copy code
constructor(app: <http://sst.App|sst.App>, name: string, props: MyStackprops) {
super(this, name)
}
You don't have to pass sst.StackProps through to super
Also in this scenario you might want to be making a generic cdk.Construct instead of a stack. You're kind of making your own
sst.Api
basically
m
the if condition you mentioned worked. could you elaborate a bit more on
Also in this scenario you might want to be making a generic cdk.Construct instead of a stack.  You're kind of making your own 
sst.Api
 basically
Right now my ApiStack has following signature
Copy code
export default class APIStack extends sst.Stack {

  constructor(scope: <http://sst.App|sst.App>, name: string, props?: MyCustomProps) {
    super(scope, id, props)
    // setup api
    // add permissions
    // add outputs
  }
}