import {z} from 'zod' export type GetApiV1PeopleIdOptions = {baseUrl?: string | undefined, MetalClientId?: string | undefined, MetalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const externalReference = z.object({source: z.string().optional(), id: z.string().optional()}); export const discoveredValue = z.object({value: z.unknown(), source: z.string().optional(), confidence: z.number().optional(), pinned: z.boolean().optional(), discoveredAt: z.string().optional()}); export const enrichmentProvenance = z.record(z.string(), z.array(discoveredValue)); export const person = z.object({id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), currentTitle: z.string().optional(), currentCompanyName: z.string().optional(), company: z.string().optional(), linkedinUrl: z.string().optional(), phoneNumber: z.string().optional(), location: location.optional(), externalId: z.string().optional(), externalReference: externalReference.optional(), enrichment: enrichmentProvenance.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const getApiV1PeopleIdResponse = z.object({data: person.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 }; /** * Get a person * * GET /v1/people/{id} */ export const getApiV1PeopleId = async (options?: GetApiV1PeopleIdOptions) => { const {baseUrl = "https://api.metal.ai", fetch: fetchFn = fetch, MetalClientId, MetalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (MetalClientId !== undefined) headers.set("x-metal-client-id", MetalClientId) if (MetalApiKey !== undefined) headers.set("x-metal-api-key", MetalApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/people/${encodeURIComponent(String(args.id))}${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(getApiV1PeopleIdResponse, await res.json()) };