import {z} from 'zod' export type UserCreationRequest = {createdByCompanyId?: string | undefined, companyId?: string | undefined, externalId?: string | undefined, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined}; export type CreateApiAppResellerV1UsersArgs = {context: 'RESELLER' | 'RESELLER_MANAGER', body: UserCreationRequest}; export type CreateApiAppResellerV1UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const appResellerAccountCompanyWS = z.object({id: z.string().optional(), externalId: z.string().nullable().optional(), name: z.string().optional(), createdOn: z.number().optional()}); export const mosiUser = z.object({username: z.string().nullable().optional()}).nullable(); export const role = z.enum(["USER", "APPLICATION", "PARTNER", "PARTNER_READ", "COMPANY", "CLIENT", "CHECKOUT_USER", "BILLING_ADMIN", "SYS_ADMIN", "DEVELOPER", "CHANNEL_PRODUCT_SUPPORT", "CHANNEL_SUPPORT", "CHANNEL_ADMIN", "CORPORATE_ADMIN", "SUPERUSER", "SUPER_SUPPORT", "ANALYTICS_ADMIN", "LAYOUT_DESIGNER", "SALES_SUPPORT", "RESTRICTED_SALES_SUPPORT", "RESELLER", "RESELLER_MANAGER", "TESTER", "MONITORING", "SYSTEM"]); export const appResellerAccountUserCompanyMembershipWS = z.object({company: appResellerAccountCompanyWS.optional(), enabled: z.boolean().optional(), mosiUser: mosiUser.optional(), roles: z.array(role).optional()}); export const userCreationResponse = z.object({id: z.string().optional(), companyId: z.string().optional(), email: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), createdOn: z.number().optional(), activated: z.boolean().optional(), invitationRequired: z.boolean().optional(), enabled: z.boolean().optional(), memberships: z.array(appResellerAccountUserCompanyMembershipWS).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 }; /** * Create user * * POST /appReseller/v1/users */ export const createApiAppResellerV1Users = async (args: CreateApiAppResellerV1UsersArgs, options?: CreateApiAppResellerV1UsersOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.context != null) query.append("context", String(args.context)) const url = `${baseUrl.replace(/\/+$/, '')}/appReseller/v1/users${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(userCreationResponse, await res.json()) };