Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
  • Loading branch information
XielQs committed Mar 24, 2024
1 parent 0020caa commit 54e20d2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ MailTM.setConfig({
#### Available props

- **mailService** _[Optional & 'mail.tm' | 'mail.gw']_ **=** 'mail.tm': Change mail service
- **axiosOptions** _[Optional & Object]_ **=** {}: Axios request options

### Emails

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mail.tm-api",
"version": "3.0.0",
"version": "3.0.1",
"description": "A powerful library to use the Mail.TM api and Mail.GW to receive emails",
"main": "./dist/index.js",
"homepage": "https://github.com/XielQs/mail.tm-api#readme",
Expand Down
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Account from './classes/Account'
import request from './utils/request'

const CONFIG: IConfig = {
mailService: 'mail.tm'
mailService: 'mail.tm',
axiosOptions: {}
}

let domains: IDomain[] = []
Expand Down Expand Up @@ -41,8 +42,8 @@ export async function fetchDomains<Random extends boolean = false> ({ page = 1,
/**
* Creates an account
* @example
* const account = await createAccount();
* console.log(account.email);
* const account = await createAccount()
* console.log(account.email)
*/
export async function createAccount (address?: string, password?: string): Promise<Account> {
return await new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -84,11 +85,11 @@ export async function loginAccount (token: string): Promise<Account>
/**
* Logs into an existing account
* @example
* const account = await loginAccount("mySuperSecretToken"); // Login with account token
* console.log(account.email);
* const account = await loginAccount("mySuperSecretToken") // Login with account token
* console.log(account.email)
* // Or
* const account = await loginAccount("myEmail@domain.com", "mySuperSecretPassword");
* console.log(account.email);
* const account = await loginAccount("myEmail@domain.com", "mySuperSecretPassword")
* console.log(account.email)
*/
export async function loginAccount (...args: [string, string] | [string]): Promise<Account> {
return await new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -136,6 +137,6 @@ export async function loginAccount (...args: [string, string] | [string]): Promi
}

export function setConfig (config: IConfig): void {
request(config.mailService)
request(config.mailService, config.axiosOptions)
Object.assign(CONFIG, config)
}
3 changes: 3 additions & 0 deletions src/types/IConfig.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { CreateAxiosDefaults } from 'axios'

export default interface IConfig {
mailService?: 'mail.tm' | 'mail.gw'
axiosOptions?: CreateAxiosDefaults
}
12 changes: 9 additions & 3 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import axios, { type AxiosInstance } from 'axios'
import axios, { type CreateAxiosDefaults, type AxiosInstance } from 'axios'

let endpoint = 'https://api.mail.tm'
let options: CreateAxiosDefaults = {}

export default function request (service?: 'mail.tm' | 'mail.gw'): AxiosInstance {
export default function request (service?: 'mail.tm' | 'mail.gw', axiosOptions?: CreateAxiosDefaults): AxiosInstance {
if (service !== undefined && ['mail.tm', 'mail.gw'].includes(service)) {
endpoint = `https://api.${service}`
}
if (axiosOptions !== undefined) {
options = axiosOptions
}

const instance = axios.create({
...options,
baseURL: endpoint,
headers: {
...options?.headers,
'Content-Type': 'application/json',
Accept: 'application/json',
Connection: 'close',
Expand All @@ -18,4 +24,4 @@ export default function request (service?: 'mail.tm' | 'mail.gw'): AxiosInstance
})

return instance
};
}

0 comments on commit 54e20d2

Please sign in to comment.