TypeScript SDK
TypeScript SDK contains CUBA data model (entities and enums), rest services and queries as TypeScript classes.
The SDK is framework-agnostic, meaning that in addition to using it with e.g. our React client, you can use it with any TypeScript-compatible framework such as Angular of Vue.
It’s possible to generate the following configurations of SDK depending on your needs (see usage instruction):
-
gen-cuba-front sdk:model- generates entities and enums. -
gen-cuba-front sdk:all- generates all toolkit - entities, enums, queries and services.
SDK can be used for front-end clients and Node.js-based BFF (Backend for Frontend) development.
Entities
Persistent Entities
Consider the Role entity class of CUBA Framework generated in TypeScript:
src/cuba/entities/base/sec$Role.ts
export class Role extends StandardEntity {
static NAME = "sec$Role";
name?: string | null;
locName?: string | null;
description?: string | null;
type?: any | null;
defaultRole?: boolean | null;
permissions?: Permission[] | null;
}
-
You can easily access entity name using static
NAMEproperty:Role.NAME. -
Roleclass contains all properties of the domain model entity including those from the class hierarchy. Reference fields have corresponding types as well, so that you can work with them in a type-safe manner:function changeRole(role: Role) { role.defaultRole = true; // ok role.defaultRole = 'foo'; // compilation fails }
Non-persistent Entities
CUBA Platform supports non-persistent entities. Entity class should be annotated with com.haulmont.chile.core.annotations.MetaClass, and extended from com.haulmont.cuba.core.entity.BaseUuidEntity. Class properties annotated with com.haulmont.chile.core.annotations.MetaProperty will be included in the generated model.
Source:
package com.company;
import com.haulmont.chile.core.annotations.MetaClass;
import com.haulmont.chile.core.annotations.MetaProperty;
import com.haulmont.cuba.core.entity.BaseUuidEntity;
@MetaClass(name = "SampleUserInfo")
public class SampleUserInfo extends BaseUuidEntity {
@MetaProperty
public String firstName;
@MetaProperty
public String lastName;
}
Generated:
export class SampleUserInfo {
static NAME = "SampleUserInfo";
firstName?: string | null;
lastName?: string | null;
}
Enums
CUBA REST API module uses enum’s constant name in client-server communication. SDK contains generated string enums e.g.:
export enum CarType {
SEDAN = "SEDAN",
HATCHBACK = "HATCHBACK"
}
In order to get enum id and localized caption, you can query full information about enums in runtime using loadEnums method of CUBA REST JS:
import {EnumInfo, initializeApp} from "@cuba-platform/rest";
const cubaREST = initializeApp();
cubaREST.loadEnums()
.then(((enums: EnumInfo[]) => {
console.log('enums', enums)
}));
Response example:
[{
"name": "com.company.mpg.entity.CarType",
"values": [
{
"name": "SEDAN",
"id": "SEDAN",
"caption": "Sedan"
},
{
"name": "HATCHBACK",
"id": "HATCHBACK",
"caption": "Hatchback"
}
]
}]