mailgun.js 10.0.1 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [10.2.0](https://github.com/mailgun/mailgun.js/compare/v10.1.0...v10.2.0) (2024-02-15)
6
+
7
+
8
+ ### Features
9
+
10
+ * Better types and handling for different FormData implementations ([c547be9](https://github.com/mailgun/mailgun.js/commits/c547be9895dc1fcfb625ddf0aadc82778e82a259))
11
+
12
+ ## [10.1.0](https://github.com/mailgun/mailgun.js/compare/v10.0.1...v10.1.0) (2024-01-31)
13
+
14
+
15
+ ### Features
16
+
17
+ * Add proxy configuration to the client ([fa013f1](https://github.com/mailgun/mailgun.js/commits/fa013f1f49792d1e42b4e718b629514198393f4e))
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * **docs:** Add url param for EU infrastructure ([360b867](https://github.com/mailgun/mailgun.js/commits/360b867c03da6f70669e6bba0c3070fdd9f10885))
23
+
24
+
25
+ ### Other changes
26
+
27
+ * Update readme ([3897e59](https://github.com/mailgun/mailgun.js/commits/3897e59554316994579960eb5f3ee7373b72f29d))
28
+
5
29
  ### [10.0.1](https://github.com/mailgun/mailgun.js/compare/v10.0.0...v10.0.1) (2024-01-26)
6
30
 
7
31
 
@@ -32,11 +32,13 @@ export declare class MultipleValidationJob implements MultipleValidationJobResul
32
32
  }
33
33
  export default class MultipleValidationClient extends NavigationThruPages<MultipleValidationJobsListResult> implements IMultipleValidationClient {
34
34
  request: Request;
35
+ private attachmentsHandler;
35
36
  constructor(request: Request);
36
37
  private handleResponse;
37
38
  protected parseList(response: MultipleValidationJobsListResponse): MultipleValidationJobsListResult;
38
39
  list(query?: MultipleValidationJobsListQuery): Promise<MultipleValidationJobsListResult>;
39
40
  get(listId: string): Promise<MultipleValidationJob>;
41
+ private convertToExpectedShape;
40
42
  create(listId: string, data: MultipleValidationCreationData): Promise<CreatedMultipleValidationJob>;
41
43
  destroy(listId: string): Promise<CanceledMultipleValidationJob>;
42
44
  }
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import { Readable } from 'stream';
5
+ import { CustomFile, CustomFileData } from '../../Types';
6
+ import { AttachmentInfo, StreamValue } from '../../Types/Common/Attachments';
7
+ declare class BlobFromStream {
8
+ private _stream;
9
+ size: number;
10
+ constructor(stream: Readable, size: number);
11
+ stream(): Readable;
12
+ get [Symbol.toStringTag](): string;
13
+ }
14
+ declare class AttachmentsHandler {
15
+ private getAttachmentOptions;
16
+ private getFileInfo;
17
+ private getCustomFileInfo;
18
+ private getBufferInfo;
19
+ isStream(data: unknown): data is StreamValue;
20
+ isCustomFile(obj: unknown): obj is CustomFile;
21
+ isBrowserFile(obj: unknown): obj is File;
22
+ isBuffer(data: unknown): data is Buffer;
23
+ getAttachmentInfo(attachment: CustomFile | File | string | CustomFileData): AttachmentInfo;
24
+ convertToFDexpectedShape(userProvidedValue: CustomFile | File | string | CustomFileData): string | Blob | Buffer | NodeJS.ReadableStream | (CustomFile & StreamValue);
25
+ getBlobFromStream(stream: Readable, size: number): BlobFromStream;
26
+ }
27
+ export default AttachmentsHandler;
@@ -4,5 +4,6 @@ export default class APIError extends Error implements APIErrorType {
4
4
  stack: string;
5
5
  details: string;
6
6
  type: string;
7
+ static getUserDataError(statusText: string, message: string): APIError;
7
8
  constructor({ status, statusText, message, body }: APIErrorOptions);
8
9
  }
@@ -1,14 +1,17 @@
1
1
  import * as NodeFormData from 'form-data';
2
- import { InputFormData } from '../../Types/Common';
2
+ import { FormDataInput, InputFormData } from '../../Types/Common';
3
+ import { MimeMessage } from '../../Types';
3
4
  declare class FormDataBuilder {
4
5
  private FormDataConstructor;
6
+ private fileKeys;
7
+ private attachmentsHandler;
5
8
  constructor(FormDataConstructor: InputFormData);
6
- createFormData(data: any): NodeFormData | FormData;
7
- private isFormDataPackage;
8
- private getAttachmentOptions;
9
+ createFormData(data: FormDataInput): NodeFormData | FormData;
9
10
  private addMimeDataToFD;
11
+ isMIME(data: unknown): data is MimeMessage;
12
+ private isFormDataPackage;
13
+ private isMessageAttachment;
10
14
  private addFilesToFD;
11
- private isStream;
12
15
  private addCommonPropertyToFD;
13
16
  }
14
17
  export default FormDataBuilder;
@@ -1,5 +1,5 @@
1
1
  import * as NodeFormData from 'form-data';
2
- import { RequestOptions, InputFormData, APIResponse, IpPoolDeleteData } from '../../Types';
2
+ import { RequestOptions, InputFormData, APIResponse, IpPoolDeleteData, FormDataInput } from '../../Types';
3
3
  declare class Request {
4
4
  private username;
5
5
  private key;
@@ -8,6 +8,7 @@ declare class Request {
8
8
  private headers;
9
9
  private formDataBuilder;
10
10
  private maxBodyLength;
11
+ private proxy;
11
12
  constructor(options: RequestOptions, formData: InputFormData);
12
13
  request(method: string, url: string, onCallOptions?: Record<string, unknown | Record<string, unknown>>): Promise<APIResponse>;
13
14
  private getResponseBody;
@@ -19,10 +20,10 @@ declare class Request {
19
20
  command(method: string, url: string, data?: Record<string, unknown> | Record<string, unknown>[] | string | NodeFormData | FormData, options?: Record<string, unknown>, addDefaultHeaders?: boolean): Promise<APIResponse>;
20
21
  get(url: string, query?: Record<string, unknown> | Array<Array<string>>, options?: Record<string, unknown>): Promise<APIResponse>;
21
22
  post(url: string, data?: Record<string, unknown> | string, options?: Record<string, unknown>): Promise<APIResponse>;
22
- postWithFD(url: string, data: Record<string, unknown> | Record<string, unknown>[]): Promise<APIResponse>;
23
- putWithFD(url: string, data: Record<string, unknown>): Promise<APIResponse>;
24
- patchWithFD(url: string, data: Record<string, unknown>): Promise<APIResponse>;
25
- put(url: string, data?: Record<string, unknown> | string, options?: Record<string, unknown>): Promise<APIResponse>;
23
+ postWithFD(url: string, data: FormDataInput): Promise<APIResponse>;
24
+ putWithFD(url: string, data: FormDataInput): Promise<APIResponse>;
25
+ patchWithFD(url: string, data: FormDataInput): Promise<APIResponse>;
26
+ put(url: string, data?: FormDataInput | string, options?: Record<string, unknown>): Promise<APIResponse>;
26
27
  delete(url: string, data?: IpPoolDeleteData): Promise<APIResponse>;
27
28
  }
28
29
  export default Request;
package/README.md CHANGED
@@ -42,6 +42,8 @@ Next, require the module and instantiate a mailgun client by calling `new Mailgu
42
42
 
43
43
  NOTE: starting from version 3.0 you need to pass FormData (we need this to keep library universal). For node.js you can use `form-data` library.
44
44
 
45
+ IMPORTANT: if you are using EU infrastructure, you need to also pass `url: 'https://api.eu.mailgun.net'` together with auth credentials as stated in [Mailgun docs](https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api)
46
+
45
47
  ### Imports
46
48
  Once the package is installed, you can import the library using `import` or `require` approach:
47
49
 
@@ -68,6 +70,29 @@ Primary accounts can make API calls on behalf of their subaccounts. [API documen
68
70
  // then, if you need to reset it back to the primary account:
69
71
  mg.resetSubaccount();
70
72
  ```
73
+
74
+ ### Proxy configuration
75
+ By leveraging client configuration options, users can effortlessly establish proxy connections that align with their network requirements.
76
+ Ex:
77
+ ```js
78
+ import * as FormData from 'form-data';
79
+ import Mailgun from 'mailgun.js';
80
+ const mailgun = new Mailgun(FormData);
81
+
82
+ const mg = mailgun.client({
83
+ username: 'api',
84
+ key: process.env.MAILGUN_API_KEY || 'key-yourkeyhere',
85
+ proxy: {
86
+ protocol: 'https' // 'http' ,
87
+ host: '127.0.0.1', // use your proxy host here
88
+ port: 9000, // use your proxy port here
89
+ auth: { // may be omitted if proxy doesn't require authentication
90
+ username: 'user_name', // provide username
91
+ password: 'user_password' // provide password
92
+ }
93
+ },
94
+ });
95
+ ```
71
96
  ### Types imports
72
97
  Starting from version **9.0.0.** Types can be includes as named import:
73
98
  ```TS
@@ -0,0 +1,12 @@
1
+ export type NodePipeFunction = (destination: WritableStream, options?: {
2
+ end?: boolean;
3
+ }) => void;
4
+ export type BrowserPipeFunction = (destination: WritableStream) => void;
5
+ export type StreamValue = {
6
+ pipe: NodePipeFunction | BrowserPipeFunction;
7
+ };
8
+ export type AttachmentInfo = {
9
+ filename?: string;
10
+ contentType?: string;
11
+ knownLength?: number;
12
+ };
@@ -1,7 +1,13 @@
1
1
  import * as NodeFormData from 'form-data';
2
+ import { FormDataInputValue } from '../Messages';
2
3
  export type FormDataOptions = {
3
- [key: string]: any;
4
+ [key: string]: NodeFormData;
4
5
  };
5
6
  export type InputFormData = {
6
- new (options?: HTMLFormElement | FormDataOptions): NodeFormData | FormData;
7
+ new (form?: HTMLFormElement | undefined, submitter?: HTMLElement | null | undefined): FormData;
8
+ } | {
9
+ new (options?: FormDataOptions): NodeFormData;
10
+ };
11
+ export type FormDataInput = {
12
+ [key: string]: FormDataInputValue;
7
13
  };
@@ -1,7 +1,9 @@
1
+ import { AxiosProxyConfig } from 'axios';
1
2
  export type MailgunClientOptions = {
2
3
  username: string;
3
4
  key: string;
4
5
  url?: string;
5
6
  public_key?: string;
6
7
  timeout?: number;
8
+ proxy?: AxiosProxyConfig;
7
9
  };
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  /**
3
4
  * Ensures the object has least one key present and not undefined
4
5
  *
@@ -7,6 +8,17 @@
7
8
  export type AtLeastOneKeyPresent<Object_, Keys extends keyof Object_ = keyof Object_> = Pick<Object_, Exclude<keyof Object_, Keys>> & {
8
9
  [K in Keys]-?: Required<Pick<Object_, K>> & Partial<Pick<Object_, Exclude<Keys, K>>>;
9
10
  }[Keys];
11
+ export type MimeMessage = string | Blob | Buffer | NodeJS.ReadableStream;
12
+ export type CustomFileData = string | Blob | File | Buffer | NodeJS.ReadableStream;
13
+ export type CustomFile = {
14
+ data: CustomFileData;
15
+ filename?: string;
16
+ contentType?: string;
17
+ knownLength?: number;
18
+ [key: string]: unknown;
19
+ };
20
+ export type MessageAttachment = CustomFile | CustomFile[] | File | File[] | string | CustomFileData | CustomFileData[];
21
+ export type FormDataInputValue = MimeMessage | CustomFileData | string | string[] | boolean | MessageAttachment | undefined | number;
10
22
  export type MailgunMessageContent = AtLeastOneKeyPresent<{
11
23
  /**
12
24
  * Body of the message. (text version)
@@ -19,7 +31,7 @@ export type MailgunMessageContent = AtLeastOneKeyPresent<{
19
31
  /**
20
32
  * Body of the message. (MIME version)
21
33
  */
22
- message?: string | Buffer | Blob;
34
+ message?: MimeMessage;
23
35
  /**
24
36
  * Name of a template stored via [template API](https://documentation.mailgun.com/en/latest/api-templates.html#api-templates). See [Templates](https://documentation.mailgun.com/en/latest/user_manual.html#templating) for more information
25
37
  */
@@ -57,7 +69,7 @@ export type MailgunMessageData = MailgunMessageContent & {
57
69
  *
58
70
  * **Important:** You must use `multipart/form-data` encoding when sending attachments.
59
71
  */
60
- attachment?: any;
72
+ attachment?: MessageAttachment;
61
73
  /**
62
74
  * Attachment with `inline` disposition. Can be used to send inline images (see example).
63
75
  *
@@ -163,7 +175,7 @@ export type MailgunMessageData = MailgunMessageContent & {
163
175
  * `v:` prefix followed by an arbitrary name allows to attach a custom JSON data to the message. See [Attaching Data to Messages](https://documentation.mailgun.com/en/latest/user_manual.html#manual-customdata) for more information.
164
176
  */
165
177
  'v:my-var'?: string;
166
- [key: string]: unknown;
178
+ [key: string]: FormDataInputValue;
167
179
  };
168
180
  export type MessagesSendAPIResponse = {
169
181
  status: number;
@@ -1,4 +1,5 @@
1
1
  import { PagesList, ParsedPagesList } from '../Common';
2
+ import { CustomFile, CustomFileData } from '../Messages';
2
3
  export type MultipleValidationJobData = {
3
4
  created_at: number;
4
5
  id: string;
@@ -57,12 +58,10 @@ export type CreatedMultipleValidationJob = {
57
58
  message: string;
58
59
  };
59
60
  export type MultipleValidationCreationData = {
60
- file: Record<string, unknown>;
61
- [key: string]: unknown | undefined;
61
+ file: CustomFileData | CustomFile;
62
62
  };
63
63
  export type MultipleValidationCreationDataUpdated = {
64
- multipleValidationFile: Record<string, unknown>;
65
- [key: string]: unknown | undefined;
64
+ multipleValidationFile: CustomFileData | CustomFile;
66
65
  };
67
66
  export type MultipleValidationJobsListResult = {
68
67
  jobs: MultipleValidationJobResult[];