Skip to content

Commit

Permalink
refactor(client): host url
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Oct 24, 2024
1 parent edca4e4 commit fac70a2
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/libs/client/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,16 @@ export interface ICreateClient {
chainId: ChainId;
networkId: string;
type?: 'local' | 'send' | 'poll' | 'listen' | 'spv';
}) => string | { host: string; headers: Record<string, string> },
}) => string | { hostUrl: string; headers: Record<string, string> },
defaults?: { confirmationDepth?: number },
): IClient;
}

const getHostData = (
hostObject: string | { host: string; headers: Record<string, string> },
hostObject: string | { hostUrl: string; headers: Record<string, string> },
) => {
const hostUrl = typeof hostObject === 'string' ? hostObject : hostObject.host;
const hostUrl =
typeof hostObject === 'string' ? hostObject : hostObject.hostUrl;
const headers = typeof hostObject === 'object' ? hostObject.headers : {};
return { hostUrl, headers };
};
Expand All @@ -295,17 +296,15 @@ export const createClient: ICreateClient = (
const client: IBaseClient = {
local(body, options) {
const cmd: IPactCommand = JSON.parse(body.cmd);
const hostUrl = getHost({
const hostObject = getHost({
chainId: cmd.meta.chainId,
networkId: cmd.networkId,
});
if (typeof hostUrl === 'object') {
return local(body, hostUrl.host, {
...options,
headers: hostUrl.headers,
});
}
return local(body, hostUrl, options);
const { hostUrl, headers } = getHostData(hostObject);
return local(body, hostUrl, {
...options,
headers: headers,
});
},
submit: (async (body) => {
const isList = Array.isArray(body);
Expand All @@ -320,9 +319,7 @@ export const createClient: ICreateClient = (
networkId: cmd.networkId,
});

const hostUrl =
typeof hostObject === 'string' ? hostObject : hostObject.host;
const headers = typeof hostObject === 'object' ? hostObject.headers : {};
const { hostUrl, headers } = getHostData(hostObject);

const { requestKeys } = await send({ cmds: commands }, hostUrl, {
headers,
Expand Down

0 comments on commit fac70a2

Please sign in to comment.