Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@
"projectChangelogs": true
},
"projectsRelationship": "independent"
}
},
"nxCloudId": "69008e1b106f8b44b91adf92"
}
98 changes: 98 additions & 0 deletions packages/storages-base-nestjs-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
</p>

[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->

## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.

## Project setup

```bash
$ yarn install
```

## Compile and run the project

```bash
# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod
```

## Run tests

```bash
# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov
```

## Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

```bash
$ yarn install -g @nestjs/mau
$ mau deploy
```

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

## Resources

Check out a few resources that may come in handy when working with NestJS:

- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).

## Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).

## Stay in touch

- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)

## License

Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import { Injectable, Module, Type } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { StorageBaseModule } from '../src/storages-base.module';
import {
IStorageAdapter,
StorageBaseModuleOptions,
StorageBaseModuleOptionsFactory,
} from '../src/typings/storage-base-module-options.interface';
import { STORAGE_ADAPTER, STORAGE_MODULE_OPTIONS } from '../src/typings/storages-base-module-providers';
import { StorageService } from '@rytass/storages-base-nestjs-module';
import { StorageGCSService } from '@rytass/storages-adapter-gcs';

const mockInstance = {
url: jest.fn(),
write: jest.fn(),
batchWrite: jest.fn(),
remove: jest.fn(),
isExists: jest.fn(),
};

jest.mock('@rytass/storages-adapter-gcs', () => ({
StorageGCSService: jest.fn(() => mockInstance),
GCSOptions: jest.fn(),
}));

describe('Storages Base Nestjs Module', () => {
@Injectable()
class mockAdapter implements IStorageAdapter {
url = jest.fn(async (key: string, _expires?: number) => `http://mock-url.com/${key}`);

write = jest.fn();
batchWrite = jest.fn();
remove = jest.fn();
isExists = jest.fn();

constructor(_config: unknown) {}
}

beforeEach(() => {
jest.clearAllMocks();
});

const mockOptions = {
adapter: mockAdapter,
config: { test: true },
commonOptions: {
formDataFieldName: 'files',
allowMultiple: true,
MaxFileSizeInBytes: 10 * 1024 * 1024,
defaultPublic: false,
},
};

const mockCommonOptions = {
formDataFieldName: 'files',
allowMultiple: true,
MaxFileSizeInBytes: 10 * 1024 * 1024,
defaultPublic: false,
};

describe('forRoot', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should throw Error if adapter is not provided', async () => {
await expect(
Test.createTestingModule({
imports: [StorageBaseModule.forRoot({ adapter: undefined as unknown as Type<IStorageAdapter>, config: {} })],
}).compile(),
).rejects.toThrow('No storage adapter class was provided in forRoot!');
});

it('should apply default commonOptions when none are provided', async () => {
const options = { ...mockOptions, commonOptions: undefined };

const module: TestingModule = await Test.createTestingModule({
imports: [StorageBaseModule.forRoot(options)],
}).compile();

const adapter = module.get(STORAGE_ADAPTER);
const service = module.get(StorageService);

expect(adapter).toBeInstanceOf(mockAdapter);

expect(service.commonOptions).toEqual(mockCommonOptions);
});

it('should merge provided commonOptions with defaults', async () => {
const options = { ...mockOptions, commonOptions: { MaxFileSizeInBytes: 999 } };

const module: TestingModule = await Test.createTestingModule({
imports: [StorageBaseModule.forRoot(options)],
}).compile();

const service = module.get(StorageService);

expect(service.commonOptions).toEqual({
...mockCommonOptions,
MaxFileSizeInBytes: 999,
});
});

it('should work with real adapter class', async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
StorageBaseModule.forRoot({
adapter: StorageGCSService,
config: {
bucket: 'test-bucket',
projectId: 'test-project-id',
credentials: {
client_email: 'test-client-email',
private_key: 'test-private-key',
},
},
}),
],
}).compile();

const service = module.get(StorageService);
const adapter = module.get(STORAGE_ADAPTER);

expect(adapter).toBe(mockInstance);
expect(service).toBeDefined();
});
});

describe('forRootAsync', () => {
@Injectable()
class MockConfigService implements StorageBaseModuleOptionsFactory<Type<IStorageAdapter>> {
createStorageBaseModuleOptions(): StorageBaseModuleOptions<Type<IStorageAdapter>> {
return mockOptions;
}
}

@Module({
providers: [MockConfigService],
exports: [MockConfigService],
})
class MockConfigModule {}

it('should throw Error if adapter is not provided', async () => {
await expect(
Test.createTestingModule({
imports: [
StorageBaseModule.forRootAsync({
useFactory: () => ({
adapter: undefined as unknown as Type<IStorageAdapter>,
config: {},
}),
}),
],
}).compile(),
).rejects.toThrow('No storage adapter class was provided in forRootAsync!');
});

it('should work with useFactory', async () => {
const options = mockOptions;

const module: TestingModule = await Test.createTestingModule({
imports: [
StorageBaseModule.forRootAsync({
useFactory: () => options,
}),
],
}).compile();

const adapter = module.get(STORAGE_ADAPTER);
const service = module.get(StorageService);

expect(adapter).toBeInstanceOf(mockAdapter);
expect(module.get(STORAGE_MODULE_OPTIONS)).toEqual(options);
expect(service.commonOptions).toEqual(mockCommonOptions);
});

it('should work with useClass', async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
StorageBaseModule.forRootAsync({
useClass: MockConfigService,
}),
],
providers: [MockConfigService],
}).compile();

const adapter = module.get(STORAGE_ADAPTER);
const service = module.get(StorageService);

expect(adapter).toBeInstanceOf(mockAdapter);
expect(service.commonOptions).toEqual(mockCommonOptions);
});

it('should work with useExisting', async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
MockConfigModule,
StorageBaseModule.forRootAsync({
imports: [MockConfigModule],
useExisting: MockConfigService,
}),
],
}).compile();

const adapter = module.get(STORAGE_ADAPTER);
const service = module.get(StorageService);

expect(adapter).toBeInstanceOf(mockAdapter);
expect(service.commonOptions).toEqual(mockCommonOptions);
});

it('should throw an error if no async provider (useFactory, useClass, or useExisting) is given', async () => {
await expect(
Test.createTestingModule({
imports: [
StorageBaseModule.forRootAsync({
imports: [],
}),
],
}).compile(),
).rejects.toThrow();
});
});
});
Loading
Loading