import {z} from 'zod' export type DateISO = string; export type GetApiNotesArgs = {limit?: number | undefined, cursor?: string | undefined, modifiedSince?: DateISO | undefined, modifiedUntil?: DateISO | undefined, candidateId?: string | undefined, clientContactId?: string | undefined, source?: ('phone' | 'phoneInbound' | 'phoneOutbound' | 'inPerson' | 'onlineMeeting' | 'callAttempted') | undefined, labelIds?: Array | undefined}; export type GetApiNotesOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dateISO = z.string(); export const userBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const noteEditorDto = z.object({editedBy: userBadgeDto, editedAt: dateISO}); export const clientBadgeDto = z.object({id: z.string(), name: z.string(), logoUrl: z.string().nullable()}); export const companyBadgeDto = z.object({id: z.string(), name: z.string(), logoUrl: z.string().nullable()}); export const noteLinkDto = z.union([z.object({id: z.string(), noteId: z.string(), entityType: z.literal("candidate"), candidate: z.object({id: z.string(), firstName: z.string(), lastName: z.string(), middleName: z.string().nullable(), secondLastName: z.string().nullable(), avatarUrl: z.string().nullable()})}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("vacancy"), vacancy: z.object({id: z.string(), name: z.string()}), client: clientBadgeDto}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("client"), client: clientBadgeDto}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("application"), applicationId: z.string()}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("clientContact"), clientContact: z.object({id: z.string(), firstName: z.string(), lastName: z.string(), middleName: z.string().nullable(), secondLastName: z.string().nullable(), avatarUrl: z.string().nullable(), candidateId: z.string(), companyId: z.string(), companyName: z.string(), companyLogoUrl: z.string().nullable()})}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("interview"), interviewId: z.string()}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("opportunity"), opportunity: z.object({id: z.string(), name: z.string(), company: companyBadgeDto})})]); export const meetingProvider = z.enum(["googleMeet", "zoom", "microsoftTeams", "audioFile", "null"]).nullable(); export const noteLinkedMeetingDto = z.object({id: z.string(), meetingProvider: meetingProvider.nullable(), title: z.string(), startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const telecomProvider = z.enum(["TELNYX", "AIRCALL", "RINGCENTRAL", "RINGOVER", "VOIPPLAY", "DIALPAD"]); export const noteLinkedCallDto = z.object({id: z.string(), provider: telecomProvider, startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const noteLinkedAudioRecordingDto = z.object({id: z.string(), startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const noteDtoSource = z.enum(["phone", "phoneInbound", "phoneOutbound", "inPerson", "onlineMeeting", "callAttempted", "null"]).nullable(); export const noteDto = z.object({id: z.string(), title: z.string().nullable(), content: z.string(), createdAt: dateISO, lastEditedAt: dateISO, createdBy: userBadgeDto.nullable(), editors: z.array(noteEditorDto), links: z.array(noteLinkDto), meeting: noteLinkedMeetingDto.nullable(), call: noteLinkedCallDto.nullable(), audioRecording: noteLinkedAudioRecordingDto.nullable(), type: z.enum(["manual", "aiSummary"]), source: noteDtoSource, pinned: z.boolean(), shouldPoll: z.boolean(), labels: z.array(z.object({id: z.string(), name: z.string(), colorHex: z.string()}))}); export const getNotesResponseDto = z.object({items: z.array(noteDto), pageInfo: z.object({nextCursor: z.string().nullable(), hasNextPage: z.boolean()})}); 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 }; /** * List notes * * GET /notes */ export const getApiNotes = async (args: GetApiNotesArgs, options: GetApiNotesOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.modifiedSince != null) query.append("modifiedSince", String(args.modifiedSince)) if (args.modifiedUntil != null) query.append("modifiedUntil", String(args.modifiedUntil)) if (args.candidateId != null) query.append("candidateId", String(args.candidateId)) if (args.clientContactId != null) query.append("clientContactId", String(args.clientContactId)) if (args.source != null) query.append("source", String(args.source)) for (const item of args.labelIds ?? []) query.append("labelIds", String(item)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/notes${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(getNotesResponseDto, await res.json()) };