Home Reference Source Repository
public class | source

OlapicCategoriesHandler

Extends:

OlapicEntitiesHandler → OlapicCategoriesHandler

The entities handler for the Olapic categories in DevKit. It's basically a set of static methods used to create and obtain categories from the API or DevKit itself.

Static Method Summary

Static Public Methods
public static

It parses a raw API JSON information and returns a category entity.

public static

When requesting a collection of entities, the API response contains an array instead of an object, so this method will loop the array, create entities using .entityFromJSON() and return a new array of entities.

public static

Gets an Olapic category by its unique ID.

public static

Gets an Olapic category by its API url.

Inherited Summary

From class OlapicEntitiesHandler
public static get

A quick shortcut to get access to the DevKit singleton.

public static

This will be used when creating an entity to parse any list of action forms and returns them with a better format.

public static

This will be used when creating an entity to parse any list of linked entities and returns them with a better format.

Static Public Methods

public static entityFromJSON(json: Object): OlapicCategoryEntity source

It parses a raw API JSON information and returns a category entity.

Params:

NameTypeAttributeDescription
json Object

The raw API JSON object to parse.

Return:

OlapicCategoryEntity

The entity created from the JSON information.

Example:

let JSONInfo = {
    metadata: {},
    data: {
        name: 'My Category',
    },
};
let category = OlapicCategoriesHandler.entityFromJSON(JSONInfo);
// It will log 'My Category'
console.log(category.get('name'));

public static extractEntities(obj: Object): Array source

When requesting a collection of entities, the API response contains an array instead of an object, so this method will loop the array, create entities using .entityFromJSON() and return a new array of entities.

Params:

NameTypeAttributeDescription
obj Object

The API response with the array.

Return:

Array

A list of parsed entities.

Example:

let JSONInfo = {
    metadata: {},
    data: {
        _embedded: {
            category: [
                {
                    name: 'Category One',
                },
                {
                    name: 'Category Two',
                }
            ],
        },
    },
};
let entities = OlapicCategoriesHandler.extractEntities(JSONInfo);
// It will log 'Category One'
console.log(entities[0].get('name'));

public static getCategoryByID(ID: Number): Promise<OlapicCategoryEntity, Error> source

Gets an Olapic category by its unique ID.

Params:

NameTypeAttributeDescription
ID Number

The category ID.

Return:

Promise<OlapicCategoryEntity, Error>

The category entity or an Error object if something goes wrong.

Example:

OlapicCategoriesHandler.getCategoryByID(12)
.then((category) => {
    console.log('Category: ', category);
});

public static getCategoryByUrl(url: String): Promise<OlapicCategoryEntity, Error> source

Gets an Olapic category by its API url.

Params:

NameTypeAttributeDescription
url String

The Olapic API url for the category.

Return:

Promise<OlapicCategoryEntity, Error>

The category entity or an Error object if something goes wrong.

Example:

OlapicCategoriesHandler.getCategoryByUrl('http://...')
.then((category) => {
    console.log('Category: ', category);
});