@upendra19/platform-sdk
v1.0.0
Published
SDK for bundling multiple REST APIs using Repository Pattern
Downloads
105
Maintainers
Readme
Platform SDK - Repository Pattern POC
A clean SDK demonstrating how to bundle multiple REST APIs using the Repository Pattern.
Architecture
1. HTTP Client Layer
- HttpClient.js: Shared HTTP client handling authentication and error normalization
- Accepts
baseUrlandtokenin constructor - Adds Authorization header automatically
- Normalizes errors across all requests
2. Repository Layer
- UserRepository.js: Encapsulates all user-related endpoints
- PostRepository.js: Encapsulates all post-related endpoints
- Hides endpoint paths from consumers
- Each repository focuses on a single domain
3. SDK Facade
- index.js: Main entry point (PlatformSDK class)
- Initializes HTTP client once
- Exposes repositories as properties:
sdk.users,sdk.posts - Simple, clean API surface
Usage
const PlatformSDK = require('./sdk');
const sdk = new PlatformSDK({
baseUrl: 'https://jsonplaceholder.typicode.com',
token: 'your-api-token',
});
// Use repository methods
const users = await sdk.users.getAllUsers();
const posts = await sdk.posts.getAllPosts();
const user = await sdk.users.getUserById(1);Run Example
node example.jsPublishing as Private NPM Package
Option 1: Private NPM Registry
# Set registry to your private NPM
npm config set registry https://your-registry.com
# Publish
npm publishOption 2: GitHub Packages
# Add to package.json
{
"publishConfig": {
"registry": "https://npm.pkg.github.com"
}
}
# Authenticate
npm login --registry=https://npm.pkg.github.com
# Publish
npm publishOption 3: Verdaccio (Self-hosted)
# Install Verdaccio
npm install -g verdaccio
# Run registry
verdaccio
# Publish to local registry
npm publish --registry http://localhost:4873Installing the Private SDK
# From private registry
npm install @yourcompany/platform-sdk --registry https://your-registry.com
# From GitHub Packages
npm install @yourcompany/platform-sdk
# From tarball
npm install /path/to/yourcompany-platform-sdk-1.0.0.tgzBenefits of This Architecture
- Separation of Concerns: HTTP logic separated from business logic
- Easy Testing: Mock repositories independently
- Scalability: Add new repositories without changing existing code
- Consistent API: All endpoints accessed through unified interface
- Type Safety: Easy to add TypeScript types later
- Private Distribution: Control who accesses your APIs
Extending the SDK
To add a new domain (e.g., Comments):
- Create
sdk/repositories/CommentRepository.js - Add to SDK facade in
sdk/index.js:this.comments = new CommentRepository(httpClient); - Use it:
sdk.comments.getAllComments()
