streamby-core 0.11.0 → 0.12.0
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/dist/dev/server.d.ts +4 -0
- package/dist/dev/server.js +14 -0
- package/dist/index.d.ts +0 -6
- package/dist/index.js +3 -19
- package/dist/middleware/createRouter.js +16 -32
- package/dist/providers/CreateDatabaseProvider.d.ts +12 -0
- package/dist/providers/CreateDatabaseProvider.js +26 -0
- package/dist/providers/createStorageProvider.d.ts +2 -0
- package/dist/providers/createStorageProvider.js +16 -0
- package/dist/types/index.d.ts +20 -5
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDevServer = createDevServer;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const createRouter_1 = require("../middleware/createRouter");
|
|
9
|
+
function createDevServer(config) {
|
|
10
|
+
const app = (0, express_1.default)();
|
|
11
|
+
app.use(express_1.default.json());
|
|
12
|
+
app.use((0, createRouter_1.createStreamByRouter)(config));
|
|
13
|
+
return app;
|
|
14
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
import { StreamByConfig, StorageAdapter } from './types';
|
|
2
|
-
export { initProjectModel } from './db/initProjectModel';
|
|
3
1
|
export { createStreamByRouter } from './middleware/createRouter';
|
|
4
|
-
export { createMongoProjectProvider } from './providers/mongoProjectProvider';
|
|
5
|
-
export declare function createStreamByApp(config: StreamByConfig & {
|
|
6
|
-
adapter?: StorageAdapter;
|
|
7
|
-
}): import("express-serve-static-core").Express;
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const createRouter_1 = require("./middleware/createRouter");
|
|
10
|
-
var initProjectModel_1 = require("./db/initProjectModel");
|
|
11
|
-
Object.defineProperty(exports, "initProjectModel", { enumerable: true, get: function () { return initProjectModel_1.initProjectModel; } });
|
|
12
|
-
var createRouter_2 = require("./middleware/createRouter");
|
|
13
|
-
Object.defineProperty(exports, "createStreamByRouter", { enumerable: true, get: function () { return createRouter_2.createStreamByRouter; } });
|
|
14
|
-
var mongoProjectProvider_1 = require("./providers/mongoProjectProvider");
|
|
15
|
-
Object.defineProperty(exports, "createMongoProjectProvider", { enumerable: true, get: function () { return mongoProjectProvider_1.createMongoProjectProvider; } });
|
|
16
|
-
function createStreamByApp(config) {
|
|
17
|
-
const app = (0, express_1.default)();
|
|
18
|
-
app.use(express_1.default.json());
|
|
19
|
-
app.use((0, createRouter_1.createStreamByRouter)(config));
|
|
20
|
-
return app;
|
|
21
|
-
}
|
|
3
|
+
exports.createStreamByRouter = void 0;
|
|
4
|
+
var createRouter_1 = require("./middleware/createRouter");
|
|
5
|
+
Object.defineProperty(exports, "createStreamByRouter", { enumerable: true, get: function () { return createRouter_1.createStreamByRouter; } });
|
|
@@ -5,21 +5,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createStreamByRouter = createStreamByRouter;
|
|
7
7
|
const express_1 = __importDefault(require("express"));
|
|
8
|
-
const s3_1 = require("../adapters/s3");
|
|
9
8
|
const file_1 = require("../services/file");
|
|
10
9
|
const presign_1 = require("../services/presign");
|
|
10
|
+
const createStorageProvider_1 = require("../providers/createStorageProvider");
|
|
11
|
+
const createDatabaseProvider_1 = require("../providers/createDatabaseProvider");
|
|
11
12
|
function isProjectMember(project, userId) {
|
|
12
13
|
return project.members?.some((m) => m.userId?.toString() === userId?.toString());
|
|
13
14
|
}
|
|
14
15
|
function createStreamByRouter(config) {
|
|
15
16
|
const router = express_1.default.Router();
|
|
16
|
-
const adapter = config.adapter || (()
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
default:
|
|
21
|
-
throw new Error('Unsupported storage type');
|
|
17
|
+
const adapter = config.adapter || (0, createStorageProvider_1.createStorageProvider)(config.storageProviders);
|
|
18
|
+
const projectProvider = config.projectProvider || (() => {
|
|
19
|
+
if (!config.databases) {
|
|
20
|
+
throw new Error('Either "projectProvider" or "databases" must be provided.');
|
|
22
21
|
}
|
|
22
|
+
const dbResult = (0, createDatabaseProvider_1.createDatabaseProvider)(config.databases, adapter);
|
|
23
|
+
return dbResult.projectProvider;
|
|
23
24
|
})();
|
|
24
25
|
router.get('/auth', async (req, res) => {
|
|
25
26
|
try {
|
|
@@ -58,30 +59,13 @@ function createStreamByRouter(config) {
|
|
|
58
59
|
return res.status(400).json({ error: 'Missing projectId' });
|
|
59
60
|
}
|
|
60
61
|
await (0, file_1.deleteProjectImage)(adapter, projectId);
|
|
61
|
-
const updated = await
|
|
62
|
+
const updated = await projectProvider.update(projectId, { image: '' });
|
|
62
63
|
res.status(201).json(updated);
|
|
63
64
|
}
|
|
64
65
|
catch (err) {
|
|
65
66
|
res.status(500).json({ error: 'Failed to delete image', details: err.message });
|
|
66
67
|
}
|
|
67
68
|
});
|
|
68
|
-
router.post('/upload-url', async (req, res) => {
|
|
69
|
-
try {
|
|
70
|
-
const auth = await config.authProvider(req);
|
|
71
|
-
if (auth.role !== 'admin' && auth.role !== 'editor') {
|
|
72
|
-
return res.status(403).json({ error: 'Permission denied' });
|
|
73
|
-
}
|
|
74
|
-
const { filename, contentType, projectId } = req.body;
|
|
75
|
-
if (!filename || !contentType || !projectId) {
|
|
76
|
-
return res.status(400).json({ error: 'Missing filename, contentType, or projectId' });
|
|
77
|
-
}
|
|
78
|
-
const url = await (0, presign_1.getPresignedUrl)(adapter, contentType, projectId);
|
|
79
|
-
res.json(url);
|
|
80
|
-
}
|
|
81
|
-
catch (err) {
|
|
82
|
-
res.status(500).json({ error: 'Failed to generate upload URL', details: err.message });
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
69
|
router.get('/files', async (req, res) => {
|
|
86
70
|
try {
|
|
87
71
|
const auth = await config.authProvider(req);
|
|
@@ -100,7 +84,7 @@ function createStreamByRouter(config) {
|
|
|
100
84
|
try {
|
|
101
85
|
const auth = await config.authProvider(req);
|
|
102
86
|
const projectId = req.params.id;
|
|
103
|
-
const project = await
|
|
87
|
+
const project = await projectProvider.getById(projectId);
|
|
104
88
|
if (!project) {
|
|
105
89
|
return res.status(404).json({ error: 'Project not found' });
|
|
106
90
|
}
|
|
@@ -120,7 +104,7 @@ function createStreamByRouter(config) {
|
|
|
120
104
|
return res.status(403).json({ error: 'Permission denied' });
|
|
121
105
|
}
|
|
122
106
|
const { name, description } = req.body;
|
|
123
|
-
const newProject = await
|
|
107
|
+
const newProject = await projectProvider.create({
|
|
124
108
|
name,
|
|
125
109
|
description: description || '',
|
|
126
110
|
members: [{ userId: auth.userId, role: "admin" }]
|
|
@@ -139,7 +123,7 @@ function createStreamByRouter(config) {
|
|
|
139
123
|
}
|
|
140
124
|
const projectId = req.params.id;
|
|
141
125
|
const updates = req.body;
|
|
142
|
-
const project = await
|
|
126
|
+
const project = await projectProvider.getById(projectId);
|
|
143
127
|
if (!project) {
|
|
144
128
|
return res.status(404).json({ error: 'Project not found' });
|
|
145
129
|
}
|
|
@@ -149,7 +133,7 @@ function createStreamByRouter(config) {
|
|
|
149
133
|
if (!isProjectMember(project, auth.userId)) {
|
|
150
134
|
return res.status(403).json({ error: 'Unauthorized project access' });
|
|
151
135
|
}
|
|
152
|
-
const updated = await
|
|
136
|
+
const updated = await projectProvider.update(projectId, updates);
|
|
153
137
|
res.status(200).json({ success: true, project: updated });
|
|
154
138
|
}
|
|
155
139
|
catch (err) {
|
|
@@ -160,14 +144,14 @@ function createStreamByRouter(config) {
|
|
|
160
144
|
try {
|
|
161
145
|
const auth = await config.authProvider(req);
|
|
162
146
|
const projectId = req.params.id;
|
|
163
|
-
const project = await
|
|
147
|
+
const project = await projectProvider.getById(projectId);
|
|
164
148
|
if (!project) {
|
|
165
149
|
return res.status(404).json({ error: 'Project not found' });
|
|
166
150
|
}
|
|
167
151
|
if (!isProjectMember(project, auth.userId)) {
|
|
168
152
|
return res.status(403).json({ error: 'Unauthorized project access' });
|
|
169
153
|
}
|
|
170
|
-
await
|
|
154
|
+
await projectProvider.delete(projectId);
|
|
171
155
|
res.status(200).json({ success: true });
|
|
172
156
|
}
|
|
173
157
|
catch (err) {
|
|
@@ -177,7 +161,7 @@ function createStreamByRouter(config) {
|
|
|
177
161
|
router.get('/projects', async (req, res) => {
|
|
178
162
|
try {
|
|
179
163
|
const auth = await config.authProvider(req);
|
|
180
|
-
const projects = await
|
|
164
|
+
const projects = await projectProvider.list(auth.userId);
|
|
181
165
|
res.json({ projects });
|
|
182
166
|
}
|
|
183
167
|
catch (err) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { StorageAdapter } from '../types';
|
|
3
|
+
type SupportedDbType = 'mongo';
|
|
4
|
+
export interface DatabaseCredential {
|
|
5
|
+
dbType: SupportedDbType;
|
|
6
|
+
connectionString: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function createDatabaseProvider(databases: DatabaseCredential[], adapter: StorageAdapter): {
|
|
9
|
+
projectProvider: import("../types").ProjectProvider;
|
|
10
|
+
mongoConnection: mongoose.Connection;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDatabaseProvider = createDatabaseProvider;
|
|
7
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
|
+
const initProjectModel_1 = require("../db/initProjectModel");
|
|
9
|
+
const mongoProjectProvider_1 = require("./mongoProjectProvider");
|
|
10
|
+
function createDatabaseProvider(databases, adapter) {
|
|
11
|
+
// Solo una conexión mongo por ahora
|
|
12
|
+
const mongoConfig = databases.find((db) => db.dbType === 'mongo');
|
|
13
|
+
if (!mongoConfig) {
|
|
14
|
+
throw new Error('No MongoDB configuration provided. Mongo is currently required.');
|
|
15
|
+
}
|
|
16
|
+
// Conexión a MongoDB
|
|
17
|
+
const connection = mongoose_1.default.createConnection(mongoConfig.connectionString, {
|
|
18
|
+
dbName: undefined, // Opcional: podrías extraer el nombre desde la URI si quisieras
|
|
19
|
+
});
|
|
20
|
+
const ProjectModel = (0, initProjectModel_1.initProjectModel)(connection);
|
|
21
|
+
const projectProvider = (0, mongoProjectProvider_1.createMongoProjectProvider)(ProjectModel, adapter);
|
|
22
|
+
return {
|
|
23
|
+
projectProvider,
|
|
24
|
+
mongoConnection: connection,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStorageProvider = createStorageProvider;
|
|
4
|
+
const s3_1 = require("../adapters/s3");
|
|
5
|
+
function createStorageProvider(providers) {
|
|
6
|
+
if (!providers.length)
|
|
7
|
+
throw new Error('No storage providers configured.');
|
|
8
|
+
const selected = providers[0];
|
|
9
|
+
switch (selected.type) {
|
|
10
|
+
case 's3':
|
|
11
|
+
return (0, s3_1.createS3Adapter)(selected.config);
|
|
12
|
+
// futuro: case 'gcs': return createGCSAdapter(selected.config);
|
|
13
|
+
default:
|
|
14
|
+
throw new Error(`Unsupported storage type: ${selected.type}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Request } from 'express';
|
|
2
|
-
import { Connection } from 'mongoose';
|
|
3
2
|
export interface S3Config {
|
|
4
3
|
bucket: string;
|
|
5
4
|
region: string;
|
|
@@ -7,6 +6,16 @@ export interface S3Config {
|
|
|
7
6
|
secretAccessKey: string;
|
|
8
7
|
}
|
|
9
8
|
export type StorageProviderType = 's3';
|
|
9
|
+
export interface S3Config {
|
|
10
|
+
bucket: string;
|
|
11
|
+
region: string;
|
|
12
|
+
accessKeyId: string;
|
|
13
|
+
secretAccessKey: string;
|
|
14
|
+
}
|
|
15
|
+
export type StorageProvider = {
|
|
16
|
+
type: StorageProviderType;
|
|
17
|
+
config: S3Config;
|
|
18
|
+
};
|
|
10
19
|
export interface StorageAdapter {
|
|
11
20
|
listFiles(projectId: string): Promise<any[]>;
|
|
12
21
|
deleteProjectImage: (projectId: string) => Promise<any>;
|
|
@@ -34,14 +43,20 @@ export interface FolderNode {
|
|
|
34
43
|
name: string;
|
|
35
44
|
children?: FolderNode[];
|
|
36
45
|
}
|
|
46
|
+
export type DatabaseType = 'mongo';
|
|
47
|
+
export interface DatabaseCredential {
|
|
48
|
+
dbType: DatabaseType;
|
|
49
|
+
connectionString: string;
|
|
50
|
+
}
|
|
37
51
|
export interface StreamByConfig {
|
|
38
|
-
|
|
52
|
+
storageProviders: {
|
|
39
53
|
type: StorageProviderType;
|
|
40
54
|
config: S3Config;
|
|
41
|
-
};
|
|
55
|
+
}[];
|
|
42
56
|
authProvider: AuthProvider;
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
databases?: DatabaseCredential[];
|
|
58
|
+
projectProvider?: ProjectProvider;
|
|
59
|
+
adapter?: StorageAdapter;
|
|
45
60
|
}
|
|
46
61
|
export interface AuthContext {
|
|
47
62
|
userId: string;
|