Skip to content

Commit

Permalink
inital prototype of param builder
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickarlt committed Apr 12, 2019
1 parent b6c40ef commit c3dd95a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 6 deletions.
112 changes: 112 additions & 0 deletions packages/arcgis-rest-items/src/SearchBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { IParamBuilder } from "@esri/arcgis-rest-request";

export class SearchQueryBuilder implements IParamBuilder {
private termStack: any[] = [];
private rangeStack: any[] = [];
private q: string;

constructor(q: string = "") {
this.q = q;
}

public match(...terms: any[]) {
this.termStack = this.termStack.concat(terms);
return this;
}

public in(field: string) {
if (field && field !== "*") {
this.q += `${field}: `;
}
this.commit();
return this;
}

public startGroup() {
this.commit();
this.q += "(";
return this;
}

public endGroup() {
this.q += ")";
return this;
}

public and() {
this.commit();
this.q += " AND ";
return this;
}

public or() {
this.commit();
this.q += " OR ";
return this;
}

public not() {
this.commit();
this.q += " NOT ";
return this;
}

public from(term: any) {
this.rangeStack[0] = term;
return this;
}

public to(term: any) {
this.rangeStack[1] = term;
return this;
}

public boost(num: number) {
this.commit();
this.q += "^${num}";
return this;
}

public toParam() {
return this.q;
}

public clone() {
return new SearchQueryBuilder(this.q + "");
}

private hasWhiteSpace(s: string) {
return /\s/g.test(s);
}

private formatTerm(term: any) {
if (term instanceof Date) {
return term.getTime();
}

if (typeof term === "string" && this.hasWhiteSpace(term)) {
return `"${term}"`;
}

return term;
}

private commit() {
if (this.rangeStack.length && this.rangeStack[0] && this.rangeStack[1]) {
this.q += `[${this.formatTerm(this.rangeStack[0])} TO ${this.formatTerm(
this.rangeStack[1]
)}]`;
this.rangeStack = [undefined, undefined];
}

if (this.termStack.length) {
this.q += this.termStack
.map(term => {
return term;
})
.join(" ");
}

this.termStack = [];
}
}
31 changes: 27 additions & 4 deletions packages/arcgis-rest-items/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
} from "@esri/arcgis-rest-request";

import { IPagingParams, IItem } from "@esri/arcgis-rest-common-types";
import { SearchQueryBuilder } from "./SearchBuilder";

// this interface still needs to be docced
export interface ISearchRequest extends IPagingParams {
q: string;
q: string | SearchQueryBuilder;
[key: string]: any;
}

Expand All @@ -29,6 +30,7 @@ export interface ISearchResult {
num: number;
nextStart: number;
results: IItem[];
nextPage?: () => Promise<ISearchRequest>;
}

/**
Expand All @@ -44,14 +46,14 @@ export interface ISearchResult {
* @returns A Promise that will resolve with the data from the response.
*/
export function searchItems(
search: string | ISearchRequestOptions
search: string | ISearchRequestOptions | SearchQueryBuilder
): Promise<ISearchResult> {
let options: ISearchRequestOptions = {
httpMethod: "GET",
params: {}
};

if (typeof search === "string") {
if (typeof search === "string" || search instanceof SearchQueryBuilder) {
options.params.q = search;
} else {
// mixin user supplied requestOptions with defaults
Expand All @@ -71,5 +73,26 @@ export function searchItems(
const url = `${getPortalUrl(options)}/search`;

// send the request
return request(url, options);
return request(url, options).then(r => {
if (options.rawResponse) {
return r;
}

if (r.nextStart === -1) {
r.nextPage = function() {
const newOptions = {
...options,
...{
params: {
...options.params,
...{ start: r.nextStart }
}
}
};
return request(url, newOptions);
};
}

return r;
});
}
1 change: 1 addition & 0 deletions packages/arcgis-rest-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./utils/ArcGISRequestError";
export * from "./utils/retryAuthError";
export * from "./utils/ErrorTypes";
export * from "./utils/params";
export * from "./utils/IParamBuilder";
export * from "./utils/process-params";
export * from "./utils/get-portal";
export * from "./utils/get-portal-url";
Expand Down
3 changes: 3 additions & 0 deletions packages/arcgis-rest-request/src/utils/IParamBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IParamBuilder {
toParam(): any;
}
13 changes: 11 additions & 2 deletions packages/arcgis-rest-request/src/utils/process-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
*/
export function requiresFormData(params: any) {
return Object.keys(params).some(key => {
const value = params[key];
let value = params[key];

if (!value) {
return false;
}

if (value.toParam) {
value = value.toParam();
}

const type = value.constructor.name;

switch (type) {
Expand Down Expand Up @@ -46,7 +50,12 @@ export function processParams(params: any): any {
const newParams: any = {};

Object.keys(params).forEach(key => {
const param = params[key];
let param = params[key];

if (param.toParams) {
param = param.toParam();
}

if (
!param &&
param !== 0 &&
Expand Down

0 comments on commit c3dd95a

Please sign in to comment.