Skip to content

Commit

Permalink
Run linter on ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachid OUBRAIM committed Jan 14, 2021
1 parent d10865e commit 8ffc7c3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 60 deletions.
15 changes: 4 additions & 11 deletions src/http-request.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
export class HttpRequest {
// constructor() { }

public post<ResponseType>(
url: string,
content: object,
headers: { [headerName: string]: string }
) {
public post<ResponseType>(url: string, content: any, headers: { [headerName: string]: string }) {
return this.request<ResponseType>({
url,
method: 'POST',
Expand All @@ -14,10 +10,7 @@ export class HttpRequest {
});
}

public get<ResponseType>(
url: string,
headers: { [headerName: string]: string }
) {
public get<ResponseType>(url: string, headers: { [headerName: string]: string }) {
return this.request<ResponseType>({ url, method: 'GET', headers });
}

Expand All @@ -29,7 +22,7 @@ export class HttpRequest {
}: {
url: string;
method: 'POST' | 'GET';
body?: object;
body?: any;
headers: { [headerName: string]: string };
}): Promise<ResponseType> {
return new Promise((resolve, reject) => {
Expand All @@ -50,7 +43,7 @@ export class HttpRequest {
}
};

request.onerror = (event) => {
request.onerror = event => {
reject(event);
};

Expand Down
47 changes: 15 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
} from './types';

export class Client {
private readonly DEFAULT_USING = [
'urn:ietf:params:jmap:core',
'urn:ietf:params:jmap:mail',
];
private readonly DEFAULT_USING = ['urn:ietf:params:jmap:core', 'urn:ietf:params:jmap:mail'];

private httpRequest: HttpRequest;
private httpHeaders: { [headerName: string]: string };
Expand Down Expand Up @@ -50,21 +47,16 @@ export class Client {
}

public fetchSession(): Promise<void> {
const sessionPromise = this.httpRequest.get<ISession>(
this.sessionUrl,
this.httpHeaders
);
return sessionPromise.then((session) => {
const sessionPromise = this.httpRequest.get<ISession>(this.sessionUrl, this.httpHeaders);
return sessionPromise.then(session => {
this.session = session;
return;
});
}

public getSession(): ISession {
if (!this.session) {
throw new Error(
'Undefined session, should call fetchSession and wait for its resolution'
);
throw new Error('Undefined session, should call fetchSession and wait for its resolution');
}
return this.session;
}
Expand All @@ -85,9 +77,7 @@ export class Client {
return accountIds[0];
}

public mailbox_get(
args: IGetArguments<IMailboxProperties>
): Promise<IMailboxGetResponse> {
public mailbox_get(args: IGetArguments<IMailboxProperties>): Promise<IMailboxGetResponse> {
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
return this.httpRequest
.post<{
Expand All @@ -99,14 +89,12 @@ export class Client {
using: this.getCapabilities(),
methodCalls: [['Mailbox/get', this.replaceAccountId(args), '0']],
},
this.httpHeaders
this.httpHeaders,
)
.then((response) => response.methodResponses[0][1]);
.then(response => response.methodResponses[0][1]);
}

public email_query(
args: IQueryArguments<IEmailFilterCondition>
): Promise<IEmailQueryResponse> {
public email_query(args: IQueryArguments<IEmailFilterCondition>): Promise<IEmailQueryResponse> {
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
return this.httpRequest
.post<{
Expand All @@ -118,14 +106,12 @@ export class Client {
using: this.getCapabilities(),
methodCalls: [['Email/query', this.replaceAccountId(args), '0']],
},
this.httpHeaders
this.httpHeaders,
)
.then((response) => response.methodResponses[0][1]);
.then(response => response.methodResponses[0][1]);
}

public email_get(
args: IGetArguments<IEmailProperties>
): Promise<IEmailGetResponse> {
public email_get(args: IGetArguments<IEmailProperties>): Promise<IEmailGetResponse> {
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
return this.httpRequest
.post<{
Expand All @@ -137,22 +123,19 @@ export class Client {
using: this.getCapabilities(),
methodCalls: [['Email/get', this.replaceAccountId(args), '0']],
},
this.httpHeaders
this.httpHeaders,
)
.then((response) => response.methodResponses[0][1]);
.then(response => response.methodResponses[0][1]);
}

private replaceAccountId<U extends { accountId: string }>(input: U): U {
return {
...input,
accountId:
input.accountId !== null ? input.accountId : this.getFirstAccountId(),
accountId: input.accountId !== null ? input.accountId : this.getFirstAccountId(),
};
}

private getCapabilities() {
return this.session?.capabilities
? Object.keys(this.session.capabilities)
: this.DEFAULT_USING;
return this.session?.capabilities ? Object.keys(this.session.capabilities) : this.DEFAULT_USING;
}
}
6 changes: 1 addition & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export interface ITypeMap {
/**
* [ name, arguments, id ]
*/
export type IMethodCall = [
keyof ITypeMap,
{ [argumentName: string]: any },
string
];
export type IMethodCall = [keyof ITypeMap, { [argumentName: string]: any }, string];

export interface IGetArguments<Properties> {
accountId: string;
Expand Down
15 changes: 3 additions & 12 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,19 @@ describe('stripSubject', () => {
assert.equal(stripSubject('[MORE [COMPLEX] ] Hello world!'), expected);
});
it('should remove text and closing square bracket between square brackets and spaces', () => {
assert.equal(
stripSubject('[What about wrong brackets] ] Hello world!'),
expected
);
assert.equal(stripSubject('[What about wrong brackets] ] Hello world!'), expected);
});
it('should remove text and colon between square brackets and spaces', () => {
assert.equal(stripSubject('[ And : inside ] Hello world!'), expected);
});
it('should remove text, colon and square brackets before semicolon', () => {
assert.equal(
stripSubject('[Inside : and ] outside : Hello world!'),
expected
);
assert.equal(stripSubject('[Inside : and ] outside : Hello world!'), expected);
});
it('should remove line break and spaces', () => {
assert.equal(stripSubject(' Hello\nworld!'), expected);
});
it('should remove closing square bracket, colons, and text before colon and text between square brackets and spaces', () => {
assert.equal(
stripSubject(']:This:one:is:tricky: Hello [Invisible] world!'),
expected
);
assert.equal(stripSubject(']:This:one:is:tricky: Hello [Invisible] world!'), expected);
});
it('should remove colon and opening square bracket between square brackets and space', () => {
assert.equal(stripSubject('Hello[:[] world!'), expected);
Expand Down

0 comments on commit 8ffc7c3

Please sign in to comment.