@xnestjs/storage 1.0.1 → 1.0.2

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.
Files changed (35) hide show
  1. package/LICENSE +21 -0
  2. package/cjs/index.js +9 -0
  3. package/cjs/interfaces/connection.interfaces.js +2 -0
  4. package/cjs/interfaces/storage.interfaces.js +2 -0
  5. package/cjs/package.json +3 -0
  6. package/cjs/providers/s3-storage-connection.js +95 -0
  7. package/cjs/services/storage-bucket.js +28 -0
  8. package/cjs/services/storage-connection.js +10 -0
  9. package/cjs/storage-core.module.js +71 -0
  10. package/cjs/storage.constants.js +4 -0
  11. package/cjs/storage.module.js +27 -0
  12. package/cjs/storage.utils.js +12 -0
  13. package/esm/index.js +6 -0
  14. package/esm/interfaces/connection.interfaces.js +1 -0
  15. package/esm/interfaces/storage.interfaces.js +1 -0
  16. package/esm/package.json +3 -0
  17. package/esm/providers/s3-storage-connection.js +90 -0
  18. package/esm/services/storage-bucket.js +24 -0
  19. package/esm/services/storage-connection.js +6 -0
  20. package/esm/storage-core.module.js +68 -0
  21. package/esm/storage.constants.js +1 -0
  22. package/esm/storage.module.js +24 -0
  23. package/esm/storage.utils.js +9 -0
  24. package/package.json +2 -23
  25. package/types/index.d.cts +6 -0
  26. package/types/index.d.ts +6 -0
  27. package/types/interfaces/connection.interfaces.d.ts +23 -0
  28. package/types/interfaces/storage.interfaces.d.ts +28 -0
  29. package/types/providers/s3-storage-connection.d.ts +17 -0
  30. package/types/services/storage-bucket.d.ts +15 -0
  31. package/types/services/storage-connection.d.ts +19 -0
  32. package/types/storage-core.module.d.ts +6 -0
  33. package/types/storage.constants.d.ts +1 -0
  34. package/types/storage.module.d.ts +6 -0
  35. package/types/storage.utils.d.ts +3 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/cjs/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./interfaces/connection.interfaces.js"), exports);
5
+ tslib_1.__exportStar(require("./interfaces/storage.interfaces.js"), exports);
6
+ tslib_1.__exportStar(require("./services/storage-bucket.js"), exports);
7
+ tslib_1.__exportStar(require("./services/storage-connection.js"), exports);
8
+ tslib_1.__exportStar(require("./storage.constants.js"), exports);
9
+ tslib_1.__exportStar(require("./storage.module.js"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.S3StorageConnection = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const Minio = tslib_1.__importStar(require("minio"));
6
+ const storage_connection_js_1 = require("../services/storage-connection.js");
7
+ class S3StorageConnection extends storage_connection_js_1.StorageConnection {
8
+ constructor(config) {
9
+ super();
10
+ this.provider = 's3';
11
+ this.config = config;
12
+ this._client = new Minio.Client(config);
13
+ this._client.setRequestOptions({ rejectUnauthorized: config.rejectUnauthorized });
14
+ }
15
+ async putObject(bucketName, objectName, source, options) {
16
+ const meta = options ? updateMetadata(options?.metadata, options) : {};
17
+ if (typeof source === 'string')
18
+ await this._client.fPutObject(bucketName, objectName, source, meta);
19
+ else
20
+ await this._client.putObject(bucketName, objectName, source, undefined, meta);
21
+ }
22
+ async getObjectInfo(bucketName, objectName) {
23
+ const o = await this._client.statObject(bucketName, objectName);
24
+ const inf = {
25
+ size: o.size,
26
+ etag: o.etag,
27
+ lastModified: o.lastModified,
28
+ metadata: o.metaData,
29
+ };
30
+ if (inf.metadata) {
31
+ const meta = inf.metadata;
32
+ removeKey(meta, 'content-length');
33
+ removeKey(meta, 'last-modified');
34
+ if (meta['Content-Type']) {
35
+ inf.contentLanguage = meta['Content-Type'];
36
+ removeKey(meta, 'content-type');
37
+ }
38
+ if (meta['Content-Language']) {
39
+ inf.contentLanguage = meta['Content-Language'];
40
+ removeKey(meta, 'content-language');
41
+ }
42
+ if (meta['Content-Encoding']) {
43
+ inf.contentEncoding = meta['Content-Encoding'];
44
+ removeKey(meta, 'content-encoding');
45
+ }
46
+ if (meta['Content-Name']) {
47
+ inf.originalName = meta['Content-Name'];
48
+ removeKey(meta, 'content-name');
49
+ }
50
+ }
51
+ return inf;
52
+ }
53
+ async removeObject(bucketName, objectName) {
54
+ await this._client.removeObject(bucketName, objectName);
55
+ }
56
+ async getFile(bucketName, objectName, filePath) {
57
+ await this._client.fGetObject(bucketName, objectName, filePath);
58
+ }
59
+ getObject(bucketName, objectName) {
60
+ return this._client.getObject(bucketName, objectName);
61
+ }
62
+ presignedGetObject(bucketName, objectName, options) {
63
+ const expires = options?.expires || 5 * 60 * 60; // 5 minutes
64
+ return this._client.presignedGetObject(bucketName, objectName, expires);
65
+ }
66
+ }
67
+ exports.S3StorageConnection = S3StorageConnection;
68
+ function updateMetadata(meta, options) {
69
+ if (!options)
70
+ return meta;
71
+ const newMeta = { ...meta };
72
+ if (options.contentType) {
73
+ removeKey(newMeta, 'content-type');
74
+ newMeta['Content-Type'] = options.contentType;
75
+ }
76
+ if (options.contentLanguage) {
77
+ removeKey(newMeta, 'content-language');
78
+ newMeta['Content-Language'] = options.contentLanguage;
79
+ }
80
+ if (options.contentEncoding) {
81
+ removeKey(newMeta, 'content-encoding');
82
+ newMeta['Content-Encoding'] = options.contentLanguage;
83
+ }
84
+ if (options.originalName) {
85
+ removeKey(newMeta, 'content-name');
86
+ newMeta['Content-Name'] = options.originalName;
87
+ }
88
+ return newMeta;
89
+ }
90
+ function removeKey(obj, key) {
91
+ key = key.toLowerCase();
92
+ const k = Object.keys(obj).find(x => x.toLowerCase() === key);
93
+ if (k)
94
+ delete obj[k];
95
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageBucket = void 0;
4
+ class StorageBucket {
5
+ constructor(_connection, _bucketName) {
6
+ this._connection = _connection;
7
+ this._bucketName = _bucketName;
8
+ }
9
+ putObject(objectName, buffer, options) {
10
+ return this._connection.putObject(this._bucketName, objectName, buffer, options);
11
+ }
12
+ getObjectInfo(objectName) {
13
+ return this._connection.getObjectInfo(this._bucketName, objectName);
14
+ }
15
+ removeObject(objectName) {
16
+ return this._connection.removeObject(this._bucketName, objectName);
17
+ }
18
+ getFile(objectName, filePath) {
19
+ return this._connection.getFile(this._bucketName, objectName, filePath);
20
+ }
21
+ getObject(objectName) {
22
+ return this._connection.getObject(this._bucketName, objectName);
23
+ }
24
+ presignedGetObject(objectName, options) {
25
+ return this._connection.presignedGetObject(this._bucketName, objectName, options);
26
+ }
27
+ }
28
+ exports.StorageBucket = StorageBucket;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageConnection = void 0;
4
+ const storage_bucket_js_1 = require("./storage-bucket.js");
5
+ class StorageConnection {
6
+ getBucket(bucketName) {
7
+ return new storage_bucket_js_1.StorageBucket(this, bucketName);
8
+ }
9
+ }
10
+ exports.StorageConnection = StorageConnection;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var StorageCoreModule_1;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.StorageCoreModule = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const storage_connection_js_1 = require("./services/storage-connection.js");
8
+ const storage_constants_js_1 = require("./storage.constants.js");
9
+ const storage_utils_js_1 = require("./storage.utils.js");
10
+ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
11
+ static register(options) {
12
+ const connectionProvider = {
13
+ provide: options.token || storage_connection_js_1.StorageConnection,
14
+ useValue: (0, storage_utils_js_1.createConnection)(options),
15
+ };
16
+ return {
17
+ module: StorageCoreModule_1,
18
+ providers: [connectionProvider],
19
+ exports: [connectionProvider],
20
+ global: options.global,
21
+ };
22
+ }
23
+ static registerAsync(asyncOptions) {
24
+ let optionsProvider;
25
+ if (asyncOptions.useFactory) {
26
+ optionsProvider = {
27
+ provide: storage_constants_js_1.STORAGE_OPTIONS,
28
+ inject: asyncOptions.inject || [],
29
+ useFactory: asyncOptions.useFactory,
30
+ };
31
+ }
32
+ else if (asyncOptions.useExisting) {
33
+ optionsProvider = {
34
+ provide: storage_constants_js_1.STORAGE_OPTIONS,
35
+ useExisting: asyncOptions.useExisting,
36
+ };
37
+ }
38
+ else if (asyncOptions.useClass) {
39
+ optionsProvider = {
40
+ provide: storage_constants_js_1.STORAGE_OPTIONS,
41
+ useClass: asyncOptions.useClass,
42
+ };
43
+ }
44
+ else {
45
+ throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
46
+ }
47
+ const storageToken = asyncOptions.token || storage_connection_js_1.StorageConnection;
48
+ const providers = [
49
+ optionsProvider,
50
+ {
51
+ provide: storageToken,
52
+ inject: [storage_constants_js_1.STORAGE_OPTIONS],
53
+ useFactory: (options) => (0, storage_utils_js_1.createConnection)(options),
54
+ },
55
+ ];
56
+ if (asyncOptions.providers)
57
+ providers.push(...asyncOptions.providers);
58
+ return {
59
+ module: StorageCoreModule_1,
60
+ imports: asyncOptions.imports || [],
61
+ exports: [storageToken, ...(asyncOptions.exports || [])],
62
+ providers,
63
+ global: asyncOptions.global,
64
+ };
65
+ }
66
+ };
67
+ exports.StorageCoreModule = StorageCoreModule;
68
+ exports.StorageCoreModule = StorageCoreModule = StorageCoreModule_1 = tslib_1.__decorate([
69
+ (0, common_1.Global)(),
70
+ (0, common_1.Module)({})
71
+ ], StorageCoreModule);
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STORAGE_OPTIONS = void 0;
4
+ exports.STORAGE_OPTIONS = Symbol('STORAGE_OPTIONS');
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var StorageModule_1;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.StorageModule = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const storage_core_module_js_1 = require("./storage-core.module.js");
8
+ let StorageModule = StorageModule_1 = class StorageModule {
9
+ static register(options) {
10
+ return {
11
+ module: StorageModule_1,
12
+ imports: [storage_core_module_js_1.StorageCoreModule.register(options)],
13
+ global: options.global,
14
+ };
15
+ }
16
+ static registerAsync(options) {
17
+ return {
18
+ module: StorageModule_1,
19
+ imports: [storage_core_module_js_1.StorageCoreModule.registerAsync(options)],
20
+ global: options.global,
21
+ };
22
+ }
23
+ };
24
+ exports.StorageModule = StorageModule;
25
+ exports.StorageModule = StorageModule = StorageModule_1 = tslib_1.__decorate([
26
+ (0, common_1.Module)({})
27
+ ], StorageModule);
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createConnection = createConnection;
4
+ const s3_storage_connection_js_1 = require("./providers/s3-storage-connection.js");
5
+ function createConnection(options) {
6
+ if (options.provider === 's3') {
7
+ if (!options.s3)
8
+ throw new Error('You must provide S3 config');
9
+ return new s3_storage_connection_js_1.S3StorageConnection(options.s3);
10
+ }
11
+ throw new Error(`Unknown Storage provider (${options.provider})`);
12
+ }
package/esm/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './interfaces/connection.interfaces.js';
2
+ export * from './interfaces/storage.interfaces.js';
3
+ export * from './services/storage-bucket.js';
4
+ export * from './services/storage-connection.js';
5
+ export * from './storage.constants.js';
6
+ export * from './storage.module.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,90 @@
1
+ import * as Minio from 'minio';
2
+ import { StorageConnection } from '../services/storage-connection.js';
3
+ export class S3StorageConnection extends StorageConnection {
4
+ constructor(config) {
5
+ super();
6
+ this.provider = 's3';
7
+ this.config = config;
8
+ this._client = new Minio.Client(config);
9
+ this._client.setRequestOptions({ rejectUnauthorized: config.rejectUnauthorized });
10
+ }
11
+ async putObject(bucketName, objectName, source, options) {
12
+ const meta = options ? updateMetadata(options?.metadata, options) : {};
13
+ if (typeof source === 'string')
14
+ await this._client.fPutObject(bucketName, objectName, source, meta);
15
+ else
16
+ await this._client.putObject(bucketName, objectName, source, undefined, meta);
17
+ }
18
+ async getObjectInfo(bucketName, objectName) {
19
+ const o = await this._client.statObject(bucketName, objectName);
20
+ const inf = {
21
+ size: o.size,
22
+ etag: o.etag,
23
+ lastModified: o.lastModified,
24
+ metadata: o.metaData,
25
+ };
26
+ if (inf.metadata) {
27
+ const meta = inf.metadata;
28
+ removeKey(meta, 'content-length');
29
+ removeKey(meta, 'last-modified');
30
+ if (meta['Content-Type']) {
31
+ inf.contentLanguage = meta['Content-Type'];
32
+ removeKey(meta, 'content-type');
33
+ }
34
+ if (meta['Content-Language']) {
35
+ inf.contentLanguage = meta['Content-Language'];
36
+ removeKey(meta, 'content-language');
37
+ }
38
+ if (meta['Content-Encoding']) {
39
+ inf.contentEncoding = meta['Content-Encoding'];
40
+ removeKey(meta, 'content-encoding');
41
+ }
42
+ if (meta['Content-Name']) {
43
+ inf.originalName = meta['Content-Name'];
44
+ removeKey(meta, 'content-name');
45
+ }
46
+ }
47
+ return inf;
48
+ }
49
+ async removeObject(bucketName, objectName) {
50
+ await this._client.removeObject(bucketName, objectName);
51
+ }
52
+ async getFile(bucketName, objectName, filePath) {
53
+ await this._client.fGetObject(bucketName, objectName, filePath);
54
+ }
55
+ getObject(bucketName, objectName) {
56
+ return this._client.getObject(bucketName, objectName);
57
+ }
58
+ presignedGetObject(bucketName, objectName, options) {
59
+ const expires = options?.expires || 5 * 60 * 60; // 5 minutes
60
+ return this._client.presignedGetObject(bucketName, objectName, expires);
61
+ }
62
+ }
63
+ function updateMetadata(meta, options) {
64
+ if (!options)
65
+ return meta;
66
+ const newMeta = { ...meta };
67
+ if (options.contentType) {
68
+ removeKey(newMeta, 'content-type');
69
+ newMeta['Content-Type'] = options.contentType;
70
+ }
71
+ if (options.contentLanguage) {
72
+ removeKey(newMeta, 'content-language');
73
+ newMeta['Content-Language'] = options.contentLanguage;
74
+ }
75
+ if (options.contentEncoding) {
76
+ removeKey(newMeta, 'content-encoding');
77
+ newMeta['Content-Encoding'] = options.contentLanguage;
78
+ }
79
+ if (options.originalName) {
80
+ removeKey(newMeta, 'content-name');
81
+ newMeta['Content-Name'] = options.originalName;
82
+ }
83
+ return newMeta;
84
+ }
85
+ function removeKey(obj, key) {
86
+ key = key.toLowerCase();
87
+ const k = Object.keys(obj).find(x => x.toLowerCase() === key);
88
+ if (k)
89
+ delete obj[k];
90
+ }
@@ -0,0 +1,24 @@
1
+ export class StorageBucket {
2
+ constructor(_connection, _bucketName) {
3
+ this._connection = _connection;
4
+ this._bucketName = _bucketName;
5
+ }
6
+ putObject(objectName, buffer, options) {
7
+ return this._connection.putObject(this._bucketName, objectName, buffer, options);
8
+ }
9
+ getObjectInfo(objectName) {
10
+ return this._connection.getObjectInfo(this._bucketName, objectName);
11
+ }
12
+ removeObject(objectName) {
13
+ return this._connection.removeObject(this._bucketName, objectName);
14
+ }
15
+ getFile(objectName, filePath) {
16
+ return this._connection.getFile(this._bucketName, objectName, filePath);
17
+ }
18
+ getObject(objectName) {
19
+ return this._connection.getObject(this._bucketName, objectName);
20
+ }
21
+ presignedGetObject(objectName, options) {
22
+ return this._connection.presignedGetObject(this._bucketName, objectName, options);
23
+ }
24
+ }
@@ -0,0 +1,6 @@
1
+ import { StorageBucket } from './storage-bucket.js';
2
+ export class StorageConnection {
3
+ getBucket(bucketName) {
4
+ return new StorageBucket(this, bucketName);
5
+ }
6
+ }
@@ -0,0 +1,68 @@
1
+ var StorageCoreModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Global, Module } from '@nestjs/common';
4
+ import { StorageConnection } from './services/storage-connection.js';
5
+ import { STORAGE_OPTIONS } from './storage.constants.js';
6
+ import { createConnection } from './storage.utils.js';
7
+ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
8
+ static register(options) {
9
+ const connectionProvider = {
10
+ provide: options.token || StorageConnection,
11
+ useValue: createConnection(options),
12
+ };
13
+ return {
14
+ module: StorageCoreModule_1,
15
+ providers: [connectionProvider],
16
+ exports: [connectionProvider],
17
+ global: options.global,
18
+ };
19
+ }
20
+ static registerAsync(asyncOptions) {
21
+ let optionsProvider;
22
+ if (asyncOptions.useFactory) {
23
+ optionsProvider = {
24
+ provide: STORAGE_OPTIONS,
25
+ inject: asyncOptions.inject || [],
26
+ useFactory: asyncOptions.useFactory,
27
+ };
28
+ }
29
+ else if (asyncOptions.useExisting) {
30
+ optionsProvider = {
31
+ provide: STORAGE_OPTIONS,
32
+ useExisting: asyncOptions.useExisting,
33
+ };
34
+ }
35
+ else if (asyncOptions.useClass) {
36
+ optionsProvider = {
37
+ provide: STORAGE_OPTIONS,
38
+ useClass: asyncOptions.useClass,
39
+ };
40
+ }
41
+ else {
42
+ throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
43
+ }
44
+ const storageToken = asyncOptions.token || StorageConnection;
45
+ const providers = [
46
+ optionsProvider,
47
+ {
48
+ provide: storageToken,
49
+ inject: [STORAGE_OPTIONS],
50
+ useFactory: (options) => createConnection(options),
51
+ },
52
+ ];
53
+ if (asyncOptions.providers)
54
+ providers.push(...asyncOptions.providers);
55
+ return {
56
+ module: StorageCoreModule_1,
57
+ imports: asyncOptions.imports || [],
58
+ exports: [storageToken, ...(asyncOptions.exports || [])],
59
+ providers,
60
+ global: asyncOptions.global,
61
+ };
62
+ }
63
+ };
64
+ StorageCoreModule = StorageCoreModule_1 = __decorate([
65
+ Global(),
66
+ Module({})
67
+ ], StorageCoreModule);
68
+ export { StorageCoreModule };
@@ -0,0 +1 @@
1
+ export const STORAGE_OPTIONS = Symbol('STORAGE_OPTIONS');
@@ -0,0 +1,24 @@
1
+ var StorageModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Module } from '@nestjs/common';
4
+ import { StorageCoreModule } from './storage-core.module.js';
5
+ let StorageModule = StorageModule_1 = class StorageModule {
6
+ static register(options) {
7
+ return {
8
+ module: StorageModule_1,
9
+ imports: [StorageCoreModule.register(options)],
10
+ global: options.global,
11
+ };
12
+ }
13
+ static registerAsync(options) {
14
+ return {
15
+ module: StorageModule_1,
16
+ imports: [StorageCoreModule.registerAsync(options)],
17
+ global: options.global,
18
+ };
19
+ }
20
+ };
21
+ StorageModule = StorageModule_1 = __decorate([
22
+ Module({})
23
+ ], StorageModule);
24
+ export { StorageModule };
@@ -0,0 +1,9 @@
1
+ import { S3StorageConnection } from './providers/s3-storage-connection.js';
2
+ export function createConnection(options) {
3
+ if (options.provider === 's3') {
4
+ if (!options.s3)
5
+ throw new Error('You must provide S3 config');
6
+ return new S3StorageConnection(options.s3);
7
+ }
8
+ throw new Error(`Unknown Storage provider (${options.provider})`);
9
+ }
package/package.json CHANGED
@@ -1,36 +1,15 @@
1
1
  {
2
2
  "name": "@xnestjs/storage",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "NestJS extension library for Storage solutions (S3,GS)",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
- "scripts": {
8
- "compile": "tsc --noEmit",
9
- "prebuild": "npm run lint && npm run clean",
10
- "build": "npm run build:cjs && npm run build:esm",
11
- "build:cjs": "tsc -b tsconfig-build-cjs.json && cp ../../support/package.cjs.json ./build/cjs/package.json",
12
- "build:esm": "tsc -b tsconfig-build-esm.json && cp ../../support/package.esm.json ./build/esm/package.json",
13
- "postbuild": "cp README.md ../../LICENSE ./build && node ../../support/postbuild.cjs",
14
- "lint": "eslint . --max-warnings=0",
15
- "lint:fix": "eslint . --max-warnings=0 --fix",
16
- "format": "prettier . --write --log-level=warn",
17
- "check": "madge --circular src/**",
18
- "test": "jest",
19
- "cover": "jest --collect-coverage",
20
- "clean": "npm run clean:src && npm run clean:build && npm run clean:cover",
21
- "clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
22
- "clean:build": "rimraf build",
23
- "clean:cover": "rimraf coverage"
24
- },
25
7
  "peerDependencies": {
26
8
  "@nestjs/common": "^10.4.15"
27
9
  },
28
10
  "optionalDependencies": {
29
11
  "minio": "^8.0.3"
30
12
  },
31
- "devDependencies": {
32
- "@nestjs/testing": "^10.4.15"
33
- },
34
13
  "type": "module",
35
14
  "exports": {
36
15
  ".": {
@@ -69,4 +48,4 @@
69
48
  "storage",
70
49
  "s3"
71
50
  ]
72
- }
51
+ }
@@ -0,0 +1,6 @@
1
+ export * from './interfaces/connection.interfaces.js';
2
+ export * from './interfaces/storage.interfaces.js';
3
+ export * from './services/storage-bucket.js';
4
+ export * from './services/storage-connection.js';
5
+ export * from './storage.constants.js';
6
+ export * from './storage.module.js';
@@ -0,0 +1,6 @@
1
+ export * from './interfaces/connection.interfaces.js';
2
+ export * from './interfaces/storage.interfaces.js';
3
+ export * from './services/storage-bucket.js';
4
+ export * from './services/storage-connection.js';
5
+ export * from './storage.constants.js';
6
+ export * from './storage.module.js';
@@ -0,0 +1,23 @@
1
+ export type FileMetadata = Record<string, any>;
2
+ export interface ObjectInfo {
3
+ originalName?: string;
4
+ contentType?: string;
5
+ contentLanguage?: string;
6
+ contentEncoding?: string;
7
+ size: number;
8
+ etag: string;
9
+ lastModified: Date;
10
+ metadata: Record<string, any>;
11
+ }
12
+ export interface PutObjectOptions {
13
+ originalName?: string;
14
+ contentType?: string;
15
+ contentLanguage?: string;
16
+ contentEncoding?: string;
17
+ metadata?: FileMetadata;
18
+ }
19
+ export interface SignedUrlOptions {
20
+ expires?: number;
21
+ }
22
+ export interface GetObjectSignedUrlOptions extends SignedUrlOptions {
23
+ }
@@ -0,0 +1,28 @@
1
+ import type { InjectionToken } from '@nestjs/common';
2
+ import type { ModuleMetadata, Type } from '@nestjs/common/interfaces';
3
+ import type * as minio from 'minio';
4
+ export type S3Config = minio.ClientOptions & {
5
+ rejectUnauthorized?: boolean;
6
+ };
7
+ export type StorageProvider = 's3' | 'gs';
8
+ export interface S3StorageOptions {
9
+ provider: StorageProvider;
10
+ s3: S3Config;
11
+ }
12
+ export interface GSStorageOptions {
13
+ provider: StorageProvider;
14
+ gs: {};
15
+ }
16
+ export type StorageOptions = S3StorageOptions | GSStorageOptions;
17
+ export type StorageModuleOptions = StorageOptions & {
18
+ token?: InjectionToken;
19
+ global?: boolean;
20
+ };
21
+ export interface StorageModuleAsyncOptions extends Pick<ModuleMetadata, 'imports' | 'exports' | 'providers'> {
22
+ token?: InjectionToken;
23
+ inject?: any[];
24
+ global?: boolean;
25
+ useClass?: Type<StorageOptions>;
26
+ useExisting?: InjectionToken;
27
+ useFactory?: (...args: any[]) => Promise<StorageOptions> | StorageOptions;
28
+ }
@@ -0,0 +1,17 @@
1
+ import { Buffer } from 'buffer';
2
+ import { Readable } from 'stream';
3
+ import { GetObjectSignedUrlOptions, ObjectInfo, PutObjectOptions } from '../interfaces/connection.interfaces.js';
4
+ import { S3Config } from '../interfaces/storage.interfaces.js';
5
+ import { StorageConnection } from '../services/storage-connection.js';
6
+ export declare class S3StorageConnection extends StorageConnection {
7
+ readonly provider = "s3";
8
+ readonly config: S3Config;
9
+ private _client;
10
+ constructor(config: S3Config);
11
+ putObject(bucketName: string, objectName: string, source: Buffer | Readable | string, options?: PutObjectOptions): Promise<void>;
12
+ getObjectInfo(bucketName: string, objectName: string): Promise<ObjectInfo>;
13
+ removeObject(bucketName: string, objectName: string): Promise<void>;
14
+ getFile(bucketName: string, objectName: string, filePath: string): Promise<void>;
15
+ getObject(bucketName: string, objectName: string): Promise<Readable>;
16
+ presignedGetObject(bucketName: string, objectName: string, options?: GetObjectSignedUrlOptions): Promise<string>;
17
+ }
@@ -0,0 +1,15 @@
1
+ import type { Buffer } from 'buffer';
2
+ import type { Readable } from 'stream';
3
+ import type { GetObjectSignedUrlOptions, ObjectInfo, PutObjectOptions } from '../interfaces/connection.interfaces.js';
4
+ import type { StorageConnection } from './storage-connection';
5
+ export declare class StorageBucket {
6
+ private _connection;
7
+ private _bucketName;
8
+ constructor(_connection: StorageConnection, _bucketName: string);
9
+ putObject(objectName: string, buffer: Buffer | Readable | string, options?: PutObjectOptions): Promise<void>;
10
+ getObjectInfo(objectName: string): Promise<ObjectInfo>;
11
+ removeObject(objectName: string): Promise<void>;
12
+ getFile(objectName: string, filePath: string): Promise<void>;
13
+ getObject(objectName: string): Promise<Readable>;
14
+ presignedGetObject(objectName: string, options?: GetObjectSignedUrlOptions): Promise<string>;
15
+ }
@@ -0,0 +1,19 @@
1
+ import type { Buffer } from 'buffer';
2
+ import type { Readable } from 'stream';
3
+ import type { GetObjectSignedUrlOptions, ObjectInfo, PutObjectOptions } from '../interfaces/connection.interfaces.js';
4
+ import type { StorageProvider } from '../interfaces/storage.interfaces.js';
5
+ import { StorageBucket } from './storage-bucket.js';
6
+ export declare abstract class StorageConnection {
7
+ abstract readonly provider: StorageProvider;
8
+ abstract readonly config: any;
9
+ getBucket(bucketName: string): StorageBucket;
10
+ abstract putObject(bucketName: string, objectName: string, buffer: Buffer, options?: PutObjectOptions): Promise<void>;
11
+ abstract putObject(bucketName: string, objectName: string, stream: Readable, options?: PutObjectOptions): Promise<void>;
12
+ abstract putObject(bucketName: string, objectName: string, filePath: string, options?: PutObjectOptions): Promise<void>;
13
+ abstract putObject(bucketName: string, objectName: string, source: Buffer | Readable | string, options?: PutObjectOptions): Promise<void>;
14
+ abstract getObjectInfo(bucketName: string, objectName: string): Promise<ObjectInfo>;
15
+ abstract removeObject(bucketName: string, objectName: string): Promise<void>;
16
+ abstract getFile(bucketName: string, objectName: string, filePath: string): Promise<void>;
17
+ abstract getObject(bucketName: string, objectName: string): Promise<Readable>;
18
+ abstract presignedGetObject(bucketName: string, objectName: string, options?: GetObjectSignedUrlOptions): Promise<string>;
19
+ }
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { StorageModuleAsyncOptions, StorageModuleOptions } from './interfaces/storage.interfaces.js';
3
+ export declare class StorageCoreModule {
4
+ static register(options: StorageModuleOptions): DynamicModule;
5
+ static registerAsync(asyncOptions: StorageModuleAsyncOptions): DynamicModule;
6
+ }
@@ -0,0 +1 @@
1
+ export declare const STORAGE_OPTIONS: unique symbol;
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { StorageModuleAsyncOptions, StorageModuleOptions } from './interfaces/storage.interfaces.js';
3
+ export declare class StorageModule {
4
+ static register(options: StorageModuleOptions): DynamicModule;
5
+ static registerAsync(options: StorageModuleAsyncOptions): DynamicModule;
6
+ }
@@ -0,0 +1,3 @@
1
+ import { StorageOptions } from './interfaces/storage.interfaces.js';
2
+ import { StorageConnection } from './services/storage-connection.js';
3
+ export declare function createConnection(options: StorageOptions): StorageConnection;