Skip to content

Commit a763c74

Browse files
committed
wip updating types and adding error catching
1 parent 822e40f commit a763c74

File tree

9 files changed

+112
-19
lines changed

9 files changed

+112
-19
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.vscode
12
node_modules
3+
4+
# Builds
25
dist
6+
7+
# Environment stuff
38
env.json
9+

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.github
12
node_modules
23
env.json
34
src

src/API/Slices/index.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Response } from "node-fetch";
2+
import { SliceModel } from "../../Models/SliceModel";
23
import HttpFetch from "../../utils/interface";
34

45

@@ -13,25 +14,40 @@ class Slices {
1314
});
1415
}
1516

16-
public async getOne(slice: string): Promise<Response> {
17+
public async getOne(slice: string): Promise<SliceModel> {
18+
try {
19+
const response = await this.http.get(`https://customtypes.prismic.io/slices/${slice}`);
20+
const sliceModel: SliceModel = await response.json();
1721

18-
return await this.http.get(`https://customtypes.prismic.io/slices/${slice}`);
22+
return sliceModel;
23+
} catch (error) {
24+
console.error(`[ERROR] Unable to fetch the slice: ${slice}`);
25+
throw new Error(`[ERROR] Unable to fetch the slice: ${slice}`)
26+
}
1927
}
2028

21-
public async getAll(): Promise<Response> {
29+
public async getAll(): Promise<Array<SliceModel>> {
30+
try {
2231

23-
return await this.http.get('https://customtypes.prismic.io/slices');
32+
const response = await this.http.get('https://customtypes.prismic.io/slices');
33+
34+
const types: Array<SliceModel> = await response.json();
35+
36+
return types;
37+
} catch (error) {
38+
throw new Error(error)
39+
}
2440
}
2541

26-
public async insert(customType: object): Promise<Response> {
42+
public async insert(slice: SliceModel): Promise<Response> {
2743

28-
return await this.http.post('https://customtypes.prismic.io/slices/insert', customType);
44+
return await this.http.post('https://slices.prismic.io/slices/insert', slice);
2945
}
3046

31-
public async update(customType: object): Promise<Response> {
47+
public async update(slice: SliceModel): Promise<Response> {
3248

33-
return await this.http.post('https://customtypes.prismic.io/slices/update', customType);
49+
return await this.http.post('https://customtypes.prismic.io/slices/update', slice);
3450
}
3551
}
3652

37-
export default Slices;
53+
export default Slices;

src/API/Types/index.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Response } from "node-fetch";
2+
import { TypeModel } from "../../Models/TypeModel";
23
import HttpFetch from "../../utils/interface";
34

45
class Types {
@@ -13,26 +14,54 @@ class Types {
1314
});
1415
}
1516

16-
public async getOne(customType: string): Promise<Response> {
17+
public async getOne(customType: string): Promise<TypeModel> {
1718

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+
}
1929
}
2030

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()
2236

23-
return await this.http.get('https://customtypes.prismic.io/customtypes');
37+
return types;
38+
} catch (error) {
39+
throw new Error(error)
40+
}
2441
}
2542

2643

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;
2855

29-
return await this.http.post('https://customtypes.prismic.io/customtypes/insert', customType);
56+
} catch (error) {
57+
throw new Error(error)
58+
}
3059
}
3160

32-
public async update(customType: object): Promise<Response> {
61+
public async update(customType: TypeModel): Promise<Response> {
3362

3463
return await this.http.post('https://customtypes.prismic.io/customtypes/update', customType);
3564
}
3665
}
3766

38-
export default Types;
67+
export default Types;

src/API/api-interface.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Response } from "node-fetch";
2+
3+
interface ApiInterface {
4+
getOne(name: string): Promise<Response>;
5+
getAll(): Promise<Response>;
6+
insert(data: object): Promise<Response>;
7+
update(data: object): Promise<Response>;
8+
}
9+
10+
export default ApiInterface;

src/Models/SliceModel.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface SliceModel {
2+
id: string,
3+
type: string,
4+
name: string,
5+
description?: string,
6+
variations: Array<SliceVariation>
7+
}
8+
9+
export interface SliceVariation {
10+
id: string,
11+
name: string,
12+
docURL?: string | null,
13+
imageUrl?: string,
14+
version: string,
15+
description?: string|null,
16+
primary?: Object,
17+
items?: Object
18+
}

src/Models/TypeModel.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* This is a custom type interface.
3+
*
4+
* This is to make it possible to determine if the returns are correct.
5+
*/
6+
7+
export interface TypeModel {
8+
id: string,
9+
label: string,
10+
repeatable: boolean,
11+
json: Object,
12+
status: boolean
13+
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Api {
4444

4545
return token;
4646
} catch (error) {
47-
throw new Error(`[Custom Type API] Unable to log in with login details`)
47+
throw new Error(`[Custom Type API] Unable to log in with using password & email details`)
4848
}
4949
}
5050

src/utils/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ class HttpFetch implements Handler{
4343
}
4444
}
4545

46-
export default HttpFetch
46+
export default HttpFetch

0 commit comments

Comments
 (0)