import {z} from 'zod' export type GoogleProtobufEmpty = Record; export type SvstoreBoolPredicate = {eq: boolean} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty} | {ne: boolean}; export type SvstoreExactStringPredicate = {eq: string} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty} | {ne: string}; export type SvstoreStringListValue = {values?: Array | undefined}; export type SvstoreMultiSelectPredicate = {contains: string} | {containsAll: SvstoreStringListValue} | {containsAny: SvstoreStringListValue} | {doesNotContain: string} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty}; export type SvstoreNumberPredicate = {eq: number} | {gt: number} | {gte: number} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty} | {lt: number} | {lte: number} | {ne: number}; export type SvstoreStringPredicate = {contains: string} | {doesNotContain: string} | {endsWith: string} | {eq: string} | {inList: SvstoreStringListValue} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty} | {like: string} | {ne: string} | {startsWith: string}; export type GoogleProtobufTimestamp = string; export type SvstoreTimestampPredicate = {eq: GoogleProtobufTimestamp} | {gt: GoogleProtobufTimestamp} | {gte: GoogleProtobufTimestamp} | {isNotNull: GoogleProtobufEmpty} | {isNull: GoogleProtobufEmpty} | {lt: GoogleProtobufTimestamp} | {lte: GoogleProtobufTimestamp} | {ne: GoogleProtobufTimestamp}; export type SvstoreEntityFieldFilter = {boolValue: SvstoreBoolPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {entityRef: SvstoreExactStringPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {enumValue: SvstoreExactStringPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {fileValue: SvstoreExactStringPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {multiSelectValue: SvstoreMultiSelectPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {number: SvstoreNumberPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {text: SvstoreStringPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {timestamp: SvstoreTimestampPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined} | {userRef: SvstoreExactStringPredicate, fieldId?: (string | null) | undefined, fieldKey?: (string | null) | undefined}; export type SvstoreEntitySourcePredicate = {eq?: string | undefined, ne?: string | undefined}; export type SvstoreEntityFilterGroup = {filters?: Array | undefined, sourceFilter?: SvstoreEntitySourcePredicate | undefined}; export type SvstoreEntitySort = {entitySource: GoogleProtobufEmpty, descending?: boolean | undefined} | {fieldId: string, descending?: boolean | undefined} | {fieldKey: string, descending?: boolean | undefined}; export type SvstorePublicapiSearchEntitiesRequest = {teamId?: string | undefined, entityTypeId?: (string | null) | undefined, entityTypeKey?: (string | null) | undefined, filterGroups?: Array | undefined, fieldSorts?: Array | undefined, pageSize?: (number | null) | undefined, pageToken?: (string | null) | undefined, key?: (string | null) | undefined}; export type CreateApiV2EntitiesSearchArgs = {body: SvstorePublicapiSearchEntitiesRequest}; export type CreateApiV2EntitiesSearchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufTimestamp = z.string(); export const svstoreEntityTypeFieldDataType = z.enum(["DATA_TYPE_UNSPECIFIED", "TEXT", "NUMBER", "BOOL", "TIMESTAMP", "ENUM", "ENTITY_REF", "USER_REF", "UNIQUE", "MULTI_SELECT", "FILE"]); export const googleProtobufValue = z.union([z.number(), z.string(), z.boolean(), z.array(z.unknown()), z.record(z.string(), z.unknown())]).nullable(); export const svstoreEntityFieldValue = z.object({id: z.string().optional(), entityId: z.string().optional(), fieldId: z.string().optional(), fieldKey: z.string().optional(), fieldName: z.string().optional(), dataType: svstoreEntityTypeFieldDataType.optional(), value: googleProtobufValue.optional(), setByUserId: z.string().nullable().optional(), setByAppInstanceId: z.string().nullable().optional(), createdAt: googleProtobufTimestamp.optional(), deletedAt: googleProtobufTimestamp.optional(), pinned: z.boolean().optional()}); export const svstoreSharedEntityMetadata = z.object({sourceTeamId: z.string().optional(), sourceTeamName: z.string().optional()}); export const svstoreEntity = z.object({id: z.string().optional(), key: z.string().optional(), name: z.string().optional(), teamId: z.string().optional(), entityTypeId: z.string().optional(), entityTypeKey: z.string().optional(), createdByUserId: z.string().optional(), updatedByUserId: z.string().optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional(), deletedAt: googleProtobufTimestamp.optional(), fieldValues: z.array(svstoreEntityFieldValue).optional(), sourceAppInstanceIds: z.array(z.string()).optional(), sharedMetadata: svstoreSharedEntityMetadata.optional()}); export const svstorePublicapiSearchEntitiesResponse = z.object({data: z.array(svstoreEntity).optional(), nextPageToken: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search Entities * * POST /v2/entities/search */ export const createApiV2EntitiesSearch = async (args: CreateApiV2EntitiesSearchArgs, options?: CreateApiV2EntitiesSearchOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/entities/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(svstorePublicapiSearchEntitiesResponse, await res.json()) };