Skip to content
← Public packages

@kody/google

Call Gmail, Calendar, Tasks, Drive, Docs, Sheets, People, YouTube, and Analytics through saved Google OAuth.

src/people.ts

21 lines · 2.0 KB · TypeScript
import { requestGoogle } from './core.ts'
import type { GoogleAuthParams } from './accounts.ts'
import type { JsonObject } from './types.ts'
export type PeopleMeParams = GoogleAuthParams & { personFields?: string }
export type PeopleSearchParams = GoogleAuthParams & { query: string; pageSize?: number; readMask?: string }
export type PeopleConnectionsParams = GoogleAuthParams & { resourceName?: string; pageSize?: number; pageToken?: string; personFields?: string }

/**
 * Return the Google People helper namespace for profile, contact search, and connections.
 * @returns Helper namespace or overview for this Google product.
 * @example
 * import people from 'kody:@kody/google/people'
 * const me = await people().getMe()
 * // => { resourceName: 'people/...', names: [...], ... }
 */
export default function people() { return { getMe, searchContacts, listConnections } }

export async function getMe(params: PeopleMeParams = {}): Promise<JsonObject> { const { personFields = 'names,emailAddresses,phoneNumbers,photos,organizations,urls' } = params; return await requestGoogle({ ...params, api: 'people', path: '/v1/people/me', query: { personFields } }) as JsonObject }
export async function searchContacts(params: PeopleSearchParams): Promise<JsonObject> { const { query, pageSize = 10, readMask = 'names,emailAddresses,phoneNumbers,organizations' } = params; if (!query) throw new Error('searchContacts requires query.'); return await requestGoogle({ ...params, api: 'people', method: 'POST', path: '/v1/people:searchContacts', query: { readMask }, body: { query, pageSize } }) as JsonObject }
export async function listConnections(params: PeopleConnectionsParams): Promise<JsonObject> { const { resourceName = 'people/me', pageSize = 10, pageToken, personFields = 'names,emailAddresses,phoneNumbers,organizations' } = params; return await requestGoogle({ ...params, api: 'people', path: '/v1/' + resourceName + '/connections', query: { pageSize, pageToken, personFields } }) as JsonObject }