import {z} from 'zod' export type GetApiAccountV2UsersArgs = {name?: string | undefined, email?: string | undefined, externalId?: string | undefined, username?: string | undefined, companyMembershipRole?: string | undefined, 'custom.customAttributeName'?: string | undefined, lastModified?: number | undefined, page?: number | undefined, size?: number | undefined, sortField?: ('DATE' | 'FIRST_NAME' | 'LAST_NAME') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, includeCustomAttributes?: boolean | undefined}; export type GetApiAccountV2UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const pageMetadata = z.object({number: z.number(), size: z.number(), totalElements: z.number(), totalPages: z.number()}); export const boostUser = z.object({apsUid: z.string().optional()}).nullable(); export const attributeType = z.enum(["TEXT", "MULTISELECT"]); export const customAttributeAccountV2 = z.object({attributeType: attributeType.optional(), hint: z.string().optional(), label: z.string().optional(), name: z.string().optional(), value: z.string().optional(), valueKeys: z.array(z.string()).optional()}); export const userAccountV2 = z.object({links: z.array(link).optional(), activated: z.boolean().optional(), boostUser: boostUser.optional(), creationDate: z.number().optional(), customAttributes: z.array(customAttributeAccountV2).optional(), email: z.string().optional(), externalId: z.string().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), allowLogin: z.boolean().optional(), lastSuccessfulLogin: z.number().optional(), ldapId: z.string().nullable().optional(), idpUuid: z.string().nullable().optional(), locale: z.string().optional(), metadata: z.object({}).optional(), openid: z.string().optional(), picture: z.string().nullable().optional(), roles: z.array(z.string()).optional(), userId: z.number().optional(), username: z.string().nullable().optional(), uuid: z.string().optional()}).nullable(); export const pagedUserAccountV2 = z.object({links: z.array(link).optional(), page: pageMetadata.optional(), content: z.array(userAccountV2).optional()}).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 all users * * GET /account/v2/users */ export const getApiAccountV2Users = async (args: GetApiAccountV2UsersArgs, options?: GetApiAccountV2UsersOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.email != null) query.append("email", String(args.email)) if (args.externalId != null) query.append("externalId", String(args.externalId)) if (args.username != null) query.append("username", String(args.username)) if (args.companyMembershipRole != null) query.append("companyMembershipRole", String(args.companyMembershipRole)) if (args['custom.customAttributeName'] != null) query.append("custom.customAttributeName", String(args['custom.customAttributeName'])) if (args.lastModified != null) query.append("lastModified", String(args.lastModified)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.includeCustomAttributes != null) query.append("includeCustomAttributes", String(args.includeCustomAttributes)) const url = `${baseUrl.replace(/\/+$/, '')}/account/v2/users${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(pagedUserAccountV2, await res.json()) };