diff --git a/packages/types/src/api/web/app/rows/index.ts b/packages/types/src/api/web/app/rows/index.ts index 001066d7fb..2642a8b04e 100644 --- a/packages/types/src/api/web/app/rows/index.ts +++ b/packages/types/src/api/web/app/rows/index.ts @@ -1,21 +1,9 @@ -import { - ArrayOperator, - BasicOperator, - EmptyFilterOption, - InternalSearchFilterOperator, - LogicalOperator, - RangeOperator, - SearchFilterKey, - SearchFilters, -} from "../../../../sdk" +import { SearchFilters } from "../../../../sdk" import { Row } from "../../../../documents" -import { - PaginationResponse, - SortOrder, - SortType, -} from "../../../../api/web/pagination" +import { SortOrder } from "../../../../api/web/pagination" import { ReadStream } from "fs" -import { z } from "zod" + +export * from "./search" export interface SaveRowRequest extends Row {} @@ -27,90 +15,6 @@ export interface PatchRowRequest extends Row { export interface PatchRowResponse extends Row {} -const fieldKey = z - .string() - .refine(s => s !== InternalSearchFilterOperator.COMPLEX_ID_OPERATOR, { - message: `Key '${InternalSearchFilterOperator.COMPLEX_ID_OPERATOR}' is not allowed`, - }) - -const stringBasicFilter = z.record(fieldKey, z.string()) -const basicFilter = z.record(fieldKey, z.any()) -const arrayFilter = z.record(fieldKey, z.union([z.any().array(), z.string()])) -const logicFilter = z.lazy(() => - z.object({ - conditions: z.array(z.object(queryFilterValidation)), - }) -) - -const stringOrNumber = z.union([z.string(), z.number()]) - -const queryFilterValidation: Record = { - [BasicOperator.STRING]: stringBasicFilter.optional(), - [BasicOperator.FUZZY]: stringBasicFilter.optional(), - [RangeOperator.RANGE]: z - .record( - fieldKey, - z.union([ - z.object({ high: stringOrNumber, low: stringOrNumber }), - z.object({ high: stringOrNumber }), - z.object({ low: stringOrNumber }), - ]) - ) - .optional(), - [BasicOperator.EQUAL]: basicFilter.optional(), - [BasicOperator.NOT_EQUAL]: basicFilter.optional(), - [BasicOperator.EMPTY]: basicFilter.optional(), - [BasicOperator.NOT_EMPTY]: basicFilter.optional(), - [ArrayOperator.ONE_OF]: arrayFilter.optional(), - [ArrayOperator.CONTAINS]: arrayFilter.optional(), - [ArrayOperator.NOT_CONTAINS]: arrayFilter.optional(), - [ArrayOperator.CONTAINS_ANY]: arrayFilter.optional(), - [LogicalOperator.AND]: logicFilter.optional(), - [LogicalOperator.OR]: logicFilter.optional(), -} - -const searchRowRequest = z.object({ - query: z - .object({ - allOr: z.boolean().optional(), - onEmptyFilter: z.nativeEnum(EmptyFilterOption).optional(), - ...queryFilterValidation, - }) - .optional(), - paginate: z.boolean().optional(), - bookmark: z.union([z.string(), z.number()]).nullish(), - limit: z.number().optional(), - sort: z.string().optional(), - sortOrder: z.nativeEnum(SortOrder).optional(), - sortType: z.nativeEnum(SortType).nullish(), - version: z.string().optional(), - disableEscaping: z.boolean().optional(), - countRows: z.boolean().optional(), -}) -export const searchRowRequestValidator = searchRowRequest -export type SearchRowRequest = z.infer - -export interface SearchViewRowRequest - extends Pick< - SearchRowRequest, - | "sort" - | "sortOrder" - | "sortType" - | "limit" - | "bookmark" - | "paginate" - | "query" - | "countRows" - > {} - -export interface SearchRowResponse { - rows: any[] -} - -export interface PaginatedSearchRowResponse - extends SearchRowResponse, - PaginationResponse {} - export interface ExportRowsRequest { rows?: string[] columns?: string[] diff --git a/packages/types/src/api/web/app/rows/search.ts b/packages/types/src/api/web/app/rows/search.ts new file mode 100644 index 0000000000..b74e490677 --- /dev/null +++ b/packages/types/src/api/web/app/rows/search.ts @@ -0,0 +1,100 @@ +import { + ArrayOperator, + BasicOperator, + EmptyFilterOption, + InternalSearchFilterOperator, + LogicalOperator, + RangeOperator, + SearchFilterKey, +} from "../../../../sdk" +import { Row } from "../../../../documents" +import { + PaginationResponse, + SortOrder, + SortType, +} from "../../../../api/web/pagination" +import { z } from "zod" + +const fieldKey = z + .string() + .refine(s => s !== InternalSearchFilterOperator.COMPLEX_ID_OPERATOR, { + message: `Key '${InternalSearchFilterOperator.COMPLEX_ID_OPERATOR}' is not allowed`, + }) + +const stringBasicFilter = z.record(fieldKey, z.string()) +const basicFilter = z.record(fieldKey, z.any()) +const arrayFilter = z.record(fieldKey, z.union([z.any().array(), z.string()])) +const logicFilter = z.lazy(() => + z.object({ + conditions: z.array(z.object(queryFilterValidation)), + }) +) + +const stringOrNumber = z.union([z.string(), z.number()]) + +const queryFilterValidation: Record = { + [BasicOperator.STRING]: stringBasicFilter.optional(), + [BasicOperator.FUZZY]: stringBasicFilter.optional(), + [RangeOperator.RANGE]: z + .record( + fieldKey, + z.union([ + z.object({ high: stringOrNumber, low: stringOrNumber }), + z.object({ high: stringOrNumber }), + z.object({ low: stringOrNumber }), + ]) + ) + .optional(), + [BasicOperator.EQUAL]: basicFilter.optional(), + [BasicOperator.NOT_EQUAL]: basicFilter.optional(), + [BasicOperator.EMPTY]: basicFilter.optional(), + [BasicOperator.NOT_EMPTY]: basicFilter.optional(), + [ArrayOperator.ONE_OF]: arrayFilter.optional(), + [ArrayOperator.CONTAINS]: arrayFilter.optional(), + [ArrayOperator.NOT_CONTAINS]: arrayFilter.optional(), + [ArrayOperator.CONTAINS_ANY]: arrayFilter.optional(), + [LogicalOperator.AND]: logicFilter.optional(), + [LogicalOperator.OR]: logicFilter.optional(), +} + +const searchRowRequest = z.object({ + query: z + .object({ + allOr: z.boolean().optional(), + onEmptyFilter: z.nativeEnum(EmptyFilterOption).optional(), + ...queryFilterValidation, + }) + .optional(), + paginate: z.boolean().optional(), + bookmark: z.union([z.string(), z.number()]).nullish(), + limit: z.number().optional(), + sort: z.string().optional(), + sortOrder: z.nativeEnum(SortOrder).optional(), + sortType: z.nativeEnum(SortType).nullish(), + version: z.string().optional(), + disableEscaping: z.boolean().optional(), + countRows: z.boolean().optional(), +}) + +export const searchRowRequestValidator = searchRowRequest + +export type SearchRowRequest = z.infer +export type SearchViewRowRequest = Pick< + SearchRowRequest, + | "sort" + | "sortOrder" + | "sortType" + | "limit" + | "bookmark" + | "paginate" + | "query" + | "countRows" +> + +export interface SearchRowResponse { + rows: Row[] +} + +export interface PaginatedSearchRowResponse + extends SearchRowResponse, + PaginationResponse {}