UNPKG

@selfcommunity/api-services

Version:
139 lines (132 loc) 5.54 kB
import { __awaiter } from "tslib"; import { apiRequest } from '../../utils/apiRequest'; import Endpoints from '../../constants/Endpoints'; /** * Contains all the endpoints needed to manage medias. */ export class MediaApiClient { /** * This endpoint performs the chunk upload of a media with type image or document. * The client must split the file into chunks and send to the server in series. After all the chunks have been uploaded the client must call the Chunk Upload Complete endpoint with the given upload_id parameter to finalize the upload and retrieve the Media. * To perform chunk upload the request must contain Content-Range header with the information about the chunk(range of the chunk upload in the format bytes start-end/total) * @param data * @param bytesStart * @param bytesEnd * @param bytesTotal * @param config */ static chunkUploadMedia(data, bytesStart, bytesEnd, bytesTotal, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.ComposerChunkUploadMedia.url({}), method: Endpoints.ComposerChunkUploadMedia.method, data: data, headers: { 'Content-Range': `bytes ${bytesStart}-${bytesEnd}/${bytesTotal}` } })); } /** * This endpoint completes the chunk upload and create the media. * @param data * @param config */ static chunkUploadMediaComplete(data, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.ComposerChunkUploadMediaComplete.url({}), method: Endpoints.ComposerChunkUploadMediaComplete.method, data: data })); } /** * This endpoint creates a media. * @param data * @param config */ static createMedia(data, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.ComposerMediaCreate.url({}), method: Endpoints.ComposerMediaCreate.method, data: data })); } /** * This endpoint saves a click on a specific media using ID. * @param id * @param ip * @param config */ static clickMedia(id, ip, config) { var _a; return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.MediaClickTracker.url({ id }), method: Endpoints.MediaClickTracker.method, data: (_a = { ip: ip }) !== null && _a !== void 0 ? _a : null })); } /** * This endpoint retrieves a specific media using ID. * @param id * @param config */ static getSpecificMedia(id, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.GetMedia.url({ id }), method: Endpoints.GetMedia.method })); } /** * This endpoint updates a media. * @param id * @param image * @param config */ static updateMedia(id, image, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.UpdateMedia.url({ id }), method: Endpoints.UpdateMedia.method, data: { image: image } })); } /** * This endpoint deletes a media. * @param id * @param config */ static deleteMedia(id, config) { return apiRequest(Object.assign(Object.assign({}, config), { url: Endpoints.DeleteMedia.url({ id }), method: Endpoints.DeleteMedia.method })); } } /** * :::tip Media service can be used in the following way: ```jsx 1. Import the service from our library: import {MediaService} from "@selfcommunity/api-services"; ``` ```jsx 2. Create a function and put the service inside it! The async function `createMedia` will return the created media obj. It takes a media obj as body param. async createMedia() { const body = {url: 'string', type: 'url'}; return await MediaService.createMedia(body); } ``` ```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 MediaService { static chunkUploadMedia(data, bytesStart, bytesEnd, bytesTotal, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.chunkUploadMedia(data, bytesStart, bytesEnd, bytesTotal, config); }); } static chunkUploadMediaComplete(data, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.chunkUploadMediaComplete(data, config); }); } static createMedia(data, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.createMedia(data, config); }); } static clickMedia(id, ip, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.clickMedia(id, ip, config); }); } static getSpecificMedia(id, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.getSpecificMedia(id, config); }); } static updateMedia(id, image, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.updateMedia(id, image, config); }); } static deleteMedia(id, config) { return __awaiter(this, void 0, void 0, function* () { return MediaApiClient.deleteMedia(id, config); }); } }