Skip to content
Merged
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
214 changes: 214 additions & 0 deletions CLIENT_IMPLEMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# Client Module Implementation Summary

## Overview

Successfully implemented comprehensive client libraries for the NestJS Storage Service that enable both backend microservices and frontend applications to interact with the storage service.

## Implementation Details

### 1. NestJS Backend Client (`src/client/nestjs/`)

**Files Created:**
- `storage-client.module.ts` - Dynamic NestJS module with sync/async registration
- `storage-client.service.ts` - Injectable service with full API coverage
- `storage-client.interfaces.ts` - Module options interface
- `storage-client.constants.ts` - DI constants
- `index.ts` - Public exports

**Features:**
- Full API coverage: namespaces, objects, uploads, health checks
- Synchronous and asynchronous module registration
- ConfigService integration support
- Uses @nestjs/axios and RxJS for HTTP communication
- Fully typed with TypeScript
- 9 unit tests with 100% passing rate

**Example Usage:**
```typescript
@Module({
imports: [
StorageClientModule.register({
baseUrl: 'http://localhost:4000',
}),
],
})
export class AppModule {}
```

### 2. Axios Frontend Client (`src/client/axios/`)

**Files Created:**
- `storage-client.ts` - Class-based client for browsers
- `index.ts` - Public exports

**Features:**
- All API methods: createObject, getObject, deleteObject, etc.
- Helper methods for file uploads
- Multipart upload support
- Authentication helpers (setAuthToken, setHeaders)
- Health check endpoints
- Fully typed with TypeScript
- 8 unit tests with 100% passing rate

**Example Usage:**
```typescript
const client = new StorageClient({
baseUrl: 'http://localhost:4000',
});

const object = await client.createObject({
namespace: 'documents',
name: 'file.pdf',
contentType: 'application/pdf',
sizeBytes: 1024000,
});

await client.uploadFile(object.uploadUrl!, file);
```

### 3. Shared Module (`src/client/shared/`)

**Files Created:**
- `types.ts` - Common TypeScript interfaces
- `index.ts` - Public exports

**Features:**
- 15+ shared TypeScript interfaces
- Request/response types for all API operations
- Client configuration interfaces
- Ensures type consistency between clients

### 4. Tests (`tests/client/`)

**Files Created:**
- `storage-client.service.spec.ts` - NestJS client tests (9 tests)
- `storage-client.spec.ts` - Axios client tests (8 tests)

**Test Coverage:**
- All API methods tested
- Mock adapters for HTTP requests
- Edge cases covered
- 17/17 tests passing

### 5. Documentation

**Files Created:**
- `src/client/README.md` - Comprehensive client documentation
- `src/client/EXAMPLES.md` - Detailed usage examples
- Updated main `README.md` with quick start guide

**Documentation Includes:**
- Installation instructions
- NestJS module setup (sync/async)
- Frontend client setup
- React, Vue, and Angular examples
- Multipart upload examples
- Authentication setup
- Full API reference

### 6. Package Configuration

**Updates to `package.json`:**
- Added axios (v1.12.2 - security patched version)
- Added @nestjs/axios (v3.1.2)
- Added axios-mock-adapter (dev dependency)
- Added exports configuration for clean import paths
- Updated keywords for better discoverability

**Export Paths:**
```json
{
"exports": {
".": "./dist/main.js",
"./client": "./dist/client/index.js",
"./client/nestjs": "./dist/client/nestjs/index.js",
"./client/axios": "./dist/client/axios/index.js",
"./client/shared": "./dist/client/shared/index.js"
}
}
```

## API Coverage

Both clients support all major API operations:

### Namespace Operations
- ✅ Create namespace
- ✅ Get namespace by name

### Object Operations
- ✅ Create object (with upload URL)
- ✅ Get object metadata
- ✅ Generate signed download URL
- ✅ Delete object
- ✅ List objects in namespace

### Upload Operations
- ✅ Upload file to pre-signed URL (Axios only)
- ✅ Initiate multipart upload
- ✅ Upload part
- ✅ Complete multipart upload

### Health Checks
- ✅ Liveness check
- ✅ Readiness check

## Security

- **Vulnerability Scan**: Passed ✅
- Updated axios from v1.7.9 to v1.12.2
- Fixes CVE related to DoS and SSRF attacks

- **CodeQL Analysis**: Passed ✅
- No security alerts found
- No code quality issues

## Testing

- **Unit Tests**: 17/17 passing ✅
- **Build**: Successful ✅
- **Type Checking**: No errors ✅

## Files Changed

**New Files (15):**
- `src/client/index.ts`
- `src/client/README.md`
- `src/client/EXAMPLES.md`
- `src/client/axios/index.ts`
- `src/client/axios/storage-client.ts`
- `src/client/nestjs/index.ts`
- `src/client/nestjs/storage-client.constants.ts`
- `src/client/nestjs/storage-client.interfaces.ts`
- `src/client/nestjs/storage-client.module.ts`
- `src/client/nestjs/storage-client.service.ts`
- `src/client/shared/index.ts`
- `src/client/shared/types.ts`
- `tests/client/storage-client.service.spec.ts`
- `tests/client/storage-client.spec.ts`

**Modified Files (4):**
- `package.json` - Added dependencies and exports
- `package-lock.json` - Updated dependencies
- `tsconfig.json` - Fixed JSON syntax error
- `README.md` - Added client module documentation

## Usage Scenarios

### Scenario 1: Backend Microservice Integration
A NestJS microservice needs to store user-uploaded files. It imports the `StorageClientModule`, injects the service, and uses it to create objects and manage files.

### Scenario 2: React Application
A React app allows users to upload and download files. It uses the `StorageClient` to create objects, upload files via pre-signed URLs, and generate download links.

### Scenario 3: Vue Application
A Vue application manages document storage. It uses the Axios client with Vue's composition API to handle file operations.

### Scenario 4: Large File Upload
An application needs to upload files larger than 100MB. It uses the multipart upload functionality to split files into chunks and upload them progressively.

## Conclusion

The implementation provides a complete, production-ready client library solution for the NestJS Storage Service. Both backend and frontend applications can now easily integrate with the storage service using clean, typed APIs with comprehensive documentation and examples.

All code follows NestJS and TypeScript best practices, includes proper error handling, and is fully tested. The implementation is secure, performant, and ready for production use.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A robust storage service built with NestJS for handling file uploads and storage
- 🔄 GraphQL support
- 📊 Bull queue for background jobs
- 🗄️ Redis caching integration
- 🔌 **Client libraries for NestJS backend and frontend applications**

## Prerequisites

Expand Down Expand Up @@ -194,6 +195,74 @@ curl -X POST http://localhost:4000/api/v1/objects \

The response will include a pre-signed `uploadUrl` that you can use to upload the file directly to S3/MinIO storage using an HTTP PUT request.

### Client Libraries

This service provides ready-to-use client libraries for both backend and frontend applications:

#### NestJS Backend Client

For NestJS microservices, use the `StorageClientModule`:

```typescript
import { Module } from '@nestjs/common';
import { StorageClientModule } from '@pnstack/nestjs-storage-service/client/nestjs';

@Module({
imports: [
StorageClientModule.register({
baseUrl: 'http://localhost:4000',
}),
],
})
export class AppModule {}
```

Then inject the service:

```typescript
import { Injectable } from '@nestjs/common';
import { StorageClientService } from '@pnstack/nestjs-storage-service/client/nestjs';

@Injectable()
export class MyService {
constructor(private readonly storageClient: StorageClientService) {}

async uploadFile(file: Express.Multer.File) {
const object = await this.storageClient.createObject({
namespace: 'documents',
name: file.originalname,
contentType: file.mimetype,
sizeBytes: file.size,
});
return object;
}
}
```

#### Frontend Client (Axios)

For frontend applications (React, Vue, Angular, etc.), use the `StorageClient`:

```typescript
import { StorageClient } from '@pnstack/nestjs-storage-service/client/axios';

const client = new StorageClient({
baseUrl: 'http://localhost:4000',
});

// Create and upload a file
const object = await client.createObject({
namespace: 'user-uploads',
name: file.name,
contentType: file.type,
sizeBytes: file.size,
});

await client.uploadFile(object.uploadUrl!, file);
```

For detailed documentation and examples, see [src/client/README.md](src/client/README.md).

### Database Schema

The service uses PostgreSQL with TypeORM. Tables are auto-created on startup:
Expand Down
Loading