import {z} from 'zod' export type GetApiCompanyV1RolesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const date = z.number(); export const userForRole = z.object({id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), avatar: z.string().optional(), dateUpdate: date.optional()}); export const userlist = z.array(userForRole); export const departmentForRole = z.object({id: z.string().optional(), name: z.string().optional(), dateUpdate: date.optional()}); export const departmentlist = z.array(departmentForRole); export const roleList = z.object({id: z.string().optional(), title: z.string().optional(), code: z.string().optional(), userlist: userlist.optional(), departmentlist: departmentlist.optional()}); export const meta = z.object({currentPage: z.number().optional(), from: z.number().optional(), lastPage: z.number().optional(), links: z.array(z.object({url: z.string().optional(), label: z.string().optional(), active: z.boolean().optional()})).optional(), path: z.string().optional(), perPage: z.number().optional(), to: z.number().optional(), total: z.number().optional()}); export const getApiCompanyV1RolesResponse = z.object({data: z.array(roleList).optional(), meta: meta.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 roles list. * * GET /company/v1/roles */ export const getApiCompanyV1Roles = async (options: GetApiCompanyV1RolesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/company/v1/roles${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(getApiCompanyV1RolesResponse, await res.json()) };