Skip to content

Commit 380c1ec

Browse files
feat(typing): narrow typing when not defining returnValues
the default returnValues is NONE which means there is no value returned from exec. The actual implementation in write.request.ts return T | void depending on returnValues, but we want the default return value of exec() to be void, so we don't have to cast any time we use the exec() method
1 parent ea37f7a commit 380c1ec

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/dynamo/request/delete/delete.request.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ export class DeleteRequest<T> extends WriteRequest<T, DynamoDB.DeleteItemInput,
2626
return this
2727
}
2828

29+
/*
30+
* kind a hacky - this is just for typing reasons so Promise<void> is the default return type when not defining a
31+
* returnValues other than NONE
32+
*
33+
* const valueVoid = new DeleteRequest(...).exec()
34+
* const valueMyModel = new DeleteRequest(...).returnValues('ALL_OLD').exec()
35+
*/
36+
exec(): Promise<void> {
37+
return <Promise<void>>super.exec()
38+
}
39+
2940
execFullResponse(): Promise<DynamoDB.DeleteItemOutput> {
3041
this.logger.debug('request', this.params)
3142
return this.dynamoDBWrapper.deleteItem(this.params)
3243
.then(promiseTap(response => this.logger.debug('response', response)))
3344
}
3445
}
35-

src/dynamo/request/put/put.request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ export class PutRequest<T> extends WriteRequest<T, DynamoDB.PutItemInput, PutReq
3838
return this
3939
}
4040

41+
/*
42+
* kind a hacky - this is just for typing reasons so Promise<void> is the default return type when not defining a
43+
* returnValues other than NONE
44+
*
45+
* const valueVoid = new DeleteRequest(...).exec()
46+
* const valueMyModel = new DeleteRequest(...).returnValues('ALL_OLD').exec()
47+
*/
48+
exec(): Promise<void> {
49+
return <Promise<void>>super.exec()
50+
}
51+
4152
execFullResponse(): Promise<DynamoDB.PutItemOutput> {
4253
this.logger.debug('request', this.params)
4354
return this.dynamoDBWrapper.putItem(this.params)

src/dynamo/request/update/update.request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ export class UpdateRequest<T> extends WriteRequest<T, DynamoDB.UpdateItemInput,
4040
return this
4141
}
4242

43+
/*
44+
* kind a hacky - this is just for typing reasons so Promise<void> is the default return type when not defining a
45+
* returnValues other than NONE
46+
*
47+
* const valueVoid = new DeleteRequest(...).exec()
48+
* const valueMyModel = new DeleteRequest(...).returnValues('ALL_OLD').exec()
49+
*/
50+
exec(): Promise<void> {
51+
return <Promise<void>>super.exec()
52+
}
53+
4354
execFullResponse(): Promise<DynamoDB.UpdateItemOutput> {
4455
this.logger.debug('request', this.params)
4556
return this.dynamoDBWrapper.updateItem(this.params)

src/dynamo/request/write.request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export abstract class WriteRequest<T,
4848
/**
4949
* @returns { void } if no ReturnValues are requested, { T } if the requested ReturnValues are ALL_OLD|ALL_NEW or {Partial<T>} if the requested ReturnValues are UPDATED_OLD|UPDATED_NEW
5050
*/
51-
exec(): Promise<T | undefined> {
51+
exec(): Promise<T | void> {
5252
return this.execFullResponse()
5353
.then(response => {
5454
if ('Attributes' in response && typeof response.Attributes === 'object' && response.Attributes !== null) {

0 commit comments

Comments
 (0)