import {z} from 'zod' export type GetApiGroupLinksArgs = {ids?: string | undefined, group_id?: number | undefined, query?: string | undefined, sort?: ('name' | '-name' | 'unlock' | '-unlock' | 'valid_until' | '-valid_until' | 'group_name' | '-group_name') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiGroupLinksOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiGroupLinksResponse = z.array(z.object({id: z.number().int(), resource_type: z.literal("GroupLink"), name: z.string(), email: z.string().nullable(), phone: z.string().nullable(), last_used_at: z.string().nullable(), link_enabled: z.boolean(), quick_response_code_type: z.enum(["online", "offline", "null"]).nullable(), valid_from: z.string().nullable(), valid_until: z.string().nullable(), created_at: z.string(), updated_at: z.string(), group_id: z.number().int(), group: z.object({id: z.number().int(), resource_type: z.literal("Group"), name: z.string()}), created_by_type: z.enum(["null", "User", "MarketplaceInstallation"]).nullable(), created_by_id: z.number().int().nullable(), created_by: z.union([z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("MarketplaceInstallation")})]).optional(), issued_by_id: z.number().int().nullable(), issued_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).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 }; /** * Fetch group links * * GET /group_links * * Also accepts auth: OAuth2 */ export const getApiGroupLinks = async (args: GetApiGroupLinksArgs, options?: GetApiGroupLinksOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ids != null) query.append("ids", String(args.ids)) if (args.group_id != null) query.append("group_id", String(args.group_id)) if (args.query != null) query.append("query", String(args.query)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/group_links${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(getApiGroupLinksResponse, await res.json()) };