Skip to content

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

Package listing

@kody/microsoft

src/smoke-test.ts

35 lines · 1.2 KB · TypeScript
import { resolveMicrosoftIntegration, type MicrosoftAccountParams } from './accounts.ts'
import { graphRequest, type JsonObject } from './core.ts'

export type SmokeTestResult = {
  ok: true
  integration: string
  id: string | null
  displayName: string | null
  mail: string | null
  userPrincipalName: string | null
}

/**
 * Verify Microsoft Graph OAuth with GET /me (User.Read). Does not send mail or write data.
 * @example
 * import smokeTest from 'kody:@kody/microsoft/smoke-test'
 * const result = await smokeTest()
 * // => { ok: true, integration: 'microsoft', id: '...', mail: '...' }
 */
export default async function smokeTest(params: MicrosoftAccountParams = {}): Promise<SmokeTestResult> {
  const integration = resolveMicrosoftIntegration(params)
  const me = await graphRequest<JsonObject>({
    integration,
    path: '/me',
    query: { $select: 'id,displayName,mail,userPrincipalName' },
  })
  return {
    ok: true,
    integration,
    id: typeof me.id === 'string' ? me.id : null,
    displayName: typeof me.displayName === 'string' ? me.displayName : null,
    mail: typeof me.mail === 'string' ? me.mail : null,
    userPrincipalName: typeof me.userPrincipalName === 'string' ? me.userPrincipalName : null,
  }
}