-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
156 lines (136 loc) · 4.14 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* This Api class lets you define an API endpoint and methods to request
* data and process it.
*
* See the [Backend API Integration](https://docs.infinite.red/ignite-cli/boilerplate/app/services/#backend-api-integration)
* documentation for more details.
*/
import { ApiResponse, ApisauceInstance, create } from "apisauce";
import Config from "../../config";
import { GeneralApiProblem, getGeneralApiProblem } from "./apiProblem";
import {
ApiConfig,
ApiFeedResponse,
ApiLoginResponse,
ApiError,
ApiErrorResponse,
ApiRegisterResponse,
} from "./api.types";
import type { EpisodeSnapshotIn } from "app/models/Episode";
import { Category, Review } from "app/types";
/**
* Configuring the apisauce instance.
*/
export const DEFAULT_API_CONFIG: ApiConfig = {
url: Config.API_URL,
timeout: 10000,
};
/**
* Manages all requests to the API. You can use this class to build out
* various requests that you need to call from your backend API.
*/
export class Api {
apisauce: ApisauceInstance;
config: ApiConfig;
/**
* Set up our API instance. Keep this lightweight!
*/
constructor(config: ApiConfig = DEFAULT_API_CONFIG) {
this.config = config;
this.apisauce = create({
baseURL: this.config.url,
timeout: this.config.timeout,
headers: {
Accept: "application/json",
},
});
}
/**
* Gets a list of recent React Native Radio episodes.
*/
async getEpisodes(): Promise<{ kind: "ok"; episodes: EpisodeSnapshotIn[] } | GeneralApiProblem> {
// make the api call
const response: ApiResponse<ApiFeedResponse> = await this.apisauce.get(
"api.json?rss_url=https%3A%2F%2Ffeeds.simplecast.com%2FhEI_f9Dx",
);
// the typical ways to die when calling an api
if (!response.ok) {
const problem = getGeneralApiProblem(response);
if (problem) return problem;
}
// transform the data into the format we are expecting
try {
const rawData = response.data;
// This is where we transform the data into the shape we expect for our MST model.
const episodes: EpisodeSnapshotIn[] =
rawData?.items.map((raw) => ({
...raw,
})) ?? [];
return { kind: "ok", episodes };
} catch (e) {
if (__DEV__ && e instanceof Error) {
console.error(`Bad data: ${e.message}\n${response.data}`, e.stack);
}
return { kind: "bad-data" };
}
}
async getCategories(): Promise<any> {
try {
const response: ApiResponse<Category[]> = await this.apisauce.get("/inventory/categories");
if (!response.ok) {
const problem = getGeneralApiProblem(response);
if (problem) return problem;
}
return response.data;
} catch (e) {
return { kind: "unknown-error", temporary: true };
}
}
async getReviews(id: string): Promise<any> {
try {
const response: ApiResponse<Review[]> = await this.apisauce.get(`/reviews/reviews/products/${id}`);
if (!response.ok) {
const problem = getGeneralApiProblem(response);
if (problem) return problem;
}
return response.data;
} catch (e) {
return { kind: "unknown-error", temporary: true };
}
}
async loginByEmail(email: string, password: string): Promise<ApiLoginResponse> {
const response: ApiResponse<ApiLoginResponse | ApiErrorResponse> = await this.apisauce.post(
"identity/auth/login",
{
email,
password,
},
);
if (!response.ok) {
throw new ApiError(response.data as ApiErrorResponse);
}
return response.data as ApiLoginResponse;
}
async signUpByEmail(
name: string,
email: string,
password: string,
confirmPassword: string,
): Promise<ApiRegisterResponse> {
const response: ApiResponse<ApiRegisterResponse | ApiErrorResponse> = await this.apisauce.post(
"identity/auth/register",
{
name,
email,
password,
confirmPassword,
},
);
if (!response.ok) {
throw new ApiError(response.data as ApiErrorResponse);
}
return response.data as ApiRegisterResponse;
}
}
// Singleton instance of the API for convenience
export const api = new Api();