Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
IroncladDev committed Oct 21, 2024
1 parent d1ad74c commit c846f89
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/core-web/src/services/FederationService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { JSONValue } from '../types'
import type { RpcFederationMaybeLoading } from '../types'
import { WorkerClient } from '../worker'

export class FederationService {
constructor(private client: WorkerClient) {}

async getConfig(): Promise<JSONValue> {
async getConfig(): Promise<RpcFederationMaybeLoading> {
return await this.client.rpcSingle('', 'get_config', {})
}

Expand All @@ -16,7 +16,7 @@ export class FederationService {
return await this.client.rpcSingle('', 'get_invite_code', { peer })
}

async listOperations(): Promise<JSONValue[]> {
async listOperations(): Promise<RpcFederationMaybeLoading[]> {
return await this.client.rpcSingle('', 'list_operations', {})
}
}
4 changes: 2 additions & 2 deletions packages/core-web/src/services/LightningService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
CreateBolt11Response,
GatewayInfo,
JSONObject,
JSONValue,
RpcFederationMaybeLoading,
LightningGateway,
LnPayState,
LnReceiveState,
Expand Down Expand Up @@ -175,7 +175,7 @@ export class LightningService {
return await this.client.rpcSingle('ln', 'list_gateways', {})
}

async updateGatewayCache(): Promise<JSONValue> {
async updateGatewayCache(): Promise<RpcFederationMaybeLoading> {
return await this.client.rpcSingle('ln', 'update_gateway_cache', {})
}
}
10 changes: 5 additions & 5 deletions packages/core-web/src/services/MintService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WorkerClient } from '../worker'
import type {
Duration,
JSONObject,
JSONValue,
RpcFederationMaybeLoading,
MintSpendNotesResponse,
MSats,
ReissueExternalNotesState,
Expand Down Expand Up @@ -30,7 +30,7 @@ export class MintService {

subscribeReissueExternalNotes(
operationId: string,
onSuccess: (state: JSONValue) => void = () => {},
onSuccess: (state: RpcFederationMaybeLoading) => void = () => {},
onError: (error: string) => void = () => {},
) {
const unsubscribe = this.client.rpcStream<ReissueExternalNotesState>(
Expand All @@ -51,7 +51,7 @@ export class MintService {
// the notes at this time, the notes will not be cancelled.
tryCancelAfter: number | Duration = 3600 * 24, // defaults to 1 day
includeInvite: boolean = false,
extraMeta: JSONValue = {},
extraMeta: RpcFederationMaybeLoading = {},
): Promise<MintSpendNotesResponse> {
const duration =
typeof tryCancelAfter === 'number'
Expand Down Expand Up @@ -91,7 +91,7 @@ export class MintService {

subscribeSpendNotes(
operationId: string,
onSuccess: (state: JSONValue) => void = () => {},
onSuccess: (state: RpcFederationMaybeLoading) => void = () => {},
onError: (error: string) => void = () => {},
) {
const unsubscribe = this.client.rpcStream(
Expand All @@ -105,7 +105,7 @@ export class MintService {
return unsubscribe
}

async awaitSpendOobRefund(operationId: string): Promise<JSONValue> {
async awaitSpendOobRefund(operationId: string): Promise<RpcFederationMaybeLoading> {
return await this.client.rpcSingle('mint', 'await_spend_oob_refund', {
operation_id: operationId,
})
Expand Down
6 changes: 3 additions & 3 deletions packages/core-web/src/services/RecoveryService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JSONValue } from '../types'
import type { RpcFederationMaybeLoading } from '../types'
import { WorkerClient } from '../worker'

export class RecoveryService {
Expand All @@ -13,12 +13,12 @@ export class RecoveryService {
}

subscribeToRecoveryProgress(
onSuccess: (progress: { module_id: number; progress: JSONValue }) => void,
onSuccess: (progress: { module_id: number; progress: RpcFederationMaybeLoading }) => void,
onError: (error: string) => void,
): () => void {
const unsubscribe = this.client.rpcStream<{
module_id: number
progress: JSONValue
progress: RpcFederationMaybeLoading
}>('', 'subscribe_to_recovery_progress', {}, onSuccess, onError)

return unsubscribe
Expand Down
18 changes: 13 additions & 5 deletions packages/core-web/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ type Duration = {
type MSats = Alias<number>
type Sats = Alias<number>

type JSONValue =
type RpcFederationMaybeLoading =
| string
| number
| boolean
| null
| { [key: string]: JSONValue }
| JSONValue[]
| { [key: string]: RpcFederationMaybeLoading }
| RpcFederationMaybeLoading[]

type JSONObject = Record<string, JSONValue>
type JSONObject = Record<string, RpcFederationMaybeLoading>

export { Alias, Resolve, Duration, MSats, Sats, JSONValue, JSONObject }
export {
Alias,
Resolve,
Duration,
MSats,
Sats,
RpcFederationMaybeLoading,
JSONObject,
}
6 changes: 3 additions & 3 deletions packages/core-web/src/types/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MSats, Duration, JSONValue } from './utils'
import { MSats, Duration, RpcFederationMaybeLoading } from './utils'

const MODULE_KINDS = ['', 'ln', 'mint'] as const
type ModuleKind = (typeof MODULE_KINDS)[number]
Expand Down Expand Up @@ -64,7 +64,7 @@ type StreamError = {
end: never
}

type StreamSuccess<T extends JSONValue> = {
type StreamSuccess<T extends RpcFederationMaybeLoading> = {
data: T
error: never
end: never
Expand All @@ -76,7 +76,7 @@ type StreamEnd = {
error: never
}

type StreamResult<T extends JSONValue> =
type StreamResult<T extends RpcFederationMaybeLoading> =
| StreamSuccess<T>
| StreamError
| StreamEnd
Expand Down
18 changes: 9 additions & 9 deletions packages/core-web/src/worker/WorkerClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
CancelFunction,
JSONValue,
RpcFederationMaybeLoading,
ModuleKind,
StreamError,
StreamResult,
Expand Down Expand Up @@ -67,8 +67,8 @@ export class WorkerClient {
// TODO: Handle multiple errors

sendSingleMessage<
Response extends JSONValue = JSONValue,
Payload extends JSONValue = JSONValue,
Response extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
Payload extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
>(type: WorkerMessageType, payload?: Payload): Promise<Response> {
return new Promise((resolve, reject) => {
const requestId = ++this.requestCounter
Expand Down Expand Up @@ -121,8 +121,8 @@ export class WorkerClient {
*
*/
rpcStream<
Response extends JSONValue = JSONValue,
Body extends JSONValue = JSONValue,
Response extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
Body extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
>(
module: ModuleKind,
method: string,
Expand Down Expand Up @@ -167,8 +167,8 @@ export class WorkerClient {
}

private async _rpcStreamInner<
Response extends JSONValue = JSONValue,
Body extends JSONValue = JSONValue,
Response extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
Body extends RpcFederationMaybeLoading = RpcFederationMaybeLoading,
>(
requestId: number,
module: ModuleKind,
Expand Down Expand Up @@ -209,10 +209,10 @@ export class WorkerClient {
})
}

rpcSingle<Response extends JSONValue = JSONValue>(
rpcSingle<Response extends RpcFederationMaybeLoading = RpcFederationMaybeLoading>(
module: ModuleKind,
method: string,
body: JSONValue,
body: RpcFederationMaybeLoading,
): Promise<Response> {
logger.debug('WorkerClient - rpcSingle', module, method, body)
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit c846f89

Please sign in to comment.