import _isNil from 'lodash/isNil'; import _omitBy from 'lodash/omitBy'; import { handleDelete, handleFetch, stringifyQuery } from './helpers'; import { Asset, AssetVariantCrop, Schema, AssetSearchRequest, AssetSearchResponse, AddAssetTagsRequest } from '../types/api'; export default class AssetApi { public static async getAssets(): Promise { const response = await handleFetch('/assets/list'); return response.json(); } public static async getSchema(): Promise { const response = await handleFetch('/assets/schema'); return await response.json(); } public static async getAssetById(id: string): Promise { const response = await handleFetch(`/assets/${id}`); // case: empty Guid as id if (response.status === 204) { return null; } return response.json(); } public static async searchAssets(searchRequest: AssetSearchRequest): Promise { const response = await handleFetch(`/assets/search`, { method: 'post', body: JSON.stringify(searchRequest) }); return response.json(); } public static async addTags(request: AddAssetTagsRequest): Promise { const response = await handleFetch(`/assets/add-tags`, { method: 'post', body: JSON.stringify(request), }); return response.json(); } public static async updateAsset(asset: Asset): Promise { const response = await handleFetch(`/assets/${asset.id}/update`, { method: 'put', body: JSON.stringify(asset), }); return response.json(); } public static async createAsset(data: FormData): Promise { const response = await handleFetch('/assets/create', { method: 'post', body: data, }, false); return response.json(); } public static async deleteAsset(id: string): Promise { return await handleDelete(`/assets/${id}/delete`); } public static async deleteAssets(ids: string[]): Promise { return await handleFetch(`/assets/delete-multiple`, { method: 'delete', body: JSON.stringify(ids) }); } public static async createPublicAssetUrl(id: string, width: number | null, height: number | null, crop: AssetVariantCrop): Promise { const query = _omitBy({ width, height, cropCenterX: crop.centerPoint.x, cropCenterY: crop.centerPoint.y, cropWidth: crop.width, cropHeight: crop.height, }, _isNil) as any; const path = stringifyQuery(`/assets/generate-url/${id}`, query); const response = await handleFetch(path); return response.json(); } }