Add contacts api to sdk (#52)

This commit is contained in:
KM Koushik
2024-08-11 10:56:48 +10:00
committed by GitHub
parent 2bfe3e0f45
commit 004cb026a6
3 changed files with 77 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
import { Unsend } from "./unsend";
import { paths } from "../types/schema";
import { ErrorResponse } from "../types";
type CreateContactPayload =
paths["/v1/contactBooks/{contactBookId}/contacts"]["post"]["requestBody"]["content"]["application/json"];
type CreateContactResponse = {
data: CreateContactResponseSuccess | null;
error: ErrorResponse | null;
};
type CreateContactResponseSuccess =
paths["/v1/contactBooks/{contactBookId}/contacts"]["post"]["responses"]["200"]["content"]["application/json"];
type GetContactResponseSuccess =
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["get"]["responses"]["200"]["content"]["application/json"];
type GetContactResponse = {
data: GetContactResponseSuccess | null;
error: ErrorResponse | null;
};
type UpdateContactPayload =
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["patch"]["requestBody"]["content"]["application/json"];
type UpdateContactResponseSuccess =
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["patch"]["responses"]["200"]["content"]["application/json"];
type UpdateContactResponse = {
data: UpdateContactResponseSuccess | null;
error: ErrorResponse | null;
};
export class Contacts {
constructor(private readonly unsend: Unsend) {
this.unsend = unsend;
}
async create(
contactBookId: string,
payload: CreateContactPayload
): Promise<CreateContactResponse> {
const data = await this.unsend.post<CreateContactResponseSuccess>(
`/contactBooks/${contactBookId}/contacts`,
payload
);
return data;
}
async get(
contactBookId: string,
contactId: string
): Promise<GetContactResponse> {
const data = await this.unsend.get<GetContactResponseSuccess>(
`/contactBooks/${contactBookId}/contacts/${contactId}`
);
return data;
}
async update(
contactBookId: string,
contactId: string,
payload: UpdateContactPayload
): Promise<UpdateContactResponse> {
const data = await this.unsend.patch<UpdateContactResponseSuccess>(
`/contactBooks/${contactBookId}/contacts/${contactId}`,
payload
);
return data;
}
}

View File

@@ -1,4 +1,5 @@
import { ErrorResponse } from "../types";
import { Contacts } from "./contact";
import { Emails } from "./email";
const defaultBaseUrl = "https://app.unsend.dev";
@@ -14,6 +15,7 @@ export class Unsend {
// readonly domains = new Domains(this);
readonly emails = new Emails(this);
readonly contacts = new Contacts(this);
url = baseUrl;
constructor(