Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit cb464df

Browse files
committed
chore: strict mode for typescript
1 parent 8b7fada commit cb464df

File tree

8 files changed

+62
-32
lines changed

8 files changed

+62
-32
lines changed

src/account.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Account {
2020

2121
if ((<IOptionsMnemonic>main.options).mnemonic) {
2222
// mnemonic
23-
this.signer = ethers.Wallet.fromMnemonic((<IOptionsMnemonic>main.options).mnemonic);
23+
this.signer = ethers.Wallet.fromMnemonic((<IOptionsMnemonic>main.options).mnemonic!);
2424
} else if ((<IOptionsPrivateKey>main.options).privateKey) {
2525
// privateKey
2626
this.signer = new ethers.Wallet((<IOptionsPrivateKey>main.options).privateKey);

src/accounts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ class Accounts {
5959
const { file, index } = await this.getPosition(this.main.account.address, account);
6060

6161
if (index !== -1) {
62-
file.profile.accounts[index].tags = tags;
62+
file.profile!.accounts![index].tags = tags;
6363
this.main.files.set(file);
64-
return file.profile.accounts[index];
64+
return file.profile!.accounts![index];
6565
} else {
6666
throw Error('Account does not exist');
6767
}
@@ -79,7 +79,7 @@ class Accounts {
7979
this.identityFormat(account);
8080
const { file, index } = await this.getPosition(this.main.account.address, account);
8181
if (index !== -1) {
82-
file.profile.accounts.splice(index, 1);
82+
file.profile!.accounts!.splice(index, 1);
8383
await this.main.files.set(file);
8484
return account;
8585
} else {

src/assets.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class Assets {
2828
async getAllList(persona: string, breakpoint?: (file: RSS3AssetsList) => boolean) {
2929
const indexFile = <RSS3Index>await this.main.files.get(persona);
3030
if (indexFile.assets) {
31-
return <RSS3Asset[]>await this.main.files.getAll(indexFile.assets, breakpoint);
31+
return <RSS3Asset[]>await this.main.files.getAll(indexFile.assets, (file) => {
32+
return breakpoint?.(<RSS3AssetsList>file) || false;
33+
});
3234
} else {
3335
return [];
3436
}
@@ -45,13 +47,16 @@ class Assets {
4547

4648
private async getPosition(asset: RSS3Asset) {
4749
let result: {
48-
file: RSS3AssetsList;
50+
file: RSS3AssetsList | null;
4951
index: number;
5052
} = {
5153
file: null,
5254
index: -1,
5355
};
5456
this.getAllList(this.main.account.address, (file) => {
57+
if (!file.list) {
58+
return false;
59+
}
5560
const index = file.list.findIndex((as) => this.assetsEquals(as, asset));
5661
if (index !== -1) {
5762
result = {
@@ -70,14 +75,16 @@ class Assets {
7075
if (utils.check.valueLength(asset) && equals<RSS3UserAsset>(asset)) {
7176
const list = await this.getAllList(
7277
this.main.account.address,
73-
(file) => file.list.findIndex((as) => this.assetsEquals(as, asset)) > -1,
78+
(file) => !!file.list && file.list.findIndex((as) => this.assetsEquals(as, asset)) > -1,
7479
);
7580
const index = list.findIndex((as) => this.assetsEquals(as, asset));
7681
if (index === -1) {
7782
let file = await this.getList(this.main.account.address, -1);
7883
if (!file) {
7984
const newID = utils.id.getAssets(this.main.account.address, 0);
8085
file = <RSS3AssetsList>this.main.files.new(newID);
86+
}
87+
if (!file.list) {
8188
file.list = [];
8289
}
8390
if (
@@ -112,10 +119,10 @@ class Assets {
112119
const position = await this.getPosition(asset);
113120

114121
if (position.index !== -1) {
115-
position.file.list[position.index] = Object.assign(position.file.list[position.index], asset);
122+
position.file!.list![position.index] = Object.assign(position.file!.list![position.index], asset);
116123

117-
this.main.files.set(position.file);
118-
return position.file.list[position.index];
124+
this.main.files.set(position.file!);
125+
return position.file!.list![position.index];
119126
} else {
120127
return null;
121128
}

src/backlinks.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ class Backlinks {
1414
return {
1515
file,
1616
index,
17-
id: index !== -1 ? file.backlinks[index].list : null,
17+
id: index !== -1 ? file.backlinks![index].list : null,
1818
};
1919
}
2020

2121
async getList(persona: string, type: string, index = -1) {
2222
if (index < 0) {
2323
const indexFile = <RSS3Index>await this.main.files.get(persona);
2424
if (indexFile.backlinks) {
25-
const linksId = indexFile.backlinks.find((links) => links.type === type).list;
25+
const linksId = indexFile.backlinks.find((links) => links.type === type)?.list;
26+
if (!linksId) {
27+
throw new Error('Backlink type does not exist');
28+
}
2629
const parsed = utils.id.parse(linksId);
2730
index = parsed.index + index + 1;
2831
const file = <RSS3List>await this.main.files.get(utils.id.getBacklinks(persona, type, index));
@@ -42,10 +45,12 @@ class Backlinks {
4245
}
4346
}
4447

45-
async getAllList(persona: string, type: string, breakpoint?: (file: RSS3LinksList) => boolean) {
48+
async getAllList(persona: string, type: string, breakpoint?: (file: RSS3BacklinksList) => boolean) {
4649
const { id } = await this.getPosition(persona, type);
4750
if (id) {
48-
return <RSS3ID[]>await this.main.files.getAll(id, breakpoint);
51+
return <RSS3ID[]>await this.main.files.getAll(id, (file) => {
52+
return breakpoint?.(<RSS3BacklinksList>file) || false;
53+
});
4954
} else {
5055
return [];
5156
}

src/files.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class File {
5858
} else {
5959
reject('Incorrectly formatted content.');
6060
}
61-
} catch (error) {
61+
} catch (error: any) {
6262
if (error.response?.data?.code === 5001) {
6363
const nowDate = new Date().toISOString();
6464
this.list[fileID] = {
@@ -80,13 +80,14 @@ class File {
8080

8181
async getAll(fileID: RSS3ListID, breakpoint?: (file: RSS3List) => boolean) {
8282
let list: (RSS3ID | RSS3ItemID | RSS3Asset | RSS3Item)[] = [];
83+
let id: string | undefined = fileID;
8384
do {
8485
const listFile = <RSS3List>await this.main.files.get(fileID);
8586
if (breakpoint && breakpoint(listFile)) {
8687
break;
8788
}
8889
list = list.concat(listFile.list || []);
89-
fileID = listFile.list_next;
90+
id = listFile.list_next;
9091
} while (fileID);
9192
return list;
9293
}

src/items.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,26 @@ class Items {
5353
async getAllList(persona: string, breakpoint?: (file: RSS3ItemsList) => boolean) {
5454
const indexFile = <RSS3Index>await this.main.files.get(persona);
5555
if (indexFile.items) {
56-
return <RSS3Item[]>await this.main.files.getAll(indexFile.items, breakpoint);
56+
return <RSS3Item[]>await this.main.files.getAll(indexFile.items, (file) => {
57+
return breakpoint?.(<RSS3ItemsList>file) || false;
58+
});
5759
} else {
5860
return [];
5961
}
6062
}
6163

6264
private async getPosition(itemID: string) {
6365
let result: {
64-
file: RSS3ItemsList;
66+
file: RSS3ItemsList | null;
6567
index: number;
6668
} = {
6769
file: null,
6870
index: -1,
6971
};
7072
this.getAllList(this.main.account.address, (file) => {
73+
if (!file.list) {
74+
return false;
75+
}
7176
const index = file.list.findIndex((item) => item.id === itemID);
7277
if (index !== -1) {
7378
result = {
@@ -85,7 +90,7 @@ class Items {
8590
async get(itemID: string) {
8691
const position = await this.getPosition(itemID);
8792
if (position.index !== -1) {
88-
return position.file.list[position.index];
93+
return position.file!.list![position.index];
8994
} else {
9095
return null;
9196
}
@@ -97,6 +102,8 @@ class Items {
97102
if (!file) {
98103
const newID = utils.id.getItems(this.main.account.address, 0);
99104
file = <RSS3ItemsList>this.main.files.new(newID);
105+
}
106+
if (!file.list) {
100107
file.list = [];
101108
}
102109

@@ -143,10 +150,10 @@ class Items {
143150
const position = await this.getPosition(itemIn.id);
144151

145152
if (position.index !== -1) {
146-
position.file.list[position.index] = Object.assign(position.file.list[position.index], itemIn);
153+
position.file!.list![position.index] = Object.assign(position.file!.list![position.index], itemIn);
147154

148-
this.main.files.set(position.file);
149-
return position.file.list[position.index];
155+
this.main.files.set(position.file!);
156+
return position.file!.list![position.index];
150157
} else {
151158
return null;
152159
}

src/links.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ class Links {
2222
return {
2323
file,
2424
index,
25-
id: index !== -1 ? file.links[index].list : null,
25+
id: index !== -1 ? file.links![index].list : null,
2626
};
2727
}
2828

2929
async getList(persona: string, type: string, index = -1) {
3030
if (index < 0) {
3131
const indexFile = <RSS3Index>await this.main.files.get(persona);
3232
if (indexFile.links) {
33-
const linksId = indexFile.links.find((links) => links.type === type).list;
33+
const linksId = indexFile.links.find((links) => links.type === type)?.list;
34+
if (!linksId) {
35+
throw new Error('Link type does not exist');
36+
}
3437
const parsed = utils.id.parse(linksId);
3538
index = parsed.index + index + 1;
3639
const file = <RSS3List>await this.main.files.get(utils.id.getLinks(persona, type, index));
@@ -53,7 +56,9 @@ class Links {
5356
async getAllList(persona: string, type: string, breakpoint?: (file: RSS3LinksList) => boolean) {
5457
const { id } = await this.getPosition(persona, type);
5558
if (id) {
56-
return <RSS3ID[]>await this.main.files.getAll(id, breakpoint);
59+
return <RSS3ID[]>await this.main.files.getAll(id, (file) => {
60+
return breakpoint?.(<RSS3LinksList>file) || false;
61+
});
5762
} else {
5863
return [];
5964
}
@@ -95,14 +100,14 @@ class Links {
95100
async deleteList(type: string) {
96101
const { index, file } = await this.getPosition(this.main.account.address, type);
97102
if (index > -1) {
98-
const links = file.links[index];
103+
const links = file.links![index];
99104
if (links.list) {
100105
const listFile = <RSS3LinksList>await this.main.files.get(links.list);
101106
listFile.list?.forEach((link) => {
102107
this.main.files.clearCache(utils.id.getBacklinks(link, links.type, ''), true);
103108
});
104109
}
105-
file.links.splice(index, 1);
110+
file.links!.splice(index, 1);
106111
this.main.files.set(file);
107112
return links;
108113
}
@@ -111,11 +116,10 @@ class Links {
111116
async patchListTags(type: string, tags: string[]) {
112117
if (utils.check.valueLength(tags) && equals<string[]>(tags)) {
113118
const { index, file } = await this.getPosition(this.main.account.address, type);
114-
const linksList = file.links;
115119
if (index > -1) {
116-
file.links[index].tags = tags;
120+
file.links![index].tags = tags;
117121
this.main.files.set(file);
118-
return linksList[index];
122+
return file.links![index];
119123
} else {
120124
throw Error('Link type does not exist');
121125
}
@@ -130,7 +134,7 @@ class Links {
130134
const list = await this.getAllList(
131135
this.main.account.address,
132136
type,
133-
(file) => file.list.indexOf(personaID) > -1,
137+
(file) => !!file.list && file.list.indexOf(personaID) > -1,
134138
);
135139
const index = list.indexOf(personaID);
136140
if (index === -1) {
@@ -150,7 +154,7 @@ class Links {
150154
newFile.list_next = file.id;
151155
this.main.files.set(newFile);
152156

153-
indexFile.links[index].list = newID;
157+
indexFile.links![index].list = newID;
154158
this.main.files.set(indexFile);
155159
} else {
156160
file.list.unshift(personaID);
@@ -175,6 +179,9 @@ class Links {
175179
let result = null;
176180
if (id) {
177181
await this.getAllList(this.main.account.address, type, (file) => {
182+
if (!file.list) {
183+
return false;
184+
}
178185
const index = file.list.indexOf(personaID);
179186
if (index > -1) {
180187
this.main.files.clearCache(utils.id.getBacklinks(personaID, type, ''), true);
@@ -183,6 +190,7 @@ class Links {
183190
this.main.files.set(file);
184191
return true;
185192
}
193+
return false;
186194
});
187195
}
188196
return result;

tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"compilerOptions": {
3+
"strict": true,
34
"module": "commonjs",
45
"esModuleInterop": true,
56
"allowSyntheticDefaultImports": true,
7+
"strictPropertyInitialization": false,
68
"target": "esnext",
79
"noImplicitAny": true,
810
"moduleResolution": "node",

0 commit comments

Comments
 (0)