mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 18:29:15 +03:00
feat: added transformToFormData helper
This commit is contained in:
parent
82c9021389
commit
1ceb083282
@ -33,3 +33,20 @@ export function userContainRoles(userRoles: IUserRole[], roles: UserRoleEnum[]):
|
|||||||
export function removeEmptyValues(obj: object) {
|
export function removeEmptyValues(obj: object) {
|
||||||
return Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== null));
|
return Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform object data to FormData
|
||||||
|
*
|
||||||
|
* @param data Input data
|
||||||
|
* @returns Data formatted in FormData
|
||||||
|
*/
|
||||||
|
export function transformToFormData(data: object): FormData {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append value to form-data format
|
||||||
|
*/
|
||||||
|
Object.entries(data).forEach(([key, value]) => value && formData.append(key, value));
|
||||||
|
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
|
import * as helpers from '@educt/helpers';
|
||||||
import { IApiRespose, ICourse } from '@educt/interfaces';
|
import { IApiRespose, ICourse } from '@educt/interfaces';
|
||||||
import { CreateCourseParamsType, FetchCoursesParams } from '@educt/types';
|
import { CreateCourseParamsType, FetchCoursesParams } from '@educt/types';
|
||||||
import { CourseStatusEnum } from '@educt/enums';
|
import { CourseStatusEnum } from '@educt/enums';
|
||||||
@ -49,14 +50,7 @@ export class CourseService {
|
|||||||
public async create(
|
public async create(
|
||||||
data: CreateCourseParamsType
|
data: CreateCourseParamsType
|
||||||
): Promise<IApiRespose<Pick<ICourse, 'id' | 'title' | 'description' | 'status' | 'created_at' | 'updated_at'>>> {
|
): Promise<IApiRespose<Pick<ICourse, 'id' | 'title' | 'description' | 'status' | 'created_at' | 'updated_at'>>> {
|
||||||
const formData = new FormData();
|
const result = await this.api.post('/v1/courses', helpers.transformToFormData(data), {
|
||||||
|
|
||||||
/**
|
|
||||||
* Append value to form-data format
|
|
||||||
*/
|
|
||||||
Object.entries(data).forEach(([key, value]) => value && formData.append(key, value));
|
|
||||||
|
|
||||||
const result = await this.api.post('/v1/courses', formData, {
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'multipart/form-data',
|
'Content-Type': 'multipart/form-data',
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user