Adam Fanello
05/06/2022, 8:50 PMthdxr
05/06/2022, 8:52 PMAdam Fanello
05/06/2022, 8:53 PMAdam Fanello
05/06/2022, 8:53 PMthdxr
05/06/2022, 8:54 PMauthorizers: {
myAuthorizer: authorizer
},
defaults: {
authorizer: authorizer ? "myauthorizer": "none"
}
thdxr
05/06/2022, 8:56 PMauthorizers: authorizer ? { myAuthorizer: authorizer } : undefined
Adam Fanello
05/06/2022, 8:56 PMAdam Fanello
05/06/2022, 8:57 PMconst userPool = this.props.userPool;
let authorizers: Record<string, ApiAuthorizer> = {};
if (userPool) {
this.authorizerName = "CognitoPool";
authorizers = {
[this.authorizerName]: {
type: "user_pool",
userPool: {
id: userPool.userPoolId,
clientIds: [this.props.userPoolClient!.userPoolClientId],
},
},
};
environment.USER_POOL_ID = userPool.userPoolId;
environment.USER_POOL_CLIENT_ID = this.props.userPoolClient!.userPoolClientId;
}
this.api = new Api(this, this.id, {
authorizers,
defaults: {
authorizer: this.authorizerName ?? "none",
function: { environment },
},
cors,
});
thdxr
05/06/2022, 8:57 PMauthorizerName = "myname" as const
Adam Fanello
05/06/2022, 8:59 PMthdxr
05/06/2022, 9:01 PMAdam Fanello
05/06/2022, 9:03 PMexport type ApiFunctionRoutePropsMap = {
[key: string]: ApiFunctionRouteProps<any>;
};
private getApiFunctionRoutePropsFromRouteMap(routeMap: RouteMap[]): ApiFunctionRoutePropsMap {
return routeMap.reduce<ApiFunctionRoutePropsMap>((routes, mapping) => {
const { name, method, path, handler, authorization } = mapping;
return {
...routes,
[`${method} ${path}`]: {
authorizer: authorization ? this.authorizerName : undefined,
function: {
handler,
functionName: this.scope.logicalPrefixedName(`${this.id}-${name}`),
},
},
};
}, {} as ApiFunctionRoutePropsMap);
}
Adam Fanello
05/06/2022, 9:04 PMthdxr
05/06/2022, 9:06 PMconst condition = false
const authorizers = !condition
? undefined
: {
authorizer: {
type: "user_pool",
} as const,
}
const api = new Api(ctx.stack, "id", {
authorizers,
defaults: {
authorizer: "authorizer",
},
})
Adam Fanello
05/06/2022, 9:07 PMthdxr
05/06/2022, 9:07 PMthdxr
05/06/2022, 9:08 PMAdam Fanello
05/06/2022, 9:12 PMAdam Fanello
05/06/2022, 9:12 PMlet authorizers: Record<string, ApiAuthorizer> | undefined;
if (userPool) {
authorizers = {
CognitoPool: {
type: "user_pool",
userPool: {
id: userPool.userPoolId,
clientIds: [this.props.userPoolClient!.userPoolClientId],
},
},
};
environment.USER_POOL_ID = userPool.userPoolId;
environment.USER_POOL_CLIENT_ID = this.props.userPoolClient!.userPoolClientId;
}
this.api = new Api(this, this.id, {
authorizers,
defaults: {
authorizer: authorizers ? "CognitoPool" : "none",
function: { environment },
},
cors,
});
thdxr
05/06/2022, 9:12 PMAdam Fanello
05/06/2022, 9:13 PMthdxr
05/06/2022, 9:13 PMthdxr
05/06/2022, 9:14 PMAdam Fanello
05/06/2022, 9:15 PMlet authorizers = !userPool
? undefined
: {
CognitoPool: {
type: "user_pool",
userPool: {
id: userPool.userPoolId,
clientIds: [this.props.userPoolClient!.userPoolClientId],
},
},
} as const;
if (userPool) {
environment.USER_POOL_ID = userPool.userPoolId;
environment.USER_POOL_CLIENT_ID = this.props.userPoolClient!.userPoolClientId;
}
this.api = new Api(this, this.id, {
authorizers,
defaults: {
authorizer: userPool ? "CognitoPool" : "none",
function: { environment },
},
cors,
});
Adam Fanello
05/06/2022, 9:16 PMauthorizers,
line now.Adam Fanello
05/06/2022, 9:17 PMnew Api
in an if/else. I don't know what types to use for routes then.Adam Fanello
05/06/2022, 9:19 PMconst userPool = this.props.userPool;
if (userPool) {
this.api = new Api(this, this.id, {
authorizers: {
CognitoPoolAuth: {
type: "user_pool",
userPool: {
id: userPool.userPoolId,
clientIds: [this.props.userPoolClient!.userPoolClientId],
},
},
},
defaults: {
authorizer: "CognitoPoolAuth",
function: { environment },
},
cors,
});
}
else {
this.api = new Api(this, this.id, {
defaults: {
authorizer: "none",
function: { environment },
},
cors,
});
}
thdxr
05/06/2022, 9:19 PMthdxr
05/06/2022, 9:20 PMconst condition = false
const api = new Api(ctx.stack, "id", {
authorizers: !condition
? undefined
: {
CognitoPool: {
type: "user_pool",
userPool: {
id: "asd",
clientIds: [],
},
},
},
defaults: {
authorizer: condition ? "none" : "CognitoPool",
},
})
thdxr
05/06/2022, 9:20 PMAdam Fanello
05/06/2022, 9:21 PMthdxr
05/06/2022, 9:22 PMthis.Api
is typed then one second we might need to patch the types here to be a bit looser for people who want to opt outAdam Fanello
05/06/2022, 9:23 PMAdam Fanello
05/06/2022, 9:24 PMthdxr
05/06/2022, 9:24 PMthdxr
05/06/2022, 9:47 PMlet authorizers: Record<string, ApiAuthorizer> = undefined
if (condition)
authorizers = {
foo: {
type: "user_pool",
userPool: {
id: "asdasd",
clientIds: [],
},
},
}
const napi = new Api(ctx.stack, "id", {
authorizers: authorizers
defaults: {
authorizer: condition ? "none" : "CognitoPool",
},
})
thdxr
05/06/2022, 9:48 PMAdam Fanello
05/06/2022, 10:15 PMAdam Fanello
05/06/2022, 10:15 PMthdxr
05/06/2022, 10:16 PMthdxr
05/06/2022, 10:16 PMDerek Kershner
05/06/2022, 10:20 PMDerek Kershner
05/06/2022, 10:22 PMAuthorizers
type into the generic everywhere, and all seems OK.Derek Kershner
05/06/2022, 10:25 PMDerek Kershner
05/06/2022, 10:28 PM@typescript-eslint/recommended
.Derek Kershner
05/06/2022, 10:30 PMDerek Kershner
05/06/2022, 10:42 PMthis.authorizerName
had type string
instead of "YourActualAuthorizerName"
I typically solve this like this:
private authorizerName: "YourActualAuthorizerName" = "YourActualAuthorizerName";
Derek Kershner
05/06/2022, 10:43 PMAdam Fanello
05/06/2022, 10:43 PMAdam Fanello
05/09/2022, 3:01 PMthdxr
05/09/2022, 3:02 PMthdxr
05/09/2022, 3:02 PM