import {z} from 'zod' export type IssuingTargetIdForAdd = string; export type ControlGroupCondition = 'all_fail' | 'any_fail'; export type IssuingControlType = 'velocity_limit' | 'mcc_limit' | 'mid_limit'; export type VelocityWindow = {type: 'daily' | 'weekly' | 'monthly' | 'all_time'}; export type MidCode = string; export type VelocityLimit = {amount_limit: number, velocity_window: VelocityWindow, mcc_list?: Array | undefined, mid_list?: Array | undefined}; export type MccLimit = {type: 'allow' | 'block', mcc_list: Array}; export type MidLimit = {type: 'allow' | 'block', mid_list: Array}; export type AddControlGroupRequestControl = {description?: string | undefined, control_type: IssuingControlType, velocity_limit: VelocityLimit} | {description?: string | undefined, control_type: IssuingControlType, mcc_limit: MccLimit} | {description?: string | undefined, control_type: IssuingControlType, mid_limit: MidLimit}; export type AddControlGroupRequest = {description?: string | undefined, target_id: IssuingTargetIdForAdd, fail_if: ControlGroupCondition, controls: Array}; export type CreateApiIssuingControlsControlGroupsArgs = {body: AddControlGroupRequest}; export type CreateApiIssuingControlsControlGroupsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const issuingControlGroupId = z.string().min(30).max(30); export const issuingTargetId = z.string().min(30).max(30); export const controlGroupCondition = z.enum(["all_fail", "any_fail"]); export const issuingControlType = z.enum(["velocity_limit", "mcc_limit", "mid_limit"]); export const velocityWindow = z.object({type: z.enum(["daily", "weekly", "monthly", "all_time"])}); export const midCode = z.string().min(1).max(15); export const velocityLimit = z.object({amount_limit: z.number().int().gte(0), velocity_window: velocityWindow, mcc_list: z.array(z.string()).optional(), mid_list: z.array(midCode).optional()}); export const mccLimit = z.object({type: z.enum(["allow", "block"]), mcc_list: z.array(z.string())}); export const midLimit = z.object({type: z.enum(["allow", "block"]), mid_list: z.array(midCode)}); export const addControlGroupRequestControl = z.discriminatedUnion("control_type", [z.object({description: z.string().max(256).optional(), control_type: issuingControlType, velocity_limit: velocityLimit}), z.object({description: z.string().max(256).optional(), control_type: issuingControlType, mcc_limit: mccLimit}), z.object({description: z.string().max(256).optional(), control_type: issuingControlType, mid_limit: midLimit})]); export const controlGroupIsEditable = z.boolean(); export const issuingLink = z.object({href: z.string().max(100), actions: z.array(z.enum(["GET", "POST", "PUT", "DELETE", "PATCH"]).max(100)), types: z.array(z.string().max(100))}); export const controlGroupLinks = z.object({self: issuingLink}); export const addControlGroupResponse = z.object({id: issuingControlGroupId, description: z.string().max(256).optional(), target_id: issuingTargetId, fail_if: controlGroupCondition, controls: z.array(addControlGroupRequestControl), is_editable: controlGroupIsEditable, created_date: z.string(), last_modified_date: z.string(), _links: controlGroupLinks}); 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 a control group * * POST /issuing/controls/control-groups */ export const createApiIssuingControlsControlGroups = async (args: CreateApiIssuingControlsControlGroupsArgs, options: CreateApiIssuingControlsControlGroupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/issuing/controls/control-groups${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(addControlGroupResponse, await res.json()) };