@unvired/react-native-wrapper-sdk
v0.0.14
Published
Unvired SDK for React Native - Enterprise mobile platform SDK with authentication, sync, and offline database
Readme
React Native Unvired SDK
A lightweight React Native wrapper for the Unvired SDK with builder pattern implementation.
Architecture
This SDK follows a three-layer architecture:
RN App ←→ RN Wrapper ←→ Core Web SDK (TS) ←→ Native Implementation- RN App: Your React Native application
- RN Wrapper:
UnviredSDKclass (this package) - Core Web SDK:
CoreWebSDKclass - JS interface layer - Native Implementation: Platform-specific implementations (iOS/Android)
Installation
npm install react-native-wrapper-sdkQuick Start
Basic Login Example
import UnviredSDK from 'react-native-wrapper-sdk';
// Create and configure SDK using builder pattern
const sdk = UnviredSDK.create()
.setServerUrl('https://api.unvired.io')
.setAppId('my-app-123')
.setTimeout(30000)
.enableLogging(true)
.build();
// Login
const response = await sdk.login({
username: 'demo',
password: 'demo123',
company: 'Unvired Inc.',
});
if (response.success) {
console.log('Login successful!');
console.log('Session Token:', response.sessionToken);
console.log('User ID:', response.userId);
} else {
console.log('Login failed:', response.message);
}API Reference
Builder Methods
The SDK uses a builder pattern for configuration. All builder methods return the builder instance for chaining.
UnviredSDK.create()
Creates a new SDK builder instance.
const builder = UnviredSDK.create();setServerUrl(url: string)
Sets the server URL for API calls.
builder.setServerUrl('https://api.unvired.io');setAppId(appId: string)
Sets the application ID.
builder.setAppId('my-app-123');setTimeout(timeout: number)
Sets the request timeout in milliseconds (default: 30000).
builder.setTimeout(60000); // 60 secondsenableLogging(enable: boolean)
Enables or disables SDK logging (default: false).
builder.enableLogging(true);build()
Builds and returns the SDK instance. Must be called after configuration.
const sdk = builder.build();Login Builder (Recommended)
The SDK provides a builder pattern for constructing login credentials, which avoids passing empty objects for optional parameters.
const response = await sdk.loginBuilder()
.setUsername('demo')
.setPassword('demo123')
.setCompany('Unvired Inc.') // Optional
.execute();SDK Instance Methods
loginBuilder(): LoginBuilder
Returns a builder to construct login credentials.
Methods:
setUsername(username: string): LoginBuildersetPassword(password: string): LoginBuildersetCompany(company: string): LoginBuilderexecute(): Promise<LoginResponse>
login(credentials: LoginCredentials): Promise<LoginResponse>
Legacy method. Authenticates a user with the provided credentials object.
Parameters:
interface LoginCredentials {
username: string;
password: string;
company?: string;
}Returns:
interface LoginResponse {
success: boolean;
message: string;
sessionToken?: string;
userId?: string;
error?: string;
}Example:
const response = await sdk.login({
username: 'demo',
password: 'demo123',
});logout(): Promise<boolean>
Logs out the current user and clears the session.
Returns: Promise<boolean> - true if logout was successful
Example:
const success = await sdk.logout();isAuthenticated(): Promise<boolean>
Checks if a user is currently authenticated.
Returns: Promise<boolean> - true if authenticated
Example:
const isAuth = await sdk.isAuthenticated();getConfig(): SDKConfig
Returns the current SDK configuration.
Returns:
interface SDKConfig {
serverUrl?: string;
timeout?: number;
enableLogging?: boolean;
appId?: string;
}Usage Examples
Complete Login/Logout Flow
import UnviredSDK from 'react-native-wrapper-sdk';
async function loginLogoutExample() {
// Build SDK
const sdk = UnviredSDK.create()
.setServerUrl('https://api.unvired.io')
.setAppId('my-app-123')
.enableLogging(true)
.build();
// Login
const loginResponse = await sdk.login({
username: 'demo',
password: 'demo123',
});
if (loginResponse.success) {
console.log('Logged in successfully');
// Check authentication
const isAuth = await sdk.isAuthenticated();
console.log('Is Authenticated:', isAuth); // true
// Logout
await sdk.logout();
// Check authentication again
const isAuthAfterLogout = await sdk.isAuthenticated();
console.log('Is Authenticated:', isAuthAfterLogout); // false
}
}Multiple SDK Instances
You can create multiple SDK instances with different configurations:
// Production instance
const prodSDK = UnviredSDK.create()
.setServerUrl('https://api.unvired.io')
.setAppId('prod-app')
.build();
// Staging instance
const stagingSDK = UnviredSDK.create()
.setServerUrl('https://staging-api.unvired.io')
.setAppId('staging-app')
.enableLogging(true)
.build();Error Handling
try {
const response = await sdk.login({
username: 'user',
password: 'pass',
});
if (!response.success) {
console.error('Login failed:', response.error);
}
} catch (error) {
console.error('Unexpected error:', error);
}TypeScript Support
This SDK is written in TypeScript and includes full type definitions. All interfaces are exported for your use:
import UnviredSDK, {
LoginCredentials,
LoginResponse,
SDKConfig,
UnviredSDKBuilder,
UnviredSDKInstance,
} from 'react-native-wrapper-sdk';Development
Build
npm run buildThis compiles the TypeScript source to JavaScript in the dist directory.
Project Structure
src/
├── index.ts # Main entry point
├── UnviredSDK.ts # RN Wrapper with builder pattern
├── LoginBuilder.ts # Login Builder implementation
core/
├── CoreWebSDK.ts # Core Web SDK (JS interface layer)
├── types.ts # TypeScript type definitionsDummy Login Credentials
For testing purposes, use these credentials:
- Username:
demo - Password:
demo123
Any other credentials will result in an authentication failure.
License
UNLICENSED
Author
Unvired Inc.
