Skip to content

Commit

Permalink
move to adapter factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kpazgan committed Oct 7, 2024
1 parent 0204f6a commit 5745ec9
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 205 deletions.
5 changes: 2 additions & 3 deletions examples/common_nestjs_remix/apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GENERAL
CORS_ORIGIN="https://app.guidebook.localhost"
EMAIL_ADAPTER="mailhog"

FILE_ADAPTER="local"
# DATABASE
DATABASE_URL="postgres://postgres:guidebook@localhost:5432/guidebook"

Expand All @@ -17,11 +17,10 @@ SMTP_USER=
SMTP_PASSWORD=

# AWS
AWS_ENDPOINT=https://s3.eu-central-1.amazonaws.com
AWS_REGION=eu-central-1
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_BUCKET_NAME=test-bucket\
AWS_BUCKET_NAME=test-bucket


# LOCAL UPLOADS
Expand Down
2 changes: 1 addition & 1 deletion examples/common_nestjs_remix/apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { EmailModule } from "./common/emails/emails.module";
import { TestConfigModule } from "./test-config/test-config.module";
import { StagingGuard } from "./common/guards/staging.guard";
import { HealthModule } from "./health/health.module";
import { FilesModule } from "./files/files.module";
import { FilesModule } from "./common/files/files.module";
import localFile from "./common/configuration/local_file";

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const schema = Type.Object({
AWS_ACCESS_KEY_ID: Type.String(),
AWS_SECRET_ACCESS_KEY: Type.String(),
BUCKET_NAME: Type.String(),
AWS_ENDPOINT: Type.String(),
});

type AWSConfigSchema = Static<typeof schema>;
Expand All @@ -20,7 +19,6 @@ export default registerAs("aws", (): AWSConfigSchema => {
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
BUCKET_NAME: process.env.AWS_BUCKET_NAME,
AWS_ENDPOINT: process.env.AWS_ENDPOINT,
};

return validateAwsConfig(values);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from "@nestjs/common";
import { S3FileAdapter } from "../adapters/s3.adapter";
import { match, P } from "ts-pattern";
import { ConfigService } from "@nestjs/config";
import { FilesAdapter } from "../adapters/files.adapter";
import { LocalFilesAdapter } from "../adapters";
import { ModuleRef } from "@nestjs/core";

type AdapterType = "local" | "s3";

@Injectable()
export class FileAdapterFactory {
constructor(
private moduleRef: ModuleRef,
private configService: ConfigService,
) {}

async createAdapter(): Promise<FilesAdapter> {
const adapterType = this.configService.get<AdapterType>("FILE_ADAPTER");
const adapter = match(adapterType)
.with("local", () => LocalFilesAdapter)
.with("s3", () => S3FileAdapter)
.with(P.nullish, () => {
throw new Error("FILE_ADAPTER is not defined in configuration");
})
.otherwise((type) => {
throw new Error(`Unknown file adapter type: ${type}`);
});

return await this.moduleRef.create<FilesAdapter>(adapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from "@nestjs/common";
import { FilesAdapter } from "./adapters/files.adapter";

@Injectable()
export class FileService {
constructor(private fileAdapter: FilesAdapter) {}

async uploadFile(
directory: string,
file: Express.Multer.File,
): Promise<{ path: string }> {
return await this.fileAdapter.uploadFile(directory, file);
}

async deleteFile(key: string): Promise<void> {
return await this.fileAdapter.deleteFile(key);
}

async getFileUrl(key: string): Promise<{ url: string }> {
return await this.fileAdapter.getFileUrl(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Module } from "@nestjs/common";
import { FileService } from "./file.service";
import { ConfigModule } from "@nestjs/config";
import { FileAdapterFactory } from "./factory/file-adapters.factory";
import { FilesAdapter } from "./adapters/files.adapter";

@Module({
imports: [ConfigModule],
providers: [
FileService,
FileAdapterFactory,
{
provide: FilesAdapter,
useFactory: (factory: FileAdapterFactory) => factory.createAdapter(),
inject: [FileAdapterFactory],
},
],
exports: [FileService],
})
export class FilesModule {}
32 changes: 0 additions & 32 deletions examples/common_nestjs_remix/apps/api/src/files/file.service.ts

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5745ec9

Please sign in to comment.