yaml-admin-api 0.0.110 → 0.0.112

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml-admin-api",
3
- "version": "0.0.110",
3
+ "version": "0.0.112",
4
4
  "license": "MIT",
5
5
  "description": "YAML Admin API package",
6
6
  "type": "commonjs",
@@ -0,0 +1,56 @@
1
+ const { withConfigLocal } = require('../upload/localUpload.js');
2
+ const { withConfigS3 } = require('../upload/s3Upload.js');
3
+
4
+ class Uploader {
5
+ constructor(uploader) {
6
+ this._uploader = uploader;
7
+ }
8
+
9
+ async getSecureUrl(key) {
10
+ return await this._uploader.getUrlSecure(key);
11
+ }
12
+
13
+ async upload(key, stream) {
14
+ return await this._uploader.upload(key, stream);
15
+ }
16
+
17
+ async uploadSecure(key, stream) {
18
+ return await this._uploader.uploadSecure(key, stream);
19
+ }
20
+
21
+ async getUrl(key) {
22
+ return await this._uploader.getUrl(key);
23
+ }
24
+
25
+ static init(yml, auth) {
26
+ const api_host = yml['api-host']?.uri;
27
+ const raw = yml.upload.s3
28
+ ? withConfigS3({
29
+ access_key_id: yml.upload.s3.access_key_id,
30
+ secret_access_key: yml.upload.s3.secret_access_key,
31
+ bucket: yml.upload.s3.bucket,
32
+ region: yml.upload.s3.region,
33
+ prefix: yml.upload.s3.prefix,
34
+ bucket_private: yml.upload.s3.bucket_private,
35
+ base_url: yml.upload.s3.base_url,
36
+ })
37
+ : withConfigLocal({
38
+ path: yml.upload.local.path,
39
+ path_private: yml.upload.local.path_private,
40
+ base_url: yml.upload.local.base_url,
41
+ api_host,
42
+ auth,
43
+ });
44
+ Uploader._instance = new Uploader(raw);
45
+ return Uploader._instance;
46
+ }
47
+
48
+ static getInstance() {
49
+ if (!Uploader._instance) throw new Error('Uploader is not initialized. Call Uploader.init(yml) first.');
50
+ return Uploader._instance;
51
+ }
52
+ }
53
+
54
+ Uploader._instance = null;
55
+
56
+ module.exports = { Uploader };
@@ -6,8 +6,7 @@ const crypto = require('crypto');
6
6
  const bcrypt = require('bcryptjs');
7
7
  const XLSX = require('xlsx');
8
8
  const moment = require('moment');
9
- const { withConfigLocal } = require('../upload/localUpload.js');
10
- const { withConfigS3 } = require('../upload/s3Upload.js');
9
+ const { Uploader } = require('../common/Uploader.js');
11
10
  const { makeMongoSortFromYml } = require('./crud-common.js');
12
11
 
13
12
  const asyncErrorHandler = (fn) => (req, res, next) => {
@@ -34,25 +33,12 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
34
33
 
35
34
  const auth = withConfig({ db, jwt_secret: yml.login["jwt-secret"] });
36
35
  const passwordEncoding = yml.login['password-encoding']
37
- const api_host = yml["api-host"].uri;
36
+
38
37
  let isS3 = yml.upload.s3
39
38
  let host_image = isS3 ? yml.upload.s3.base_url : yml.upload.local.base_url
40
39
  let api_prefix = options?.api_prefix || ''
41
40
 
42
- const uploader = yml.upload.s3 ? withConfigS3({
43
- access_key_id: yml.upload.s3.access_key_id,
44
- secret_access_key: yml.upload.s3.secret_access_key,
45
- bucket: yml.upload.s3.bucket,
46
- region: yml.upload.s3.region,
47
- prefix: yml.upload.s3.prefix,
48
- bucket_private: yml.upload.s3.bucket_private,
49
- base_url: yml.upload.s3.base_url,
50
- }) : withConfigLocal({
51
- path: yml.upload.local.path,
52
- path_private: yml.upload.local.path_private,
53
- base_url: yml.upload.local.base_url,
54
- api_host,
55
- })
41
+ const uploader = Uploader.getInstance()
56
42
 
57
43
  let key_field = yml_entity.fields?.find(field => field.key)
58
44
  if (!key_field) {
@@ -207,7 +193,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
207
193
  url= host_image + '/' + url
208
194
 
209
195
  if(private) {
210
- url = await uploader.getUrlSecure(key, auth);
196
+ url = await uploader.getSecureUrl(key);
211
197
  }
212
198
 
213
199
  return url
@@ -594,7 +580,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
594
580
  const currentTime = moment().format('YYYYMMDD_HHmmss');
595
581
  const key = `excel/${filename}${currentTime}.xlsx`;
596
582
  await uploader.uploadSecure(key, excelBuffer);
597
- let url = await uploader.getUrlSecure(key, auth);
583
+ let url = await uploader.getSecureUrl(key);
598
584
  return res.json({ r: true, url });
599
585
  }))
600
586
  }
@@ -869,8 +855,8 @@ const makeApiGenerateAggregate = async (apiGenerate) => {
869
855
  return aggregate
870
856
  }
871
857
 
872
- const generateEntityApi = async ({ app, db, entity_name, entity, yml, options }) => {
873
- await generateCrud({ app, db, entity_name, yml_entity: entity, yml, options })
858
+ const generateEntityApi = async ({ app, db, entity_name, entity, yml, options, uploader }) => {
859
+ await generateCrud({ app, db, entity_name, yml_entity: entity, yml, options, uploader })
874
860
  }
875
861
 
876
862
  module.exports = {
package/src/index.js CHANGED
@@ -1,6 +1,4 @@
1
1
  const registerRoutes = require('./yml-admin-api.js');
2
2
  const { genEntityIdWithKey } = require('./common/util.js');
3
- const { withConfig } = require('./login/auth.js');
4
- const { withConfigLocal } = require('./upload/localUpload.js');
5
- const { withConfigS3 } = require('./upload/s3Upload.js');
6
- module.exports = { registerRoutes, genEntityIdWithKey, withConfigLocal, withConfigS3, withConfig };
3
+ const { Uploader } = require('./common/Uploader.js');
4
+ module.exports = { registerRoutes, genEntityIdWithKey, Uploader };
@@ -1,7 +1,7 @@
1
1
  const moment = require('moment')
2
2
  const fs = require('fs')
3
3
 
4
- const withConfigLocal = ({path, path_private, base_url, api_host}) => {
4
+ const withConfigLocal = ({path, path_private, base_url, api_host, auth}) => {
5
5
 
6
6
  const upload = async (key, stream) => {
7
7
  return await fs.writeFileSync(path + '/' + key, stream)
@@ -11,7 +11,7 @@ const withConfigLocal = ({path, path_private, base_url, api_host}) => {
11
11
  return await fs.writeFileSync(path_private + '/' + key, stream)
12
12
  }
13
13
 
14
- const getUrlSecure = async (Key, auth) => {
14
+ const getUrlSecure = async (Key) => {
15
15
  let r = `${api_host}/local-secure-download?key=${Key}`
16
16
  let shortToken = await auth.genenrateShortToken()
17
17
  r += `&token=${shortToken}`
@@ -6,6 +6,7 @@ const { generateLoginApi } = require('./crud/login-api-generator');
6
6
  const { generateChartApi } = require('./crud/chart-api-generator');
7
7
  const { withConfig } = require('./login/auth.js');
8
8
  const { generateUploadApi } = require('./upload/upload-api-generator');
9
+ const { Uploader } = require('./common/Uploader.js');
9
10
 
10
11
  const changeEnv = (yamlString, env = {}) => {
11
12
  if (!yamlString) return yamlString;
@@ -41,6 +42,8 @@ async function registerRoutes(app, options = {}) {
41
42
  yml = yaml.parse(yamlString);
42
43
  }
43
44
 
45
+
46
+
44
47
  const {database, entity} = yml;
45
48
  let db = null;
46
49
  if(database) {
@@ -52,14 +55,19 @@ async function registerRoutes(app, options = {}) {
52
55
  }
53
56
  }
54
57
 
58
+ //local secure download api
59
+ const auth = withConfig({ db, jwt_secret: yml.login["jwt-secret"] });
60
+
61
+ const uploader = Uploader.init(yml, auth);
62
+
55
63
  await generateLoginApi(app, db, yml, api_prefix)
56
64
  await generateChartApi(app, db, yml, api_prefix)
57
-
65
+
58
66
  entity && Object.keys(entity).forEach(async (entity_name) => {
59
67
  await generateEntityApi({
60
- app, db,
61
- entity_name,
62
- entity:entity[entity_name],
68
+ app, db,
69
+ entity_name,
70
+ entity:entity[entity_name],
63
71
  yml,
64
72
  options,
65
73
  })
@@ -70,9 +78,6 @@ async function registerRoutes(app, options = {}) {
70
78
  yml,
71
79
  options,
72
80
  })
73
-
74
- //local secure download api
75
- const auth = withConfig({ db, jwt_secret: yml.login["jwt-secret"] });
76
81
  app.get(api_prefix + '/local-secure-download', auth.isAuthenticated, async (req, res) => {
77
82
  const {key} = req.query;
78
83
  const a = `${yml.upload.local.path_private}/${key}`