@selfcommunity/api-services
Version:
Client api for SelfCommunity.
151 lines (143 loc) • 5.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagApiClient = void 0;
const tslib_1 = require("tslib");
const apiRequest_1 = require("../../utils/apiRequest");
const Endpoints_1 = tslib_1.__importDefault(require("../../constants/Endpoints"));
const url_1 = require("../../utils/url");
/**
* Contains all the endpoints needed to manage tags.
* All endpoints require admin role.
*/
class TagApiClient {
/**
* This endpoint retrieves all tags.
* @param params
* @param config
*/
static getAllTags(params, config) {
const p = (0, url_1.urlParams)(params);
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: `${Endpoints_1.default.TagsList.url({})}?${p.toString()}`, method: Endpoints_1.default.TagsList.method }));
}
/**
* This endpoint creates a tag
* @param data
* @param config
*/
static createTag(data, config) {
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: Endpoints_1.default.CreateTag.url({}), method: Endpoints_1.default.CreateTag.method, data: data }));
}
/**
* This endpoint performs tag search.
* @param params
* @param config
*/
static searchUserTags(params, config) {
const p = (0, url_1.urlParams)(params);
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: `${Endpoints_1.default.SearchUserTags.url({})}?${p.toString()}`, method: Endpoints_1.default.SearchUserTags.method }));
}
/**
* This endpoint retrieves a specific tag.
* @param id
* @param config
*/
static getSpecificTag(id, config) {
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: Endpoints_1.default.Tag.url({ id }), method: Endpoints_1.default.Tag.method }));
}
/**
* This endpoint updates a specific tag.
* @param id
* @param data
* @param config
*/
static updateTag(id, data, config) {
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: Endpoints_1.default.UpdateTag.url({ id }), method: Endpoints_1.default.UpdateTag.method, data: data !== null && data !== void 0 ? data : null }));
}
/**
* This endpoint patches a specific tag.
* @param id
* @param data
* @param config
*/
static patchTag(id, data, config) {
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: Endpoints_1.default.PatchTag.url({ id }), method: Endpoints_1.default.PatchTag.method, data: data !== null && data !== void 0 ? 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, user, category, config) {
return (0, apiRequest_1.apiRequest)(Object.assign(Object.assign({}, config), { url: Endpoints_1.default.AssignTag.url({ id }), method: Endpoints_1.default.AssignTag.method, data: user ? { user: user } : { category: category } }));
}
}
exports.TagApiClient = TagApiClient;
/**
*
:::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!
```
:::
*/
class TagService {
static getAllTags(params, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.getAllTags(params, config);
});
}
static createTag(data, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.createTag(data, config);
});
}
static searchUserTags(params, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.searchUserTags(params, config);
});
}
static getSpecificTag(id, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.getSpecificTag(id, config);
});
}
static updateTag(id, data, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.updateTag(id, data, config);
});
}
static patchTag(id, data, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.patchTag(id, data, config);
});
}
static assignATag(id, user, category, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return TagApiClient.assignATag(id, user, category, config);
});
}
}
exports.default = TagService;