Sunbird Client Services
In order to facilitate Adopters to connect with Sunbird System. Typescript based library to connect with APIs. It can facilitate the adopters with readily available data models.
Criteria
Value
Current Release Branch
release-4.5.0
NPM Published version (Latest)
4.5.1
NPM Package Name
@project-sunbird/client-services
Works
Across All JavaScript Based Projects
​

Overview

The library is grouped into Modules and SubModules as shown below -
1
@project-sunbird/client-services
2
β”œβ”€β”€ /core
3
β”‚ └── /http-service
4
β”‚ └── /utilities
5
β”‚ └── /interceptors
6
β”œβ”€β”€ /models
7
β”‚ β”œβ”€β”€ /channel
8
β”‚ β”œβ”€β”€ /content
9
β”‚ β”œβ”€β”€ /course
10
β”‚ β”œβ”€β”€ /device
11
β”‚ β”œβ”€β”€ /faq
12
β”‚ β”œβ”€β”€ /form
13
β”‚ β”œβ”€β”€ /group
14
β”‚ β”œβ”€β”€ /organisation
15
β”‚ β”œβ”€β”€ /page
16
β”‚ └── /user
17
└── /services
18
β”œβ”€β”€ /content
19
β”‚ └── /utilities
20
β”‚ └── /content-group-generator
21
└── /group
Copied!
The public facing API is prefixed with 'Cs' namespace, as in -
  • CsModule
  • CsConfig
  • CsGroupService
  • ...
For instance,
  • CsModule is part of the root module
  • CsContentsGroupGenerator is a utlility within content service
Their respective imports would be -
1
import {CsModule} from "@project-sunbird/client-services";
2
import {CsContentsGroupGenerator} from "@project-sunbird/client-services/services/content/utilities/content-group-generator";
Copied!
​

Installation

To install the package
1
npm i @project-sunbird/[email protected]
Copied!
The package requires the consumer to have [email protected] installed as the only peerDependency

Getting Started

To use the library CsModule, it needs to be initialised with basic configuration. CsModule is a singleton and it would be best to check if it has already been initialised before attempting to initialise -
1
if (!CsModule.instance.isInitialised) { // Singleton initialised or not
2
await CsModule.instance.init({
3
core: {
4
httpAdapter: 'HttpClientBrowserAdapter', // optional - HttpClientBrowserAdapter or HttpClientCordovaAdapter, default - HttpClientBrowserAdapter
5
global: {
6
channelId: 'some_channel_id', // required
7
producerId: 'some_producer_id', // required
8
deviceId: 'some_random_device_id' // required
9
},
10
api: {
11
host: 'https://staging.ntp.net.in', // default host
12
authentication: {
13
// userToken: string; // optional
14
// bearerToken: string; // optional
15
}
16
}
17
},
18
services: {
19
// groupServiceConfig: CsGroupServiceConfig // optional
20
}
21
);
22
}
Copied!

Adapters

If the client for the library is a cordova project, use the 'HttpClientCordovaAdapter' adapter or use 'HttpClientBrowserAdapter'. 'HttpClientBrowserAdapter' is the default if not specified.

Update Configuration

The configuration can be dynamically reset after initialisation
1
const newConfig = {...CsModule.instance.config}; // copy existing config
2
​
3
newConfig.core.api.authentication = {
4
bearerToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXV.eyJpc3MiOiJzdGFnaW5nLmRpa3NoYS5hcHAtYTMzM2YzZjExZDM0YzlkNWYwZTE2MjUyYWMwZDVhYTZmMTBjYSIsImlhdCI6MTU4ODkxNDc1NX0.dEmjK6LStoenL_pRX1KwEtU4-opuUOUGI05ecex',
5
};
6
​
7
CsModule.instance.updateConfig(CsModule.instance.config);
8
​
9
// after login
10
​
11
newConfig.core.api.authentication = {
12
userToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXV.eyJpc3MiOiJzdGFnaW5nLmRpa3NoYS5hcHAtYTMzM2YzZjExZDM0YzlkNWYwZTE2MjUyYWMwZDVhYTZmMTBjYSIsImlhdCI6MTU4ODkxNDc1NX0.dEmjK6LStoenL_pRX1KwEtU4-opuUOUGI05ecex',
13
};
14
​
15
CsModule.instance.updateConfig(CsModule.instance.config);
Copied!

Accessing a service

The CsModule being a singleton, every service within would also be a singleton, To access any service -
1
const httpService = CsModule.instance.httpService; // handles API calls, interceptors (tokens, logging)
2
const groupService = CsModule.instance.groupService; // internally uses httpService
Copied!

Services

Every service by default will utilise the optional configuration declared during init() or updateConfig()
1
CsModule.instance.init({
2
...
3
services: {
4
groupServiceConfig: { // optional
5
apiPath: '/api/v1/group'
6
}
7
}
8
);
Copied!
Additionally, methods of the service may have an optional argument that can override the global configuration explicitly just for that method call
1
const group = await groupService.getById(
2
group1.identifier,
3
{ apiPath: '/api/v2/group' } // optional
4
).toPromise();
Copied!

CsGroupService

This service deals with user group management and has the following interface -
1
CsModule.instance.init({
2
...
3
services: {
4
groupServiceConfig: { // optional
5
apiPath: '/api/v1/group'
6
}
7
}
8
);
9
​
10
interface CsGroupService {
11
create(
12
name: string,
13
board: string,
14
medium: string | string[],
15
gradeLevel: string | string[],
16
subject: string | string[],
17
config?: CsGroupServiceConfig
18
): Observable<Group>;
19
​
20
deleteById(id: string, config?: CsGroupServiceConfig): Observable<void>;
21
​
22
getAll(config?: CsGroupServiceConfig): Observable<Group[]>;
23
​
24
getById(id: string, config?: CsGroupServiceConfig): Observable<Group>;
25
​
26
addMemberById(memberId: string, groupId: string, config?: CsGroupServiceConfig): Observable<Group>;
27
​
28
removeMemberById(memberId: string, groupId: string, config?: CsGroupServiceConfig): Observable<void>;
29
}
Copied!

CsFrameworkService

1
CsModule.instance.init({
2
...
3
services: {
4
frameworkServiceConfig: { // optional
5
apiPath: '<path>'
6
}
7
}
8
);
9
​
10
export interface CsFrameworkService {
11
getFramework(id: string, options?: GetFrameworkOptions, config?: CsFrameworkServiceConfig): Observable<Framework>;
12
}
Copied!

CsLocationService

1
CsModule.instance.init({
2
...
3
services: {
4
locationServiceConfig: { // optional
5
apiPath: '<path>'
6
}
7
}
8
);
9
​
10
export interface CsLocationService {
11
searchLocations(request?: SearchLocationRequests, config?: CsLocationServiceConfig): Observable<Location[]>;
12
}
Copied!

CsLocationService

1
CsModule.instance.init({
2
...
3
services: {
4
courseServiceConfig: { // optional
5
apiPath: '<path>'
6
}
7
}
8
);
9
​
10
export interface CsCourseService {
11
getUserEnrollmentList(request: GetUserEnrollmentListRequests, additionalParams?: { [key: string]: string }, config?: CsCourseServiceConfig): Observable<Course[]>;
12
}
Copied!
Copy link
Edit on GitHub