import {z} from 'zod' export type CustomActionSchema = {app_id?: (string | null) | undefined, color?: (string | null) | undefined, context: 'ASSET' | 'ASSET_SUBCLIP' | 'SHARED_ASSET' | 'COLLECTION' | 'SHARED_COLLECTION' | 'BULK' | 'SAVED_SEARCH' | 'NONE' | 'PLAYLIST' | 'SHARED_PLAYLIST', date_created?: string | undefined, date_modified?: string | undefined, disabled?: boolean | undefined, headers?: {[key: string]: string} | undefined, id?: string | undefined, last_error?: string | undefined, last_error_date?: string | undefined, metadata_view?: (string | null) | undefined, publish_template_id?: (string | null) | undefined, status?: ('FAILING' | 'HEALTHY') | undefined, system_domain_id?: string | undefined, title: string, transcoder_id?: (string | null) | undefined, type?: ('OPEN' | 'POST' | 'PUBLISH') | undefined, url: string}; export type PatchApiV1CustomActionsContextActionIdArgs = {'App-ID': string, 'Auth-Token': string, context: string, action_id: string, body: CustomActionSchema}; export type PatchApiV1CustomActionsContextActionIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const customActionSchema = z.object({app_id: z.string().nullable().optional(), color: z.string().nullable().optional(), context: z.enum(["ASSET", "ASSET_SUBCLIP", "SHARED_ASSET", "COLLECTION", "SHARED_COLLECTION", "BULK", "SAVED_SEARCH", "NONE", "PLAYLIST", "SHARED_PLAYLIST"]), date_created: z.string().optional(), date_modified: z.string().optional(), disabled: z.boolean().optional(), headers: z.record(z.string(), z.string()).optional(), id: z.string().optional(), last_error: z.string().optional(), last_error_date: z.string().optional(), metadata_view: z.string().nullable().optional(), publish_template_id: z.string().nullable().optional(), status: z.enum(["FAILING", "HEALTHY"]).optional(), system_domain_id: z.string().optional(), title: z.string(), transcoder_id: z.string().nullable().optional(), type: z.enum(["OPEN", "POST", "PUBLISH"]).optional(), url: 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 }; /** * Update a custom action * * PATCH /v1/custom_actions/{context}/{action_id}/ */ export const patchApiV1CustomActionsContextActionId = async (args: PatchApiV1CustomActionsContextActionIdArgs, options: PatchApiV1CustomActionsContextActionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/custom_actions/${encodeURIComponent(String(args.context))}/${encodeURIComponent(String(args.action_id))}/${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(customActionSchema, await res.json()) };