/
index.ts
155 lines (140 loc) · 5.75 KB
/
index.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
import {apiRequest} from '../../utils/apiRequest';
import {TagParams, SCPaginatedResponse, TagGetParams} from '../../types';
import Endpoints from '../../constants/Endpoints';
import {SCTagType} from '@selfcommunity/types';
import {AxiosRequestConfig} from 'axios';
import {urlParams} from '../../utils/url';
export interface TagApiClientInterface {
getAllTags(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>>;
createTag(data: TagParams, config?: AxiosRequestConfig): Promise<SCTagType>;
searchTag(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>>;
getSpecificTag(id: number | string, config?: AxiosRequestConfig): Promise<SCTagType>;
updateTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType>;
patchTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType>;
assignATag(id: number | string, user?: number, category?: number, config?: AxiosRequestConfig): Promise<SCTagType>;
}
/**
* Contains all the endpoints needed to manage tags.
* All endpoints require admin role.
*/
export class TagApiClient {
/**
* This endpoint retrieves all tags.
* @param params
* @param config
*/
static getAllTags(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>> {
const p = urlParams(params);
return apiRequest({...config, url: `${Endpoints.TagsList.url({})}?${p.toString()}`, method: Endpoints.TagsList.method});
}
/**
* This endpoint creates a tag
* @param data
* @param config
*/
static createTag(data: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return apiRequest({...config, url: Endpoints.CreateTag.url({}), method: Endpoints.CreateTag.method, data: data});
}
/**
* This endpoint performs tag search.
* @param params
* @param config
*/
static searchTag(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>> {
const p = urlParams(params);
return apiRequest({...config, url: `${Endpoints.SearchTag.url({})}?${p.toString()}`, method: Endpoints.SearchTag.method});
}
/**
* This endpoint retrieves a specific tag.
* @param id
* @param config
*/
static getSpecificTag(id: number | string, config?: AxiosRequestConfig): Promise<SCTagType> {
return apiRequest({...config, url: Endpoints.Tag.url({id}), method: Endpoints.Tag.method});
}
/**
* This endpoint updates a specific tag.
* @param id
* @param data
* @param config
*/
static updateTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return apiRequest({...config, url: Endpoints.UpdateTag.url({id}), method: Endpoints.UpdateTag.method, data: data ?? null});
}
/**
* This endpoint patches a specific tag.
* @param id
* @param data
* @param config
*/
static patchTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return apiRequest({...config, url: Endpoints.PatchTag.url({id}), method: Endpoints.PatchTag.method, data: data ?? null});
}
/**
* This endpoint assigns a tag to a user or to a category.
* One param between "user" and "category" need to be passed to this endpoint.
* @param id
* @param user
* @param category
* @param config
*/
static assignATag(id: number | string, user?: number, category?: number, config?: AxiosRequestConfig): Promise<SCTagType> {
return apiRequest({
...config,
url: Endpoints.AssignTag.url({id}),
method: Endpoints.AssignTag.method,
data: user ? {user: user} : {category: category}
});
}
}
/**
*
:::tip Tag service can be used in the following way:
```jsx
1. Import the service from our library:
import {TagService} from "@selfcommunity/api-services";
```
```jsx
2. Create a function and put the service inside it!
The async function `getAllTags` will return the paginated list of tags.
async getAllTags() {
return await TagService.getAllTags();
}
```
```jsx
In case of required `params`, just add them inside the brackets.
async getSpecificTag(tagId) {
return await TagService.getSpecificTag(tagId);
}
```
```jsx
If you need to customize the request, you can add optional config params (`AxiosRequestConfig` type).
1. Declare it(or declare them, it is possible to add multiple params)
const headers = headers: {Authorization: `Bearer ${yourToken}`}
2. Add it inside the brackets and pass it to the function, as shown in the previous example!
```
:::
*/
export default class TagService {
static async getAllTags(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>> {
return TagApiClient.getAllTags(params, config);
}
static async createTag(data: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return TagApiClient.createTag(data, config);
}
static async searchTag(params?: TagGetParams, config?: AxiosRequestConfig): Promise<SCPaginatedResponse<SCTagType>> {
return TagApiClient.searchTag(params, config);
}
static async getSpecificTag(id: number | string, config?: AxiosRequestConfig): Promise<SCTagType> {
return TagApiClient.getSpecificTag(id, config);
}
static async updateTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return TagApiClient.updateTag(id, data, config);
}
static async patchTag(id: number | string, data?: TagParams, config?: AxiosRequestConfig): Promise<SCTagType> {
return TagApiClient.patchTag(id, data, config);
}
static async assignATag(id: number | string, user?: number, category?: number, config?: AxiosRequestConfig): Promise<SCTagType> {
return TagApiClient.assignATag(id, user, category, config);
}
}