1 import { createApiError } from './ApiError';
3 const appendQueryParams = (url: URL, params: { [key: string]: any }) => {
4 Object.keys(params).forEach((key) => {
5 const value = params[key];
6 if (typeof value === 'undefined') {
9 url.searchParams.append(key, value);
13 export const createUrl = (urlString: string, params: { [key: string]: any } = {}, origin?: string) => {
16 url = new URL(urlString, origin);
18 url = new URL(urlString);
20 appendQueryParams(url, params);
24 export const checkStatus = (response: Response, config: any) => {
25 const { status } = response;
27 if (status >= 200 && status < 300) {
37 throw createApiError('StatusCodeError', response, config, data);
41 export const getDateHeader = (headers: Headers) => {
42 const dateHeader = headers?.get?.('date');
46 const newServerTime = new Date(dateHeader);
47 if (Number.isNaN(+newServerTime)) {
53 export const serializeFormData = (data: { [key: string]: any }) => {
54 const formData = new FormData();
55 Object.keys(data).forEach((key) => {
56 if (Array.isArray(data[key])) {
57 data[key].forEach((val: any) => formData.append(key, val));
59 formData.append(key, data[key]);
65 export const serializeData = (data: any, input: string) => {
69 if (input === 'json') {
71 body: JSON.stringify(data),
73 'Content-Type': 'application/json',
77 if (input === 'form') {
79 body: serializeFormData(data),
82 if (input === 'protobuf') {
86 'content-type': 'application/x-protobuf',