Skip to content

Commit

Permalink
fix: stricter TypeScript and TSLint code checks (PR #404)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweck authored Aug 6, 2019
1 parent 474d16b commit 52894cc
Show file tree
Hide file tree
Showing 93 changed files with 707 additions and 394 deletions.
51 changes: 51 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"version": "2.0.0",
"isBackground": false,
"presentation": {
"reveal": "always",
"panel": "new"
},
"env": {
"DEBUG": "*",
"NODE_DEBUG": "1",
"NODE_ENV": "development"
},
"tasks": [
{
"label": "build",
"group": "build",
"type": "shell",
"command": "npm",
"args": [
"run",
"build"
],
"problemMatcher": [
"$tsc",
"$eslint-stylish"
],
"presentation": {
"reveal": "always",
"panel": "shared"
}
},
{
"label": "lint",
"group": "build",
"type": "shell",
"command": "npm",
"args": [
"run",
"lint"
],
"problemMatcher": [
"$tsc",
"$eslint-stylish"
],
"presentation": {
"reveal": "always",
"panel": "shared"
}
}
]
}
33 changes: 33 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "cross-env NODE_ENV=production webpack --config webpack.config.js && ncp src/package.json dist/package.json",
"build:dev:main": "webpack --config webpack.config.main.js",
"start": "npm run build && cross-env DEBUG=r2:*,readium-desktop:* electron .",
"lint:ts": "tslint './src/**/*.ts' './src/**/*.tsx'",
"lint:ts": "tslint --project tsconfig.json -c tslint.json './src/**/*.ts' './src/**/*.tsx' './test/**/*.ts' './test/**/*.tsx'",
"lint:editorconfig": "eclint check '**/*' '!.vscode/**/*' '!.git/**/*' '!node_modules/**/*' '!resources/**/*' '!src/renderer/assets/**/*' '!dist/**/*' '!**/.DS_Store' '!src/typings/en.translation.d.ts'",
"lint": "npm run lint:editorconfig && npm run lint:ts",
"start:dev:renderer-reader": "concurrently --kill-others \"npm run start:dev:renderer\" \"npm run start:dev:reader\"",
Expand Down Expand Up @@ -270,6 +270,7 @@
"svg-sprite-loader": "^3.9.2",
"ts-jest": "^23.10.5",
"tslint": "^5.16.0",
"tslint-language-service": "^0.9.9",
"typescript": "^3.4.3",
"webpack": "^4.30.0",
"webpack-cli": "^3.1.2",
Expand Down
15 changes: 12 additions & 3 deletions src/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

export class CodeError extends Error {
public static fromJson(json: any): CodeError {
export interface CodeMessage {
code: number | string;
message: string;
}

export interface CodeMessageWithClass extends CodeMessage {
class: string;
}

export class CodeError extends Error implements CodeMessage {
public static fromJson(json: CodeMessage): CodeError {
return new CodeError(
json.code,
json.message,
Expand All @@ -22,7 +31,7 @@ export class CodeError extends Error {
this.message = message;
}

public toJson() {
public toJson(): CodeMessageWithClass {
return {
class: "CodeError",
code: this.code,
Expand Down
12 changes: 12 additions & 0 deletions src/common/ipc/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@
* Synchronization of redux actions
*/

import { Action } from "../models/redux";

import { WindowSender } from "../models/sync";

export enum EventType {
RendererAction = "RENDERER_ACTION",
MainAction = "MAIN_ACTION",
}

export const CHANNEL = "SYNC";

export interface EventPayload {
type: EventType;
payload: {
action: Action;
};
sender: WindowSender;
}
7 changes: 7 additions & 0 deletions src/common/ipc/win.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export enum EventType {
}

export const CHANNEL = "WIN";

export interface EventPayload {
type: EventType;
payload: {
winId: string;
};
}
8 changes: 0 additions & 8 deletions src/common/models/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import { Catalog } from "./catalog";
import { Download } from "./download";
import { Publication } from "./publication";

interface LibraryImportMessage {
paths: string[]; // List of files to import
}

interface UIMessage {
message: string;
}

export interface UrlMessage {
url: string;
}
Expand Down
1 change: 0 additions & 1 deletion src/common/models/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { Contributor } from "./contributor";
import { CustomCover } from "./custom-cover";
import { Downloadable } from "./downloadable";
import { File } from "./file";
import { Identifiable } from "./identifiable";
import { Language } from "./language";
Expand Down
11 changes: 11 additions & 0 deletions src/common/models/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { Action } from "redux";

export enum SenderType {
Main, // Main process
Renderer, // Renderer process
}

export interface WindowSender {
type: SenderType;
winId: string;
}

export interface ActionWithSender extends Action {
sender: WindowSender;
}
10 changes: 9 additions & 1 deletion src/common/redux/actions/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export enum ActionType {
Set = "LOCALE_SET",
}

export function setLocale(locale: string): Action {
export interface PayloadLocale {
locale: string;
}

export interface ActionLocale extends Action {
payload: PayloadLocale;
}

export function setLocale(locale: string): ActionLocale {
return {
type: ActionType.Set,
payload: {
Expand Down
6 changes: 2 additions & 4 deletions src/common/redux/reducers/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { Action } from "readium-desktop/common/models/redux";

import { i18nActions } from "readium-desktop/common/redux/actions";

import { I18NState } from "readium-desktop/common/redux/states/i18n";
Expand All @@ -17,13 +15,13 @@ const initialState: I18NState = {

export function i18nReducer(
state: I18NState = initialState,
action: Action,
action: i18nActions.ActionLocale,
): I18NState {
switch (action.type) {
case i18nActions.ActionType.Set:
return Object.assign({}, state, {
locale: action.payload.locale,
});
} as I18NState);
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/services/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class OPDSParser {
/**
* Parse OPDS feed and returns a catalog
*/
public parse(opdsFeed: string): Promise<Catalog> {
public async parse(opdsFeed: string): Promise<Catalog> {
return this.parser
.parse(opdsFeed)
.then((feed: AcquisitionFeed) => {
Expand Down
20 changes: 11 additions & 9 deletions src/common/services/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import { injectable} from "inversify";

import { CodeError } from "readium-desktop/common/errors";

import { Action } from "../models/redux";

@injectable()
export class ActionSerializer {
public serialize(action: any) {
public serialize(action: Action): Action {
if (action.error && action.payload instanceof CodeError) {
return Object.assign(
{},
action,
{
payload: action.payload.toJson(),
},
payload: (action.payload as CodeError).toJson(),
} as Action,
);
} else {
return action;
}
}

public deserialize(json: any): any {
public deserialize(json: Action): Action {
if (json.error &&
json.payload &&
json.payload.class &&
Expand All @@ -35,11 +37,11 @@ export class ActionSerializer {
{},
json,
{
payload: new CodeError(
json.payload.code,
json.payload.message,
),
},
payload: CodeError.fromJson({
code: json.payload.code,
message: json.payload.message,
}),
} as Action,
);
} else {
return json;
Expand Down
14 changes: 10 additions & 4 deletions src/common/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import { Store } from "redux";
import * as request from "request";
import { promisify } from "util";

type TRequestCoreOptionsRequiredUriUrl = request.CoreOptions & request.RequiredUriUrl;
type TRequestCoreOptionsOptionalUriUrl = request.CoreOptions & request.OptionalUriUrl;

/**
* @param url url of your GET request
* @param options request options
* @returns body of url response. 'String' type returned in many cases except for options.json = true
*/
// tslint:disable-next-line: max-line-length
export async function httpGet<T extends JsonMap | string = string>(url: string, options?: request.CoreOptions): Promise<T> {
options = options || {};
export async function httpGet<T extends JsonMap | string = string>(url: string, options?: TRequestCoreOptionsOptionalUriUrl): Promise<T> {
options = options || {} as TRequestCoreOptionsOptionalUriUrl;
options.headers = options.headers || {};

const headerFromOptions = {};
Expand All @@ -44,8 +47,11 @@ export async function httpGet<T extends JsonMap | string = string>(url: string,
encoding: undefined,
headers,
},
);
const response = await promisify<request.CoreOptions, request.Response>(request)(requestOptions);
) as TRequestCoreOptionsRequiredUriUrl;

const promisifiedRequest = promisify<TRequestCoreOptionsRequiredUriUrl, request.Response>(request);
const response = await promisifiedRequest(requestOptions);

if (!response) {
throw new Error(`No HTTP response?! ${url}`);
}
Expand Down
Loading

0 comments on commit 52894cc

Please sign in to comment.