// Copyright 2022 Luca Casonato. All rights reserved. MIT license. /** * Gmail API Client for Deno * ========================= * * The Gmail API lets you view and manage Gmail mailbox data like threads, messages, and labels. * * Docs: https://developers.google.com/gmail/api/ * Source: https://googleapis.deno.dev/v1/gmail:v1.ts */ import { auth, CredentialsClient, GoogleAuth, request } from "/_/base@v1/mod.ts"; export { auth, GoogleAuth }; export type { CredentialsClient }; /** * The Gmail API lets you view and manage Gmail mailbox data like threads, * messages, and labels. */ export class Gmail { #client: CredentialsClient | undefined; #baseUrl: string; constructor(client?: CredentialsClient, baseUrl: string = "https://gmail.googleapis.com/") { this.#client = client; this.#baseUrl = baseUrl; } /** * Creates a new draft with the `DRAFT` label. * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsCreate(userId: string, req: Draft): Promise { req = serializeDraft(req); const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return deserializeDraft(data); } /** * Immediately and permanently deletes the specified draft. Does not simply * trash it. * * @param id The ID of the draft to delete. * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsDelete(id: string, userId: string): Promise { const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts/${ id }`); const data = await request(url.href, { client: this.#client, method: "DELETE", }); } /** * Gets the specified draft. * * @param id The ID of the draft to retrieve. * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsGet(id: string, userId: string, opts: UsersDraftsGetOptions = {}): Promise { const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts/${ id }`); if (opts.format !== undefined) { url.searchParams.append("format", String(opts.format)); } const data = await request(url.href, { client: this.#client, method: "GET", }); return deserializeDraft(data); } /** * Lists the drafts in the user's mailbox. * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsList(userId: string, opts: UsersDraftsListOptions = {}): Promise { const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts`); if (opts.includeSpamTrash !== undefined) { url.searchParams.append("includeSpamTrash", String(opts.includeSpamTrash)); } if (opts.maxResults !== undefined) { url.searchParams.append("maxResults", String(opts.maxResults)); } if (opts.pageToken !== undefined) { url.searchParams.append("pageToken", String(opts.pageToken)); } if (opts.q !== undefined) { url.searchParams.append("q", String(opts.q)); } const data = await request(url.href, { client: this.#client, method: "GET", }); return deserializeListDraftsResponse(data); } /** * Sends the specified, existing draft to the recipients in the `To`, `Cc`, * and `Bcc` headers. * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsSend(userId: string, req: Draft): Promise { req = serializeDraft(req); const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts/send`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return deserializeMessage(data); } /** * Replaces a draft's content. * * @param id The ID of the draft to update. * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersDraftsUpdate(id: string, userId: string, req: Draft): Promise { req = serializeDraft(req); const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/drafts/${ id }`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "PUT", body, }); return deserializeDraft(data); } /** * Gets the current user's Gmail profile. * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersGetProfile(userId: string): Promise { const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/profile`); const data = await request(url.href, { client: this.#client, method: "GET", }); return deserializeProfile(data); } /** * Lists the history of all changes to the given mailbox. History results are * returned in chronological order (increasing `historyId`). * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersHistoryList(userId: string, opts: UsersHistoryListOptions = {}): Promise { opts = serializeUsersHistoryListOptions(opts); const url = new URL(`${this.#baseUrl}gmail/v1/users/${ userId }/history`); if (opts.historyTypes !== undefined) { url.searchParams.append("historyTypes", String(opts.historyTypes)); } if (opts.labelId !== undefined) { url.searchParams.append("labelId", String(opts.labelId)); } if (opts.maxResults !== undefined) { url.searchParams.append("maxResults", String(opts.maxResults)); } if (opts.pageToken !== undefined) { url.searchParams.append("pageToken", String(opts.pageToken)); } if (opts.startHistoryId !== undefined) { url.searchParams.append("startHistoryId", String(opts.startHistoryId)); } const data = await request(url.href, { client: this.#client, method: "GET", }); return deserializeListHistoryResponse(data); } /** * Creates a new label. * * @param userId The user's email address. The special value `me` can be used to indicate the authenticated user. */ async usersLabelsCreate(userId: string, req: Label): Promise