import {z} from 'zod' export type OIDCCloudAuthClaimMatchMode = 'exact' | 'glob'; export type OIDCCloudAuthClaimMatcherCreate = {claim: string, mode?: OIDCCloudAuthClaimMatchMode | undefined, values: Array}; export type OIDCCloudAuthPersonaMappingCreateAttributes = {account_uuid: string, claim_matchers?: Array | undefined, identity_pattern?: string | undefined, provider_id: string}; export type OIDCCloudAuthPersonaMappingType = 'custom_oidc_identity_mapping'; export type OIDCCloudAuthPersonaMappingCreateData = {attributes: OIDCCloudAuthPersonaMappingCreateAttributes, type: OIDCCloudAuthPersonaMappingType}; export type OIDCCloudAuthPersonaMappingCreateRequest = {data: OIDCCloudAuthPersonaMappingCreateData}; export type CreateApiApiV2WifOidcPersonaMappingArgs = {body: OIDCCloudAuthPersonaMappingCreateRequest}; export type CreateApiApiV2WifOidcPersonaMappingOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const oIDCCloudAuthClaimMatchMode = z.enum(["exact", "glob"]); export const oIDCCloudAuthClaimMatcher = z.object({claim: z.string(), mode: oIDCCloudAuthClaimMatchMode.optional(), values: z.array(z.string())}); export const oIDCCloudAuthPersonaMappingAttributesResponse = z.object({account_identifier: z.string().optional(), account_uuid: z.string().optional(), claim_matchers: z.array(oIDCCloudAuthClaimMatcher).optional(), created_at: z.string().optional(), created_by: z.string().optional(), identity_pattern: z.string().optional(), provider_id: z.string().optional()}); export const oIDCCloudAuthPersonaMappingType = z.literal("custom_oidc_identity_mapping"); export const oIDCCloudAuthPersonaMappingDataResponse = z.object({attributes: oIDCCloudAuthPersonaMappingAttributesResponse, id: z.string(), type: oIDCCloudAuthPersonaMappingType}); export const oIDCCloudAuthPersonaMappingResponse = z.object({data: oIDCCloudAuthPersonaMappingDataResponse}); 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 }; /** * Create a Custom OIDC persona mapping * * POST /api/v2/wif/oidc/persona_mapping * * Also accepts auth: AuthZ */ export const createApiApiV2WifOidcPersonaMapping = async (args: CreateApiApiV2WifOidcPersonaMappingArgs, options?: CreateApiApiV2WifOidcPersonaMappingOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/wif/oidc/persona_mapping${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(oIDCCloudAuthPersonaMappingResponse, await res.json()) };