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

69 lines
1.7 KiB
TypeScript
Raw Normal View History

import { get } from "svelte/store"
2025-01-02 16:27:43 +01:00
import DataFetch from "./DataFetch"
import { TableNames } from "../constants"
import { utils } from "@budibase/shared-core"
2025-01-02 16:25:25 +01:00
import { BasicOperator, Table } from "@budibase/types"
2025-01-02 15:55:50 +01:00
import { APIClient } from "../api/types.js"
2025-01-02 13:38:38 +01:00
export default class UserFetch extends DataFetch<{ tableId: string }, {}> {
2025-01-02 15:55:50 +01:00
constructor(opts: { API: APIClient; datasource: Table; options?: {} }) {
super({
...opts,
datasource: {
tableId: TableNames.USERS,
},
})
}
determineFeatureFlags() {
return {
supportsSearch: true,
supportsSort: false,
supportsPagination: true,
}
}
async getDefinition() {
return {
schema: {},
}
}
2025-01-02 13:38:38 +01:00
getSchema(_datasource: any, definition: Table | null) {
return definition?.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-02 13:38:38 +01:00
const { appId, paginated, ...rest } = query || ({} as any) // TODO
const finalQuery = utils.isSupportedUserSearch(rest)
? query
2025-01-02 15:55:50 +01:00
: { [BasicOperator.EMPTY]: { email: true } } // TODO: check
try {
const opts = {
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,
}
}
}
}