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' },
|
2023-02-14 08:26:22 +00:00
|
|
|
{ id: 'getBadges', name: 'Query Badges', description: 'Query a list of badges' },
|
2023-02-23 10:18:30 +00:00
|
|
|
// { id: 'getKey', name: 'Query Key', description: 'Query a key by ID to see if it's whitelisted' },
|
|
|
|
// { id: 'getKeys', name: 'Query Keys', description: 'Query the list of whitelisted 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
|
2023-02-14 08:26:22 +00:00
|
|
|
startAfterNumber: number
|
|
|
|
startAfterString: string
|
|
|
|
limit: number
|
2023-01-31 17:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const dispatchQuery = (props: DispatchQueryProps) => {
|
2023-02-14 08:26:22 +00:00
|
|
|
const { id, pubkey, messages, type, startAfterNumber, startAfterString, limit } = 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': {
|
2023-02-14 08:26:22 +00:00
|
|
|
return messages?.getBadges(startAfterNumber, limit)
|
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': {
|
2023-02-14 08:26:22 +00:00
|
|
|
return messages?.getKeys(id, startAfterString, limit)
|
2023-01-31 17:31:06 +00:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error('unknown query type')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|