-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi.js
More file actions
39 lines (32 loc) · 732 Bytes
/
api.js
File metadata and controls
39 lines (32 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
const path = require('node:path');
const fs = require('node:fs');
const methods = new Map();
const loadMethod = (apiPath, name) => {
const filePath = apiPath + name;
const key = path.basename(filePath, '.js');
try {
const libPath = require.resolve(filePath);
delete require.cache[libPath];
} catch {
return;
}
try {
const method = require(filePath);
methods.set(key, method);
} catch {
methods.delete(name);
}
};
const api = {};
api.load = (apiPath) => {
fs.readdir(apiPath, (err, files) => {
if (err) return;
files.forEach((name) => {
loadMethod(apiPath, name);
});
});
return api;
};
api.get = (name) => methods.get(name);
module.exports = api;