1
1
import { Response } from "node-fetch" ;
2
+ import { TypeModel } from "../../Models/TypeModel" ;
2
3
import HttpFetch from "../../utils/interface" ;
3
4
4
5
class Types {
@@ -13,26 +14,54 @@ class Types {
13
14
} ) ;
14
15
}
15
16
16
- public async getOne ( customType : string ) : Promise < Response > {
17
+ public async getOne ( customType : string ) : Promise < TypeModel > {
17
18
18
- return await this . http . get ( `https://customtypes.prismic.io/customtypes/${ customType } ` ) ;
19
+ try {
20
+ const response = await this . http . get ( `https://customtypes.prismic.io/customtypes/${ customType } ` ) ;
21
+
22
+ const type : TypeModel = await response . json ( ) ;
23
+
24
+ return type ;
25
+
26
+ } catch ( error ) {
27
+ throw new Error ( error )
28
+ }
19
29
}
20
30
21
- public async getAll ( ) : Promise < Response > {
31
+ public async getAll ( ) : Promise < Array < TypeModel > > {
32
+
33
+ try {
34
+ const response = await this . http . get ( 'https://customtypes.prismic.io/customtypes' ) ;
35
+ const types : Array < TypeModel > = await response . json ( )
22
36
23
- return await this . http . get ( 'https://customtypes.prismic.io/customtypes' ) ;
37
+ return types ;
38
+ } catch ( error ) {
39
+ throw new Error ( error )
40
+ }
24
41
}
25
42
26
43
27
- public async insert ( customType : object , safe ?: boolean ) : Promise < Response > {
44
+ public async insert ( customType : TypeModel ) : Promise < Response > {
45
+
46
+ if ( typeof customType . json !== 'object' ) {
47
+ throw new Error ( "JSON field of custom type isn't and object. Please check if it is correct" )
48
+ }
49
+
50
+ try {
51
+
52
+ const insertResponse = await this . http . post ( 'https://customtypes.prismic.io/customtypes/insert' , customType ) ;
53
+ const result = insertResponse . json ( )
54
+ return result ;
28
55
29
- return await this . http . post ( 'https://customtypes.prismic.io/customtypes/insert' , customType ) ;
56
+ } catch ( error ) {
57
+ throw new Error ( error )
58
+ }
30
59
}
31
60
32
- public async update ( customType : object ) : Promise < Response > {
61
+ public async update ( customType : TypeModel ) : Promise < Response > {
33
62
34
63
return await this . http . post ( 'https://customtypes.prismic.io/customtypes/update' , customType ) ;
35
64
}
36
65
}
37
66
38
- export default Types ;
67
+ export default Types ;
0 commit comments