-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: error interactions regression #293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
import { Result, ResultAsync, errAsync, okAsync } from 'neverthrow' | ||
import { | ||
WalletInteractionResponse, | ||
WalletInteractionSuccessResponse, | ||
} from '../schemas' | ||
import { Result } from 'neverthrow' | ||
import { WalletInteractionResponse } from '../schemas' | ||
import { SdkError } from '../error' | ||
import { parse } from 'valibot' | ||
|
||
export const validateWalletResponse = ( | ||
walletResponse: unknown, | ||
): ResultAsync< | ||
WalletInteractionSuccessResponse, | ||
SdkError | { discriminator: 'failure'; interactionId: string; error: string } | ||
> => { | ||
): Result<WalletInteractionResponse, SdkError> => { | ||
const fn = Result.fromThrowable( | ||
(_) => parse(WalletInteractionResponse, _), | ||
(error) => error, | ||
) | ||
|
||
const result = fn(walletResponse) | ||
if (result.isErr()) { | ||
return errAsync(SdkError('walletResponseValidation', '', 'Invalid input')) | ||
} else if (result.isOk()) { | ||
return result.value.discriminator === 'success' | ||
? okAsync(result.value) | ||
: errAsync(result.value) | ||
} | ||
|
||
return errAsync(SdkError('walletResponseValidation', '')) | ||
return fn(walletResponse).mapErr((response) => | ||
SdkError( | ||
'walletResponseValidation', | ||
(walletResponse as any)?.interactionId, | ||
'Invalid input', | ||
response, | ||
), | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { okAsync, ResultAsync } from 'neverthrow' | ||
import { TransportProvider } from '../../../../_types' | ||
import { WalletInteractionResponse } from '../../../../schemas' | ||
|
||
export const TestingTransportModule = ({ | ||
requestResolverModule, | ||
}: { | ||
requestResolverModule: { | ||
addWalletResponses: ( | ||
responses: WalletInteractionResponse[], | ||
) => ResultAsync<unknown, unknown> | ||
} | ||
}): TransportProvider & { | ||
setNextWalletResponse: (response: WalletInteractionResponse) => void | ||
} => { | ||
let _isSupported = true | ||
let id = 'TestingTransportModule' | ||
let _sendResponse: WalletInteractionResponse | ||
|
||
return { | ||
id, | ||
isSupported: () => _isSupported, | ||
destroy: () => {}, | ||
disconnect: () => {}, | ||
send: () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use vitest fn for this purpose? |
||
requestResolverModule.addWalletResponses([_sendResponse]) | ||
return okAsync(_sendResponse) | ||
}, | ||
|
||
// Test helpers | ||
setNextWalletResponse: (response: WalletInteractionResponse) => { | ||
_sendResponse = response | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { Result, ResultAsync, err, ok } from 'neverthrow' | ||
import { Result, ResultAsync, err, errAsync, ok, okAsync } from 'neverthrow' | ||
import { TransportProvider } from '../../_types' | ||
import { Logger, validateWalletResponse } from '../../helpers' | ||
import { Logger } from '../../helpers' | ||
import { | ||
Metadata, | ||
CallbackFns, | ||
WalletInteractionItems, | ||
WalletInteraction, | ||
WalletInteractionFailureResponse, | ||
WalletInteractionResponse, | ||
WalletInteractionSuccessResponse, | ||
} from '../../schemas' | ||
import { parse } from 'valibot' | ||
import { SdkError } from '../../error' | ||
|
@@ -22,6 +24,7 @@ export type WalletRequestSdkInput = { | |
) => Promise<WalletInteraction> | ||
providers: { | ||
transports: TransportProvider[] | ||
interactionIdFactory?: () => string | ||
} | ||
} | ||
export type WalletRequestSdk = ReturnType<typeof WalletRequestSdk> | ||
|
@@ -34,6 +37,8 @@ export const WalletRequestSdk = (input: WalletRequestSdkInput) => { | |
origin: input.origin || window.location.origin, | ||
} as Metadata | ||
|
||
const interactionIdFactory = input.providers.interactionIdFactory ?? uuidV4 | ||
|
||
parse(Metadata, metadata) | ||
|
||
const logger = input?.logger?.getSubLogger({ name: 'WalletSdk' }) | ||
|
@@ -50,7 +55,7 @@ export const WalletRequestSdk = (input: WalletRequestSdkInput) => { | |
|
||
const createWalletInteraction = ( | ||
items: WalletInteractionItems, | ||
interactionId = uuidV4(), | ||
interactionId = interactionIdFactory(), | ||
): WalletInteraction => ({ | ||
items, | ||
interactionId, | ||
|
@@ -86,15 +91,24 @@ export const WalletRequestSdk = (input: WalletRequestSdkInput) => { | |
items, | ||
}: { interactionId?: string; items: WalletInteraction['items'] }, | ||
callbackFns: Partial<CallbackFns> = {}, | ||
): ResultAsync<unknown, SdkError> => | ||
): ResultAsync< | ||
WalletInteractionSuccessResponse, | ||
SdkError | WalletInteractionFailureResponse | ||
> => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we keep it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No because it'd not be adequate. It can error with |
||
withInterceptor({ | ||
items, | ||
interactionId, | ||
metadata, | ||
}).andThen((walletInteraction) => | ||
getTransport(walletInteraction.interactionId).asyncAndThen((transport) => | ||
transport.send(walletInteraction, callbackFns), | ||
), | ||
getTransport(walletInteraction.interactionId) | ||
.asyncAndThen((transport) => | ||
transport.send(walletInteraction, callbackFns), | ||
) | ||
.andThen((response) => | ||
response.discriminator === 'failure' | ||
? errAsync(response) | ||
: okAsync(response), | ||
), | ||
) | ||
|
||
return { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returns the logger object
ILogObjMeta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah saw that decided to not change as it's not a problem, this error is not consumed anywhere after that. I can make it return undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or map to
SdkError
just for the sake of consistency?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not consumed. Does not really matter