import {z} from 'zod' export type DropletAsset = {id?: number | undefined, drop_id?: number | undefined, bucket_id?: number | undefined}; export type BucketAsset = {id?: number | undefined, name?: string | undefined, weight?: number | undefined, droplets?: Array | undefined}; export type AssetsBucketsUpdateRequest = {buckets: Array}; export type CreateApiApiSpendTrackerBucketAssetsArgs = {body: AssetsBucketsUpdateRequest}; export type CreateApiApiSpendTrackerBucketAssetsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const assetDropletResource = z.object({id: z.number().int().optional(), bucket_id: z.number().int().optional(), drop_id: z.number().int().optional(), drop_name: z.string().optional(), drop_description: z.string().optional(), item_platform: z.string().optional(), item_subplatform: z.string().optional(), item_grouping: z.string().optional()}); export const assetBucketResource = z.object({id: z.number().int().optional(), name: z.string().optional(), weight: z.number().optional(), droplets: z.array(assetDropletResource).optional()}); export const assetBucketsCollection = z.array(assetBucketResource); 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 }; /** * Update asset buckets * * POST /api/spend-tracker/bucket/assets */ export const createApiApiSpendTrackerBucketAssets = async (args: CreateApiApiSpendTrackerBucketAssetsArgs, options: CreateApiApiSpendTrackerBucketAssetsOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/spend-tracker/bucket/assets${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(assetBucketsCollection, await res.json()) };