import {z} from 'zod' export type CommandInput = {type: 'PURCHASE' | 'EXPIRY' | 'REFUND' | 'CHARGEBACK', command: string, game_server_id?: number | undefined, offline_execute?: boolean | undefined, schedule?: (number | null) | undefined, execute_once?: boolean | undefined}; export type DiscordActionInput = {type: 'PURCHASE' | 'EXPIRY' | 'REFUND' | 'CHARGEBACK', action: 'ADD_ROLE' | 'REMOVE_ROLE' | 'SEND_MESSAGE' | 'ADD_BAN' | 'REMOVE_BAN', parameters: {role_id?: (string | null) | undefined, channel_id?: (string | null) | undefined, message?: (string | null) | undefined}}; export type CreateApiAppPackageArgs = {body: {name: string, description?: (string | null) | undefined, category_id: number, enabled?: boolean | undefined, price: number, compare_at_price?: (number | null) | undefined, expiry_days: number | null, type: 'SINGLE' | 'MULTIPLE', inventory_amount: number | null, per_user_limit?: boolean | undefined, min_amount?: (number | null) | undefined, image?: (string | null) | undefined, parent_id?: (number | null) | undefined, is_variation_parent?: boolean | undefined, commands?: Array | undefined, discord_actions?: Array | undefined, variables?: Array | undefined, content?: ({title: string | null, value: string, is_plain: boolean, send_email: boolean}) | undefined, license_keys?: Array | undefined, license_key_format?: {separator: string, fields: Array} | undefined, chat_delivery?: ({enabled: boolean, initial_message?: (string | null) | undefined} | null) | undefined}}; export type CreateApiAppPackageOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const package = z.object({id: z.number().optional(), store_id: z.number().optional(), category_id: z.number().optional(), position: z.number().optional(), enabled: z.boolean().optional(), name: z.string().optional(), slug: z.string().optional(), type: z.enum(["SINGLE", "MULTIPLE"]).optional(), price: z.number().optional(), compare_at_price: z.number().nullable().optional(), expiry_days: z.number().nullable().optional(), inventory_amount: z.number().nullable().optional(), per_user_limit: z.boolean().optional(), description: z.string().nullable().optional(), image: z.string().nullable().optional(), icon: z.string().nullable().optional(), parent_id: z.number().nullable().optional(), is_variation_parent: z.boolean().optional(), download_key: z.string().nullable().optional(), min_amount: z.number().nullable().optional(), license_key_format: z.object({}).nullable().optional(), chat_delivery: z.object({}).nullable().optional(), price_display: z.string().optional(), created_at: z.string().optional(), updated_at: 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 }; /** * Create Package * * POST /app/package */ export const createApiAppPackage = async (args: CreateApiAppPackageArgs, options?: CreateApiAppPackageOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/app/package${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(package, await res.json()) };