initial commit. gotta go
This commit is contained in:
@@ -34,18 +34,18 @@ bun add usesend
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
import { UseSend } from "usesend";
|
||||
import { UseSend } from 'usesend';
|
||||
|
||||
const usesend = new UseSend("us_12345");
|
||||
const usesend = new UseSend('us_12345');
|
||||
|
||||
// for self-hosted installations you can pass your base URL
|
||||
// const usesend = new UseSend("us_12345", "https://app.usesend.com");
|
||||
|
||||
usesend.emails.send({
|
||||
to: "hello@acme.com",
|
||||
from: "hello@company.com",
|
||||
subject: "useSend email",
|
||||
html: "<p>useSend is the best open source product to send emails</p>",
|
||||
text: "useSend is the best open source product to send emails",
|
||||
to: 'hello@acme.com',
|
||||
from: 'hello@company.com',
|
||||
subject: 'useSend email',
|
||||
html: '<p>useSend is the best open source product to send emails</p>',
|
||||
text: 'useSend is the best open source product to send emails',
|
||||
});
|
||||
```
|
||||
|
@@ -1,2 +1,2 @@
|
||||
export { UseSend } from "./src/usesend";
|
||||
export { UseSend as Unsend } from "./src/usesend"; // deprecated alias
|
||||
export { UseSend } from './src/usesend';
|
||||
export { UseSend as Unsend } from './src/usesend'; // deprecated alias
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { UseSend } from "./usesend";
|
||||
import { paths } from "../types/schema";
|
||||
import { ErrorResponse } from "../types";
|
||||
import { UseSend } from './usesend';
|
||||
import { paths } from '../types/schema';
|
||||
import { ErrorResponse } from '../types';
|
||||
|
||||
type CreateContactPayload =
|
||||
paths["/v1/contactBooks/{contactBookId}/contacts"]["post"]["requestBody"]["content"]["application/json"];
|
||||
paths['/v1/contactBooks/{contactBookId}/contacts']['post']['requestBody']['content']['application/json'];
|
||||
|
||||
type CreateContactResponse = {
|
||||
data: CreateContactResponseSuccess | null;
|
||||
@@ -11,10 +11,10 @@ type CreateContactResponse = {
|
||||
};
|
||||
|
||||
type CreateContactResponseSuccess =
|
||||
paths["/v1/contactBooks/{contactBookId}/contacts"]["post"]["responses"]["200"]["content"]["application/json"];
|
||||
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"];
|
||||
paths['/v1/contactBooks/{contactBookId}/contacts/{contactId}']['get']['responses']['200']['content']['application/json'];
|
||||
|
||||
type GetContactResponse = {
|
||||
data: GetContactResponseSuccess | null;
|
||||
@@ -22,10 +22,10 @@ type GetContactResponse = {
|
||||
};
|
||||
|
||||
type UpdateContactPayload =
|
||||
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["patch"]["requestBody"]["content"]["application/json"];
|
||||
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"];
|
||||
paths['/v1/contactBooks/{contactBookId}/contacts/{contactId}']['patch']['responses']['200']['content']['application/json'];
|
||||
|
||||
type UpdateContactResponse = {
|
||||
data: UpdateContactResponseSuccess | null;
|
||||
@@ -33,10 +33,10 @@ type UpdateContactResponse = {
|
||||
};
|
||||
|
||||
type UpsertContactPayload =
|
||||
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["put"]["requestBody"]["content"]["application/json"];
|
||||
paths['/v1/contactBooks/{contactBookId}/contacts/{contactId}']['put']['requestBody']['content']['application/json'];
|
||||
|
||||
type UpsertContactResponseSuccess =
|
||||
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["put"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/contactBooks/{contactBookId}/contacts/{contactId}']['put']['responses']['200']['content']['application/json'];
|
||||
|
||||
type UpsertContactResponse = {
|
||||
data: UpsertContactResponseSuccess | null;
|
||||
@@ -55,11 +55,11 @@ export class Contacts {
|
||||
|
||||
async create(
|
||||
contactBookId: string,
|
||||
payload: CreateContactPayload
|
||||
payload: CreateContactPayload,
|
||||
): Promise<CreateContactResponse> {
|
||||
const data = await this.usesend.post<CreateContactResponseSuccess>(
|
||||
`/contactBooks/${contactBookId}/contacts`,
|
||||
payload
|
||||
payload,
|
||||
);
|
||||
|
||||
return data;
|
||||
@@ -67,10 +67,10 @@ export class Contacts {
|
||||
|
||||
async get(
|
||||
contactBookId: string,
|
||||
contactId: string
|
||||
contactId: string,
|
||||
): Promise<GetContactResponse> {
|
||||
const data = await this.usesend.get<GetContactResponseSuccess>(
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
@@ -78,11 +78,11 @@ export class Contacts {
|
||||
async update(
|
||||
contactBookId: string,
|
||||
contactId: string,
|
||||
payload: UpdateContactPayload
|
||||
payload: UpdateContactPayload,
|
||||
): Promise<UpdateContactResponse> {
|
||||
const data = await this.usesend.patch<UpdateContactResponseSuccess>(
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`,
|
||||
payload
|
||||
payload,
|
||||
);
|
||||
|
||||
return data;
|
||||
@@ -91,11 +91,11 @@ export class Contacts {
|
||||
async upsert(
|
||||
contactBookId: string,
|
||||
contactId: string,
|
||||
payload: UpsertContactPayload
|
||||
payload: UpsertContactPayload,
|
||||
): Promise<UpsertContactResponse> {
|
||||
const data = await this.usesend.put<UpsertContactResponseSuccess>(
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`,
|
||||
payload
|
||||
payload,
|
||||
);
|
||||
|
||||
return data;
|
||||
@@ -103,10 +103,10 @@ export class Contacts {
|
||||
|
||||
async delete(
|
||||
contactBookId: string,
|
||||
contactId: string
|
||||
contactId: string,
|
||||
): Promise<DeleteContactResponse> {
|
||||
const data = await this.usesend.delete<{ success: boolean }>(
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`
|
||||
`/contactBooks/${contactBookId}/contacts/${contactId}`,
|
||||
);
|
||||
|
||||
return data;
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { paths } from "../types/schema";
|
||||
import { ErrorResponse } from "../types";
|
||||
import { UseSend } from "./usesend";
|
||||
import { paths } from '../types/schema';
|
||||
import { ErrorResponse } from '../types';
|
||||
import { UseSend } from './usesend';
|
||||
|
||||
type CreateDomainPayload =
|
||||
paths["/v1/domains"]["post"]["requestBody"]["content"]["application/json"];
|
||||
paths['/v1/domains']['post']['requestBody']['content']['application/json'];
|
||||
|
||||
type CreateDomainResponse = {
|
||||
data: CreateDomainResponseSuccess | null;
|
||||
@@ -11,7 +11,7 @@ type CreateDomainResponse = {
|
||||
};
|
||||
|
||||
type CreateDomainResponseSuccess =
|
||||
paths["/v1/domains"]["post"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/domains']['post']['responses']['200']['content']['application/json'];
|
||||
|
||||
type GetDomainsResponse = {
|
||||
data: GetDomainsResponseSuccess | null;
|
||||
@@ -19,7 +19,7 @@ type GetDomainsResponse = {
|
||||
};
|
||||
|
||||
type GetDomainsResponseSuccess =
|
||||
paths["/v1/domains"]["get"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/domains']['get']['responses']['200']['content']['application/json'];
|
||||
|
||||
type VerifyDomainResponse = {
|
||||
data: VerifyDomainResponseSuccess | null;
|
||||
@@ -27,7 +27,7 @@ type VerifyDomainResponse = {
|
||||
};
|
||||
|
||||
type VerifyDomainResponseSuccess =
|
||||
paths["/v1/domains/{id}/verify"]["put"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/domains/{id}/verify']['put']['responses']['200']['content']['application/json'];
|
||||
|
||||
export class Domains {
|
||||
constructor(private readonly usesend: UseSend) {
|
||||
@@ -35,14 +35,14 @@ export class Domains {
|
||||
}
|
||||
|
||||
async list(): Promise<GetDomainsResponse> {
|
||||
const data = await this.usesend.get<GetDomainsResponseSuccess>("/domains");
|
||||
const data = await this.usesend.get<GetDomainsResponseSuccess>('/domains');
|
||||
return data;
|
||||
}
|
||||
|
||||
async create(payload: CreateDomainPayload): Promise<CreateDomainResponse> {
|
||||
const data = await this.usesend.post<CreateDomainResponseSuccess>(
|
||||
"/domains",
|
||||
payload
|
||||
'/domains',
|
||||
payload,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export class Domains {
|
||||
async verify(id: number): Promise<VerifyDomainResponse> {
|
||||
const data = await this.usesend.put<VerifyDomainResponseSuccess>(
|
||||
`/domains/${id}/verify`,
|
||||
{}
|
||||
{},
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
import { render } from "@react-email/render";
|
||||
import * as React from "react";
|
||||
import { UseSend } from "./usesend";
|
||||
import { paths } from "../types/schema";
|
||||
import { ErrorResponse } from "../types";
|
||||
import { render } from '@react-email/render';
|
||||
import * as React from 'react';
|
||||
import { UseSend } from './usesend';
|
||||
import { paths } from '../types/schema';
|
||||
import { ErrorResponse } from '../types';
|
||||
|
||||
type SendEmailPayload =
|
||||
paths["/v1/emails"]["post"]["requestBody"]["content"]["application/json"] & {
|
||||
paths['/v1/emails']['post']['requestBody']['content']['application/json'] & {
|
||||
react?: React.ReactElement;
|
||||
};
|
||||
|
||||
@@ -15,10 +15,10 @@ type CreateEmailResponse = {
|
||||
};
|
||||
|
||||
type CreateEmailResponseSuccess =
|
||||
paths["/v1/emails"]["post"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/emails']['post']['responses']['200']['content']['application/json'];
|
||||
|
||||
type GetEmailResponseSuccess =
|
||||
paths["/v1/emails/{emailId}"]["get"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/emails/{emailId}']['get']['responses']['200']['content']['application/json'];
|
||||
|
||||
type GetEmailResponse = {
|
||||
data: GetEmailResponseSuccess | null;
|
||||
@@ -26,7 +26,7 @@ type GetEmailResponse = {
|
||||
};
|
||||
|
||||
type UpdateEmailPayload =
|
||||
paths["/v1/emails/{emailId}"]["patch"]["requestBody"]["content"]["application/json"] & {
|
||||
paths['/v1/emails/{emailId}']['patch']['requestBody']['content']['application/json'] & {
|
||||
react?: React.ReactElement;
|
||||
};
|
||||
|
||||
@@ -36,7 +36,7 @@ type UpdateEmailResponse = {
|
||||
};
|
||||
|
||||
type UpdateEmailResponseSuccess =
|
||||
paths["/v1/emails/{emailId}"]["patch"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/emails/{emailId}']['patch']['responses']['200']['content']['application/json'];
|
||||
|
||||
type CancelEmailResponse = {
|
||||
data: CancelEmailResponseSuccess | null;
|
||||
@@ -44,26 +44,26 @@ type CancelEmailResponse = {
|
||||
};
|
||||
|
||||
type CancelEmailResponseSuccess =
|
||||
paths["/v1/emails/{emailId}/cancel"]["post"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/emails/{emailId}/cancel']['post']['responses']['200']['content']['application/json'];
|
||||
|
||||
// Batch emails types
|
||||
/**
|
||||
* Payload for sending multiple emails in a single batch request.
|
||||
*/
|
||||
type BatchEmailPayload =
|
||||
paths["/v1/emails/batch"]["post"]["requestBody"]["content"]["application/json"];
|
||||
paths['/v1/emails/batch']['post']['requestBody']['content']['application/json'];
|
||||
|
||||
/**
|
||||
* Successful response schema for batch email send.
|
||||
*/
|
||||
type BatchEmailResponseSuccess =
|
||||
paths["/v1/emails/batch"]["post"]["responses"]["200"]["content"]["application/json"];
|
||||
paths['/v1/emails/batch']['post']['responses']['200']['content']['application/json'];
|
||||
|
||||
/**
|
||||
* Response structure for the batch method.
|
||||
*/
|
||||
type BatchEmailResponse = {
|
||||
data: BatchEmailResponseSuccess["data"] | null;
|
||||
data: BatchEmailResponseSuccess['data'] | null;
|
||||
error: ErrorResponse | null;
|
||||
};
|
||||
|
||||
@@ -83,8 +83,8 @@ export class Emails {
|
||||
}
|
||||
|
||||
const data = await this.usesend.post<CreateEmailResponseSuccess>(
|
||||
"/emails",
|
||||
payload
|
||||
'/emails',
|
||||
payload,
|
||||
);
|
||||
|
||||
return data;
|
||||
@@ -99,8 +99,8 @@ export class Emails {
|
||||
async batch(payload: BatchEmailPayload): Promise<BatchEmailResponse> {
|
||||
// Note: React element rendering is not supported in batch mode.
|
||||
const response = await this.usesend.post<BatchEmailResponseSuccess>(
|
||||
"/emails/batch",
|
||||
payload
|
||||
'/emails/batch',
|
||||
payload,
|
||||
);
|
||||
return {
|
||||
data: response.data ? response.data.data : null,
|
||||
@@ -110,18 +110,18 @@ export class Emails {
|
||||
|
||||
async get(id: string): Promise<GetEmailResponse> {
|
||||
const data = await this.usesend.get<GetEmailResponseSuccess>(
|
||||
`/emails/${id}`
|
||||
`/emails/${id}`,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
payload: UpdateEmailPayload
|
||||
payload: UpdateEmailPayload,
|
||||
): Promise<UpdateEmailResponse> {
|
||||
const data = await this.usesend.patch<UpdateEmailResponseSuccess>(
|
||||
`/emails/${id}`,
|
||||
payload
|
||||
payload,
|
||||
);
|
||||
return data;
|
||||
}
|
||||
@@ -129,7 +129,7 @@ export class Emails {
|
||||
async cancel(id: string): Promise<CancelEmailResponse> {
|
||||
const data = await this.usesend.post<CancelEmailResponseSuccess>(
|
||||
`/emails/${id}/cancel`,
|
||||
{}
|
||||
{},
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import { ErrorResponse } from "../types";
|
||||
import { Contacts } from "./contact";
|
||||
import { Emails } from "./email";
|
||||
import { ErrorResponse } from '../types';
|
||||
import { Contacts } from './contact';
|
||||
import { Emails } from './email';
|
||||
|
||||
const defaultBaseUrl = "https://app.usesend.com";
|
||||
const defaultBaseUrl = 'https://app.usesend.com';
|
||||
// eslint-disable-next-line turbo/no-undeclared-env-vars
|
||||
const baseUrl = `${process?.env?.USESEND_BASE_URL ?? process?.env?.UNSEND_BASE_URL ?? defaultBaseUrl}/api/v1`;
|
||||
|
||||
@@ -20,16 +20,16 @@ export class UseSend {
|
||||
|
||||
constructor(
|
||||
readonly key?: string,
|
||||
url?: string
|
||||
url?: string,
|
||||
) {
|
||||
if (!key) {
|
||||
if (typeof process !== "undefined" && process.env) {
|
||||
if (typeof process !== 'undefined' && process.env) {
|
||||
this.key = process.env.USESEND_API_KEY ?? process.env.UNSEND_API_KEY;
|
||||
}
|
||||
|
||||
if (!this.key) {
|
||||
throw new Error(
|
||||
'Missing API key. Pass it to the constructor `new UseSend("us_123")`'
|
||||
'Missing API key. Pass it to the constructor `new UseSend("us_123")`',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -40,17 +40,17 @@ export class UseSend {
|
||||
|
||||
this.headers = new Headers({
|
||||
Authorization: `Bearer ${this.key}`,
|
||||
"Content-Type": "application/json",
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
}
|
||||
|
||||
async fetchRequest<T>(
|
||||
path: string,
|
||||
options = {}
|
||||
options = {},
|
||||
): Promise<{ data: T | null; error: ErrorResponse | null }> {
|
||||
const response = await fetch(`${this.url}${path}`, options);
|
||||
const defaultError = {
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
message: response.statusText,
|
||||
};
|
||||
|
||||
@@ -80,7 +80,7 @@ export class UseSend {
|
||||
|
||||
async post<T>(path: string, body: unknown) {
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
method: 'POST',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
@@ -90,7 +90,7 @@ export class UseSend {
|
||||
|
||||
async get<T>(path: string) {
|
||||
const requestOptions = {
|
||||
method: "GET",
|
||||
method: 'GET',
|
||||
headers: this.headers,
|
||||
};
|
||||
|
||||
@@ -99,7 +99,7 @@ export class UseSend {
|
||||
|
||||
async put<T>(path: string, body: any) {
|
||||
const requestOptions = {
|
||||
method: "PUT",
|
||||
method: 'PUT',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
@@ -109,7 +109,7 @@ export class UseSend {
|
||||
|
||||
async patch<T>(path: string, body: any) {
|
||||
const requestOptions = {
|
||||
method: "PATCH",
|
||||
method: 'PATCH',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
@@ -119,7 +119,7 @@ export class UseSend {
|
||||
|
||||
async delete<T>(path: string, body?: unknown) {
|
||||
const requestOptions = {
|
||||
method: "DELETE",
|
||||
method: 'DELETE',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
|
1313
packages/sdk/types/schema.d.ts
vendored
1313
packages/sdk/types/schema.d.ts
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user