From fe7308e6b7f5b29cc668a61e8ab3619c15772d84 Mon Sep 17 00:00:00 2001 From: Nato Boram Date: Wed, 8 Jul 2020 03:42:42 -0400 Subject: [PATCH] Strict mode --- angular.json | 3 +++ package.json | 1 + src/app/enums/protocol.enum.ts | 4 +++ src/app/pages/pages.component.ts | 39 ++++++++++++++--------------- src/app/services/gateway.service.ts | 26 +++++++++++-------- tsconfig.base.json | 8 ++++-- 6 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 src/app/enums/protocol.enum.ts diff --git a/angular.json b/angular.json index 64341b5..1f283f5 100644 --- a/angular.json +++ b/angular.json @@ -8,6 +8,9 @@ "schematics": { "@schematics/angular:component": { "style": "scss" + }, + "@schematics/angular:application": { + "strict": true } }, "root": "", diff --git a/package.json b/package.json index 9532f69..4dc22ad 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "publish:ipfs": "yarn run build:ipfs && ipfs add --recursive --chunker=buzhash --cid-version=1 dist/angular" }, "private": false, + "sideEffects": false, "dependencies": { "@angular/animations": "~10.0.2", "@angular/cdk": "~10.0.1", diff --git a/src/app/enums/protocol.enum.ts b/src/app/enums/protocol.enum.ts new file mode 100644 index 0000000..d8b27e4 --- /dev/null +++ b/src/app/enums/protocol.enum.ts @@ -0,0 +1,4 @@ +export enum Protocol { + IPFS = 'ipfs', + IPNS = 'ipns', +} diff --git a/src/app/pages/pages.component.ts b/src/app/pages/pages.component.ts index 251fcc1..fb61c2f 100644 --- a/src/app/pages/pages.component.ts +++ b/src/app/pages/pages.component.ts @@ -2,6 +2,7 @@ import { HttpErrorResponse } from '@angular/common/http'; import { Component, OnInit, ViewChild } from '@angular/core'; import { MatTable, MatTableDataSource } from '@angular/material/table'; import { Subscription } from 'rxjs'; +import { Protocol } from '../enums/protocol.enum'; import { GatewayService } from '../services/gateway.service'; @Component({ @@ -11,65 +12,63 @@ import { GatewayService } from '../services/gateway.service'; }) export class PagesComponent implements OnInit { - gateways: string[]; - ipfs: string; - ipns: string; - dataSource: MatTableDataSource; - displayedColumns = [ - 'error', - 'gateway', - ]; - subscriptions: Subscription[] = []; + @ViewChild(MatTable) matTable!: MatTable; - @ViewChild(MatTable) matTable: MatTable; + gateways!: string[]; + ipfs = ''; + ipns = ''; + + readonly dataSource = new MatTableDataSource([]); + readonly displayedColumns: ['error', 'gateway'] = ['error', 'gateway']; + readonly subscriptions: Subscription[] = []; constructor( private readonly gatewayService: GatewayService ) { } ngOnInit(): void { - this.dataSource = new MatTableDataSource([]); this.gatewayService.list().subscribe((gateways): void => { this.gateways = gateways; }); } cacheIPFS(): void { - this.cache('ipfs', this.ipfs); + this.cache(Protocol.IPFS, this.ipfs); } cacheIPNS(): void { - this.cache('ipns', this.ipns); + this.cache(Protocol.IPNS, this.ipns); } - cache(type: string, hash: string): void { + cache(protocol: Protocol, hashpath: string): void { + // Clear subscriptions while (this.subscriptions.length) { const sub = this.subscriptions.pop(); - if (!sub.closed) { + if (sub && !sub.closed) { sub.unsubscribe(); } } + // Clear table this.dataSource.data = []; this.matTable.renderRows(); console.clear(); this.gateways.forEach((gateway): void => { this.subscriptions.push( - this.gatewayService.get(gateway, type, hash).subscribe((): void => { - this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, type, hash), error: null }); + this.gatewayService.get(gateway, protocol, hashpath).subscribe((): void => { + this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, protocol, hashpath), error: null }); this.matTable.renderRows(); }, (error: HttpErrorResponse): void => { - this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, type, hash), error }); + this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, protocol, hashpath), error }); this.matTable.renderRows(); }) ); }); - } } interface Result { gateway: string; - error: HttpErrorResponse; + error: HttpErrorResponse | null; } diff --git a/src/app/services/gateway.service.ts b/src/app/services/gateway.service.ts index a115c29..4eddfd3 100644 --- a/src/app/services/gateway.service.ts +++ b/src/app/services/gateway.service.ts @@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { environment } from '../../environments/environment'; +import { Protocol } from '../enums/protocol.enum'; @Injectable({ providedIn: 'root' @@ -13,22 +14,27 @@ export class GatewayService { ) { } list(): Observable { - return this.http.get( - environment.base_href && environment.base_href !== '/' - ? `${environment.base_href}/assets/json/gateways.json` - : `${document.querySelector('base').href}/assets/json/gateways.json` - ); + if (environment && environment.base_href !== '/') { + return this.http.get(`${environment.base_href}/assets/json/gateways.json`); + } + + const base = document.querySelector('base'); + if (base) { + return this.http.get(`${base.href}/assets/json/gateways.json`); + } + + throw new Error('Couldn\'t find environment nor base.') } - get(gateway: string, type: string, hash: string): Observable { - return this.http.get(`${this.url(gateway, type, hash)}#x-ipfs-companion-no-redirect`, { + get(gateway: string, protocol: Protocol, hashpath: string): Observable { + return this.http.get(`${this.url(gateway, protocol, hashpath)}#x-ipfs-companion-no-redirect`, { responseType: 'blob' as 'json' }); } - url(gateway: string, type: string, hashpath: string): string { - const splits = hashpath.split('/'); - const url = gateway.replace(':type', type).replace(':hash', splits.shift()); + url(gateway: string, protocol: Protocol, hashpath: string): string { + const splits: string[] = hashpath.split('/'); + const url: string = gateway.replace(':type', protocol).replace(':hash', splits.shift() || ''); return splits.length ? [url, splits.join('/')].join('/') : url; } diff --git a/tsconfig.base.json b/tsconfig.base.json index a409145..f41a0bd 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -17,10 +17,14 @@ "lib": [ "es2018", "dom" - ] + ], + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "strict": true }, "angularCompilerOptions": { "fullTemplateTypeCheck": true, - "strictInjectionParameters": true + "strictInjectionParameters": true, + "strictTemplates": true } }