All files / sn-client-js/src ODataHelper.ts

100% Statements 59/59
92.68% Branches 38/41
100% Functions 7/7
100% Lines 59/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                    1x 1x                     1x 42x 16x   26x 26x 16x 3x 3x   13x 12x     1x       10x   26x   140x 41x 28x 28x 3x   25x 25x 68x 68x 68x 43x               13x 13x     41x     26x 25x     26x                 1x 1x 1x             1x 22x 1x   21x 2x   19x 19x 19x     19x 3x   16x 19x             1x 86x             1x 23x             1x 255x 513x 2x   513x 173x   513x     255x  
/**
 * @module ODataHelper
 * @preferred
 * 
 * @description Helper methods for OData Operations
 */ /** */
 
// TODO: ezeket vhova kivezetni
import { ODataApi } from './SN';
 
const ODATA_PARAMS = ['select', 'expand', 'orderby', 'top', 'skip', 'filter', 'format', 'inlinecount'];
export const DATA_ROOT = 'OData.svc';
 
/**
 * Method to build proper parameter string to OData requests based on the given option Object.
 *
 * Checks whether a given parameter is standard OData param or not and based on this information this params get the '$' sign.
 *
 * If there's no select param given, or it is empty 'Id' is the default, so only this field will be on the content in the JSON result. To get all the field values, without selection, set it to 'all', but please avoid this if it's possible.
 * @param {IODataOptions} options Represents an ODataOptions obejct based through the IODataOptions interface. Holds the possible url parameters as properties.
 * @returns {string} String with the url params in the correct format e.g. '$select=DisplayName,Index'&$top=2&metadata=no'.
 */
export const buildUrlParamString: (options?: ODataApi.IODataParams) => string = (options?: ODataApi.IODataParams): string => {
    if (!options) {
        return '';
    }
    let params: string = '?';
    if (options.select) {
        if (typeof options.select === 'string' && options.select !== 'all') {
            let value = options.select;
            options.select = [value, 'Id', 'Type'];
        }
        else if (options.select !== 'all') {
            options.select = [...options.select, 'Id', 'Type']
        }
        else {
            delete options['select'];
        }
    }
    else {
        options.select = ['Id', 'Type'];
    }
    for (let key in options) {
 
        if (options[key]) {
            if (ODATA_PARAMS.indexOf(key) > -1) {
                params += `$${key}=`;
                if (typeof options[key] === 'string') {
                    params += options[key];
                }
                else Eif (typeof options[key] !== 'undefined') {
                    for (let x = 0; x < options[key].length; x++) {
                        Eif (typeof options[key] !== 'undefined') {
                            params += `${options[key][x]}`;
                            if (x < options[key].length - 1) {
                                params += `,`;
                            }
                        }
                    }
                }
 
            }
            else {
                Eif (typeof options[key] !== 'undefined') {
                    params += `${key}=${options[key]}`;
                }
            }
            params += `&`;
        }
    }
    if (typeof options.metadata === 'undefined') {
        params += 'metadata=no&';
    }
 
    return params.slice(0, params.length - 1);
}
/**
 * Method to help building body part of a POST OData request.
 *
 * Converts the given JSON to string and wraps it into a 'models' array.
 * @param options
 * @returns {string} Models array with the given options in string format e.g. 'models=["{ '__ContentType':'EventList' , 'DisplayName': 'Calendar', 'Index': 2 }"]'.
 */
export const buildRequestBody = (options) => {
    let stringifiedOptions = JSON.stringify(options);
    return `models=[${stringifiedOptions}]`;
}
/**
 * Method that gets the URL that refers to a single item in the Sense/Net Content Repository
 * @param path {string} Path that you want to format.
 * @returns {string} Path in entity format e.g. /workspaces('project') from /workspaces/project
 */
export const getContentURLbyPath: (path: string) => string = (path: string): string => {
    if (typeof path === 'undefined' || path.indexOf('/') < 0 || path.length <= 1) {
        throw new Error('This is not a valid path.');
    }
    if (isItemPath(path))
        return path;
 
    let lastSlashPosition = path.lastIndexOf('/');
    let name = path.substring(lastSlashPosition + 1);
    let parentPath = path.substring(0, lastSlashPosition);
 
    let url;
    if (name.indexOf('Root') > -1)
        url = `${parentPath}/('${name}')`;
    else
        url = `${parentPath}('${name}')`
    return url;
}
/**
 * Method that gets the URL that refers to a single item in the Sense/Net Content Repository by its Id
 * @param id {number} Id of the Content.
 * @returns {string} e.g. /content(123)
 */
export const getContentUrlbyId: (id: number) => string = (id: number) => {
    return `/content(${id})`;
}
/**
 * Method that tells if a path is an item path.
 * @param path {string} Path that you want to test.
 * @returns {boolean} Returns if the given path is a path of a Content or not.
 */
export const isItemPath: (path: string) => boolean = (path) => {
    return path.indexOf("('") >= 0 && path.indexOf("')") === path.length - 2;
}
 
/**
 * Method that allows to join paths without multiple or missing slashes
 * @param args The list of the paths to join
 */
export const joinPaths = (...args: string[]) => {
    const trimSlashes = (path: string) => {
        if (path.endsWith('/')) {
            path = path.substring(0, path.length - 1)
        }
        if (path.startsWith('/')) {
            path = path.substring(1, path.length);
        }
        return path;
    }
 
    return args.map(trimSlashes).join('/');
}