import {z} from 'zod' export type DeleteApiV2LoyaltiesTierStructuresTierStructureIdTiersTierIdArgs = {id: string, tierId: string}; export type DeleteApiV2LoyaltiesTierStructuresTierStructureIdTiersTierIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tierQualificationRulesPoints = z.object({min_value: z.number().optional(), max_value: z.number().optional()}); export const tierQualificationRulesSegment = z.object({id: z.string().optional()}); export const tierQualificationRules = z.object({type: z.enum(["POINTS", "POINTS_AND_SEGMENT"]).optional(), points: tierQualificationRulesPoints.optional(), segment: tierQualificationRulesSegment.optional()}); export const tierDowngrade = z.object({type: z.enum(["INHERIT", "NO_DOWNGRADE"]).optional()}); export const tier = z.object({id: z.string(), tier_structure_id: z.string(), name: z.string(), qualification_rules: tierQualificationRules.optional(), downgrade: tierDowngrade, metadata: z.object({}), created_at: z.string(), updated_at: z.string(), object: 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 }; /** * Delete tier * * DELETE /v2/loyalties/tier-structures/{tierStructureId}/tiers/{tierId} */ export const deleteApiV2LoyaltiesTierStructuresTierStructureIdTiersTierId = async (args: DeleteApiV2LoyaltiesTierStructuresTierStructureIdTiersTierIdArgs, options?: DeleteApiV2LoyaltiesTierStructuresTierStructureIdTiersTierIdOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/loyalties/tier-structures/${encodeURIComponent(String(args.tierStructureId))}/tiers/${encodeURIComponent(String(args.tierId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tier, await res.json()) };