Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/datadog

src/validate.ts

35 lines · 973 B · TypeScript
import { datadogRequest, type DatadogAuthOptions } from './core.ts'

/**
 * Validate the API key, then read the current user with both keys.
 * GET /api/v1/validate plus GET /api/v2/current_user.
 */
export async function validateKeys(input: DatadogAuthOptions = {}) {
	const validation = await datadogRequest('/api/v1/validate', {
		...input,
		label: 'Datadog validate API key',
	})
	let currentUser: any = null
	try {
		currentUser = await datadogRequest('/api/v2/current_user', {
			...input,
			label: 'Datadog current user',
		})
	} catch {
		currentUser = null
	}
	const user = currentUser?.data?.attributes ?? currentUser?.data ?? null
	return {
		ok: validation?.valid === true || validation?.valid === undefined,
		valid: validation?.valid ?? true,
		user: user
			? {
					id: currentUser?.data?.id ?? user.id ?? null,
					name: user.name ?? user.handle ?? null,
					email: user.email ?? user.handle ?? null,
				}
			: null,
	}
}

export default validateKeys