import {z} from 'zod' export type GetApiConceptsConceptIdArgs = {concept_id: number}; export type GetApiConceptsConceptIdOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const conceptResponse = z.object({id: z.number().int().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), entity_type: z.string().optional(), atomic_creative_list: z.array(z.number().int()).nullable().optional(), media_type_list: z.array(z.enum(["audio", "display", "native", "video"])).nullable().optional(), strategy_list: z.array(z.number().int()).nullable().optional(), advertiser_name: z.string().optional(), version: z.number().int().optional(), name: z.string().min(1).max(256), status: z.boolean().optional(), archived: z.boolean().optional(), advertiser_id: z.number().int()}); export const singleMetadata = z.object({status: z.string()}); export const getApiConceptsConceptIdResponse = z.object({data: conceptResponse.optional(), meta: singleMetadata.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 a Concept * * GET /concepts/{concept_id} */ export const getApiConceptsConceptId = async (args: GetApiConceptsConceptIdArgs, options?: GetApiConceptsConceptIdOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/concepts/${encodeURIComponent(String(args.concept_id))}${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(getApiConceptsConceptIdResponse, await res.json()) };