-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathThePracticalDevClient.ts
135 lines (109 loc) · 3.75 KB
/
ThePracticalDevClient.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
import axios, { AxiosInstance } from 'axios'
import applyCaseMiddleware from 'axios-case-converter'
import { PageParam, CommentsQuery } from '../interfaces/parameters/shared'
import { ListArticlesQuery } from '../interfaces/parameters/ListArticlesQuery'
import {
ArticleIndex,
ArticleCreate,
ArticleShow,
ArticleUpdate,
ArticleMe,
ArticleVideo,
Comment,
Listing,
ListingCreate,
ListingCategory,
ListingUpdate,
PodcastEpisode,
Tag,
User,
WebhookIndex,
WebhookCreate,
WebhookShow
} from '../interfaces/responses/models'
export class ThePracticalDevClient {
private readonly client: AxiosInstance
constructor (private readonly APIKey: string) {
this.client = applyCaseMiddleware(axios.create({
baseURL: 'https://dev.to/api',
headers: {
'api-key': this.APIKey
}
}))
}
async listArticles (query: ListArticlesQuery) {
return this.client.get<ArticleIndex[]>('/articles', { params: query })
}
async createArticle (payload: ArticleCreate) {
return this.client.post<ArticleShow>('/articles', payload)
}
async getArticle (id: number) {
return this.client.get<ArticleShow>(`/articles/${id}`)
}
async updateArticle (id: number, payload: ArticleUpdate) {
return this.client.put<ArticleShow>(`/articles/${id}`, payload)
}
async selfArticles (query: PageParam) {
return this.client.get<ArticleMe[]>('/articles/me', { params: query })
}
async selfPublishedArticles (query: PageParam) {
return this.client.get<ArticleMe[]>('/articles/me/published', { params: query })
}
async selfUnpublishedArticles (query: PageParam) {
return this.client.get<ArticleMe[]>('/articles/me/unpublished', { params: query })
}
async selfAllArticles (query: PageParam) {
return this.client.get<ArticleMe[]>('/articles/me/all', { params: query })
}
async listArticleComments (query: CommentsQuery) {
return this.client.get<ArticleVideo[]>('/videos', { params: query })
}
async getComment (commentId: string) {
return this.client.get<Comment[]>(`/comments/${commentId}`)
}
async listListings (query: PageParam) {
return this.client.get<Listing[]>(`/listings`, { params: query })
}
async createListing (payload: ListingCreate) {
return this.client.post<Listing>(`/listings`, payload)
}
async getListingByCategory (category: ListingCategory, query: PageParam) {
return this.client.get<Listing[]>(`/listings/category/${category}`, { params: query })
}
async getListingById (listingId: string) {
return this.client.get<Listing>(`/listings/${listingId}`)
}
async updateListing (payload: ListingUpdate, id: string) {
return this.client.put<ArticleShow>(`/listings/${id}`, payload)
}
async getPodcastEpisodes (query: PageParam) {
return this.client.get<PodcastEpisode[]>(`/podcast_episodes`, { params: query })
}
async getArticleTags (query: PageParam) {
return this.client.get<Tag[]>(`/tags`, { params: query })
}
async getUserById (userId: number) {
return this.client.get<User>(`/users/${userId}`)
}
async getUserByName (username: string) {
return this.client.get<User>(`/users/by_username`, { params: { url: username } })
}
async getSelfInformation () {
return this.client.get<User>(`/users/me`)
}
async getArticlesWithVideo (query: PageParam) {
return this.client.get<ArticleVideo[]>(`/videos`, { params: query })
}
async getWebhooks () {
return this.client.get<WebhookIndex[]>(`/webhooks`)
}
async createWebhook (payload: WebhookCreate) {
return this.client.post<WebhookShow>(`/webhooks`, payload)
}
async getWebhookById (id: number) {
return this.client.get<WebhookShow>(`/webhooks/${id}`)
}
async deleteWebhook (id: number) {
return this.client.delete<void>(`/webhooks/${id}`)
}
}