import {z} from 'zod' export type GetApiMeDashboardsArgs = {includeWidgets?: boolean | undefined}; export type GetApiMeDashboardsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const widget = z.object({id: z.string().optional(), type: z.string().nullable().optional(), name: z.string().nullable().optional(), entityId: z.string().nullable().optional(), filterId: z.string().nullable().optional(), createdOn: z.string().optional(), createdBy: z.string().nullable().optional(), updatedOn: z.string().optional(), updatedBy: z.string().nullable().optional(), column: z.number().int().optional(), order: z.number().int().optional(), settings: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const dashboard = z.object({id: z.string().optional(), userId: z.string().optional(), createdOn: z.string().optional(), updatedOn: z.string().optional(), widgets: z.array(widget).nullable().optional()}); export const getApiMeDashboardsResponse = z.array(dashboard); 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 }; /** * Returns dashboards for the current user. * * GET /me/dashboards */ export const getApiMeDashboards = async (args: GetApiMeDashboardsArgs, options?: GetApiMeDashboardsOptions) => { const {baseUrl = "https://api.awork.com/api/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeWidgets != null) query.append("includeWidgets", String(args.includeWidgets)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/me/dashboards${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(getApiMeDashboardsResponse, await res.json()) };