@xnestjs/storage 0.10.4 → 1.0.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.
@@ -5,10 +5,12 @@ const tslib_1 = require("tslib");
5
5
  const Minio = tslib_1.__importStar(require("minio"));
6
6
  const storage_connection_js_1 = require("../services/storage-connection.js");
7
7
  class S3StorageConnection extends storage_connection_js_1.StorageConnection {
8
- constructor(options) {
8
+ constructor(config) {
9
9
  super();
10
- this._client = new Minio.Client(options);
11
- this._client.setRequestOptions({ rejectUnauthorized: options.rejectUnauthorized });
10
+ this.provider = 's3';
11
+ this.config = config;
12
+ this._client = new Minio.Client(config);
13
+ this._client.setRequestOptions({ rejectUnauthorized: config.rejectUnauthorized });
12
14
  }
13
15
  async putObject(bucketName, objectName, source, options) {
14
16
  const meta = options ? updateMetadata(options?.metadata, options) : {};
@@ -44,10 +44,11 @@ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
44
44
  else {
45
45
  throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
46
46
  }
47
+ const storageToken = asyncOptions.token || storage_connection_js_1.StorageConnection;
47
48
  const providers = [
48
49
  optionsProvider,
49
50
  {
50
- provide: asyncOptions.token || storage_connection_js_1.StorageConnection,
51
+ provide: storageToken,
51
52
  inject: [storage_constants_js_1.STORAGE_OPTIONS],
52
53
  useFactory: (options) => (0, storage_utils_js_1.createConnection)(options),
53
54
  },
@@ -57,7 +58,7 @@ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
57
58
  return {
58
59
  module: StorageCoreModule_1,
59
60
  imports: asyncOptions.imports || [],
60
- exports: asyncOptions.exports || [],
61
+ exports: [storageToken, ...(asyncOptions.exports || [])],
61
62
  providers,
62
63
  global: asyncOptions.global,
63
64
  };
@@ -3,13 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createConnection = createConnection;
4
4
  const s3_storage_connection_js_1 = require("./providers/s3-storage-connection.js");
5
5
  function createConnection(options) {
6
- switch (options.type) {
7
- case 's3': {
8
- if (!options.s3)
9
- throw new Error('You must provide S3 config');
10
- return new s3_storage_connection_js_1.S3StorageConnection(options.s3);
11
- }
12
- default:
13
- throw new Error(`Unknown Storage provider type ${options.type}`);
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);
14
10
  }
11
+ throw new Error(`Unknown Storage provider (${options.provider})`);
15
12
  }
@@ -1,10 +1,12 @@
1
1
  import * as Minio from 'minio';
2
2
  import { StorageConnection } from '../services/storage-connection.js';
3
3
  export class S3StorageConnection extends StorageConnection {
4
- constructor(options) {
4
+ constructor(config) {
5
5
  super();
6
- this._client = new Minio.Client(options);
7
- this._client.setRequestOptions({ rejectUnauthorized: options.rejectUnauthorized });
6
+ this.provider = 's3';
7
+ this.config = config;
8
+ this._client = new Minio.Client(config);
9
+ this._client.setRequestOptions({ rejectUnauthorized: config.rejectUnauthorized });
8
10
  }
9
11
  async putObject(bucketName, objectName, source, options) {
10
12
  const meta = options ? updateMetadata(options?.metadata, options) : {};
@@ -41,10 +41,11 @@ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
41
41
  else {
42
42
  throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
43
43
  }
44
+ const storageToken = asyncOptions.token || StorageConnection;
44
45
  const providers = [
45
46
  optionsProvider,
46
47
  {
47
- provide: asyncOptions.token || StorageConnection,
48
+ provide: storageToken,
48
49
  inject: [STORAGE_OPTIONS],
49
50
  useFactory: (options) => createConnection(options),
50
51
  },
@@ -54,7 +55,7 @@ let StorageCoreModule = StorageCoreModule_1 = class StorageCoreModule {
54
55
  return {
55
56
  module: StorageCoreModule_1,
56
57
  imports: asyncOptions.imports || [],
57
- exports: asyncOptions.exports || [],
58
+ exports: [storageToken, ...(asyncOptions.exports || [])],
58
59
  providers,
59
60
  global: asyncOptions.global,
60
61
  };
@@ -1,12 +1,9 @@
1
1
  import { S3StorageConnection } from './providers/s3-storage-connection.js';
2
2
  export function createConnection(options) {
3
- switch (options.type) {
4
- case 's3': {
5
- if (!options.s3)
6
- throw new Error('You must provide S3 config');
7
- return new S3StorageConnection(options.s3);
8
- }
9
- default:
10
- throw new Error(`Unknown Storage provider type ${options.type}`);
3
+ if (options.provider === 's3') {
4
+ if (!options.s3)
5
+ throw new Error('You must provide S3 config');
6
+ return new S3StorageConnection(options.s3);
11
7
  }
8
+ throw new Error(`Unknown Storage provider (${options.provider})`);
12
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/storage",
3
- "version": "0.10.4",
3
+ "version": "1.0.0",
4
4
  "description": "NestJS extension library for Storage solutions (S3,GS)",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -4,13 +4,13 @@ import type * as minio from 'minio';
4
4
  export type S3Config = minio.ClientOptions & {
5
5
  rejectUnauthorized?: boolean;
6
6
  };
7
- export type AbstractType<T> = abstract new (...args: any[]) => T;
7
+ export type StorageProvider = 's3' | 'gs';
8
8
  export interface S3StorageOptions {
9
- type: 's3';
9
+ provider: StorageProvider;
10
10
  s3: S3Config;
11
11
  }
12
12
  export interface GSStorageOptions {
13
- type: 'gs';
13
+ provider: StorageProvider;
14
14
  gs: {};
15
15
  }
16
16
  export type StorageOptions = S3StorageOptions | GSStorageOptions;
@@ -18,11 +18,11 @@ export type StorageModuleOptions = StorageOptions & {
18
18
  token?: InjectionToken;
19
19
  global?: boolean;
20
20
  };
21
- export interface StorageModuleAsyncOptions<I extends [any]> extends Pick<ModuleMetadata, 'imports' | 'exports' | 'providers'> {
21
+ export interface StorageModuleAsyncOptions extends Pick<ModuleMetadata, 'imports' | 'exports' | 'providers'> {
22
22
  token?: InjectionToken;
23
- inject?: I;
23
+ inject?: any[];
24
24
  global?: boolean;
25
25
  useClass?: Type<StorageOptions>;
26
26
  useExisting?: InjectionToken;
27
- useFactory?: (...args: I) => Promise<StorageOptions> | StorageOptions;
27
+ useFactory?: (...args: any[]) => Promise<StorageOptions> | StorageOptions;
28
28
  }
@@ -4,8 +4,10 @@ import { GetObjectSignedUrlOptions, ObjectInfo, PutObjectOptions } from '../inte
4
4
  import { S3Config } from '../interfaces/storage.interfaces.js';
5
5
  import { StorageConnection } from '../services/storage-connection.js';
6
6
  export declare class S3StorageConnection extends StorageConnection {
7
+ readonly provider = "s3";
8
+ readonly config: S3Config;
7
9
  private _client;
8
- constructor(options: S3Config);
10
+ constructor(config: S3Config);
9
11
  putObject(bucketName: string, objectName: string, source: Buffer | Readable | string, options?: PutObjectOptions): Promise<void>;
10
12
  getObjectInfo(bucketName: string, objectName: string): Promise<ObjectInfo>;
11
13
  removeObject(bucketName: string, objectName: string): Promise<void>;
@@ -1,8 +1,11 @@
1
1
  import type { Buffer } from 'buffer';
2
2
  import type { Readable } from 'stream';
3
3
  import type { GetObjectSignedUrlOptions, ObjectInfo, PutObjectOptions } from '../interfaces/connection.interfaces.js';
4
+ import type { StorageProvider } from '../interfaces/storage.interfaces.js';
4
5
  import { StorageBucket } from './storage-bucket.js';
5
6
  export declare abstract class StorageConnection {
7
+ abstract readonly provider: StorageProvider;
8
+ abstract readonly config: any;
6
9
  getBucket(bucketName: string): StorageBucket;
7
10
  abstract putObject(bucketName: string, objectName: string, buffer: Buffer, options?: PutObjectOptions): Promise<void>;
8
11
  abstract putObject(bucketName: string, objectName: string, stream: Readable, options?: PutObjectOptions): Promise<void>;
@@ -2,5 +2,5 @@ import { DynamicModule } from '@nestjs/common';
2
2
  import { StorageModuleAsyncOptions, StorageModuleOptions } from './interfaces/storage.interfaces.js';
3
3
  export declare class StorageCoreModule {
4
4
  static register(options: StorageModuleOptions): DynamicModule;
5
- static registerAsync<I extends [any] = never>(asyncOptions: StorageModuleAsyncOptions<I>): DynamicModule;
5
+ static registerAsync(asyncOptions: StorageModuleAsyncOptions): DynamicModule;
6
6
  }
@@ -2,5 +2,5 @@ import { DynamicModule } from '@nestjs/common';
2
2
  import { StorageModuleAsyncOptions, StorageModuleOptions } from './interfaces/storage.interfaces.js';
3
3
  export declare class StorageModule {
4
4
  static register(options: StorageModuleOptions): DynamicModule;
5
- static registerAsync<I extends [any] = never>(options: StorageModuleAsyncOptions<I>): DynamicModule;
5
+ static registerAsync(options: StorageModuleAsyncOptions): DynamicModule;
6
6
  }
@@ -1,3 +1,3 @@
1
- import { StorageModuleOptions } from './interfaces/storage.interfaces.js';
1
+ import { StorageOptions } from './interfaces/storage.interfaces.js';
2
2
  import { StorageConnection } from './services/storage-connection.js';
3
- export declare function createConnection(options: StorageModuleOptions): StorageConnection;
3
+ export declare function createConnection(options: StorageOptions): StorageConnection;