-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move horizon requests from server to browser for all the remaining ac…
…counts tabs
- Loading branch information
Chris Hatch
committed
Mar 22, 2024
1 parent
bc51463
commit fbbb9c1
Showing
16 changed files
with
561 additions
and
519 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,81 @@ | ||
import type { LoaderFunctionArgs, TypedResponse } from '@remix-run/node' | ||
import { json } from '@remix-run/node' | ||
import HorizonServer, { HorizonServerDetails, requestToServer } from './stellar/server' | ||
import type { HorizonServerDetails } from './stellar/server' | ||
import HorizonServer, { requestToServer } from './stellar/server' | ||
import * as serverRequestUtils from './stellar/server_request_utils' | ||
|
||
export type ServerReqFnName = | ||
| 'effects' | ||
| 'ledgers' | ||
| 'offers' | ||
| 'operations' | ||
| 'payments' | ||
| 'trades' | ||
| 'transactions' | ||
| 'liquidityPools' | ||
| 'claimableBalances' | ||
| 'effects' | ||
| 'ledgers' | ||
| 'offers' | ||
| 'operations' | ||
| 'payments' | ||
| 'trades' | ||
| 'transactions' | ||
| 'liquidityPools' | ||
| 'claimableBalances' | ||
|
||
export function getHorizonRecords<RecordsType>( | ||
serverDetails: HorizonServerDetails, | ||
serverReqFnName: ServerReqFnName, | ||
limit: number, | ||
): Promise< | ||
{ | ||
records: RecordsType | ||
cursor?: string | ||
horizonURL: string | ||
}> { | ||
const url = new URL(serverDetails.requestURL) | ||
serverDetails: HorizonServerDetails, | ||
serverReqFnName: ServerReqFnName, | ||
limit: number, | ||
): Promise<{ | ||
records: RecordsType | ||
cursor?: string | ||
horizonURL: string | ||
}> { | ||
const url = new URL(serverDetails.requestURL) | ||
const cursor: string | undefined = url.searchParams.get('cursor') ?? undefined | ||
const order: string | undefined = url.searchParams.get('order') ?? undefined | ||
|
||
const server = new HorizonServer( | ||
serverDetails.serverAddress, | ||
serverDetails.networkType as string, | ||
) | ||
|
||
return serverRequestUtils[serverReqFnName](server, { | ||
cursor, | ||
order: order as 'asc' | 'desc', | ||
limit, | ||
}).then((records: readonly any[]) => ({ | ||
records: (order === 'asc' | ||
? [...records].reverse() | ||
: records) as RecordsType, | ||
cursor, | ||
horizonURL: server.serverURL.toString(), | ||
})) | ||
} | ||
|
||
export function horizonRecordsLoader<RecordsType>( | ||
serverReqFnName: ServerReqFnName, | ||
limit: number, | ||
) { | ||
return async function ({ request }: LoaderFunctionArgs): Promise< | ||
TypedResponse<{ | ||
records: RecordsType | ||
cursor?: string | ||
horizonURL: string | ||
}> | ||
> { | ||
const url = new URL(request.url) | ||
const cursor: string | undefined = | ||
url.searchParams.get('cursor') ?? undefined | ||
url.searchParams.get('cursor') ?? undefined | ||
const order: string | undefined = url.searchParams.get('order') ?? undefined | ||
|
||
const server = new HorizonServer( | ||
serverDetails.serverAddress, | ||
serverDetails.networkType as string, | ||
) | ||
const server = await requestToServer(request) | ||
|
||
return serverRequestUtils[serverReqFnName](server, { | ||
cursor, | ||
order: order as 'asc' | 'desc', | ||
limit, | ||
cursor, | ||
order: order as 'asc' | 'desc', | ||
limit, | ||
}).then((records: readonly any[]) => | ||
({ | ||
json({ | ||
records: (order === 'asc' | ||
? [...records].reverse() | ||
: records) as RecordsType, | ||
? [...records].reverse() | ||
: records) as RecordsType, | ||
cursor, | ||
horizonURL: server.serverURL.toString(), | ||
})) | ||
} | ||
|
||
export function horizonRecordsLoader<RecordsType>( | ||
serverReqFnName: ServerReqFnName, | ||
limit: number, | ||
) { | ||
return async function ({ request }: LoaderFunctionArgs): Promise< | ||
TypedResponse<{ | ||
records: RecordsType | ||
cursor?: string | ||
horizonURL: string | ||
}> | ||
> { | ||
const url = new URL(request.url) | ||
const cursor: string | undefined = | ||
url.searchParams.get('cursor') ?? undefined | ||
const order: string | undefined = url.searchParams.get('order') ?? undefined | ||
|
||
const server = await requestToServer(request) | ||
|
||
return serverRequestUtils[serverReqFnName](server, { | ||
cursor, | ||
order: order as 'asc' | 'desc', | ||
limit, | ||
}).then((records: readonly any[]) => | ||
json({ | ||
records: (order === 'asc' | ||
? [...records].reverse() | ||
: records) as RecordsType, | ||
cursor, | ||
horizonURL: server.serverURL.toString(), | ||
}), | ||
) | ||
} | ||
}), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.