Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add Subaccounts and 'On-behalf-of' header #392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __Table of Contents__
- [Install](#install)
- [Setup Client](#setup-client)
- [Available Imports](#imports)
- [Using Subaccounts](#using-subaccounts)
- [Types imports](#types-imports)
- [Interfaces and Enums imports](#interfaces-and-enums-imports)
- [Generated docs](#generated-docs)
Expand Down Expand Up @@ -56,6 +57,17 @@ Once the package is installed, you can import the library using `import` or `req
const mailgun = new Mailgun(FormData);
const mg = mailgun.client({username: 'api', key: process.env.MAILGUN_API_KEY || 'key-yourkeyhere'});
```
### Using Subaccounts
Primary accounts can make API calls on behalf of their subaccounts. [API documentation](https://documentation.mailgun.com/en/latest/subaccounts.html#subaccounts)
```js
import * as FormData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(FormData);
const mg = mailgun.client({username: 'api', key: process.env.MAILGUN_API_KEY || 'key-yourkeyhere'});
mg.setSubaccount('subaccount-id');
// then, if you need to reset it back to the primary account:
mg.resetSubaccount();
```
### Types imports
Starting from version **9.0.0.** Types can be includes as named import:
```TS
Expand Down Expand Up @@ -152,6 +164,12 @@ The following service methods are available to instantiated clients. The example
- [createMembers](#createmembers)
- [updateMember](#updatemember)
- [destroyMember](#destroymember)
- [subaccounts](#subaccounts)
- [list](#list-6)
- [get](#get-8)
- [create](#create-7)
- [enable](#enable)
- [disable](#disable)
- [Navigation thru lists](#navigation-thru-lists)
- [Browser Demo](#browser-demo)
- [Development](#development)
Expand Down Expand Up @@ -2044,6 +2062,115 @@ A client to manage members within a specific mailing list.
message: 'Mailing list member has been deleted'
}
```
### Subaccounts

A client to manage subaccounts.

- #### list

`mg.subaccounts.list(query)` - [api docs](https://documentation.mailgun.com/en/latest/subaccounts.html)

Example:

```js
mg.subaccounts.list()
.then(subaccounts => console.log(subaccounts)) // logs array of subaccounts
.catch(err => console.error(err)); // logs any error
```

Promise returns: array of Subaccounts instances

```JS
[
{ id: "XYZ", name: "test.subaccount1", status: "open" },
{ id: "YYY", name: "test.subaccount2", status: "open" }
]
```

Query data may have next properties:

| Property | Description |
|:---------|:-----------------------------------------------------------------------|
| limit | Maximum number of records to return. (10 by default) |
| skip | Number of records to skip. (0 by default) |
| sort | "asc" or "desc". |
| enabled | Returns all enabled/disabled subaccounts. (Defaults to all if omitted) |

- #### get

`mg.subaccounts.get(subaccount_id)`

Example:

```JS
mg.subaccounts.get('123')
.then(subaccount => console.log(subaccount)) // logs subaccount object
.catch(err => console.error(err)); // logs any error
```

Promise returns: Subaccount instance

```JS
{ id: "123", name: "test.subaccount1", status: "open" }
```

- #### create

`mg.subaccounts.create(name)`

Example:

```js
mg.subaccounts.create('foobar')
.then(msg => console.log(msg)) // logs response data
.catch(err => console.error(err)); // logs any error
```

Promise returns: Subaccount instance

```JS
{ id: "123", name: "foobar", status: "open" }
```

Create method accepts data object with next properties:

| Parameter | Description |
|-------------|-----------------------------------------------------------|
| name | Name of the subaccount being created (ex. 'mysubaccount') |

- #### enable

`mg.subaccounts.enable(subaccount_id)`

Example:

```js
mg.subaccounts.enable('123')
.then(msg => console.log(msg)) // logs response data
.catch(err => console.error(err)); // logs any error
```
Promise returns: Subaccount instance

```JS
{ id: "123", name: "foobar", status: "open" }
```

- #### disable

`mg.subaccounts.disable(subaccount_id)`

Example:

```js
mg.subaccounts.disable('123')
.then(msg => console.log(msg)) // logs response data
.catch(err => console.error(err)); // logs any error
```
Promise returns: Subaccount instance

```JS
{ id: "123", name: "foobar", status: "disabled" }
```

## Navigation thru lists
Most of the methods that return items in a list support pagination.
Expand Down Expand Up @@ -2211,7 +2338,6 @@ A client to manage members within a specific mailing list.
);
```


## Browser Demo

![image](https://cloud.githubusercontent.com/assets/399776/10718632/e8fe56e4-7b34-11e5-84c8-cfcfde978711.png)
Expand Down
8 changes: 5 additions & 3 deletions dist/Classes/MailgunClient.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MailgunClientOptions } from '../Types/MailgunClient';
import { InputFormData } from '../Types/Common';
import { IDomainsClient, IWebHooksClient, IMailgunClient, IMailingListsClient, IEventClient, IStatsClient, ISuppressionClient, IMessagesClient, IRoutesClient, IValidationClient, IIPsClient, IIPPoolsClient } from '../Interfaces';
import { MailgunClientOptions, InputFormData } from '../Types';
import { IDomainsClient, IWebHooksClient, IMailgunClient, IMailingListsClient, IEventClient, IStatsClient, ISuppressionClient, IMessagesClient, IRoutesClient, IValidationClient, IIPsClient, IIPPoolsClient, ISubaccountsClient } from '../Interfaces';
export default class MailgunClient implements IMailgunClient {
private request;
domains: IDomainsClient;
Expand All @@ -14,5 +13,8 @@ export default class MailgunClient implements IMailgunClient {
ips: IIPsClient;
ip_pools: IIPPoolsClient;
lists: IMailingListsClient;
subaccounts: ISubaccountsClient;
constructor(options: MailgunClientOptions, formData: InputFormData);
setSubaccount(subaccountId: string): void;
resetSubaccount(): void;
}
2 changes: 1 addition & 1 deletion dist/Classes/Messages.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailgunMessageData, MessagesSendAPIResponse, MessagesSendResult } from '../Types/Messages';
import { MailgunMessageData, MessagesSendAPIResponse, MessagesSendResult } from '../Types';
import Request from './common/Request';
import { IMessagesClient } from '../Interfaces';
export default class MessagesClient implements IMessagesClient {
Expand Down
13 changes: 13 additions & 0 deletions dist/Classes/Subaccounts.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Request from './common/Request';
import { ISubaccountsClient } from '../Interfaces';
import { SubaccountListResponseData, SubaccountResponseData, SubaccountsQuery } from '../Types';
export default class SubaccountsClient implements ISubaccountsClient {
request: Request;
static SUBACCOUNT_HEADER: string;
constructor(request: Request);
list(query?: SubaccountsQuery): Promise<SubaccountListResponseData>;
get(id: string): Promise<SubaccountResponseData>;
create(name: string): Promise<SubaccountResponseData>;
enable(id: string): Promise<SubaccountResponseData>;
disable(id: string): Promise<SubaccountResponseData>;
}
5 changes: 3 additions & 2 deletions dist/Classes/common/Request.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as NodeFormData from 'form-data';
import { RequestOptions, InputFormData, APIResponse } from '../../Types/Common';
import { IpPoolDeleteData } from '../../Types/IPPools';
import { RequestOptions, InputFormData, APIResponse, IpPoolDeleteData } from '../../Types';
declare class Request {
private username;
private key;
Expand All @@ -14,6 +13,8 @@ declare class Request {
private getResponseBody;
private joinAndTransformHeaders;
private makeHeadersFromObject;
setSubaccountHeader(subaccountId: string): void;
resetSubaccountHeader(): void;
query(method: string, url: string, query?: Record<string, unknown> | Array<Array<string>>, options?: Record<string, unknown>): Promise<APIResponse>;
command(method: string, url: string, data?: Record<string, unknown> | Record<string, unknown>[] | string | NodeFormData | FormData, options?: Record<string, unknown>, addDefaultHeaders?: boolean): Promise<APIResponse>;
get(url: string, query?: Record<string, unknown> | Array<Array<string>>, options?: Record<string, unknown>): Promise<APIResponse>;
Expand Down
4 changes: 4 additions & 0 deletions dist/Interfaces/MailgunClient/IMailgunClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IValidationClient } from '../Validations';
import { IIPsClient } from '../IPs';
import { IIPPoolsClient } from '../IPPools';
import { IMailingListsClient } from '../MailingLists';
import { ISubaccountsClient } from '../Subaccounts';
export interface IMailgunClient {
domains: IDomainsClient;
webhooks: IWebHooksClient;
Expand All @@ -21,4 +22,7 @@ export interface IMailgunClient {
ips: IIPsClient;
ip_pools: IIPPoolsClient;
lists: IMailingListsClient;
subaccounts: ISubaccountsClient;
setSubaccount(subaccountId: string): void;
resetSubaccount(): void;
}
8 changes: 8 additions & 0 deletions dist/Interfaces/Subaccounts/ISubaccountsClient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SubaccountListResponseData, SubaccountResponseData, SubaccountsQuery } from '../../Types';
export interface ISubaccountsClient {
list(query?: SubaccountsQuery): Promise<SubaccountListResponseData>;
get(id: string): Promise<SubaccountResponseData>;
create(name: string): Promise<SubaccountResponseData>;
disable(id: string): Promise<SubaccountResponseData>;
enable(id: string): Promise<SubaccountResponseData>;
}
1 change: 1 addition & 0 deletions dist/Interfaces/Subaccounts/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ISubaccountsClient';
1 change: 1 addition & 0 deletions dist/Interfaces/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './Messages';
export * from './Routes';
export * from './IPs';
export * from './IPPools';
export * from './Subaccounts';
Loading
Loading