import {z} from 'zod' export type GetApiV1CustomActionsContextArgs = {'App-ID': string, 'Auth-Token': string, context: string}; export type GetApiV1CustomActionsContextOptions = {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 customActionsSchema = z.object({objects: z.array(customActionSchema).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 }; /** * Get list of custom actions by context * * GET /v1/custom_actions/{context}/ */ export const getApiV1CustomActionsContext = async (args: GetApiV1CustomActionsContextArgs, options: GetApiV1CustomActionsContextOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customActionsSchema, await res.json()) };