TypeScript Types
TypeScript Types
This page explains a few important concepts used by the Client SDK that you need to understand to correctly use the features offered by the package.
PromiseResult
and Result
return types
PromiseResult
and Result
return typesAll methods that require authentication return PromiseResult
object. Actually, that is a Result
type wrapped in a Promise
so if you await
the method call, you will get a Result
object. Check the example:
const promiseResult = lensClient.transaction.broadcast();
const result = await lensClient.transaction.broadcast(); // or await promiseResult;
// now what can we do with the Result object
// check if success or failure
result.isSuccess();
result.isFailure();
// access the value if success
if(result.isSuccess()) {
const value = result.value;
}
// access the error if failure
if(result.isFailure()) {
throw result.error;
}
// unwrap to see what is inside
// it returns value if success and throws error if failure
result.unwrap();
PaginatedResult
return type
PaginatedResult
return typeThe result of a type of PaginatedResult
offers you the utility of requesting more results for the same query. Check the example below.
const paginatedResult = await lensClient.explore.profiles({
limit: 10
});
// now what can we do with the PaginatedResult object
const firstTenItems = [...paginatedResult.items]; // clone as items will be overwritten when calling .next()
// fetch next batch of items
await paginatedResult.next()
const nextTenItems = [...paginatedResult.items];
// you can also request previous batch
await paginatedResult.prev()
Updated 6 months ago