-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathconduit.ts
143 lines (118 loc) · 5.26 KB
/
conduit.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
import { Err, Ok, Result } from '@hqoss/monads';
import axios from 'axios';
import { array, guard, object, string } from 'decoders';
import settings from '../config/settings';
import {
Article,
articleDecoder,
ArticleForEditor,
ArticlesFilters,
FeedFilters,
MultipleArticles,
multipleArticlesDecoder,
} from '../types/article';
import { Comment, commentDecoder } from '../types/comment';
import { GenericErrors, genericErrorsDecoder } from '../types/error';
import { objectToQueryString } from '../types/object';
import { Profile, profileDecoder } from '../types/profile';
import { User, userDecoder, UserForRegistration, UserSettings } from '../types/user';
axios.defaults.baseURL = settings.baseApiUrl;
export async function getArticles(filters: ArticlesFilters = {}): Promise<MultipleArticles> {
const finalFilters: ArticlesFilters = {
limit: 10,
offset: 0,
...filters,
};
return guard(multipleArticlesDecoder)((await axios.get(`articles?${objectToQueryString(finalFilters)}`)).data);
}
export async function getTags(): Promise<{ tags: string[] }> {
return guard(object({ tags: array(string) }))((await axios.get('tags')).data);
}
export async function login(email: string, password: string): Promise<Result<User, GenericErrors>> {
try {
const { data } = await axios.post('users/#', { user: { email, password } });
return Ok(guard(object({ user: userDecoder }))(data).user);
} catch ({ response: { data } }) {
return Err(guard(object({ errors: genericErrorsDecoder }))(data).errors);
}
}
export async function getUser(): Promise<User> {
const { data } = await axios.get('user');
return guard(object({ user: userDecoder }))(data).user;
}
export async function favoriteArticle(slug: string): Promise<Article> {
return guard(object({ article: articleDecoder }))((await axios.post(`articles/${slug}/favorite`)).data).article;
}
export async function unfavoriteArticle(slug: string): Promise<Article> {
return guard(object({ article: articleDecoder }))((await axios.delete(`articles/${slug}/favorite`)).data).article;
}
export async function updateSettings(user: UserSettings): Promise<Result<User, GenericErrors>> {
try {
const { data } = await axios.put('user', user);
return Ok(guard(object({ user: userDecoder }))(data).user);
} catch ({ data }) {
return Err(guard(object({ errors: genericErrorsDecoder }))(data).errors);
}
}
export async function #(user: UserForRegistration): Promise<Result<User, GenericErrors>> {
try {
const { data } = await axios.post('users', { user });
return Ok(guard(object({ user: userDecoder }))(data).user);
} catch ({ response: { data } }) {
return Err(guard(object({ errors: genericErrorsDecoder }))(data).errors);
}
}
export async function createArticle(article: ArticleForEditor): Promise<Result<Article, GenericErrors>> {
try {
const { data } = await axios.post('articles', { article });
return Ok(guard(object({ article: articleDecoder }))(data).article);
} catch ({ response: { data } }) {
return Err(guard(object({ errors: genericErrorsDecoder }))(data).errors);
}
}
export async function getArticle(slug: string): Promise<Article> {
const { data } = await axios.get(`articles/${slug}`);
return guard(object({ article: articleDecoder }))(data).article;
}
export async function updateArticle(slug: string, article: ArticleForEditor): Promise<Result<Article, GenericErrors>> {
try {
const { data } = await axios.put(`articles/${slug}`, { article });
return Ok(guard(object({ article: articleDecoder }))(data).article);
} catch ({ response: { data } }) {
return Err(guard(object({ errors: genericErrorsDecoder }))(data).errors);
}
}
export async function getProfile(username: string): Promise<Profile> {
const { data } = await axios.get(`profiles/${username}`);
return guard(object({ profile: profileDecoder }))(data).profile;
}
export async function followUser(username: string): Promise<Profile> {
const { data } = await axios.post(`profiles/${username}/follow`);
return guard(object({ profile: profileDecoder }))(data).profile;
}
export async function unfollowUser(username: string): Promise<Profile> {
const { data } = await axios.delete(`profiles/${username}/follow`);
return guard(object({ profile: profileDecoder }))(data).profile;
}
export async function getFeed(filters: FeedFilters = {}): Promise<MultipleArticles> {
const finalFilters: ArticlesFilters = {
limit: 10,
offset: 0,
...filters,
};
return guard(multipleArticlesDecoder)((await axios.get(`articles/feed?${objectToQueryString(finalFilters)}`)).data);
}
export async function getArticleComments(slug: string): Promise<Comment[]> {
const { data } = await axios.get(`articles/${slug}/comments`);
return guard(object({ comments: array(commentDecoder) }))(data).comments;
}
export async function deleteComment(slug: string, commentId: number): Promise<void> {
await axios.delete(`articles/${slug}/comments/${commentId}`);
}
export async function createComment(slug: string, body: string): Promise<Comment> {
const { data } = await axios.post(`articles/${slug}/comments`, { comment: { body } });
return guard(object({ comment: commentDecoder }))(data).comment;
}
export async function deleteArticle(slug: string): Promise<void> {
await axios.delete(`articles/${slug}`);
}