import {z} from 'zod' export type UpdateApiAmsClientIDMemberGroupsArgs = {ClientID: number, body: {PageNumber?: number | undefined, PageSize?: number | undefined, OffSet?: number | undefined, Status?: ('None' | 'Joined' | 'Pending' | 'NotJoined' | 'JoinedOrPending') | undefined, GroupTypeId?: number | undefined, SearchTerm?: string | undefined, AddAsMember?: boolean | undefined, IsPrimaryGroup?: boolean | undefined, AddAsGroupAdmin?: boolean | undefined, AddGroupRepTitle?: string | undefined, RemoveAsMember?: boolean | undefined, RemoveAsGroupAdmin?: boolean | undefined, RemoveGroupRepTitle?: string | undefined, ID?: number | undefined, GroupCode?: string | undefined, MemberID?: number | undefined, ResponseStatus?: string | undefined, BypassCache?: boolean | undefined, DateCached?: string | undefined, Device?: string | undefined}}; export type UpdateApiAmsClientIDMemberGroupsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const group = z.object({Id: z.number().int().optional(), Name: z.string().optional(), Status: z.string().optional(), JoinOption: z.string().optional()}); export const groupType = z.object({Id: z.number().int().optional(), TypeName: z.string().optional(), SortIndex: z.number().optional(), Groups: z.array(group).optional()}); export const dictionaryStringString = z.record(z.string(), z.string()); export const responseError = z.object({ErrorCode: z.string().optional(), FieldName: z.string().optional(), Message: z.string().optional(), Meta: dictionaryStringString.optional()}); export const responseStatus = z.object({ErrorCode: z.string().optional(), Message: z.string().optional(), StackTrace: z.string().optional(), Errors: z.array(responseError).optional(), Meta: dictionaryStringString.optional()}); export const memberGroupsResponse = z.object({GroupTypeList: z.array(groupType).optional(), PageNumber: z.number().int().optional(), PageSize: z.number().int().optional(), OffSet: z.number().int().optional(), Status: z.enum(["None", "Joined", "Pending", "NotJoined", "JoinedOrPending"]).optional(), GroupTypeId: z.number().int().optional(), SearchTerm: z.string().optional(), AddAsMember: z.boolean().optional(), IsPrimaryGroup: z.boolean().optional(), AddAsGroupAdmin: z.boolean().optional(), AddGroupRepTitle: z.string().optional(), RemoveAsMember: z.boolean().optional(), RemoveAsGroupAdmin: z.boolean().optional(), RemoveGroupRepTitle: z.string().optional(), ID: z.number().int().optional(), GroupCode: z.string().optional(), MemberID: z.number().int().optional(), ClientID: z.number().int().optional(), ResponseStatus: responseStatus.optional(), BypassCache: z.boolean().optional(), DateCached: z.string().optional(), Device: 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 }; /** * Remove relationships between the supplied Member ID and Group Code. * * PUT /Ams/{ClientID}/MemberGroups */ export const updateApiAmsClientIDMemberGroups = async (args: UpdateApiAmsClientIDMemberGroupsArgs, options?: UpdateApiAmsClientIDMemberGroupsOptions) => { const {baseUrl = "https://ws.yourmembership.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/Ams/${encodeURIComponent(String(args.ClientID))}/MemberGroups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(memberGroupsResponse, await res.json()) };