Isomorphic JavaScript client library for Kowloon social network. Works seamlessly in:
- ✅ Node.js (18+)
- ✅ Browsers (modern browsers with fetch support)
- ✅ React Native (with AsyncStorage)
npm install @kowloon/clientFor React Native, also install AsyncStorage:
npm install @react-native-async-storage/async-storageimport { KowloonClient } from '@kowloon/client';
// Create client
const client = new KowloonClient({
baseUrl: 'https://kowloon.org'
});
// Register a new user
const { user, token } = await client.auth.register({
username: 'alice',
password: 'secret123',
email: 'alice@example.com',
profile: {
name: 'Alice',
description: 'Just joined Kowloon!'
}
});
// Login
await client.auth.login({
username: 'alice',
password: 'secret123'
});
// Check if authenticated
const isAuth = await client.auth.isAuthenticated(); // true
// Get current user
const currentUser = client.auth.getUser();
// Logout
await client.auth.logout();The client automatically stores tokens and restores sessions:
// First app load
const client = new KowloonClient({ baseUrl: 'https://kowloon.org' });
// Restore previous session
const user = await client.init();
if (user) {
console.log('Restored session for', user.username);
} else {
console.log('No previous session');
}Create a new client instance.
Options:
baseUrl(string, required) - Base URL of Kowloon serverstorage(object, optional) - Custom storage adapterheaders(object, optional) - Default headers for all requeststimeout(number, optional) - Request timeout in ms (default: 30000)
Register a new user.
Parameters:
username(string, required)password(string, required)email(string, optional)profile(object, optional)
Returns: { user, token }
Login with username/ID and password.
Parameters:
username(string) - Usernameid(string) - User ID (@user@domain) - alternative to usernamepassword(string, required)
Returns: { user, token }
Logout and clear session.
Returns: Promise<void>
Check if user is currently authenticated.
Returns: Promise<boolean>
Get current user object (synchronous).
Returns: Object | null
Get current auth token.
Returns: Promise<string | null>
Restore session from stored token.
Returns: Promise<Object | null> - User object if session restored
The client automatically detects the environment and uses appropriate storage:
- Browser:
localStorage - React Native:
AsyncStorage(requires@react-native-async-storage/async-storage) - Node.js: In-memory storage (fallback)
You can provide a custom storage adapter:
const customStorage = {
async getItem(key) { /* ... */ },
async setItem(key, value) { /* ... */ },
async removeItem(key) { /* ... */ },
async clear() { /* ... */ }
};
const client = new KowloonClient({
baseUrl: 'https://kowloon.org',
storage: customStorage
});All API errors are typed:
import {
AuthenticationError,
ValidationError,
NetworkError
} from '@kowloon/client';
try {
await client.auth.login({ username: 'alice', password: 'wrong' });
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid credentials');
} else if (error instanceof NetworkError) {
console.error('Network error:', error.message);
}
}Error Types:
AuthenticationError(401)AuthorizationError(403)NotFoundError(404)ValidationError(400)ServerError(500+)NetworkError(connection failures, timeouts)
# Start Kowloon server first
cd ../kowloon
npm start
# Run tests
cd ../kowloon-client
npm testKOWLOON_BASE_URL- Base URL for tests (default:http://localhost:3000)
MIT