sdk-core-machin
v1.0.1
Published
Machin SDK Core - Domain logic and use cases (adapter-agnostic)
Downloads
190
Maintainers
Readme
@machin/sdk-core
Core SDK for Machin mobile application. This package contains the domain logic, entities, and use cases without any adapter implementation.
Architecture
This SDK follows Clean Architecture principles:
- Domain Layer: Entities and repository interfaces (contracts)
- Application Layer: Use cases that orchestrate business logic
The actual implementation of repositories (adapters) is provided by separate packages:
@machin/sdk-supabase- Supabase backend adapter@machin/sdk-http- HTTP/REST API adapter- Or create your own custom adapter!
Installation
npm install @machin/sdk-core
# Also install an adapter (example with Supabase)
npm install @machin/sdk-supabaseUsage
1. Initialize the SDK with an adapter
import { MachinSDK } from '@machin/sdk-core';
import { AuthSupabaseAdapter } from '@machin/sdk-supabase';
// Initialize once in your app
const sdk = MachinSDK.init({
auth: new AuthSupabaseAdapter(
'https://your-project.supabase.co',
'your-anon-key'
)
});2. Use the SDK
// Sign in
const result = await sdk.auth.sign_in('[email protected]', 'password');
if (result.error) {
console.error('Login failed:', result.error.message);
} else {
console.log('User:', result.data.user);
}
// Sign out
await sdk.auth.sign_out();
// Password recovery
await sdk.auth.recovery_password('[email protected]');3. Get instance anywhere in your app
import { MachinSDK } from '@machin/sdk-core';
const sdk = MachinSDK.getInstance();
await sdk.auth.sign_out();Creating a Custom Adapter
You can create your own adapter for any backend by implementing the repository interfaces:
import { AuthRepository, AuthResponse, User } from '@machin/sdk-core';
import axios from 'axios';
export class AuthCustomAdapter implements AuthRepository {
private apiUrl: string;
constructor(apiUrl: string) {
this.apiUrl = apiUrl;
}
async sign_in(email: string, password: string): Promise<AuthResponse> {
try {
const response = await axios.post(`${this.apiUrl}/auth/login`, {
email,
password
});
const user = User.fromDTO(response.data.user);
return {
data: { user },
error: null
};
} catch (error: any) {
return {
data: { user: null },
error: {
status: error.response?.status || 500,
code: error.code || 'UNKNOWN_ERROR',
message: error.message
}
};
}
}
async sign_up(email: string, password: string): Promise<AuthResponse> {
// Implementation...
}
async sign_out(): Promise<void> {
// Implementation...
}
async recovery_password(email: string): Promise<{}> {
// Implementation...
}
async update_password(password: string): Promise<{}> {
// Implementation...
}
}
// Use it
const sdk = MachinSDK.init({
auth: new AuthCustomAdapter('https://my-api.com')
});API Reference
MachinSDK
Static Methods
MachinSDK.init(config: MachinSDKConfig): MachinSDK- Initialize the SDK with adaptersMachinSDK.getInstance(): MachinSDK- Get the current SDK instanceMachinSDK.reset(): void- Reset the SDK (useful for testing)
Instance Properties
sdk.auth: AuthUseCase- Authentication use case
AuthUseCase
sign_up(email: string, password: string): Promise<AuthResponse>sign_in(email: string, password: string): Promise<AuthResponse>sign_out(): Promise<void>recovery_password(email: string): Promise<{}>update_password(password: string): Promise<{}>
Types
interface AuthResponse {
data: {
user: User | null;
};
error: AuthError | null;
}
interface AuthError {
status: number;
code: string | {};
message: string;
}
class User {
id: string;
email: string;
emailVerified: boolean;
role: string;
lastSignInAt: Date | null;
}Available Adapters
- @machin/sdk-supabase - Supabase backend
- @machin/sdk-http - Generic HTTP/REST API
Philosophy
This SDK is designed to be adapter-agnostic. The core package only defines:
- What operations are available (interfaces)
- The business logic (use cases)
- The data structures (entities)
The how (implementation details) is delegated to adapter packages, giving you:
- ✅ Flexibility to switch backends without changing your code
- ✅ Easy testing with mock adapters
- ✅ No vendor lock-in
- ✅ Small bundle size (only install what you need)
License
MIT © David Luna
