learnworlds-js
Version:
TypeScript SDK for LearnWorlds API with full type safety and comprehensive method coverage
220 lines (216 loc) • 6.31 kB
TypeScript
interface LearnWorldsConfig {
schoolDomain: string;
clientId: string;
clientSecret: string;
apiHost: string;
redirectUri?: string;
accessToken?: string;
refreshToken?: string;
onTokenRefresh?: (tokens: TokenResponse) => void | Promise<void>;
}
interface TokenResponse {
access_token: string;
token_type: string;
expires_in: number;
refresh_token?: string;
scope?: string;
}
interface AuthorizationCodeRequest {
code: string;
redirectUri: string;
}
interface ResourceOwnerPasswordRequest {
username: string;
password: string;
scope?: string;
}
interface ApiResponse<T> {
success: boolean;
data: T;
message?: string;
errors?: string[];
}
interface PaginationParams {
page?: number;
per_page?: number;
}
interface Course {
id: string;
title: string;
description?: string;
status: 'published' | 'draft' | 'archived';
price?: number;
currency?: string;
created_at: string;
updated_at: string;
image_url?: string;
slug?: string;
category_id?: string;
instructor_id?: string;
duration?: number;
difficulty_level?: 'beginner' | 'intermediate' | 'advanced';
enrolled_users_count?: number;
}
interface Bundle {
id: string;
title: string;
description?: string;
status: 'published' | 'draft' | 'archived';
price?: number;
currency?: string;
created_at: string;
updated_at: string;
image_url?: string;
slug?: string;
course_ids: string[];
discount_percentage?: number;
}
interface User {
id: string;
email: string;
first_name?: string;
last_name?: string;
username?: string;
avatar_url?: string;
bio?: string;
created_at: string;
updated_at: string;
last_login?: string;
is_active: boolean;
roles: string[];
tags: string[];
custom_fields?: Record<string, unknown>;
}
interface CreateUserRequest {
email: string;
first_name?: string;
last_name?: string;
username?: string;
password?: string;
bio?: string;
tags?: string[];
custom_fields?: Record<string, unknown>;
send_welcome_email?: boolean;
}
interface UpdateUserRequest {
first_name?: string;
last_name?: string;
username?: string;
bio?: string;
custom_fields?: Record<string, unknown>;
is_active?: boolean;
}
interface UpdateUserTagsRequest {
tags: string[];
action: 'add' | 'remove' | 'replace';
}
interface EnrollUserRequest {
user_id: string;
product_id: string;
product_type: 'course' | 'bundle';
enrollment_type?: 'free' | 'paid';
expires_at?: string;
}
interface UnenrollUserRequest {
user_id: string;
product_id: string;
product_type: 'course' | 'bundle';
}
interface Enrollment {
id: string;
user_id: string;
product_id: string;
product_type: 'course' | 'bundle';
enrollment_type: 'free' | 'paid';
status: 'active' | 'inactive' | 'expired';
enrolled_at: string;
expires_at?: string;
progress?: number;
completion_date?: string;
}
interface LearnWorldsError extends Error {
status?: number;
code?: string;
details?: unknown;
}
declare class OAuth2Client {
private http;
private config;
private accessToken?;
private refreshToken?;
private tokenExpiresAt?;
constructor(config: LearnWorldsConfig);
/**
* Get the authorization URL for the authorization code grant flow
*/
getAuthorizationUrl(scope?: string, state?: string): string;
/**
* Exchange authorization code for access token
*/
exchangeAuthorizationCode(request: AuthorizationCodeRequest): Promise<TokenResponse>;
/**
* Get access token using resource owner password credentials grant
*/
authenticateWithPassword(request: ResourceOwnerPasswordRequest): Promise<TokenResponse>;
/**
* Get access token using client credentials grant
* This is typically used for server-to-server authentication
*/
authenticateWithClientCredentials(scope?: string): Promise<TokenResponse>;
/**
* Refresh the access token using the refresh token
*/
refreshAccessToken(): Promise<TokenResponse>;
/**
* Revoke the access token
*/
revokeToken(token?: string): Promise<void>;
/**
* Get the current access token, refreshing if necessary
*/
getAccessToken(): Promise<string>;
/**
* Check if authenticated
*/
isAuthenticated(): boolean;
/**
* Set tokens manually (useful for restoring session)
*/
setTokens(tokens: {
accessToken: string;
refreshToken?: string;
expiresAt?: Date;
}): void;
/**
* Get current tokens (useful for persisting session)
*/
getTokens(): {
accessToken?: string;
refreshToken?: string;
expiresAt?: Date;
};
private handleTokenResponse;
}
declare class LearnWorldsClient {
private http;
private oauth;
constructor(config: LearnWorldsConfig);
/**
* Get the OAuth2 client for authentication operations
*/
get auth(): OAuth2Client;
private handleError;
private makeRequest;
getAllCourses(params?: PaginationParams): Promise<Course[]>;
getAllBundles(params?: PaginationParams): Promise<Bundle[]>;
createUser(userData: CreateUserRequest): Promise<User>;
updateUser(userId: string, userData: UpdateUserRequest): Promise<User>;
updateUserTags(userId: string, tagData: UpdateUserTagsRequest): Promise<User>;
enrollUserToProduct(enrollmentData: EnrollUserRequest): Promise<Enrollment>;
unenrollUserFromProduct(unenrollmentData: UnenrollUserRequest): Promise<void>;
getUser(userId: string): Promise<User>;
getCourse(courseId: string): Promise<Course>;
getBundle(bundleId: string): Promise<Bundle>;
getUserEnrollments(userId: string): Promise<Enrollment[]>;
}
export { type ApiResponse, type AuthorizationCodeRequest, type Bundle, type Course, type CreateUserRequest, type EnrollUserRequest, type Enrollment, LearnWorldsClient, type LearnWorldsConfig, type LearnWorldsError, OAuth2Client, type PaginationParams, type ResourceOwnerPasswordRequest, type TokenResponse, type UnenrollUserRequest, type UpdateUserRequest, type UpdateUserTagsRequest, type User };