powr-sdk-api 2.0.1 → 2.0.3

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.
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ const winston = require('winston');
4
+ const {
5
+ Storage
6
+ } = require('@google-cloud/storage');
7
+
8
+ // Create GCS client using environment variables for credentials
9
+ const storage = new Storage({
10
+ projectId: process.env.GCS_PROJECT_ID,
11
+ credentials: {
12
+ client_email: process.env.GCS_CLIENT_EMAIL,
13
+ private_key: process.env.GCS_PRIVATE_KEY ? process.env.GCS_PRIVATE_KEY.replace(/\\n/g, '\n') : undefined
14
+ }
15
+ });
16
+
17
+ /**
18
+ * Custom Winston transport for logging to Google Cloud Storage
19
+ */
20
+ class GCSTransport extends winston.Transport {
21
+ constructor(opts) {
22
+ super(opts);
23
+ this.bucket = opts.bucket;
24
+ this.prefix = opts.prefix || '';
25
+ this.buffer = [];
26
+ this.bufferSize = opts.bufferSize || 100;
27
+ this.flushInterval = opts.flushInterval || 5000;
28
+ this.setupFlushInterval();
29
+ }
30
+ setupFlushInterval() {
31
+ setInterval(() => {
32
+ this.flush();
33
+ }, this.flushInterval);
34
+ }
35
+ async flush() {
36
+ if (this.buffer.length === 0) return;
37
+ const logs = this.buffer.splice(0, this.buffer.length);
38
+ const date = new Date().toISOString().split('T')[0];
39
+ const filename = `${this.prefix}/${date}/${Date.now()}.json`;
40
+ try {
41
+ const bucket = storage.bucket(this.bucket);
42
+ const file = bucket.file(filename);
43
+ await file.save(JSON.stringify(logs), {
44
+ contentType: 'application/json',
45
+ metadata: {
46
+ contentType: 'application/json'
47
+ }
48
+ });
49
+ } catch (error) {
50
+ console.error('Failed to write logs to Google Cloud Storage:', {
51
+ error: error.message,
52
+ code: error.code,
53
+ bucket: this.bucket,
54
+ filename: filename,
55
+ projectId: process.env.GCS_PROJECT_ID,
56
+ stack: error.stack
57
+ });
58
+ // Put the logs back in the buffer
59
+ this.buffer.unshift(...logs);
60
+ }
61
+ }
62
+ log(info, callback) {
63
+ setImmediate(() => {
64
+ this.emit('logged', info);
65
+ });
66
+ this.buffer.push({
67
+ timestamp: new Date().toISOString(),
68
+ ...info
69
+ });
70
+ if (this.buffer.length >= this.bufferSize) {
71
+ this.flush();
72
+ }
73
+ callback();
74
+ }
75
+ }
76
+ module.exports = {
77
+ GCSTransport
78
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ // Centralized database service for powr-sdk-api
4
+ // This provides a single connection point for all projects
5
+
6
+ const {
7
+ MongoClient
8
+ } = require('mongodb');
9
+ const {
10
+ config
11
+ } = require('../config');
12
+ let client = null;
13
+ let db = null;
14
+ const connectDB = async () => {
15
+ if (!config.mongoUri) {
16
+ throw new Error('MongoDB connection URL is required. Please provide mongoUrl in createAdminRoutes options.');
17
+ }
18
+ try {
19
+ client = new MongoClient(config.mongoUri);
20
+ await client.connect();
21
+ db = client.db();
22
+ console.log('Connected to MongoDB via SDK');
23
+ } catch (error) {
24
+ console.error('MongoDB connection error:', error);
25
+ throw error;
26
+ }
27
+ };
28
+ const getDb = async () => {
29
+ if (!db) {
30
+ await connectDB();
31
+ }
32
+ return db;
33
+ };
34
+ module.exports = {
35
+ getDb
36
+ };
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ const winston = require('winston');
4
+ const {
5
+ S3Client,
6
+ PutObjectCommand
7
+ } = require('@aws-sdk/client-s3');
8
+
9
+ // Create S3 client
10
+ const s3Client = new S3Client({
11
+ region: process.env.AWS_REGION,
12
+ credentials: {
13
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID,
14
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
15
+ }
16
+ });
17
+
18
+ /**
19
+ * Custom Winston transport for logging to S3
20
+ */
21
+ class S3Transport extends winston.Transport {
22
+ constructor(opts) {
23
+ super(opts);
24
+ this.bucket = opts.bucket;
25
+ this.prefix = opts.prefix || '';
26
+ this.buffer = [];
27
+ this.bufferSize = opts.bufferSize || 100;
28
+ this.flushInterval = opts.flushInterval || 5000;
29
+ this.setupFlushInterval();
30
+ }
31
+ setupFlushInterval() {
32
+ setInterval(() => {
33
+ this.flush();
34
+ }, this.flushInterval);
35
+ }
36
+ async flush() {
37
+ if (this.buffer.length === 0) return;
38
+ const logs = this.buffer.splice(0, this.buffer.length);
39
+ const date = new Date().toISOString().split('T')[0];
40
+ const key = `${this.prefix}/${date}/${Date.now()}.json`;
41
+ try {
42
+ const command = new PutObjectCommand({
43
+ Bucket: this.bucket,
44
+ Key: key,
45
+ Body: JSON.stringify(logs),
46
+ ContentType: 'application/json'
47
+ });
48
+ await s3Client.send(command);
49
+ } catch (error) {
50
+ console.error('Failed to write logs to S3:', {
51
+ error: error.message,
52
+ code: error.code,
53
+ bucket: this.bucket,
54
+ key: key,
55
+ region: process.env.AWS_REGION,
56
+ stack: error.stack
57
+ });
58
+ // Put the logs back in the buffer
59
+ this.buffer.unshift(...logs);
60
+ }
61
+ }
62
+ log(info, callback) {
63
+ setImmediate(() => {
64
+ this.emit('logged', info);
65
+ });
66
+ this.buffer.push({
67
+ timestamp: new Date().toISOString(),
68
+ ...info
69
+ });
70
+ if (this.buffer.length >= this.bufferSize) {
71
+ this.flush();
72
+ }
73
+ callback();
74
+ }
75
+ }
76
+ module.exports = {
77
+ S3Transport
78
+ };
@@ -112,4 +112,6 @@ const createSwaggerSpec = (options = {}) => {
112
112
  }
113
113
  return swaggerSpec;
114
114
  };
115
- module.exports = createSwaggerSpec;
115
+ module.exports = {
116
+ createSwaggerSpec
117
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",