import {z} from 'zod' export type SearchIndexedRequest = {search_term: string, with_relationships?: Array | undefined, team_type_id?: number | undefined, excluded_id?: Array | undefined, with_deleted?: boolean | undefined, is_parent?: boolean | undefined, is_contact_of?: number | undefined, brand_typo_tolerance?: boolean | undefined, organization_access?: (Array<'brands' | 'properties' | 'agencies'>) | undefined, linkedin_url?: string | undefined, email?: string | undefined, for_primary_profile?: boolean | undefined, for_secondary_profile?: boolean | undefined, with_organization_users?: boolean | undefined, show_secondary_profiles?: boolean | undefined}; export type GetApiApiSearchIndexedEntityArgs = {indexed_entity: 'agency' | 'brand' | 'property' | 'contact' | 'user' | 'league' | 'category' | 'market' | 'organization' | 'venue' | 'subcategory' | 'genre' | 'subgenre' | 'asset', SearchIndexedRequest?: SearchIndexedRequest | undefined}; export type GetApiApiSearchIndexedEntityOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const searchIndexedResource = z.object({items: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), type: z.string().nullable().optional(), ulid: z.string().optional(), logo: z.string().nullable().optional(), is_primary_profile: z.boolean().optional()})).optional(), total: z.number().int().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 }; /** * Global search over a Meilisearch-indexed entity * * GET /api/search/{indexed_entity} */ export const getApiApiSearchIndexedEntity = async (args: GetApiApiSearchIndexedEntityArgs, options: GetApiApiSearchIndexedEntityOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.SearchIndexedRequest != null) query.append("SearchIndexedRequest", JSON.stringify(args.SearchIndexedRequest)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/search/${encodeURIComponent(String(args.indexed_entity))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(searchIndexedResource, await res.json()) };