Move types

This commit is contained in:
Adria Navarro 2024-11-28 16:33:31 +01:00
parent 792388f1e1
commit e3e82f2fb8
2 changed files with 104 additions and 100 deletions

View File

@ -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<SearchFilterKey, z.ZodTypeAny> = {
[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<typeof searchRowRequest>
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[]

View File

@ -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<SearchFilterKey, z.ZodTypeAny> = {
[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<typeof searchRowRequest>
export type SearchViewRowRequest = Pick<
SearchRowRequest,
| "sort"
| "sortOrder"
| "sortType"
| "limit"
| "bookmark"
| "paginate"
| "query"
| "countRows"
>
export interface SearchRowResponse {
rows: Row[]
}
export interface PaginatedSearchRowResponse
extends SearchRowResponse,
PaginationResponse {}