Make search a post request

This commit is contained in:
Adria Navarro 2023-08-03 04:58:37 +02:00
parent b69943c074
commit d28120de3a
4 changed files with 23 additions and 60 deletions

View File

@ -1,14 +1,8 @@
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { import { UserCtx, SearchResponse, ViewV2, SearchRequest } from "@budibase/types"
UserCtx,
SearchResponse,
SortOrder,
SortType,
ViewV2,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
export async function searchView(ctx: UserCtx<void, SearchResponse>) { export async function searchView(ctx: UserCtx<SearchRequest, SearchResponse>) {
const { viewId } = ctx.params const { viewId } = ctx.params
const view = await sdk.views.get(viewId) const view = await sdk.views.get(viewId)
@ -35,7 +29,7 @@ export async function searchView(ctx: UserCtx<void, SearchResponse>) {
tableId: view.tableId, tableId: view.tableId,
query: view.query || {}, query: view.query || {},
fields: viewFields, fields: viewFields,
...getSortOptions(ctx, view), ...getSortOptions(ctx.request.body, view),
}), }),
{ {
datasourceId: view.tableId, datasourceId: view.tableId,
@ -46,32 +40,12 @@ export async function searchView(ctx: UserCtx<void, SearchResponse>) {
ctx.body = result ctx.body = result
} }
function getSortOptions( function getSortOptions(request: SearchRequest, view: ViewV2) {
ctx: UserCtx, if (request.sort?.column) {
view: ViewV2
):
| {
sort: string
sortOrder?: SortOrder
sortType?: SortType
}
| undefined {
const { sort_column, sort_order, sort_type } = ctx.query
if (Array.isArray(sort_column)) {
ctx.throw(400, "sort_column cannot be an array")
}
if (Array.isArray(sort_order)) {
ctx.throw(400, "sort_order cannot be an array")
}
if (Array.isArray(sort_type)) {
ctx.throw(400, "sort_type cannot be an array")
}
if (sort_column) {
return { return {
sort: sort_column, sort: request.sort.column,
sortOrder: sort_order as SortOrder, sortOrder: request.sort.order,
sortType: sort_type as SortType, sortType: request.sort.type,
} }
} }
if (view.sort) { if (view.sort) {
@ -82,5 +56,5 @@ function getSortOptions(
} }
} }
return return undefined
} }

View File

@ -269,7 +269,7 @@ router
) )
router router
.get( .post(
"/api/v2/views/:viewId/search", "/api/v2/views/:viewId/search",
authorized(PermissionType.VIEW, PermissionLevel.READ), authorized(PermissionType.VIEW, PermissionLevel.READ),
rowController.views.searchView rowController.views.searchView

View File

@ -1,13 +1,12 @@
import { import {
CreateViewRequest, CreateViewRequest,
SortOrder,
SortType,
UpdateViewRequest, UpdateViewRequest,
DeleteRowRequest, DeleteRowRequest,
PatchRowRequest, PatchRowRequest,
PatchRowResponse, PatchRowResponse,
Row, Row,
ViewV2, ViewV2,
SearchRequest,
} from "@budibase/types" } from "@budibase/types"
import TestConfiguration from "../TestConfiguration" import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base" import { TestAPI } from "./base"
@ -81,31 +80,12 @@ export class ViewV2API extends TestAPI {
search = async ( search = async (
viewId: string, viewId: string,
options?: { params?: SearchRequest,
sort: {
column: string
order?: SortOrder
type?: SortType
}
},
{ expectStatus } = { expectStatus: 200 } { expectStatus } = { expectStatus: 200 }
) => { ) => {
const qs: [string, any][] = []
if (options?.sort.column) {
qs.push(["sort_column", options.sort.column])
}
if (options?.sort.order) {
qs.push(["sort_order", options.sort.order])
}
if (options?.sort.type) {
qs.push(["sort_type", options.sort.type])
}
let url = `/api/v2/views/${viewId}/search`
if (qs.length) {
url += "?" + qs.map(q => q.join("=")).join("&")
}
return this.request return this.request
.get(url) .post(`/api/v2/views/${viewId}/search`)
.send(params)
.set(this.config.defaultHeaders()) .set(this.config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(expectStatus) .expect(expectStatus)

View File

@ -1,4 +1,5 @@
import { Row } from "../../../documents" import { Row } from "../../../documents"
import { SortOrder, SortType } from "../pagination"
export interface PatchRowRequest extends Row { export interface PatchRowRequest extends Row {
_id: string _id: string
@ -8,6 +9,14 @@ export interface PatchRowRequest extends Row {
export interface PatchRowResponse extends Row {} export interface PatchRowResponse extends Row {}
export interface SearchRequest {
sort?: {
column: string
order?: SortOrder
type?: SortType
}
}
export interface SearchResponse { export interface SearchResponse {
rows: any[] rows: any[]
} }