Skip to content

Commit

Permalink
Convert Rest to TS. Fixes dojo#146
Browse files Browse the repository at this point in the history
  • Loading branch information
maier49 committed Sep 11, 2015
1 parent 067d9bd commit d91e51e
Show file tree
Hide file tree
Showing 11 changed files with 730 additions and 862 deletions.
171 changes: 85 additions & 86 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,53 +113,6 @@ abstract class Request<T> extends Store<T> implements dstore.Collection<T> {
this.descendingPrefix = '-';
this.accepts = 'application/json';
}

protected _request(kwArgs: dstore.FetchArgs = {}): RequestResponse<T> {
// perform the actual query
const headers = <Hash<string>> lang.mixin(Object.create(this.headers), { Accept: this.accepts });

if ('headers' in kwArgs) {
lang.mixin(headers, kwArgs.headers);
}

const requestUrl = this._renderUrl(kwArgs.queryParams);

const response = request(requestUrl, {
method: 'GET',
headers: headers
});

const parsedResponse = response.then((response: Response<string>) => {
return this.parse(response.data);
});
return {
data: parsedResponse.then((data) => {
// support items in the results
const results = data.items || data;
for (let i = 0, l = results.length; i < l; i++) {
results[i] = this._restore(results[i], true);
}
return results;
}),
total: parsedResponse.then(function (data) {
// check for a total property
const total = data.total;
if (total > -1) {
// if we have a valid positive number from the data,
// we can use that
return total;
}
// else use headers
return response.then(function (response) {
const rangeHeader = response.getHeader('Content-Range');
let rangeMatch = rangeHeader ? rangeHeader.match(/\/(.*)/) : null;
return rangeMatch ? Number(rangeMatch[1]) : null;
});
}),
response: response
};
}

/**
* Constructs filter-related params to be inserted into the query string
*
Expand Down Expand Up @@ -201,24 +154,36 @@ abstract class Request<T> extends Store<T> implements dstore.Collection<T> {
return [ encodedFilterArg + '=' + encodedFilterType + encodedTarget ];
}

protected _renderQueryParams(): string[] {
const queryParams: string[] = [];

this.queryLog.forEach(function (entry: dstore.QueryLogEntry<any>) {
const type = entry.type,
renderMethod = '_render' + type[0].toUpperCase() + type.substr(1) + 'Params';

if (this[renderMethod]) {
push.apply(queryParams, this[renderMethod].apply(this, entry.normalizedArguments));
} else {
console.warn('Unable to render query params for "' + type + '" query', entry);
}
}, this);

return queryParams;
}

/**
* Constructs sort-related params to be inserted in the query string
* Applies a Range header if this collection incorporates a range query
*
* @param sort An array of sort options indicating to render as a query string
* @return Sort-related params to be inserted in the query string
* @param start The start of the range
* @param end The end of the range
* @return The headers to which a Range property is added
*/
protected _renderSortParams(sort: dstore.SortOption[]): string[] {
const sortString = sort.map(function (sortOption: dstore.SortOption) {
const prefix = sortOption.descending ? this.descendingPrefix : this.ascendingPrefix;
return prefix + encodeURIComponent(sortOption.property);
}, this);

const params: string[] = [];
params.push(this.sortParam
? encodeURIComponent(this.sortParam) + '=' + sortString
: 'sort(' + sortString + ')'
);
return params;
protected _renderRangeHeaders(start: number, end: number): { Range: string; 'X-Range': string } {
const value = 'items=' + start + '-' + (end - 1);
return {
'Range': value,
'X-Range': value // set X-Range for Opera since it blocks "Range" header
};
}

/**
Expand Down Expand Up @@ -257,21 +222,24 @@ abstract class Request<T> extends Store<T> implements dstore.Collection<T> {
return params;
}

protected _renderQueryParams(): string[] {
const queryParams: string[] = [];

this.queryLog.forEach(function (entry: dstore.QueryLogEntry<any>) {
const type = entry.type,
renderMethod = '_render' + type[0].toUpperCase() + type.substr(1) + 'Params';

if (this[ renderMethod ]) {
push.apply(queryParams, this[renderMethod].apply(this, entry.normalizedArguments));
} else {
console.warn('Unable to render query params for "' + type + '" query', entry);
}
/**
* Constructs sort-related params to be inserted in the query string
*
* @param sort An array of sort options indicating to render as a query string
* @return Sort-related params to be inserted in the query string
*/
protected _renderSortParams(sort: dstore.SortOption[]): string[] {
const sortString = sort.map(function (sortOption: dstore.SortOption) {
const prefix = sortOption.descending ? this.descendingPrefix : this.ascendingPrefix;
return prefix + encodeURIComponent(sortOption.property);
}, this);

return queryParams;
const params: string[] = [];
params.push(this.sortParam
? encodeURIComponent(this.sortParam) + '=' + sortString
: 'sort(' + sortString + ')'
);
return params;
}

/**
Expand All @@ -294,18 +262,49 @@ abstract class Request<T> extends Store<T> implements dstore.Collection<T> {
return requestUrl;
}

/**
* Applies a Range header if this collection incorporates a range query
*
* @param start The start of the range
* @param end The end of the range
* @return The headers to which a Range property is added
*/
protected _renderRangeHeaders(start: number, end: number): { Range: string; 'X-Range': string } {
const value = 'items=' + start + '-' + (end - 1);
protected _request(kwArgs: dstore.FetchArgs = {}): RequestResponse<T> {
// perform the actual query
const headers = <Hash<string>> lang.mixin(Object.create(this.headers), { Accept: this.accepts });

if ('headers' in kwArgs) {
lang.mixin(headers, kwArgs.headers);
}

const requestUrl = this._renderUrl(kwArgs.queryParams);

const response = request(requestUrl, {
method: 'GET',
headers: headers
});

const parsedResponse = response.then((response: Response<string>) => {
return this.parse(response.data);
});
return {
'Range': value,
'X-Range': value // set X-Range for Opera since it blocks "Range" header
data: parsedResponse.then((data) => {
// support items in the results
const results = data.items || data;
for (let i = 0, l = results.length; i < l; i++) {
results[i] = this._restore(results[i], true);
}
return results;
}),
total: parsedResponse.then(function (data) {
// check for a total property
const total = data.total;
if (total > -1) {
// if we have a valid positive number from the data,
// we can use that
return total;
}
// else use headers
return response.then(function (response) {
const rangeHeader = response.getHeader('Content-Range');
let rangeMatch = rangeHeader ? rangeHeader.match(/\/(.*)/) : null;
return rangeMatch ? Number(rangeMatch[1]) : null;
});
}),
response: response
};
}

Expand Down
159 changes: 0 additions & 159 deletions src/Rest.js

This file was deleted.

Loading

0 comments on commit d91e51e

Please sign in to comment.