import {z} from 'zod' export type AppResellerSubscriptionAssignmentRequestWS = {userId: string, subscriptionId: string}; export type CreateApiAppResellerV1SubscriptionAssignmentsArgs = {body: AppResellerSubscriptionAssignmentRequestWS}; export type CreateApiAppResellerV1SubscriptionAssignmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userEntitlementStatus = z.enum(["PENDING_REQUIRED_DATA", "PENDING_SEAT_AVAILABILITY", "PENDING_USER_ACTIVATION", "PENDING_REMOTE_CREATION", "SUBSCRIPTION_SUSPENDED_FREE_TRIAL_EXPIRED", "SUBSCRIPTION_SUSPENDED_RENEW_CONTRACT", "SUBSCRIPTION_SUSPENDED_PAY_INVOICE", "SUBSCRIPTION_SUSPENDED_MANUAL_SUSPENSION", "FAILED", "ACTIVE", "PENDING_REMOTE_CANCELLATION", "CANCELLED"]); export const userAssociation = z.object({id: z.string(), firstName: z.string(), lastName: z.string()}); export const appResellerSubscriptionAssignmentWS = z.object({id: z.string(), status: userEntitlementStatus, createdOn: z.number(), user: userAssociation, subscriptionId: z.string()}); 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 }; /** * Assign product * * POST /appReseller/v1/subscriptionAssignments */ export const createApiAppResellerV1SubscriptionAssignments = async (args: CreateApiAppResellerV1SubscriptionAssignmentsArgs, options?: CreateApiAppResellerV1SubscriptionAssignmentsOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/appReseller/v1/subscriptionAssignments${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(appResellerSubscriptionAssignmentWS, await res.json()) };