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

#3 Migrate to ESlint and code formatting using prettier and husky #13

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
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
extends: [
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
},
};
6 changes: 6 additions & 0 deletions .huskyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hooks": {
"pre-commit": "lint-staged",
"pre-push":"npm run lint && npm run test"
}
}
5 changes: 5 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.ts": [
"eslint --fix"
]
}
7 changes: 6 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"singleQuote": true
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"semi": true,
"trailingComma":"all",
"arrowParens": "avoid"
}
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "jmap-client-ts.js",
"scripts": {
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"lint": "eslint 'src/**/*.ts'",
"lint:fix": "npm run lint -- --quiet --fix",
"test": "mocha -r ts-node/register -r jsdom-global/register **/*.spec.ts",
"prepare": "npm run build",
"prePublish": "npm run build"
Expand Down Expand Up @@ -34,14 +34,19 @@
"devDependencies": {
"@types/chai": "4.1.3",
"@types/mocha": "8.0.3",
"@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0",
"chai": "4.2.0",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.1",
"husky": "^4.3.7",
"jsdom": "16.4.0",
"jsdom-global": "3.0.2",
"lint-staged": "^10.5.3",
"mocha": "8.1.3",
"prettier": "2.1.2",
"ts-node": "9.0.0",
"tslint": "6.1.3",
"tslint-config-prettier": "1.18.0",
"typescript": "4.0.3"
}
}
14 changes: 7 additions & 7 deletions src/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export class HttpRequest {

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

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

Expand All @@ -29,7 +29,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 +50,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
6 changes: 0 additions & 6 deletions tslint.json

This file was deleted.