import {z} from 'zod' export type ExternalAPIContractsRequestsV1GetTimelogsRequest = {workspaceId: string, startDate: string, endDate: string, boardIds?: (Array | null) | undefined, rowIds?: (Array | null) | undefined, userIds?: (Array | null) | undefined, tags?: (Array | null) | undefined}; export type CreateApiApiV1TimelogsArgs = {body: ExternalAPIContractsRequestsV1GetTimelogsRequest}; export type CreateApiApiV1TimelogsOptions = {baseUrl: string, TeamhoodAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const externalAPIContractsResponsesV1EntitiesTimelog = z.object({timelogId: z.string(), userId: z.string(), userFullName: z.string().nullable(), itemId: z.string(), itemTitle: z.string().nullable(), dateAdded: z.string(), durationSeconds: z.number().int(), description: z.string().nullable(), boardId: z.string(), boardTitle: z.string().nullable(), rowId: z.string(), rowTitle: z.string().nullable(), tags: z.array(z.string()).nullable()}); export const externalAPIContractsResponsesV1GetTimelogsResponse = z.object({timelogs: z.array(externalAPIContractsResponsesV1EntitiesTimelog).nullable()}); 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 Timelogs * * POST /api/v1/timelogs */ export const createApiApiV1Timelogs = async (args: CreateApiApiV1TimelogsArgs, options: CreateApiApiV1TimelogsOptions) => { const {baseUrl, fetch: fetchFn = fetch, TeamhoodAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TeamhoodAPIKey !== undefined) headers.set("X-ApiKey", TeamhoodAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/timelogs${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(externalAPIContractsResponsesV1GetTimelogsResponse, await res.json()) };