import {z} from 'zod' export type PatchApiStoresStoreNameBagsBagNameMetaArgs = {store_name: string, bag_name: string, trigger?: string | undefined, body: Record}; export type PatchApiStoresStoreNameBagsBagNameMetaOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const tag = z.object({tag: z.string(), color: z.string()}); export const topic = z.object({name: z.string(), msg_type: z.string(), msg_type_hash: z.string(), msg_definition: z.string().optional(), msg_count: z.number().int(), avg_frequency: z.number()}); export const topicMapping = z.object({original_topic: z.string(), plugin_topic: z.string()}); export const fileSummary = z.object({detail_type: z.string(), store_name: z.string(), uid: z.number().int().optional(), name: z.string(), link: z.string().optional()}); export const productFile = z.object({key: z.string().optional(), file: fileSummary.optional()}); export const product = z.object({uid: z.string(), plugin: z.string(), product_type: z.string(), product_data: z.object({}), created: z.string(), title: z.string().optional(), configuration_tag: z.string().optional(), configuration_rule: z.string().optional(), topics: z.array(topicMapping), files: z.array(productFile)}); export const bagDetailed = z.object({detail_type: z.string(), name: z.string(), store_data: z.object({}), discovered: z.string(), extraction_failure: z.boolean().optional(), in_trash: z.boolean().optional(), is_extracted: z.boolean(), meta_available: z.boolean(), size: z.number().int().optional(), start_time: z.string().optional(), end_time: z.string().optional(), duration: z.number().optional(), messages: z.number().int().optional(), tags: z.array(tag).optional(), topics: z.array(topic), products: z.array(product), comment: 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 }; /** * Partial update of bag information (this only supports a few fields) * * PATCH /stores/{store_name}/bags/{bag_name}/meta */ export const patchApiStoresStoreNameBagsBagNameMeta = async (args: PatchApiStoresStoreNameBagsBagNameMetaArgs, options: PatchApiStoresStoreNameBagsBagNameMetaOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.trigger != null) query.append("trigger", String(args.trigger)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.store_name))}/bags/${encodeURIComponent(String(args.bag_name))}/meta${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(bagDetailed, await res.json()) };