import {z} from 'zod' export type SvstoreEntityTypeFieldDataType = 'DATA_TYPE_UNSPECIFIED' | 'TEXT' | 'NUMBER' | 'BOOL' | 'TIMESTAMP' | 'ENUM' | 'ENTITY_REF' | 'USER_REF' | 'UNIQUE' | 'MULTI_SELECT' | 'FILE'; export type SvstoreEnumOptionInput = {value?: string | undefined, displayName?: string | undefined, displayOrder?: number | undefined}; export type SvstoreEntityTypeFieldInput = {key?: (string | null) | undefined, name?: string | undefined, dataType?: SvstoreEntityTypeFieldDataType | undefined, enumOptions?: Array | undefined, isKeyField?: boolean | undefined, isNameField?: boolean | undefined, refEntityTypeId?: (string | null) | undefined}; export type SvstoreHelpAgentEntityVisibility = 'HELP_AGENT_ENTITY_VISIBILITY_UNSPECIFIED' | 'HELP_AGENT_ENTITY_VISIBILITY_NONE' | 'HELP_AGENT_ENTITY_VISIBILITY_REQUESTER_ASSOCIATED' | 'HELP_AGENT_ENTITY_VISIBILITY_ALWAYS'; export type SvstorePublicapiCreateEntityTypeRequest = {teamId?: string | undefined, key?: (string | null) | undefined, name?: string | undefined, fields?: Array | undefined, parentEntityTypeId?: (string | null) | undefined, schemaKey?: (string | null) | undefined, helpAgentVisibility?: SvstoreHelpAgentEntityVisibility | undefined, isEdgeType?: (boolean | null) | undefined}; export type CreateApiV2EntityTypesArgs = {body: SvstorePublicapiCreateEntityTypeRequest}; export type CreateApiV2EntityTypesOptions = {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 svstoreEnumOption = z.object({id: z.string().optional(), value: z.string().optional(), displayName: z.string().optional(), displayOrder: z.number().int().optional()}); export const svstoreEntityTypeField = z.object({id: z.string().optional(), entityTypeId: z.string().optional(), key: z.string().optional(), name: z.string().optional(), dataType: svstoreEntityTypeFieldDataType.optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional(), createdByUserId: z.string().optional(), updatedByUserId: z.string().optional(), enumOptions: z.array(svstoreEnumOption).optional(), isKeyField: z.boolean().optional(), isNameField: z.boolean().optional(), refEntityTypeId: z.string().nullable().optional(), description: z.string().nullable().optional(), sourceEntityTypeId: z.string().nullable().optional(), sectionId: z.string().nullable().optional()}); export const svstoreHelpAgentEntityVisibility = z.enum(["HELP_AGENT_ENTITY_VISIBILITY_UNSPECIFIED", "HELP_AGENT_ENTITY_VISIBILITY_NONE", "HELP_AGENT_ENTITY_VISIBILITY_REQUESTER_ASSOCIATED", "HELP_AGENT_ENTITY_VISIBILITY_ALWAYS"]); export const svstoreEntityFieldSection = z.object({id: z.string().optional(), entityTypeId: z.string().optional(), name: z.string().optional(), sourceEntityTypeId: z.string().nullable().optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional(), createdByUserId: z.string().optional(), updatedByUserId: z.string().optional()}); export const svstoreEntityTypeWithFields = z.object({id: z.string().optional(), teamId: z.string().optional(), key: z.string().optional(), name: z.string().optional(), parentEntityTypeId: z.string().nullable().optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional(), createdByUserId: z.string().optional(), updatedByUserId: z.string().optional(), fields: z.array(svstoreEntityTypeField).optional(), color: z.string().nullable().optional(), iconSlug: z.string().optional(), entitySchemaId: z.string().optional(), helpAgentVisibility: svstoreHelpAgentEntityVisibility.optional(), sections: z.array(svstoreEntityFieldSection).optional(), isEdgeType: z.boolean().optional(), description: z.string().nullable().optional()}); export const svstorePublicapiCreateEntityTypeResponse = z.object({data: svstoreEntityTypeWithFields.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 }; /** * Create Entity Type * * POST /v2/entity-types */ export const createApiV2EntityTypes = async (args: CreateApiV2EntityTypesArgs, options?: CreateApiV2EntityTypesOptions) => { 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/entity-types${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(svstorePublicapiCreateEntityTypeResponse, await res.json()) };