Skip to content

Commit

Permalink
fix review: encapsulate headers
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-nayaka committed Nov 29, 2024
1 parent 492f0bf commit 2220248
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 9 deletions.
3 changes: 1 addition & 2 deletions idea/frontend/src/features/balance/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CONTENT_TYPE_HEADERS, GENESIS } from '@/shared/config';
import { GENESIS } from '@/shared/config';
import { fetchWithGuard } from '@/shared/helpers';
import { FAUCET_API_URL } from './consts';

Expand All @@ -10,7 +10,6 @@ type GetTestBalanceParameters = {
const getTestBalance = ({ token, address }: GetTestBalanceParameters) =>
fetchWithGuard(`${FAUCET_API_URL}/balance`, {
method: 'POST',
headers: CONTENT_TYPE_HEADERS,
body: JSON.stringify({ token, payload: { address, genesis: GENESIS.TESTNET } }),
});
export { getTestBalance };
3 changes: 1 addition & 2 deletions idea/frontend/src/features/metadata/api/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HexString } from '@polkadot/util/types';

import { CONTENT_TYPE_HEADERS, METADATA_STORAGE_API_URL } from '@/shared/config';
import { METADATA_STORAGE_API_URL } from '@/shared/config';
import { fetchWithGuard } from '@/shared/helpers';
import { GetMetaResponse } from './types';

Expand All @@ -14,7 +14,6 @@ const fetchMetadata = (hash: HexString) => {
const addMetadata = (hash: HexString, hex: HexString) =>
fetchWithGuard(`${METADATA_STORAGE_API_URL}/meta`, {
method: 'POST',
headers: CONTENT_TYPE_HEADERS,
body: JSON.stringify({ hash, hex }),
});

Expand Down
3 changes: 1 addition & 2 deletions idea/frontend/src/features/sails/api/requests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HexString } from '@gear-js/api';

import { PaginationResponse } from '@/api';
import { CONTENT_TYPE_HEADERS, METADATA_STORAGE_API_URL } from '@/shared/config';
import { METADATA_STORAGE_API_URL } from '@/shared/config';
import { fetchWithGuard } from '@/shared/helpers';
import { INDEXER_RPC_SERVICE } from '@/shared/services/rpcService';

Expand All @@ -18,7 +18,6 @@ const getIdl = (codeId: HexString) => {
const addIdl = (codeId: HexString, data: string) =>
fetchWithGuard(`${METADATA_STORAGE_API_URL}/sails`, {
method: 'POST',
headers: CONTENT_TYPE_HEADERS,
body: JSON.stringify({ codeId, data }),
});

Expand Down
3 changes: 0 additions & 3 deletions idea/frontend/src/shared/config/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const PROGRAM_ERRORS = {
PROGRAM_INIT_FAILED: 'Program initialization failed',
};

const CONTENT_TYPE_HEADERS = { 'Content-Type': 'application/json;charset=utf-8' };

enum LocalStorage {
Node = 'node',
Nodes = 'nodes',
Expand Down Expand Up @@ -112,7 +110,6 @@ export {
PROGRAM_ERRORS,
GENESIS,
ACCOUNT_ADDRESS_SCHEMA,
CONTENT_TYPE_HEADERS,
LocalStorage,
GasMethod,
TransactionName,
Expand Down
3 changes: 3 additions & 0 deletions idea/frontend/src/shared/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ const isHex = (value: unknown): value is HexString => {
};

const fetchWithGuard = async <T extends object>(...args: Parameters<typeof fetch>) => {
if (args?.[1]?.method === 'POST') {
args[1].headers = { 'Content-Type': 'application/json;charset=utf-8', ...args[1]?.headers };
}
const response = await fetch(...args);

if (!response.ok) throw new Error(response.statusText);
Expand Down

0 comments on commit 2220248

Please sign in to comment.