stargaze-studio/contracts/badgeHub/messages/query.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-01-31 17:31:06 +00:00
import type { BadgeHubInstance } from '../contract'
export type QueryType = typeof QUERY_TYPES[number]
2023-02-01 12:55:52 +00:00
export const QUERY_TYPES = ['config', 'getBadge', 'getBadges', 'getKey', 'getKeys'] as const
2023-01-31 17:31:06 +00:00
export interface QueryListItem {
id: QueryType
name: string
description?: string
}
export const QUERY_LIST: QueryListItem[] = [
{ id: 'config', name: 'Config', description: 'View current config' },
2023-02-01 12:55:52 +00:00
{ id: 'getBadge', name: 'Query Badge', description: 'Query a badge by ID' },
{ id: 'getBadges', name: 'Query a list of Badges', description: 'Query the list of badges' },
{ id: 'getKey', name: 'Query Key', description: 'Query a key by ID' },
{ id: 'getKeys', name: 'Query a list of Keys', description: 'Query the list of keys' },
2023-01-31 17:31:06 +00:00
]
export interface DispatchQueryProps {
2023-02-01 12:55:52 +00:00
id: number
pubkey: string
2023-01-31 17:31:06 +00:00
messages: BadgeHubInstance | undefined
type: QueryType
}
export const dispatchQuery = (props: DispatchQueryProps) => {
2023-02-01 12:55:52 +00:00
const { id, pubkey, messages, type } = props
2023-01-31 17:31:06 +00:00
switch (type) {
case 'config': {
return messages?.getConfig()
}
2023-02-01 12:55:52 +00:00
case 'getBadge': {
return messages?.getBadge(id)
2023-01-31 17:31:06 +00:00
}
2023-02-01 12:55:52 +00:00
case 'getBadges': {
return messages?.getBadges()
2023-01-31 17:31:06 +00:00
}
2023-02-01 12:55:52 +00:00
case 'getKey': {
return messages?.getKey(id, pubkey)
2023-01-31 17:31:06 +00:00
}
2023-02-01 12:55:52 +00:00
case 'getKeys': {
return messages?.getKeys(id)
2023-01-31 17:31:06 +00:00
}
default: {
throw new Error('unknown query type')
}
}
}