budibase/packages/frontend-core/src/fetch/UserFetch.ts

82 lines
1.8 KiB
TypeScript
Raw Normal View History

import { get } from "svelte/store"
2025-01-14 10:48:50 +01:00
import BaseDataFetch, { DataFetchParams } from "./DataFetch"
import { utils } from "@budibase/shared-core"
2025-01-16 10:28:06 +01:00
import {
InternalTable,
SearchFilters,
SearchUsersRequest,
UserDatasource,
} from "@budibase/types"
2025-01-07 11:17:42 +01:00
interface UserFetchQuery {
appId: string
paginated: boolean
}
2025-01-14 12:51:27 +01:00
interface UserDefinition {
schema?: Record<string, any> | null
primaryDisplay?: string
}
2025-01-09 15:23:20 +01:00
2025-01-14 10:48:50 +01:00
export default class UserFetch extends BaseDataFetch<
2025-01-08 14:17:10 +01:00
UserDatasource,
2025-01-09 15:23:20 +01:00
UserDefinition,
2025-01-07 11:17:42 +01:00
UserFetchQuery
> {
2025-01-08 14:17:10 +01:00
constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) {
super({
...opts,
datasource: {
2025-01-09 15:23:20 +01:00
type: "user",
2025-01-16 10:28:06 +01:00
tableId: InternalTable.USER_METADATA,
},
})
}
2025-01-08 12:49:17 +01:00
async determineFeatureFlags() {
return {
supportsSearch: true,
supportsSort: false,
supportsPagination: true,
}
}
async getDefinition() {
2025-01-08 13:05:27 +01:00
return { schema: {} }
}
async getData() {
const { limit, paginate } = this.options
const { cursor, query } = get(this.store)
// Convert old format to new one - we now allow use of the lucene format
2025-01-08 13:05:27 +01:00
const { appId, paginated, ...rest } = query
2025-01-07 11:17:42 +01:00
const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest)
? rest
2025-01-10 16:39:55 +01:00
: {}
try {
2025-01-07 11:17:42 +01:00
const opts: SearchUsersRequest = {
2025-01-02 15:55:50 +01:00
bookmark: cursor ?? undefined,
query: finalQuery ?? undefined,
appId: appId,
paginate: paginated || paginate,
limit,
}
const res = await this.API.searchUsers(opts)
return {
rows: res?.data || [],
hasNextPage: res?.hasNextPage || false,
cursor: res?.nextPage || null,
}
} catch (error) {
return {
rows: [],
hasNextPage: false,
error,
}
}
}
}