Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release/2.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gallayl committed Sep 19, 2017
2 parents a94889b + 53f09b5 commit 14cf468
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sn-client-js",
"version": "2.3.0",
"version": "2.4.0",
"description": "A JavaScript client for Sense/Net ECM that makes it easy to use the REST API of the Content Repository.",
"main": "dist/src/SN.js",
"files": [
Expand Down
71 changes: 68 additions & 3 deletions src/ODataApi/ODataParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,86 @@
import { Content } from '../Content';

export type ODataFieldParameter<T extends Content> = (keyof T['options'])[] | keyof T['options'];
export type ODataMetadataType = 'full' | 'minimal' | 'no';
export type ODataOrderParameter<T extends Content> = keyof T['options'] | (keyof T['options'] | [keyof T['options'], 'asc' | 'desc'])[];

export type ODataMetadataType = 'full' | 'minimal' | 'no';
export type ODataFormatType = 'json' | 'verbosejson';
export type ODataInlineCountType = 'none' | 'allpages';

/**
* Model class to define specific OData Request parameters. See http://wiki.sensenet.com/OData_REST_API
*/
export class IODataParams<T extends Content> {
/**
* The field(s) to be include in a $select list. Can be a field (e.g. 'DisplayName'), an array of fields (e.g. ['Name', 'Type']) or 'all'
*/
select?: ODataFieldParameter<T> | 'all';
/**
* The field(s) to be include in an $expand list. Can be a reference field (e.g. 'Owner') or an array of fields (e.g. ['CreatedBy', 'ModifiecBy'])
*/
expand?: ODataFieldParameter<T>;
orderby?: ODataFieldParameter<T>;

/**
* Sets the OData $orderby parameter. Usage example
* ```ts
* // simple field
* {
* ...
* orderby: 'Name'
* }
* // list with fields or tuples with order direction
* {
* ...
* orderby: [
* ['CreationDate', 'desc']
* 'Name',
* 'DisplayName'
* ]
*
* }
*
* ```
*/
orderby?: ODataOrderParameter<T>;
/**
* Sets the OData $top parameter
*/
top?: number;

/**
* Sets the OData $skip parameter
*/
skip?: number;

/**
* Sets the OData $filter parameter
*/
filter?: string;

/**
* Sets the OData $format parameter. Can be 'json' or 'verbosejson'
*/
format?: ODataFormatType;
inlinecount?: string;

/**
* Sets the OData $format parameter. Can be 'json' or 'verbosejson'
*/
inlinecount?: ODataInlineCountType;

/**
* Sets the OData 'query' parameter. Can be a Content Query
*/
query?: string;
/**
* Sets the OData metadata parameter. Can be 'full', 'minimal' or 'no'
*/
metadata?: ODataMetadataType;
/**
* Sets the OData post data object
*/
data?: Object;
/**
* Sets the OData Scenario parameter
*/
scenario?: string;
}
4 changes: 2 additions & 2 deletions src/ODataHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const combineODataFieldParameters: <T extends Content>(...params: ODataFi
}
});
params = params.filter(param => param && param.length > 0);
return [...new Set([...params])] as ODataFieldParameter<T>;
return [...new Set([].concat.apply([], params))] as ODataFieldParameter<T>;
}

/**
Expand Down Expand Up @@ -54,7 +54,7 @@ export const buildUrlParamString: <T extends Content>(config: Partial<SnConfigMo
const plainValue = options[key];
let parsedValue = plainValue;
if (plainValue instanceof Array && plainValue.length && plainValue.length > 0){
parsedValue = plainValue.join(',');
parsedValue = plainValue.map(v => v.join && v.join(' ') || v).join(',');
}
if (name && parsedValue && parsedValue.length){
segments.push({name, value: parsedValue});
Expand Down
26 changes: 26 additions & 0 deletions test/ODataHelperTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ describe('#buildUrlParamString()', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: 'DisplayName' });
expect(urlParamString).to.be.eq('$orderby=DisplayName&metadata=no');
});

it('should parse a single orderby expression', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: 'Name' });
expect(urlParamString).to.be.eq('$orderby=Name&metadata=no');
});

it('should parse an orderby array with fields expression', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: ['Name', 'DisplayName'] });
expect(urlParamString).to.be.eq('$orderby=Name,DisplayName&metadata=no');
});

it('should parse an orderby field expression with order', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: [['Name', 'asc']] });
expect(urlParamString).to.be.eq('$orderby=Name asc&metadata=no');
});

it('should parse an orderby array with ordered fields list expression', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: [['Name', 'asc'], ['DisplayName', 'desc']] });
expect(urlParamString).to.be.eq('$orderby=Name asc,DisplayName desc&metadata=no');
});

it('should parse an orderby array with ordered fields list expression and field names', () => {
const urlParamString = ODataHelper.buildUrlParamString({DefaultMetadata: 'no'}, { orderby: [['Name', 'asc'], 'DisplayName'] });
expect(urlParamString).to.be.eq('$orderby=Name asc,DisplayName&metadata=no');
});

it('should return a string without any param', () => {
const urlParamString = ODataHelper.buildUrlParamString({RequiredSelect: ['Id', 'Type'], DefaultMetadata: 'no'});
expect(urlParamString).to.be.eq('');
Expand Down

0 comments on commit 14cf468

Please sign in to comment.