Skip to content

Commit

Permalink
Merge pull request #16 from spacemeshos/v2alpha_v2beta
Browse files Browse the repository at this point in the history
v2alpha -> v2beta
  • Loading branch information
pigmej authored Jan 31, 2025
2 parents 3765c72 + 6f86cd5 commit e9044d0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
30 changes: 23 additions & 7 deletions src/api/getFetchAll.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
type FetchChunkFn<Arg, Res> = (
type FetchChunkFn<Arg, Res extends T[] | Record<string, T>, T> = (
rpc: string,
arg: Arg,
limit: number,
offset: number
) => Promise<Res[]>;
) => Promise<Res>;
type FetchAllFn<Arg, Res> = (rpc: string, arg: Arg) => Promise<Res[]>;

const getFetchAll = <Arg, Res>(
fn: FetchChunkFn<Arg, Res>,
const getFetchAll = <Arg, Res extends T[] | Record<string, T>, T>(
fn: FetchChunkFn<Arg, Res, T>,
perPage = 100,
maxCycles = 10
): FetchAllFn<Arg, Res> => {
Expand All @@ -16,11 +16,27 @@ const getFetchAll = <Arg, Res>(
rpc: string,
arg: Arg,
page: number
): Promise<Res[]> => {
): Promise<Res> => {
const res = await fn(rpc, arg, perPage, page * perPage);
if (res.length === 100 && cycle < maxCycles) {

// Fetch and merge arrays
if (Array.isArray(res) && res.length === 100 && cycle < maxCycles) {
cycle += 1;
const merged: T[] = [
...res,
...((await fetchNextChunk(rpc, arg, page + 1)) as T[]),
];
return merged as Res;
}

// Fetch and merge records
if (Object.keys(res).length === 100 && cycle < maxCycles) {
cycle += 1;
return [...res, ...(await fetchNextChunk(rpc, arg, page + 1))];
const merged = {
...(res as Record<string, T>),
...((await fetchNextChunk(rpc, arg, page + 1)) as Record<string, T>),
};
return merged as Res;
}
return res;
};
Expand Down
4 changes: 2 additions & 2 deletions src/api/requests/eligibilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { parseResponse } from '../schemas/error';
// eslint-disable-next-line import/prefer-default-export
export const fetchEligibilities = (rpc: string) =>
fetchJSON(
`${rpc}/spacemesh.v2alpha1.SmeshingIdentitiesService/Eligibilities`,
`${rpc}/spacemesh.v2beta1.SmeshingIdentitiesService/Eligibilities`,
{
method: 'POST',
method: 'GET',
}
)
.then(parseResponse(EligibilitiesResponseSchema))
Expand Down
6 changes: 3 additions & 3 deletions src/api/requests/netinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { NetworkInfoResponseSchema } from '../schemas/network';
import { NodeStatusSchema, NodeSyncStatus } from '../schemas/node';

export const fetchNetworkInfo = (rpc: string) =>
fetchJSON(`${rpc}/spacemesh.v2alpha1.NetworkService/Info`, {
method: 'POST',
fetchJSON(`${rpc}/spacemesh.v2beta1.NetworkService/Info`, {
method: 'GET',
})
.then(parseResponse(NetworkInfoResponseSchema))
.then((res) => ({
Expand All @@ -20,7 +20,7 @@ export const fetchNetworkInfo = (rpc: string) =>
}));

export const fetchNodeStatus = (rpc: string) =>
fetchJSON(`${rpc}/spacemesh.v2alpha1.NodeService/Status`, { method: 'POST' })
fetchJSON(`${rpc}/spacemesh.v2beta1.NodeService/Status`, { method: 'GET' })
.then(parseResponse(NodeStatusSchema))
.then((status) => ({
connectedPeers: parseInt(status.connectedPeers, 10),
Expand Down
4 changes: 2 additions & 2 deletions src/api/requests/poets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { PoETInfoResponseSchema } from '../schemas/poets';

// eslint-disable-next-line import/prefer-default-export
export const fetchPoETInfo = (rpc: string) =>
fetchJSON(`${rpc}/spacemesh.v2alpha1.SmeshingIdentitiesService/PoetInfo`, {
method: 'POST',
fetchJSON(`${rpc}/spacemesh.v2beta1.SmeshingIdentitiesService/PoetInfo`, {
method: 'GET',
})
.then(parseResponse(PoETInfoResponseSchema))
.then((res) => ({
Expand Down
4 changes: 2 additions & 2 deletions src/api/requests/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ProposalsResponseSchema } from '../schemas/proposals';

// eslint-disable-next-line import/prefer-default-export
export const fetchProposals = (rpc: string) =>
fetchJSON(`${rpc}/spacemesh.v2alpha1.SmeshingIdentitiesService/Proposals`, {
method: 'POST',
fetchJSON(`${rpc}/spacemesh.v2beta1.SmeshingIdentitiesService/Proposals`, {
method: 'GET',
})
.then(parseResponse(ProposalsResponseSchema))
.then((res) => res.proposals);
29 changes: 24 additions & 5 deletions src/api/requests/smesherState.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import fetchJSON from '../../utils/fetchJSON';
import getFetchAll from '../getFetchAll';
import { parseResponse } from '../schemas/error';
import { SmesherStatesResponseSchema } from '../schemas/smesherStates';

// eslint-disable-next-line import/prefer-default-export
export const fetchSmesherStates = (rpc: string) =>
fetchJSON(`${rpc}/spacemesh.v2alpha1.SmeshingIdentitiesService/States`, {
method: 'POST',
})
const fetchSmesherStatesChunk = (
rpc: string,
limit: number,
offset: number
) => {
const params = new URLSearchParams({
limit: limit.toString(),
offset: offset.toString(),
});

return fetchJSON(
`${rpc}/spacemesh.v2beta1.SmeshingIdentitiesService/States?${params}`,
{
method: 'GET',
}
)
.then(parseResponse(SmesherStatesResponseSchema))
.then((res) => res.identities);
};

export const fetchSmesherStates = getFetchAll((rpc, _, limit, offset) =>
fetchSmesherStatesChunk(rpc, limit, offset)
);

export default fetchSmesherStates;

0 comments on commit e9044d0

Please sign in to comment.