import {z} from 'zod' export type ProductCatalogSKUsAPIVersion = 'v1'; export type GetApiApiV2ProductCatalogSkusArgs = {version: ProductCatalogSKUsAPIVersion, as_of_date?: string | undefined}; export type GetApiApiV2ProductCatalogSkusOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const productCatalogSKUAllotment = z.object({child_sku_code: z.string(), hourly_quantity: z.number(), monthly_quantity: z.number().int(), parent_sku_code: z.string()}); export const productCatalogSKUOnDemandOption = z.enum(["hourly", "monthly"]); export const productCatalogSKUPricingUnitType = z.enum(["block", "unit"]); export const productCatalogSKUPricingTier = z.object({max_usage_quantity: z.number().int().nullable(), min_usage_quantity: z.number().int(), price: z.string(), pricing_unit_type: productCatalogSKUPricingUnitType}); export const productCatalogSKUTieredPricing = z.object({tiers: z.array(productCatalogSKUPricingTier)}).nullable(); export const productCatalogSKUPricingType = z.enum(["usage", "percent"]); export const productCatalogSKUDataAttributesResponse = z.object({allotments: z.array(productCatalogSKUAllotment), billing_dimension: z.string(), billing_units: z.string().nullable(), currency: z.string(), default_on_demand_option: productCatalogSKUOnDemandOption, number_of_units_included_in_price: z.number().int(), on_demand_list_price: z.string().nullable(), on_demand_tiered: productCatalogSKUTieredPricing, pricing_type: productCatalogSKUPricingType, sku_name: z.string()}); export const productCatalogSKUType = z.literal("Sku"); export const productCatalogSKUDataResponse = z.object({attributes: productCatalogSKUDataAttributesResponse, id: z.string(), type: productCatalogSKUType}); export const productCatalogSKUsResponse = z.object({data: z.array(productCatalogSKUDataResponse)}); 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 }; /** * List SKUs * * GET /api/v2/product-catalog/skus * * Also accepts auth: AuthZ, AuthZ */ export const getApiApiV2ProductCatalogSkus = async (args: GetApiApiV2ProductCatalogSkusArgs, options?: GetApiApiV2ProductCatalogSkusOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) query.append("version", String(args.version)) if (args.as_of_date != null) query.append("as_of_date", String(args.as_of_date)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/product-catalog/skus${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(productCatalogSKUsResponse, await res.json()) };