1
1
import { DynamoDB } from 'aws-sdk' ;
2
- import * as moment from 'moment' ;
3
- import * as uuidv4 from 'uuid/v4 ' ;
2
+ import moment from 'moment' ;
3
+ import { v4 } from 'uuid' ;
4
4
import Item from './Item' ;
5
5
import ResponseError from './ResponseError' ;
6
6
7
- const db = process . env . IS_OFFLINE ?
8
- new DynamoDB . DocumentClient ( {
9
- region : 'localhost' ,
10
- accessKeyId : 'MOCK_ACCESS_KEY_ID' ,
11
- secretAccessKey : 'MOCK_SECRET_ACCESS_KEY' ,
12
- endpoint : `http://${ process . env . DYNAMODB_HOST || 'localhost' } :${ process . env . DYNAMODB_PORT || 8000 } ` ,
13
- } ) :
14
- new DynamoDB . DocumentClient ( ) ;
7
+ const db = process . env . IS_OFFLINE
8
+ ? new DynamoDB . DocumentClient ( {
9
+ region : 'localhost' ,
10
+ accessKeyId : 'MOCK_ACCESS_KEY_ID' ,
11
+ secretAccessKey : 'MOCK_SECRET_ACCESS_KEY' ,
12
+ endpoint : `http://${ process . env . DYNAMODB_HOST || 'localhost' } :${
13
+ process . env . DYNAMODB_PORT || 8000
14
+ } `,
15
+ } )
16
+ : new DynamoDB . DocumentClient ( ) ;
15
17
16
18
export async function getItems ( userId : string ) : Promise < Item [ ] > {
17
19
const params = {
@@ -28,7 +30,10 @@ export async function getItems(userId: string): Promise<Item[]> {
28
30
return data . Items as Item [ ] ;
29
31
}
30
32
31
- export async function getItemById ( userId : string , itemId : string ) : Promise < Item > {
33
+ export async function getItemById (
34
+ userId : string ,
35
+ itemId : string
36
+ ) : Promise < Item > {
32
37
const params = {
33
38
TableName : 'items' ,
34
39
Key : {
@@ -40,7 +45,10 @@ export async function getItemById(userId: string, itemId: string): Promise<Item>
40
45
const data = await db . get ( params ) . promise ( ) ;
41
46
42
47
if ( data . Item === undefined ) {
43
- throw new ResponseError ( { statusCode : 404 , message : `An item could not be found with id: ${ itemId } ` } ) ;
48
+ throw new ResponseError ( {
49
+ statusCode : 404 ,
50
+ message : `An item could not be found with id: ${ itemId } ` ,
51
+ } ) ;
44
52
}
45
53
46
54
return data . Item as Item ;
@@ -49,9 +57,10 @@ export async function getItemById(userId: string, itemId: string): Promise<Item>
49
57
export async function createItem ( userId : string , name : string ) : Promise < Item > {
50
58
const params = {
51
59
TableName : 'items' ,
52
- ConditionExpression : 'attribute_not_exists(id) AND attribute_not_exists(userId)' ,
60
+ ConditionExpression :
61
+ 'attribute_not_exists(id) AND attribute_not_exists(userId)' ,
53
62
Item : {
54
- id : uuidv4 ( ) ,
63
+ id : v4 ( ) ,
55
64
userId,
56
65
name,
57
66
createdUtc : moment ( ) . utc ( ) . toISOString ( ) ,
@@ -63,7 +72,11 @@ export async function createItem(userId: string, name: string): Promise<Item> {
63
72
return params . Item ;
64
73
}
65
74
66
- export async function updateItem ( userId : string , itemId : string , name : string ) : Promise < void > {
75
+ export async function updateItem (
76
+ userId : string ,
77
+ itemId : string ,
78
+ name : string
79
+ ) : Promise < void > {
67
80
try {
68
81
const params = {
69
82
TableName : 'items' ,
@@ -83,16 +96,22 @@ export async function updateItem(userId: string, itemId: string, name: string):
83
96
} ;
84
97
85
98
await db . update ( params ) . promise ( ) ;
86
- } catch ( err ) {
99
+ } catch ( err : any ) {
87
100
if ( err . code === 'ConditionalCheckFailedException' ) {
88
- throw new ResponseError ( { statusCode : 404 , message : `An item could not be found with id: ${ itemId } ` } ) ;
101
+ throw new ResponseError ( {
102
+ statusCode : 404 ,
103
+ message : `An item could not be found with id: ${ itemId } ` ,
104
+ } ) ;
89
105
}
90
106
91
107
throw err ;
92
108
}
93
109
}
94
110
95
- export async function deleteItem ( userId : string , itemId : string ) : Promise < void > {
111
+ export async function deleteItem (
112
+ userId : string ,
113
+ itemId : string
114
+ ) : Promise < void > {
96
115
try {
97
116
const params = {
98
117
TableName : 'items' ,
@@ -104,9 +123,12 @@ export async function deleteItem(userId: string, itemId: string): Promise<void>
104
123
} ;
105
124
106
125
await db . delete ( params ) . promise ( ) ;
107
- } catch ( err ) {
126
+ } catch ( err : any ) {
108
127
if ( err . code === 'ConditionalCheckFailedException' ) {
109
- throw new ResponseError ( { statusCode : 404 , message : `An item could not be found with id: ${ itemId } ` } ) ;
128
+ throw new ResponseError ( {
129
+ statusCode : 404 ,
130
+ message : `An item could not be found with id: ${ itemId } ` ,
131
+ } ) ;
110
132
}
111
133
112
134
throw err ;
0 commit comments