Skip to content

Commit 62b8d55

Browse files
committed
refactor(ts): change file db module to TS and ESM
1 parent 522d3f7 commit 62b8d55

File tree

5 files changed

+138
-106
lines changed

5 files changed

+138
-106
lines changed

src/db/file/index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/db/file/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as users from './users';
2+
import * as repo from './repo';
3+
import * as pushes from './pushes';
4+
5+
export const {
6+
getPushes,
7+
writeAudit,
8+
getPush,
9+
authorise,
10+
cancel,
11+
reject,
12+
canUserCancelPush,
13+
canUserApproveRejectPush,
14+
} = pushes;
15+
16+
export const {
17+
getRepos,
18+
getRepo,
19+
createRepo,
20+
addUserCanPush,
21+
addUserCanAuthorise,
22+
removeUserCanPush,
23+
removeUserCanAuthorise,
24+
deleteRepo,
25+
isUserPushAllowed,
26+
canUserApproveRejectPushRepo,
27+
} = repo;
28+
29+
export const {
30+
findUser,
31+
getUsers,
32+
createUser,
33+
deleteUser,
34+
updateUser,
35+
} = users;

src/db/file/pushes.js renamed to src/db/file/pushes.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
const fs = require('fs');
2-
const _ = require('lodash');
3-
const Datastore = require('@seald-io/nedb');
4-
const Action = require('../../proxy/actions/Action').Action;
5-
const toClass = require('../helper').toClass;
6-
const repo = require('./repo');
1+
import fs from 'fs';
2+
import _ from 'lodash';
3+
import Datastore from '@seald-io/nedb';
4+
import { Action } from '../../proxy/actions/Action';
5+
import { toClass } from '../helper';
6+
import * as repo from './repo';
7+
import { PushQuery } from '../types'
78

89
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
910
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
1011

1112
const db = new Datastore({ filename: './.data/db/pushes.db', autoload: true });
1213

13-
const defaultPushQuery = {
14+
const defaultPushQuery: PushQuery = {
1415
error: false,
1516
blocked: true,
1617
allowPush: false,
1718
authorised: false,
1819
};
1920

20-
const getPushes = (query, logger) => {
21+
export const getPushes = (query: PushQuery) => {
2122
if (!query) query = defaultPushQuery;
2223
return new Promise((resolve, reject) => {
23-
db.find(query, (err, docs) => {
24+
db.find(query, (err: Error, docs: Action[]) => {
2425
if (err) {
2526
reject(err);
2627
} else {
@@ -34,8 +35,8 @@ const getPushes = (query, logger) => {
3435
});
3536
};
3637

37-
const getPush = async (id, logger) => {
38-
return new Promise((resolve, reject) => {
38+
export const getPush = async (id: string) => {
39+
return new Promise<Action | null>((resolve, reject) => {
3940
db.findOne({ id: id }, (err, doc) => {
4041
if (err) {
4142
reject(err);
@@ -50,7 +51,7 @@ const getPush = async (id, logger) => {
5051
});
5152
};
5253

53-
const writeAudit = async (action, logger) => {
54+
export const writeAudit = async (action: Action) => {
5455
return new Promise((resolve, reject) => {
5556
const options = { multi: false, upsert: true };
5657
db.update({ id: action.id }, action, options, (err) => {
@@ -63,8 +64,12 @@ const writeAudit = async (action, logger) => {
6364
});
6465
};
6566

66-
const authorise = async (id, attestation) => {
67+
export const authorise = async (id: string, attestation: any) => {
6768
const action = await getPush(id);
69+
if (!action) {
70+
throw new Error(`push ${id} not found`);
71+
}
72+
6873
action.authorised = true;
6974
action.canceled = false;
7075
action.rejected = false;
@@ -73,28 +78,39 @@ const authorise = async (id, attestation) => {
7378
return { message: `authorised ${id}` };
7479
};
7580

76-
const reject = async (id, logger) => {
77-
const action = await getPush(id, logger);
81+
export const reject = async (id: string) => {
82+
const action = await getPush(id);
83+
if (!action) {
84+
throw new Error(`push ${id} not found`);
85+
}
86+
7887
action.authorised = false;
7988
action.canceled = false;
8089
action.rejected = true;
8190
await writeAudit(action);
8291
return { message: `reject ${id}` };
8392
};
8493

85-
const cancel = async (id, logger) => {
86-
const action = await getPush(id, logger);
94+
export const cancel = async (id: string) => {
95+
const action = await getPush(id);
96+
if (!action) {
97+
throw new Error(`push ${id} not found`);
98+
}
8799
action.authorised = false;
88100
action.canceled = true;
89101
action.rejected = false;
90102
await writeAudit(action);
91103
return { message: `cancel ${id}` };
92104
};
93105

94-
const canUserCancelPush = async (id, user) => {
95-
return new Promise(async (resolve) => {
106+
export const canUserCancelPush = async (id: string, user: any) => {
107+
return new Promise<boolean>(async (resolve) => {
96108
const pushDetail = await getPush(id);
97-
const repoName = pushDetail.repoName.replace('.git', '');
109+
if (!pushDetail) {
110+
resolve(false);
111+
}
112+
113+
const repoName = pushDetail?.repoName.replace('.git', '');
98114
const isAllowed = await repo.isUserPushAllowed(repoName, user);
99115

100116
if (isAllowed) {
@@ -105,21 +121,15 @@ const canUserCancelPush = async (id, user) => {
105121
});
106122
};
107123

108-
const canUserApproveRejectPush = async (id, user) => {
109-
return new Promise(async (resolve) => {
124+
export const canUserApproveRejectPush = async (id: string, user: any) => {
125+
return new Promise<boolean>(async (resolve) => {
110126
const action = await getPush(id);
111-
const repoName = action.repoName.replace('.git', '');
127+
if (!action) {
128+
resolve(false);
129+
}
130+
const repoName = action?.repoName.replace('.git', '');
112131
const isAllowed = await repo.canUserApproveRejectPushRepo(repoName, user);
113132

114133
resolve(isAllowed);
115134
});
116135
};
117-
118-
module.exports.getPushes = getPushes;
119-
module.exports.writeAudit = writeAudit;
120-
module.exports.getPush = getPush;
121-
module.exports.authorise = authorise;
122-
module.exports.reject = reject;
123-
module.exports.cancel = cancel;
124-
module.exports.canUserCancelPush = canUserCancelPush;
125-
module.exports.canUserApproveRejectPush = canUserApproveRejectPush;

src/db/file/repo.js renamed to src/db/file/repo.ts

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const fs = require('fs');
2-
const Datastore = require('@seald-io/nedb');
1+
import fs from 'fs';
2+
import Datastore from '@seald-io/nedb'
3+
import { Action } from '../../proxy/actions/Action';
4+
import { Repo } from '../types';
35

46
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
57
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
68

79
const db = new Datastore({ filename: './.data/db/repos.db', autoload: true });
810

9-
exports.getRepos = async (query = {}) => {
10-
return new Promise((resolve, reject) => {
11-
db.find({}, (err, docs) => {
11+
export const getRepos = async (query = {}) => {
12+
return new Promise<Repo[]>((resolve, reject) => {
13+
db.find({}, (err: Error, docs: Repo[]) => {
1214
if (err) {
1315
reject(err);
1416
} else {
@@ -18,29 +20,26 @@ exports.getRepos = async (query = {}) => {
1820
});
1921
};
2022

21-
exports.getRepo = async (name) => {
22-
return new Promise((resolve, reject) => {
23-
db.findOne({ name: name }, (err, doc) => {
23+
export const getRepo = async (name: string) => {
24+
return new Promise<Repo | null>((resolve, reject) => {
25+
db.findOne({ name }, (err: Error | null, doc: Repo) => {
2426
if (err) {
2527
reject(err);
2628
} else {
27-
if (!doc) {
28-
resolve(null);
29-
} else {
30-
resolve(doc);
31-
}
29+
resolve(doc);
3230
}
3331
});
3432
});
3533
};
3634

37-
exports.createRepo = async (repo) => {
35+
36+
export const createRepo = async (repo: Repo) => {
3837
repo.users = {
3938
canPush: [],
4039
canAuthorise: [],
4140
};
4241

43-
return new Promise((resolve, reject) => {
42+
return new Promise<Repo>((resolve, reject) => {
4443
db.insert(repo, (err, doc) => {
4544
if (err) {
4645
reject(err);
@@ -51,9 +50,13 @@ exports.createRepo = async (repo) => {
5150
});
5251
};
5352

54-
exports.addUserCanPush = async (name, user) => {
53+
export const addUserCanPush = async (name: string, user: string) => {
5554
return new Promise(async (resolve, reject) => {
56-
const repo = await exports.getRepo(name);
55+
const repo = await getRepo(name);
56+
if (!repo) {
57+
reject(new Error('Repo not found'));
58+
return;
59+
}
5760

5861
if (repo.users.canPush.includes(user)) {
5962
resolve(null);
@@ -72,9 +75,13 @@ exports.addUserCanPush = async (name, user) => {
7275
});
7376
};
7477

75-
exports.addUserCanAuthorise = async (name, user) => {
78+
export const addUserCanAuthorise = async (name: string, user: string) => {
7679
return new Promise(async (resolve, reject) => {
77-
const repo = await exports.getRepo(name);
80+
const repo = await getRepo(name);
81+
if (!repo) {
82+
reject(new Error('Repo not found'));
83+
return;
84+
}
7885

7986
if (repo.users.canAuthorise.includes(user)) {
8087
resolve(null);
@@ -94,11 +101,15 @@ exports.addUserCanAuthorise = async (name, user) => {
94101
});
95102
};
96103

97-
exports.removeUserCanAuthorise = async (name, user) => {
104+
export const removeUserCanAuthorise = async (name: string, user: string) => {
98105
return new Promise(async (resolve, reject) => {
99-
const repo = await exports.getRepo(name);
106+
const repo = await getRepo(name);
107+
if (!repo) {
108+
reject(new Error('Repo not found'));
109+
return;
110+
}
100111

101-
repo.users.canAuthorise = repo.users.canAuthorise.filter((x) => x != user);
112+
repo.users.canAuthorise = repo.users.canAuthorise.filter((x: string) => x != user);
102113

103114
const options = { multi: false, upsert: false };
104115
db.update({ name: name }, repo, options, (err) => {
@@ -111,9 +122,13 @@ exports.removeUserCanAuthorise = async (name, user) => {
111122
});
112123
};
113124

114-
exports.removeUserCanPush = async (name, user) => {
125+
export const removeUserCanPush = async (name: string, user: string) => {
115126
return new Promise(async (resolve, reject) => {
116-
const repo = await exports.getRepo(name);
127+
const repo = await getRepo(name);
128+
if (!repo) {
129+
reject(new Error('Repo not found'));
130+
return;
131+
}
117132

118133
repo.users.canPush = repo.users.canPush.filter((x) => x != user);
119134

@@ -128,8 +143,8 @@ exports.removeUserCanPush = async (name, user) => {
128143
});
129144
};
130145

131-
exports.deleteRepo = async (name) => {
132-
return new Promise((resolve, reject) => {
146+
export const deleteRepo = async (name: string) => {
147+
return new Promise<void>((resolve, reject) => {
133148
db.remove({ name: name }, (err) => {
134149
if (err) {
135150
reject(err);
@@ -140,9 +155,9 @@ exports.deleteRepo = async (name) => {
140155
});
141156
};
142157

143-
exports.isUserPushAllowed = async (name, user) => {
158+
export const isUserPushAllowed = async (name: string, user: string) => {
144159
name = name.toLowerCase();
145-
return new Promise(async (resolve, reject) => {
160+
return new Promise<boolean>(async (resolve, reject) => {
146161
const repo = await exports.getRepo(name);
147162
console.log(repo.users.canPush);
148163
console.log(repo.users.canAuthorise);
@@ -155,10 +170,10 @@ exports.isUserPushAllowed = async (name, user) => {
155170
});
156171
};
157172

158-
exports.canUserApproveRejectPushRepo = async (name, user) => {
173+
export const canUserApproveRejectPushRepo = async (name: string, user: string) => {
159174
name = name.toLowerCase();
160175
console.log(`checking if user ${user} can approve/reject for ${name}`);
161-
return new Promise(async (resolve, reject) => {
176+
return new Promise<boolean>(async (resolve, reject) => {
162177
const repo = await exports.getRepo(name);
163178
if (repo.users.canAuthorise.includes(user)) {
164179
console.log(`user ${user} can approve/reject to repo ${name}`);

0 commit comments

Comments
 (0)