Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Feb 4, 2024
1 parent dccd93e commit 6039c22
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ node_modules/

.DS_Store

.env
.env
/examples/**/package-lock.json
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Note:
1. Get your **free API key** here: <https://app.templateless.com>
1. There are more JavaScript examples in the [examples](examples) folder

```bash
TEMPLATELESS_API_KEY=<Your API Key> \
TEMPLATELESS_EMAIL_ADDRESS=<Your Own Email Address> \
node examples/simple
```

## 🤝 Contributing

- Contributions are more than welcome <3
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.umd.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions dist/collection.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, SocialItem } from './components';
import { Result } from '.';
export declare class Collection {
components: Component[];
constructor(builder: CollectionBuilder);
Expand All @@ -15,7 +14,7 @@ declare class CollectionBuilder {
socials(data: SocialItem[]): this;
text(text: string): this;
viewInBrowser(text: string): this;
build(): Result<Collection>;
build(): Collection;
private push;
}
export {};
4 changes: 2 additions & 2 deletions dist/content.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, SocialItem } from './components';
import { Result, Header, Footer } from '.';
import { Header, Footer } from '.';
export declare enum Theme {
Unstyled = "UNSTYLED",
Simple = "SIMPLE"
Expand Down Expand Up @@ -29,7 +29,7 @@ declare class ContentBuilder {
socials(data: SocialItem[]): this;
text(text: string): this;
viewInBrowser(text: string): this;
build(): Result<Content>;
build(): Content;
private push;
}
export {};
4 changes: 2 additions & 2 deletions dist/email.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmailAddress, Result } from '.';
import { EmailAddress } from '.';
interface EmailOptions {
deleteAfter?: number;
}
Expand All @@ -24,6 +24,6 @@ declare class EmailBuilder {
subject(subject: string): this;
content(content: Content): this;
deleteAfter(seconds: number): this;
build(): Result<Email>;
build(): Email;
}
export {};
1 change: 0 additions & 1 deletion dist/errors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ export declare class UnknownError extends Error {
constructor();
}
export type TemplatelessError = UnauthorizedError | ForbiddenError | InvalidParameterError | BadRequestError | UnavailableError | UnknownError;
export type Result<T> = T | TemplatelessError;
6 changes: 3 additions & 3 deletions dist/templateless.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Email, Result, ObjectId } from '.';
import { Email, ObjectId } from '.';
export interface EmailResponse {
emails: ObjectId[];
}
Expand All @@ -7,6 +7,6 @@ export declare class Templateless {
private _domain;
constructor(apiKey: string);
domain(domain: string): this;
send(email: Email): Promise<Result<ObjectId[]>>;
sendMany(emails: Email[]): Promise<Result<ObjectId[]>>;
send(email: Email): Promise<ObjectId[]>;
sendMany(emails: Email[]): Promise<ObjectId[]>;
}
Empty file removed examples/simple.js
Empty file.
31 changes: 31 additions & 0 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dotenv/config'
import { Content, Email, EmailAddress, Templateless } from 'templateless-js'

const sendEmail = async () => {
let api_key = process.env.TEMPLATELESS_API_KEY
if (!api_key) {
console.error('Set TEMPLATELESS_API_KEY to your Templateless API key')
return
}

let email_address = process.env.TEMPLATELESS_EMAIL_ADDRESS
if (!email_address) {
console.error('Set TEMPLATELESS_EMAIL_ADDRESS to your own email address')
return
}

const content = Content.builder().text('Hello world').build()

const email = Email.builder()
.to(new EmailAddress(email_address))
.subject('Hello')
.content(content)
.build()

const templateless = new Templateless(api_key)
const result = await templateless.send(email)

console.log(result)
}

sendEmail()
13 changes: 13 additions & 0 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "templateless-js-example-simple",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "node index.js"
},
"dependencies": {
"templateless-js": "file:../..",
"dotenv": "16.4.1"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "templateless-js",
"version": "0.1.0-alpha.0",
"version": "0.1.0-alpha.1",
"description": "Ship faster by sending elegant emails using just code",
"author": "",
"keywords": [
Expand Down
3 changes: 1 addition & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ViewInBrowser,
SocialItem,
} from './components'
import { Result } from '.'

export class Collection {
components: Component[]
Expand Down Expand Up @@ -66,7 +65,7 @@ class CollectionBuilder {
return this.push(new ViewInBrowser(text))
}

build(): Result<Collection> {
build(): Collection {
return new Collection(this)
}

Expand Down
4 changes: 2 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ViewInBrowser,
SocialItem,
} from './components'
import { Result, Header, Footer } from '.'
import { Header, Footer } from '.'

export enum Theme {
Unstyled = 'UNSTYLED',
Expand Down Expand Up @@ -100,7 +100,7 @@ class ContentBuilder {
return this.push(new ViewInBrowser(text))
}

build(): Result<Content> {
build(): Content {
return new Content(this)
}

Expand Down
4 changes: 2 additions & 2 deletions src/email.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmailAddress, Result } from '.'
import { EmailAddress } from '.'

interface EmailOptions {
deleteAfter?: number
Expand Down Expand Up @@ -66,7 +66,7 @@ class EmailBuilder {
return this
}

build(): Result<Email> {
build(): Email {
return new Email(this)
}
}
2 changes: 0 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,3 @@ export type TemplatelessError =
| BadRequestError
| UnavailableError
| UnknownError

export type Result<T> = T | TemplatelessError
7 changes: 3 additions & 4 deletions src/templateless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import axios from 'axios'
import pkg from '../package.json'
import {
Email,
Result,
ObjectId,
UnauthorizedError,
ForbiddenError,
Expand Down Expand Up @@ -31,11 +30,11 @@ export class Templateless {
return this
}

async send(email: Email): Promise<Result<ObjectId[]>> {
async send(email: Email): Promise<ObjectId[]> {
return this.sendMany([email])
}

async sendMany(emails: Email[]): Promise<Result<ObjectId[]>> {
async sendMany(emails: Email[]): Promise<ObjectId[]> {
try {
const response = await axios.post(
`${this._domain}/v1/emails`,
Expand Down Expand Up @@ -68,7 +67,7 @@ export class Templateless {
throw new UnknownError()
}
} catch (error) {
return new UnknownError()
throw new UnknownError()
}
}
}

0 comments on commit 6039c22

Please sign in to comment.