import {z} from 'zod' export type GetApiNotesArgs = {groupId?: string | undefined, from?: unknown | undefined, to?: unknown | undefined, limit?: number | undefined, lastNoteId?: string | undefined, lastNoteDate?: string | undefined}; export type GetApiNotesOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const publicNoteResponseDto = z.object({id: z.string().optional(), note: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), dateCreated: z.string().optional(), dateUpdated: z.string().optional()}); export const getPublicNotesResponseDto = z.object({notes: z.array(publicNoteResponseDto).optional(), hasMore: z.boolean().optional(), lastNoteId: z.string().nullable().optional(), lastNoteDate: z.string().nullable().optional(), totalCount: z.number().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 }; /** * List Notes * * GET /notes */ export const getApiNotes = async (args: GetApiNotesArgs, options?: GetApiNotesOptions) => { const {baseUrl = "https://public.doubletick.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.groupId != null) query.append("groupId", String(args.groupId)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.lastNoteId != null) query.append("lastNoteId", String(args.lastNoteId)) if (args.lastNoteDate != null) query.append("lastNoteDate", String(args.lastNoteDate)) if (basic !== undefined) headers.set("Authorization", basic) 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(getPublicNotesResponseDto, await res.json()) };