Skip to content

Commit

Permalink
fix (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
voloshinskii authored May 16, 2024
1 parent ef1ef9b commit 09c0eb6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions packages/@core-js/src/utils/network/network.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
import { BackoffOptions } from "./backoff";
import { jsonToUrl } from "./network.utils";
import { BackoffOptions } from './backoff';
import { jsonToUrl } from './network.utils';

type NetworkMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';

export type NetworkOptions = {
params?: Record<string, any>;
backoff?: BackoffOptions | boolean;
headers?: any;
}
};

export type NetworkResponse<TData = any> = {
statusText: string;
headers: Record<string, any>;
status: number;
ok: boolean;
data: TData;
}
};

export function createNetworkMethod(method: NetworkMethods) {
return async <T = any>(url: string, options: NetworkOptions): Promise<NetworkResponse<T>> => {
return async <T = any>(
url: string,
options: NetworkOptions,
): Promise<NetworkResponse<T>> => {
const fetchOptions: RequestInit = { method, headers: options.headers ?? {} };

let query: string | undefined;
if (options.params) {
if (method === 'GET') {
query = jsonToUrl(options.params);
} else {
} else {
fetchOptions.body = JSON.stringify(options.params);
(fetchOptions.headers as Record<string, any>)['Content-Type'] = 'application/json';
(fetchOptions.headers as Record<string, any>)['Content-Type'] =
'application/json';
}
}

const endpoint = url + (query ? `?${query}` : '');
const response = await fetch(endpoint, fetchOptions);
let data = {} as T;
try {
data = await response.json();
} catch (err) {}

return {
if (response.status >= 400) {
throw new Error('Bat response');
}

let data = (await response.json()) as T;

return {
statusText: response.statusText,
headers: response.headers,
status: response.status,
ok: response.ok,
data,
data,
};
}
};
}

export const network = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const RechargeMethods = memo(() => {
nav.navigate('/recharge-by-promo');
}, []);

if (isLoading) {
if (isLoading || !methods?.length) {
return null;
}

Expand Down

0 comments on commit 09c0eb6

Please sign in to comment.