From e63c92b1952475469c01b4afb84ebc65a8138a4f Mon Sep 17 00:00:00 2001 From: dalechyn Date: Sun, 20 Oct 2024 01:30:52 +0300 Subject: [PATCH] feat: finish `UserData` --- playground/src/index.ts | 7 ++- .../Actions/Cast/getAllCastMessagesByFid.ts | 56 ++++++++++++++----- src/Internal/Actions/Cast/getCast.ts | 1 - src/Internal/Actions/Cast/getCastsByFid.ts | 9 ++- .../Actions/Cast/getCastsByMention.ts | 1 - src/Internal/Actions/Cast/getCastsByParent.ts | 1 - .../UserData/getAllUserDataMessagesByFid.ts | 31 ++++++---- .../Actions/UserData/getUserDataBio.ts | 5 +- .../Actions/UserData/getUserDataByFid.ts | 40 +++++++++---- .../Actions/UserData/getUserDataDisplay.ts | 6 +- .../Actions/UserData/getUserDataLocation.ts | 6 +- .../Actions/UserData/getUserDataPfp.ts | 6 +- .../Actions/UserData/getUserDataUrl.ts | 6 +- .../Actions/UserData/getUserDataUsername.ts | 6 +- src/Internal/UserData/errors.ts | 12 ++++ src/Internal/UserData/fromMessage.ts | 29 ++++++---- 16 files changed, 152 insertions(+), 70 deletions(-) create mode 100644 src/Internal/UserData/errors.ts diff --git a/playground/src/index.ts b/playground/src/index.ts index fd95aaf..7d9e08b 100644 --- a/playground/src/index.ts +++ b/playground/src/index.ts @@ -12,10 +12,13 @@ const client = Client.create( // hash: '880700eca6c454facaa9bd05ef15fa0b6996a0d2' as any, // }).then((res) => console.log(res)) -Actions.Cast.getCastsByFid(client, { +const casts = await Actions.Cast.getCastsByFid(client, { fid: 3n, pageSize: 2, -}).then((res) => console.dir(res, { depth: null })) +}) +// biome-ignore lint/suspicious/noConsoleLog: +console.log(casts) const myBio = await Actions.UserData.getUserDataBio(client, { fid: 11517n }) +// biome-ignore lint/suspicious/noConsoleLog: console.log(myBio) diff --git a/src/Internal/Actions/Cast/getAllCastMessagesByFid.ts b/src/Internal/Actions/Cast/getAllCastMessagesByFid.ts index 73a842d..23c787e 100644 --- a/src/Internal/Actions/Cast/getAllCastMessagesByFid.ts +++ b/src/Internal/Actions/Cast/getAllCastMessagesByFid.ts @@ -1,29 +1,59 @@ -import { fromJson, toJson } from '@bufbuild/protobuf' import type { CallOptions } from '@connectrpc/connect' +import { Hex, type Types } from 'ox' +import { Cast_fromMessage } from '../../Cast/fromMessage.js' +import type { Cast } from '../../Cast/types.js' import type { Client } from '../../Client/types.js' import type { GlobalErrorType } from '../../Errors/error.js' -import { - type FidTimestampRequestJson, - FidTimestampRequestSchema, - type MessagesResponseJson, - MessagesResponseSchema, -} from '../../Protobufs/request_response_pb.js' +import { MessageType } from '../../Protobufs/message_pb.js' export declare namespace Actions_Cast_GetAllCastMessagesByFid { - type ReturnType = MessagesResponseJson - // @TODO: proper error handling - type ErrorType = GlobalErrorType + type ReturnType = { + messages: ( + | { type: 'casted'; cast: Cast } + | { type: 'removed'; hash: Types.Hex } + )[] + nextPageToken: Types.Hex | undefined + } + type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType } export async function Actions_Cast_getAllCastMessagesByFid( client: Client, - parameters: Required, + parameters: { + fid: bigint + pageSize?: number | undefined + pageToken?: Types.Hex | undefined + reverse?: boolean | undefined + }, options?: CallOptions, ): Promise { const message = await client.connectRpcClient.getAllCastMessagesByFid( - fromJson(FidTimestampRequestSchema, parameters), + { + fid: parameters.fid, + ...(parameters.pageSize ? { pageSize: parameters.pageSize } : {}), + ...(parameters.pageToken + ? { pageToken: Hex.toBytes(parameters.pageToken) } + : {}), + ...(parameters.reverse ? { reverse: parameters.reverse } : {}), + }, options, ) - return toJson(MessagesResponseSchema, message) + + return { + messages: message.messages.map((message) => { + if ( + message.data?.type === MessageType.CAST_REMOVE && + message.data.body.case === 'castRemoveBody' + ) + return { + type: 'removed' as const, + hash: Hex.fromBytes(message.data.body.value.targetHash), + } + return { type: 'casted', cast: Cast_fromMessage(message) } + }), + nextPageToken: message.nextPageToken + ? Hex.fromBytes(message.nextPageToken) + : undefined, + } } Actions_Cast_getAllCastMessagesByFid.parseError = (error: unknown) => error as Actions_Cast_GetAllCastMessagesByFid.ErrorType diff --git a/src/Internal/Actions/Cast/getCast.ts b/src/Internal/Actions/Cast/getCast.ts index 27f2db2..bd9d2ce 100644 --- a/src/Internal/Actions/Cast/getCast.ts +++ b/src/Internal/Actions/Cast/getCast.ts @@ -7,7 +7,6 @@ import type { GlobalErrorType } from '../../Errors/error.js' export declare namespace Actions_Cast_GetCast { type ReturnType = Cast - // @TODO: proper error handling type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType } export async function Actions_Cast_getCast( diff --git a/src/Internal/Actions/Cast/getCastsByFid.ts b/src/Internal/Actions/Cast/getCastsByFid.ts index 9b068c9..aa4a853 100644 --- a/src/Internal/Actions/Cast/getCastsByFid.ts +++ b/src/Internal/Actions/Cast/getCastsByFid.ts @@ -10,7 +10,6 @@ export declare namespace Actions_Cast_GetCastsByFid { casts: Cast[] nextPageToken: Types.Hex | undefined } - // @TODO: proper error handling type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType } export async function Actions_Cast_getCastsByFid( @@ -20,6 +19,8 @@ export async function Actions_Cast_getCastsByFid( pageSize?: number | undefined pageToken?: Types.Hex | undefined reverse?: boolean | undefined + startTimestamp?: bigint + stopTimestamp?: bigint }, options?: CallOptions, ): Promise { @@ -31,6 +32,12 @@ export async function Actions_Cast_getCastsByFid( ? { pageToken: Hex.toBytes(parameters.pageToken) } : {}), ...(parameters.reverse ? { reverse: parameters.reverse } : {}), + ...(parameters.startTimestamp + ? { startTimestamp: parameters.startTimestamp } + : {}), + ...(parameters.stopTimestamp + ? { stopTimestamp: parameters.stopTimestamp } + : {}), }, options, ) diff --git a/src/Internal/Actions/Cast/getCastsByMention.ts b/src/Internal/Actions/Cast/getCastsByMention.ts index 07fe9db..61a031a 100644 --- a/src/Internal/Actions/Cast/getCastsByMention.ts +++ b/src/Internal/Actions/Cast/getCastsByMention.ts @@ -10,7 +10,6 @@ export declare namespace Actions_Cast_GetCastsByMention { casts: Cast[] nextPageToken: Types.Hex | undefined } - // @TODO: proper error handling type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType } export async function Actions_Cast_getCastsByMention( diff --git a/src/Internal/Actions/Cast/getCastsByParent.ts b/src/Internal/Actions/Cast/getCastsByParent.ts index 7027e73..7518b93 100644 --- a/src/Internal/Actions/Cast/getCastsByParent.ts +++ b/src/Internal/Actions/Cast/getCastsByParent.ts @@ -11,7 +11,6 @@ export declare namespace Actions_Cast_GetCastsByParent { casts: Cast[] nextPageToken: Types.Hex | undefined } - // @TODO: proper error handling type ErrorType = | Cast_fromMessage.ErrorType | Parent_toMessage.ErrorType diff --git a/src/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts b/src/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts index 2954a01..bbfd767 100644 --- a/src/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts +++ b/src/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts @@ -1,28 +1,37 @@ -import { type MessageJsonType, fromJson, toJson } from '@bufbuild/protobuf' import type { CallOptions } from '@connectrpc/connect' +import { Hex, type Types } from 'ox' import type { Client } from '../../Client/types.js' import type { GlobalErrorType } from '../../Errors/error.js' -import { - type FidTimestampRequestJson, - FidTimestampRequestSchema, - MessagesResponseSchema, -} from '../../Protobufs/request_response_pb.js' +import { UserData_fromMessage } from '../../UserData/fromMessage.js' +import type { UserData } from '../../UserData/types.js' export declare namespace Actions_UserData_GetAllUserDataMessagesByFid { - type ReturnType = MessageJsonType + type ReturnType = UserData[] // @TODO: proper error handling - type ErrorType = GlobalErrorType + type ErrorType = UserData_fromMessage.ErrorType | GlobalErrorType } export async function Actions_UserData_getAllUserDataMessagesByFid( client: Client, - parameters: Required, + parameters: { + fid: bigint + pageSize?: number | undefined + pageToken?: Types.Hex | undefined + reverse?: boolean | undefined + }, options?: CallOptions, ): Promise { const message = await client.connectRpcClient.getAllUserDataMessagesByFid( - fromJson(FidTimestampRequestSchema, parameters), + { + fid: parameters.fid, + ...(parameters.pageSize ? { pageSize: parameters.pageSize } : {}), + ...(parameters.pageToken + ? { pageToken: Hex.toBytes(parameters.pageToken) } + : {}), + ...(parameters.reverse ? { reverse: parameters.reverse } : {}), + }, options, ) - return toJson(MessagesResponseSchema, message) + return message.messages.map(UserData_fromMessage) } Actions_UserData_getAllUserDataMessagesByFid.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataBio.ts b/src/Internal/Actions/UserData/getUserDataBio.ts index 64faa57..9b3b045 100644 --- a/src/Internal/Actions/UserData/getUserDataBio.ts +++ b/src/Internal/Actions/UserData/getUserDataBio.ts @@ -8,7 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataBio { - type ReturnType = Actions_UserData_GetUserData.ReturnType + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] // @TODO: proper error handling type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +18,7 @@ export async function Actions_UserData_getUserDataBio( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +26,7 @@ export async function Actions_UserData_getUserDataBio( }, options, ) + return data.value } Actions_UserData_getUserDataBio.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataByFid.ts b/src/Internal/Actions/UserData/getUserDataByFid.ts index 6e3b31e..d627306 100644 --- a/src/Internal/Actions/UserData/getUserDataByFid.ts +++ b/src/Internal/Actions/UserData/getUserDataByFid.ts @@ -1,28 +1,44 @@ -import { type MessageJsonType, fromJson, toJson } from '@bufbuild/protobuf' import type { CallOptions } from '@connectrpc/connect' +import { Hex, type Types } from 'ox' import type { Client } from '../../Client/types.js' import type { GlobalErrorType } from '../../Errors/error.js' -import { - type FidRequestJson, - FidRequestSchema, - MessagesResponseSchema, -} from '../../Protobufs/request_response_pb.js' +import { UserData_fromMessage } from '../../UserData/fromMessage.js' +import type { UserData } from '../../UserData/types.js' export declare namespace Actions_UserData_GetUserDataByFid { - type ReturnType = MessageJsonType - // @TODO: proper error handling - type ErrorType = GlobalErrorType + type ReturnType = UserData[] + type ErrorType = UserData_fromMessage.ErrorType | GlobalErrorType } export async function Actions_UserData_getUserDataByFid( client: Client, - parameters: Required, + parameters: { + fid: bigint + pageSize?: number | undefined + pageToken?: Types.Hex | undefined + reverse?: boolean | undefined + startTimestamp?: bigint + stopTimestamp?: bigint + }, options?: CallOptions, ): Promise { const message = await client.connectRpcClient.getUserDataByFid( - fromJson(FidRequestSchema, parameters), + { + fid: parameters.fid, + ...(parameters.pageSize ? { pageSize: parameters.pageSize } : {}), + ...(parameters.pageToken + ? { pageToken: Hex.toBytes(parameters.pageToken) } + : {}), + ...(parameters.reverse ? { reverse: parameters.reverse } : {}), + ...(parameters.startTimestamp + ? { startTimestamp: parameters.startTimestamp } + : {}), + ...(parameters.stopTimestamp + ? { stopTimestamp: parameters.stopTimestamp } + : {}), + }, options, ) - return toJson(MessagesResponseSchema, message) + return message.messages.map(UserData_fromMessage) } Actions_UserData_getUserDataByFid.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataDisplay.ts b/src/Internal/Actions/UserData/getUserDataDisplay.ts index 9978286..572ca9f 100644 --- a/src/Internal/Actions/UserData/getUserDataDisplay.ts +++ b/src/Internal/Actions/UserData/getUserDataDisplay.ts @@ -8,8 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataDisplay { - type ReturnType = Actions_UserData_GetUserData.ReturnType - // @TODO: proper error handling + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +17,7 @@ export async function Actions_UserData_getUserDataDisplay( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +25,7 @@ export async function Actions_UserData_getUserDataDisplay( }, options, ) + return data.value } Actions_UserData_getUserDataDisplay.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataLocation.ts b/src/Internal/Actions/UserData/getUserDataLocation.ts index ed64b44..272de05 100644 --- a/src/Internal/Actions/UserData/getUserDataLocation.ts +++ b/src/Internal/Actions/UserData/getUserDataLocation.ts @@ -8,8 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataLocation { - type ReturnType = Actions_UserData_GetUserData.ReturnType - // @TODO: proper error handling + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +17,7 @@ export async function Actions_UserData_getUserDataLocation( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +25,7 @@ export async function Actions_UserData_getUserDataLocation( }, options, ) + return data.value } Actions_UserData_getUserDataLocation.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataPfp.ts b/src/Internal/Actions/UserData/getUserDataPfp.ts index 225eede..763a6b2 100644 --- a/src/Internal/Actions/UserData/getUserDataPfp.ts +++ b/src/Internal/Actions/UserData/getUserDataPfp.ts @@ -8,8 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataPfp { - type ReturnType = Actions_UserData_GetUserData.ReturnType - // @TODO: proper error handling + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +17,7 @@ export async function Actions_UserData_getUserDataPfp( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +25,7 @@ export async function Actions_UserData_getUserDataPfp( }, options, ) + return data.value } Actions_UserData_getUserDataPfp.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataUrl.ts b/src/Internal/Actions/UserData/getUserDataUrl.ts index 55778a3..664e652 100644 --- a/src/Internal/Actions/UserData/getUserDataUrl.ts +++ b/src/Internal/Actions/UserData/getUserDataUrl.ts @@ -8,8 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataUrl { - type ReturnType = Actions_UserData_GetUserData.ReturnType - // @TODO: proper error handling + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +17,7 @@ export async function Actions_UserData_getUserDataUrl( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +25,7 @@ export async function Actions_UserData_getUserDataUrl( }, options, ) + return data.value } Actions_UserData_getUserDataUrl.parseError = (error: unknown) => diff --git a/src/Internal/Actions/UserData/getUserDataUsername.ts b/src/Internal/Actions/UserData/getUserDataUsername.ts index 6df819f..6b54ca3 100644 --- a/src/Internal/Actions/UserData/getUserDataUsername.ts +++ b/src/Internal/Actions/UserData/getUserDataUsername.ts @@ -8,8 +8,7 @@ import { } from './getUserData.js' export declare namespace Actions_UserData_GetUserDataUsername { - type ReturnType = Actions_UserData_GetUserData.ReturnType - // @TODO: proper error handling + type ReturnType = Actions_UserData_GetUserData.ReturnType['value'] type ErrorType = Actions_UserData_GetUserData.ErrorType | GlobalErrorType } @@ -18,7 +17,7 @@ export async function Actions_UserData_getUserDataUsername( parameters: { fid: bigint }, options?: CallOptions, ): Promise { - return Actions_UserData_getUserData( + const data = await Actions_UserData_getUserData( client, { ...parameters, @@ -26,6 +25,7 @@ export async function Actions_UserData_getUserDataUsername( }, options, ) + return data.value } Actions_UserData_getUserDataUsername.parseError = (error: unknown) => diff --git a/src/Internal/UserData/errors.ts b/src/Internal/UserData/errors.ts new file mode 100644 index 0000000..e2de798 --- /dev/null +++ b/src/Internal/UserData/errors.ts @@ -0,0 +1,12 @@ +import { BaseError } from 'ox/Errors' + +// @TODO: replace by our own BaseError +export class UserData_InvalidMessageTypeError extends BaseError { + override readonly name = 'UserData.InvalidMessageTypeError' + + constructor({ hash }: { hash: string }) { + super(`Message "${hash}" has type other than USER_DATA_ADD.`, { + docsPath: '/errors#invalidmessagetypeerror', + }) + } +} diff --git a/src/Internal/UserData/fromMessage.ts b/src/Internal/UserData/fromMessage.ts index e23c408..de287ed 100644 --- a/src/Internal/UserData/fromMessage.ts +++ b/src/Internal/UserData/fromMessage.ts @@ -1,34 +1,41 @@ +import { Hex } from 'ox' import type { GlobalErrorType } from '../Errors/error.js' import { type Message, MessageType, UserDataType, } from '../Protobufs/message_pb.js' +import { UserData_InvalidMessageTypeError } from './errors.js' import type { UserData } from './types.js' export function UserData_fromMessage( message: Message, ): UserData_fromMessage.ReturnType { - // @TODO: error here - if (!message.data) throw new Error('') // @TODO: error here if ( - message.data.type !== MessageType.USER_DATA_ADD || - message.data.body.case !== 'userDataBody' + message.data?.type !== MessageType.USER_DATA_ADD || + message.data?.body.case !== 'userDataBody' ) - throw new Error('') + throw new UserData_InvalidMessageTypeError({ + hash: Hex.fromBytes(message.hash), + }) const type = (() => { if (message.data.body.value.type === UserDataType.PFP) { return 'pfp' - } else if (message.data.body.value.type === UserDataType.BIO) { + } + if (message.data.body.value.type === UserDataType.BIO) { return 'bio' - } else if (message.data.body.value.type === UserDataType.DISPLAY) { + } + if (message.data.body.value.type === UserDataType.DISPLAY) { return 'display' - } else if (message.data.body.value.type === UserDataType.URL) { + } + if (message.data.body.value.type === UserDataType.URL) { return 'url' - } else if (message.data.body.value.type === UserDataType.USERNAME) { + } + if (message.data.body.value.type === UserDataType.USERNAME) { return 'username' - } else if (message.data.body.value.type === UserDataType.LOCATION) { + } + if (message.data.body.value.type === UserDataType.LOCATION) { return 'location' } return 'none' @@ -40,7 +47,7 @@ export function UserData_fromMessage( export declare namespace UserData_fromMessage { type ReturnType = UserData - type ErrorType = GlobalErrorType + type ErrorType = UserData_InvalidMessageTypeError | GlobalErrorType } UserData_fromMessage.parseError = (error: unknown) =>