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

feat: TS #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions dist/contact.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export = Contact;
declare class Contact {
/**
* Parse an address
* @param {string} value
* @returns {Contact}
*/
static parse(value: string): Contact;
/**
* Format a contact into a string
* @param {Contact} obj
* @returns {string}
*/
static format(obj: Contact): string;
/**
* Create a new Contact
* @param {string} [address]
* @param {string} [name]
*/
constructor(address?: string | undefined, name?: string | undefined);
/** @type {string} Contact name */
name: string;
/** @type {string} Email address */
address: string;
/**
* Parse an address
* @param {string} value
* @returns {Contact}
*/
parse(value: string): Contact;
/**
* Stringify the contact
* @returns {string}
*/
toString(): string;
}
declare namespace Contact {
let patterns: Array<[RegExp, number, number?]>;
}
//# sourceMappingURL=contact.d.ts.map
1 change: 1 addition & 0 deletions dist/contact.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions dist/envelope.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export = Envelope;
/**
* @extends {Array}
*/
declare class Envelope extends Array<any> {
/**
* Parse an email
* @param {Buffer|string} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Envelope}
*/
static parse(buffer: Buffer | string, offset?: number | undefined, length?: number | undefined): Envelope;
/**
* Create a new Envelope
* @param {Buffer|string} [value]
*/
constructor(value?: string | Buffer | undefined);
header: import("./header");
body: string | Buffer | null;
/**
* Whether it is a multipart message
* @return {boolean}
*/
get isMultipart(): boolean;
/**
* @param {string} type
* @param {boolean} [withDisposition=true]
*/
getContent(type: string, withDisposition?: boolean | undefined): any;
/**
* Get the plaintext part of a message
* @returns {string}
*/
getText(): string;
/**
* Get the HTML part of a message
* @returns {string}
*/
getHTML(): string;
/**
* Get all attachments contained in the message
* @param {boolean} [withInline=true] - Whether to include inline attachments
* @returns {Array<Envelope>}
*/
getAttachments(withInline?: boolean | undefined): Array<Envelope>;
/**
* Parse an email
* @param {Buffer|string} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Envelope}
*/
parse(buffer: Buffer | string, offset?: number | undefined, length?: number | undefined): Envelope;
}
declare namespace Envelope {
let Header: typeof import("./header");
}
//# sourceMappingURL=envelope.d.ts.map
1 change: 1 addition & 0 deletions dist/envelope.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions dist/header.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export = Header;
/**
* @extends {Map}
*/
declare class Header extends Map<any, any> {
/**
* Parse an email message header
* @param {Buffer} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Header}
*/
static parse(buffer: Buffer, offset?: number | undefined, length?: number | undefined): Header;
/**
* @param {Header} header
*/
static format(header: Header): void;
/**
* Create a new envelope header
* @param {...[any, any]} argv
*/
constructor(...argv: [any, any][]);
/** @type {Array<Array<string>>} Raw headers */
raw: Array<Array<string>>;
/**
* Parse an email message header
* @param {Buffer} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Header}
*/
parse(buffer: Buffer, offset?: number | undefined, length?: number | undefined): Header;
toString(): void;
}
//# sourceMappingURL=header.d.ts.map
1 change: 1 addition & 0 deletions dist/header.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions dist/quoted-printable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param {Uint8Array|Buffer|string} input
* @param {string} charset
* @returns {string}
*/
export function encodeBase64(input: Uint8Array | Buffer | string, charset: string): string;
/**
* @param {string} input
* @param {string} charset
* @returns {string|Buffer}
*/
export function decodeBase64(input: string, charset: string): string | Buffer;
/**
* @param {Buffer|string} input
* @param {boolean} wordMode
* @returns {string}
*/
export function encode(input: Buffer | string, wordMode: boolean): string;
/**
* @param {string|Buffer} input
* @param {string} charset
* @param {boolean} [wordMode]
* @returns {string|Buffer}
*/
export function decode(input: string | Buffer, charset: string, wordMode?: boolean | undefined): string | Buffer;
/**
* @param {string|Buffer} input
* @param {"B"|"Q"|"b"|"q"} type
* @param {string} charset
* @returns {string}
*/
export function encodeWord(input: string | Buffer, type: "B" | "Q" | "b" | "q", charset: string): string;
/**
* @param {string} input
* @param {boolean} [consumeSubsequentSeparator]
* @returns {string}
*/
export function decodeWord(input: string, consumeSubsequentSeparator?: boolean | undefined): string;
//# sourceMappingURL=quoted-printable.d.ts.map
1 change: 1 addition & 0 deletions dist/quoted-printable.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions lib/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ class Contact {

/**
* Create a new Contact
* @param {String} [address]
* @param {String} [name]
* @returns {Contact}
* @param {string} [address]
* @param {string} [name]
*/
constructor( address, name ) {
/** @type {String} Contact name */
/** @type {string} Contact name */
this.name = name || ''
/** @type {String} Email address */
/** @type {string} Email address */
this.address = address || ''
}

/**
* Parse an address
* @param {String} value
* @param {string} value
* @returns {Contact}
*/
static parse( value ) {
Expand All @@ -26,8 +25,8 @@ class Contact {

/**
* Format a contact into a string
* @param {Object} obj
* @returns {String}
* @param {Contact} obj
* @returns {string}
*/
static format( obj ) {
return obj.name ?
Expand All @@ -37,7 +36,7 @@ class Contact {

/**
* Parse an address
* @param {String} value
* @param {string} value
* @returns {Contact}
*/
parse( value ) {
Expand All @@ -48,7 +47,7 @@ class Contact {
pattern = Contact.patterns[i]
if( (match = pattern[0].exec( value )) ) {
this.address = match[ pattern[1] ] || ''
this.name = match[ pattern[2] ] || ''
this.name = pattern[2] ? match[ pattern[2] ] || '' : ''
break
}
}
Expand All @@ -67,7 +66,7 @@ class Contact {

/**
* Stringify the contact
* @returns {String}
* @returns {string}
*/
toString() {
return Contact.format( this )
Expand All @@ -77,7 +76,7 @@ class Contact {

/**
* Contact format patterns and match groups
* @type {Array<Array<RegExp,Number,Number>>}
* @type {Array<[RegExp,Number,Number?]>}
* @constant
*/
Contact.patterns = [
Expand Down
27 changes: 18 additions & 9 deletions lib/envelope.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ var qp = require( './quoted-printable' )

var EOH = Buffer.from( '\r\n\r\n', 'ascii' )

/**
* @extends {Array}
*/
class Envelope extends Array {

/**
* Create a new Envelope
* @extends {Array}
* @param {Buffer|String} [value]
* @returns {Envelope}
* @param {Buffer|string} [value]
*/
constructor( value ) {

Expand All @@ -26,7 +27,7 @@ class Envelope extends Array {

/**
* Parse an email
* @param {Buffer|String} buffer
* @param {Buffer|string} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Envelope}
Expand All @@ -37,14 +38,19 @@ class Envelope extends Array {

/**
* Whether it is a multipart message
* @return {Boolean} [description]
* @return {boolean}
*/
get isMultipart() {
var content = this.header.get( 'content-type' )
return content && content.type.startsWith( 'multipart' )
}

// TODO: Generalize this further, so it can also be used by `.getAttachments()`

/**
* @param {string} type
* @param {boolean} [withDisposition=true]
*/
getContent( type, withDisposition = true ) {

if( !this.isMultipart ) {
Expand All @@ -68,27 +74,30 @@ class Envelope extends Array {

/**
* Get the plaintext part of a message
* @returns {String}
* @returns {string}
*/
getText() {
return this.getContent( 'text/plain', false )
}

/**
* Get the HTML part of a message
* @returns {String}
* @returns {string}
*/
getHTML() {
return this.getContent( 'text/html', false )
}

/**
* Get all attachments contained in the message
* @param {Boolean} [withInline=true] - Whether to include inline attachments
* @param {boolean} [withInline=true] - Whether to include inline attachments
* @returns {Array<Envelope>}
*/
getAttachments( withInline = true ) {

/**
* @type {Envelope[]}
*/
var attachments = []

if( !this.isMultipart ) {
Expand All @@ -115,7 +124,7 @@ class Envelope extends Array {

/**
* Parse an email
* @param {Buffer|String} buffer
* @param {Buffer|string} buffer
* @param {Number} [offset=0]
* @param {Number} [length]
* @returns {Envelope}
Expand Down
Loading