Skip to content

Commit 330c520

Browse files
committed
lint: lib/models
- add more typing annotate - install @types/cheerio
1 parent b77c61f commit 330c520

File tree

8 files changed

+111
-65
lines changed

8 files changed

+111
-65
lines changed

lib/models/author.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// external modules
22

33
import {Model, DataTypes} from "sequelize";
4-
import {AuthorAttributes} from "./baseModel";
4+
import {MySequelize, AuthorAttributes, ModelObj} from "./baseModel";
55

66
export class Author extends Model<AuthorAttributes> implements AuthorAttributes {
77
color: string;
88
id: string;
99

10-
static initialize(sequelize): void {
10+
static initialize(sequelize: MySequelize): void {
1111
Author.init(
1212
{
1313
id: {
@@ -30,7 +30,7 @@ export class Author extends Model<AuthorAttributes> implements AuthorAttributes
3030
})
3131
}
3232

33-
static associate(models: any): void {
33+
static associate(models: ModelObj): void {
3434
Author.belongsTo(models.Note, {
3535
foreignKey: 'noteId',
3636
as: 'note',

lib/models/baseModel.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import {Sequelize, Model} from "sequelize";
1+
import {Moment} from "moment";
2+
import {Sequelize, Model, ModelCtor} from "sequelize";
23

34
export type MySequelize = Sequelize & {
45
processData?: any
56
stripNullByte?: any
67
}
78

8-
export type BaseModel<T> = {
9+
export type BaseModel = {
910
name: string;
1011
initialize(sequelize: Sequelize): void
11-
associate(models: any): void
12+
associate(models: ModelObj): void
1213
}
1314

14-
export function StaticImplements<T>(t: T) {
15-
return t;
16-
}
17-
18-
export interface BaseModelStatic extends Model {
19-
initialize(sequelize: Sequelize): void
15+
export type UserModel = Model<UserAttributes> & UserAttributes
16+
export type AuthorModel = Model<AuthorAttributes> & AuthorAttributes
17+
export type RevisionModel = Model<RevisionAttributes> & RevisionAttributes
18+
export type NoteModel = Model<NoteAttributes> & NoteAttributes
2019

21-
associate(models: any): void
20+
export type ModelObj = {
21+
['Author']: ModelCtor<AuthorModel>
22+
['Note']: ModelCtor<NoteModel>
23+
['User']: ModelCtor<UserModel>
24+
['Revision']: ModelCtor<RevisionModel>
2225
}
2326

2427
export interface AuthorAttributes {
@@ -35,7 +38,7 @@ export interface NoteAttributes {
3538
title?: string
3639
content?: string
3740
authorship?: string
38-
lastchangeAt?: Date
41+
lastchangeAt?: Date | Moment
3942
savedAt?: Date
4043

4144
ownerId?: string
@@ -88,3 +91,17 @@ export interface UserAttributes {
8891
email?: string
8992
password?: string
9093
}
94+
95+
export interface NoteMeta {
96+
title: string
97+
tags: string[]
98+
99+
description: string
100+
robots: string
101+
GA: string
102+
disqus: string
103+
// eslint-disable-next-line
104+
slideOptions: any
105+
image: string
106+
}
107+

lib/models/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// external modules
2-
import {Model, Sequelize} from "sequelize";
2+
import {Sequelize} from "sequelize";
33
import {cloneDeep} from "lodash";
44

55
// core
66
import config from "../config";
77
import {logger} from "../logger";
88

99

10-
import {BaseModel, AuthorAttributes, NoteAttributes, RevisionAttributes, UserAttributes, GenericProfile} from "./baseModel";
10+
import {
11+
BaseModel,
12+
AuthorAttributes,
13+
NoteAttributes,
14+
RevisionAttributes,
15+
UserAttributes,
16+
GenericProfile,
17+
ModelObj
18+
} from "./baseModel";
1119

1220
import {Author} from './author'
1321
import {User} from './user'
@@ -48,17 +56,17 @@ function processData(data, _default, process) {
4856

4957
sequelize.processData = processData
5058

51-
const db: any = {}
59+
const db: Partial<ModelObj> = {}
5260

53-
const models: BaseModel<Model>[] = [User, Note, Author, Revision]
61+
const models: BaseModel[] = [User, Note, Author, Revision]
5462

5563
models.forEach(m => {
5664
m.initialize(sequelize)
5765
db[m.name] = m
5866
})
5967
models.forEach(m => {
6068
if ('associate' in m) {
61-
m.associate(db)
69+
m.associate(db as ModelObj)
6270
}
6371
})
6472

lib/models/note.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import base64url from "base64url";
77
import cheerio from "cheerio";
88
import * as fs from "fs";
99
import markdownIt from "markdown-it";
10-
import moment from "moment";
10+
import moment, {Moment} from "moment";
1111

1212
// ot
1313
import ot from "ot";
@@ -20,19 +20,24 @@ import config from "../config";
2020
import {logger} from "../logger";
2121
import {createNoteWithRevision, syncNote} from "../services/note";
2222
import {stripTags} from "../string";
23-
import {MySequelize, NoteAttributes} from "./baseModel";
23+
import {ModelObj, MySequelize, NoteAttributes, NoteMeta} from "./baseModel";
2424

2525
const md = markdownIt()
2626
export const dmp = new DiffMatchPatch()
2727
// permission types
2828
const permissionTypes = ['freely', 'editable', 'limited', 'locked', 'protected', 'private']
2929

30+
interface ParsedMeta {
31+
markdown: string,
32+
meta: Record<string, string>
33+
}
34+
3035
export class Note extends Model<NoteAttributes> implements NoteAttributes {
3136
alias: string;
3237
authorship: string;
3338
content: string;
3439
id: string;
35-
lastchangeAt: Date;
40+
lastchangeAt: Date | Moment;
3641
permission: string;
3742
savedAt: Date;
3843
shortid: string;
@@ -107,7 +112,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
107112
})
108113

109114
Note.addHook('beforeCreate', function (note: Note): Promise<void> {
110-
return new Promise(function (resolve, reject) {
115+
return new Promise(function (resolve) {
111116
// if no content specified then use default note
112117
if (!note.content) {
113118
let filePath = config.defaultNotePath
@@ -124,8 +129,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
124129
note.title = noteInFS.title
125130
note.content = noteInFS.content
126131
if (filePath !== config.defaultNotePath) {
127-
note.createdAt = noteInFS.lastchangeAt.toDate()
128-
note.lastchangeAt = noteInFS.lastchangeAt.toDate()
132+
note.createdAt = (noteInFS.lastchangeAt as Moment).toDate()
133+
note.lastchangeAt = (noteInFS.lastchangeAt as Moment).toDate()
129134
}
130135
}
131136
}
@@ -142,7 +147,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
142147
})
143148
}
144149

145-
static associate(models: any): void {
150+
static associate(models: ModelObj): void {
146151
Note.belongsTo(models.User, {
147152
foreignKey: 'ownerId',
148153
as: 'owner',
@@ -167,22 +172,22 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
167172
}
168173

169174

170-
static checkFileExist(filePath) {
175+
static checkFileExist(filePath: string): boolean {
171176
try {
172177
return fs.statSync(filePath).isFile()
173178
} catch (err) {
174179
return false
175180
}
176181
}
177182

178-
static encodeNoteId(id) {
183+
static encodeNoteId(id: string): string {
179184
// remove dashes in UUID and encode in url-safe base64
180185
const str = id.replace(/-/g, '')
181186
const hexStr = Buffer.from(str, 'hex')
182187
return base64url.encode(hexStr)
183188
}
184189

185-
static decodeNoteId(encodedId) {
190+
static decodeNoteId(encodedId: string): string {
186191
// decode from url-safe base64
187192
const id = base64url.toBuffer(encodedId).toString('hex')
188193
// add dashes between the UUID string parts
@@ -195,7 +200,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
195200
return idParts.join('-')
196201
}
197202

198-
static checkNoteIdValid(id) {
203+
static checkNoteIdValid(id: string): boolean {
199204
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
200205
const result = id.match(uuidRegex)
201206
if (result && result.length === 1) {
@@ -205,7 +210,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
205210
}
206211
}
207212

208-
static parseNoteIdAsync(noteId) {
213+
static parseNoteIdAsync(noteId: string): Promise<string> {
209214
return new Promise((resolve, reject) => {
210215
Note.parseNoteId(noteId, (err, id) => {
211216
if (err) {
@@ -216,7 +221,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
216221
})
217222
}
218223

219-
static parseNoteId(noteId, callback) {
224+
static parseNoteId(noteId: string, callback: (err: Error | null, id: string) => void): void {
220225
async.series({
221226
parseNoteIdByAlias: function (_callback) {
222227
// try to parse note id by alias (e.g. doc)
@@ -320,7 +325,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
320325
return _callback(err, null)
321326
}
322327
}
323-
}, function (err, result) {
328+
}, function (err) {
324329
if (err) {
325330
logger.error(err)
326331
return callback(err, null)
@@ -329,7 +334,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
329334
})
330335
}
331336

332-
static parseNoteInfo(body) {
337+
static parseNoteInfo(body: string): Partial<NoteMeta> {
333338
const parsed = Note.extractMeta(body)
334339
const $ = cheerio.load(md.render(parsed.markdown))
335340
return {
@@ -338,13 +343,13 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
338343
}
339344
}
340345

341-
static parseNoteTitle(body) {
346+
static parseNoteTitle(body: string): string {
342347
const parsed = Note.extractMeta(body)
343348
const $ = cheerio.load(md.render(parsed.markdown))
344349
return Note.extractNoteTitle(parsed.meta, $)
345350
}
346351

347-
static extractNoteTitle(meta, $) {
352+
static extractNoteTitle(meta: Record<string, string>, $: cheerio.Root): string {
348353
let title = ''
349354
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
350355
title = meta.title
@@ -358,20 +363,20 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
358363
return title
359364
}
360365

361-
static generateDescription(markdown) {
366+
static generateDescription(markdown: string): string {
362367
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ')
363368
}
364369

365-
static decodeTitle(title) {
370+
static decodeTitle(title: string): string {
366371
return title || 'Untitled'
367372
}
368373

369-
static generateWebTitle(title) {
374+
static generateWebTitle(title: string): string {
370375
title = !title || title === 'Untitled' ? 'CodiMD - Collaborative markdown notes' : title + ' - CodiMD'
371376
return title
372377
}
373378

374-
static extractNoteTags(meta, $) {
379+
static extractNoteTags(meta: Record<string, string>, $: cheerio.Root): string[] {
375380
const tags = []
376381
const rawtags = []
377382
let metaTags
@@ -412,7 +417,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
412417
return tags
413418
}
414419

415-
static extractMeta(content) {
420+
static extractMeta(content: string): ParsedMeta {
416421
let obj = null
417422
try {
418423
obj = metaMarked(content)
@@ -427,8 +432,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
427432
return obj
428433
}
429434

430-
static parseMeta(meta) {
431-
const _meta: any = {}
435+
static parseMeta(meta: Record<string, string>): Partial<NoteMeta> {
436+
const _meta: Partial<NoteMeta> = {}
432437
if (meta) {
433438
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
434439
_meta.title = meta.title
@@ -452,7 +457,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
452457
return _meta
453458
}
454459

455-
static updateAuthorshipByOperation(operation, userId, authorships) {
460+
static updateAuthorshipByOperation(operation: any, userId: string, authorships: any): any {
456461
let index = 0
457462
const timestamp = Date.now()
458463
for (let i = 0; i < operation.length; i++) {
@@ -554,7 +559,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
554559
return authorships
555560
}
556561

557-
static transformPatchToOperations(patch, contentLength) {
562+
static transformPatchToOperations(patch: any, contentLength: number) {
558563
const operations = []
559564
if (patch.length > 0) {
560565
// calculate original content length
@@ -615,7 +620,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
615620
}
616621
}
617622

618-
function readFileSystemNote(filePath) {
623+
function readFileSystemNote(filePath: string): Partial<Note> {
619624
const fsModifiedTime = moment(fs.statSync(filePath).mtime)
620625
const content = fs.readFileSync(filePath, 'utf8')
621626

0 commit comments

Comments
 (0)