├── .gitignore ├── lib ├── Cache │ ├── KeyValueCache.js │ ├── KeyValueCache.d.ts │ ├── NoCache.d.ts │ ├── InMemoryCache.d.ts │ ├── NoCache.js │ └── InMemoryCache.js ├── Transformer │ ├── ReadTransformer.js │ ├── WriteTransformer.js │ ├── ReadTransformer.d.ts │ ├── WriteTransformer.d.ts │ └── Transformer.d.ts ├── Http │ ├── HttpClient.js │ ├── Client.d.ts │ ├── Response.d.ts │ ├── RetryMap.d.ts │ ├── HttpClient.d.ts │ ├── TestClient.d.ts │ ├── Response.js │ ├── Client.js │ ├── CacheClient.d.ts │ ├── Retry.d.ts │ ├── AxiosClient.d.ts │ ├── RetryMap.js │ ├── Retry.js │ ├── TestClient.js │ └── CacheClient.js ├── Error │ ├── ErrorWithMessage.d.ts │ ├── UnknownError.d.ts │ ├── EventError.d.ts │ ├── ConnectionError.d.ts │ ├── ErrorWithMessage.js │ ├── UnsupportedContentTypeError.d.ts │ ├── UnknownError.js │ ├── EventError.js │ ├── ConnectionError.js │ ├── ForbiddenError.d.ts │ ├── ResourceExistsError.d.ts │ ├── InvalidTokenError.d.ts │ ├── UnsupportedContentTypeError.js │ ├── ResourceNotAvailableError.d.ts │ ├── InvalidTokenError.js │ ├── ForbiddenError.js │ ├── ResourceExistsError.js │ ├── ResourceNotAvailableError.js │ └── InvalidFormatError.d.ts ├── Model │ ├── Metadata.d.ts │ ├── User.d.ts │ ├── Coordinate.d.ts │ ├── AppUUID.d.ts │ ├── IndexUUID.d.ts │ ├── ItemUUID.d.ts │ ├── AppUUID.js │ ├── Coordinate.js │ ├── IndexUUID.js │ ├── Index.d.ts │ ├── Metadata.js │ ├── User.js │ ├── Changes.d.ts │ ├── ItemUUID.js │ ├── Index.js │ └── Changes.js ├── Query │ ├── Range.d.ts │ ├── ScoreStrategies.d.ts │ ├── Range.js │ ├── Filter.d.ts │ ├── Aggregation.d.ts │ └── ScoreStrategies.js ├── Config │ ├── Synonym.d.ts │ ├── ImmutableConfig.d.ts │ ├── Synonym.js │ ├── Config.d.ts │ └── ImmutableConfig.js ├── Result │ ├── ResultAggregations.d.ts │ ├── Counter.d.ts │ ├── ResultAggregation.d.ts │ ├── ResultAggregations.js │ └── Counter.js ├── index.d.ts ├── index.js └── Apisearch.d.ts ├── tslint.json ├── tsconfig.json ├── test ├── Transformer │ ├── Product.ts │ ├── ProductReadTransformer.ts │ ├── ProductWriteTransformer.ts │ └── Transformer.test.ts ├── Error │ ├── ForbiddenErrorTest.ts │ ├── ConnectionErrorTest.ts │ ├── InvalidTokenErrorTest.ts │ ├── InvalidFormatErrorTest.ts │ ├── ResourceExistsErrorTest.ts │ ├── ResourceNotAvailableErrorTest.ts │ └── UnsupportedContentTypeErrorTest.ts ├── HttpHelper.ts ├── Model │ ├── Metadata.test.ts │ ├── AppUUID.test.ts │ ├── IndexUUID.test.ts │ ├── Coordinate.test.ts │ ├── User.test.ts │ ├── ItemUUID.test.ts │ └── Index.test.ts ├── Query │ ├── ScoreStrategies.test.ts │ ├── Range.test.ts │ └── Filter.test.ts ├── Config │ ├── Synonym.test.ts │ └── Config.test.ts ├── Result │ └── ResultAggregations.test.ts └── Geo │ ├── CoordinateAndDistance.test.ts │ └── Square.test.ts ├── src ├── Error │ ├── UnknownError.ts │ ├── ErrorWithMessage.ts │ ├── ConnectionError.ts │ ├── EventError.ts │ ├── UnsupportedContentTypeError.ts │ ├── InvalidTokenError.ts │ ├── ResourceExistsError.ts │ ├── ForbiddenError.ts │ └── ResourceNotAvailableError.ts ├── Transformer │ ├── ReadTransformer.ts │ └── WriteTransformer.ts ├── Http │ ├── Response.ts │ ├── Client.ts │ ├── HttpClient.ts │ ├── TestClient.ts │ └── CacheClient.ts ├── Config │ ├── Synonym.ts │ └── Config.ts ├── Model │ ├── AppUUID.ts │ ├── Metadata.ts │ ├── IndexUUID.ts │ ├── Coordinate.ts │ ├── User.ts │ ├── ItemUUID.ts │ ├── Index.ts │ └── Changes.ts ├── index.ts ├── Query │ ├── Range.ts │ └── ScoreStrategies.ts └── Result │ └── ResultAggregations.ts ├── .editorconfig ├── .circleci └── config.yml ├── webpack.base.js ├── webpack.dev.js ├── webpack.min.js ├── docker-compose └── docker-compose-infra.yml ├── LICENSE ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | dist/apisearch.min.js.LICENSE.txt 3 | -------------------------------------------------------------------------------- /lib/Cache/KeyValueCache.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | -------------------------------------------------------------------------------- /lib/Transformer/ReadTransformer.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | -------------------------------------------------------------------------------- /lib/Transformer/WriteTransformer.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | "tslint:recommended" 5 | ], 6 | "jsRules": {}, 7 | "rules": {}, 8 | "rulesDirectory": [] 9 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": [ "es2015", "DOM"], 4 | "importHelpers": true 5 | }, 6 | "include": [ 7 | "src" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /lib/Http/HttpClient.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.HttpClient = void 0; 4 | /** 5 | * Http class 6 | */ 7 | var HttpClient = /** @class */ (function () { 8 | function HttpClient() { 9 | } 10 | return HttpClient; 11 | }()); 12 | exports.HttpClient = HttpClient; 13 | -------------------------------------------------------------------------------- /lib/Error/ErrorWithMessage.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * ConnectError 3 | */ 4 | export declare abstract class ErrorWithMessage { 5 | /** 6 | * Message 7 | */ 8 | private message; 9 | /** 10 | * Constructor 11 | * 12 | * @param message 13 | */ 14 | constructor(message: string); 15 | } 16 | -------------------------------------------------------------------------------- /lib/Error/UnknownError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Connection error 4 | */ 5 | export declare class UnknownError extends ErrorWithMessage { 6 | /** 7 | * Unknown error 8 | * 9 | * @return this 10 | */ 11 | static createUnknownError(): UnknownError; 12 | } 13 | -------------------------------------------------------------------------------- /lib/Error/EventError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * EventError 4 | */ 5 | export declare class EventError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static throwEndpointNotAvailable(): EventError; 12 | } 13 | -------------------------------------------------------------------------------- /lib/Error/ConnectionError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Connection error 4 | */ 5 | export declare class ConnectionError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | } 13 | -------------------------------------------------------------------------------- /test/Transformer/Product.ts: -------------------------------------------------------------------------------- 1 | export class Product { 2 | public id; 3 | public sku; 4 | public name; 5 | 6 | /** 7 | * 8 | * @param id 9 | * @param sku 10 | * @param name 11 | */ 12 | constructor(id, sku, name) { 13 | this.id = id; 14 | this.sku = sku; 15 | this.name = name; 16 | } 17 | } -------------------------------------------------------------------------------- /src/Error/UnknownError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Connection error 5 | */ 6 | export class UnknownError extends ErrorWithMessage { 7 | 8 | /** 9 | * Unknown error 10 | * 11 | * @return this 12 | */ 13 | public static createUnknownError() { 14 | return new this("Unknown error."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Error/ErrorWithMessage.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * ConnectError 3 | */ 4 | export abstract class ErrorWithMessage { 5 | 6 | /** 7 | * Message 8 | */ 9 | private message: string; 10 | 11 | /** 12 | * Constructor 13 | * 14 | * @param message 15 | */ 16 | constructor(message: string) { 17 | this.message = message; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Error/ConnectionError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Connection error 5 | */ 6 | export class ConnectionError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 500; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/Error/ForbiddenErrorTest.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {ForbiddenError} from "../../src/Error/ForbiddenError"; 3 | 4 | describe('ForbiddenError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(ForbiddenError.getTransportableHTTPError()).to.be.equal(403); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | root = true; 4 | 5 | [*] 6 | # Ensure there's no lingering whitespace 7 | trim_trailing_whitespace = true 8 | # Ensure a newline at the end of each file 9 | insert_final_newline = true 10 | 11 | [*.ts] 12 | # Unix-style newlines 13 | end_of_line = lf 14 | charset = utf-8 15 | indent_style = space 16 | indent_size = 4 17 | -------------------------------------------------------------------------------- /test/Error/ConnectionErrorTest.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {ConnectionError} from "../../src/Error/ConnectionError"; 3 | 4 | describe('ConnectionError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(ConnectionError.getTransportableHTTPError()).to.be.equal(500); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/Error/InvalidTokenErrorTest.ts: -------------------------------------------------------------------------------- 1 | import {expect} from 'chai'; 2 | import {InvalidTokenError} from "../../src/Error/InvalidTokenError"; 3 | 4 | describe('InvalidTokenError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(InvalidTokenError.getTransportableHTTPError()).to.be.equal(401); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/Error/EventError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * EventError 5 | */ 6 | export class EventError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static throwEndpointNotAvailable() { 14 | return new EventError("Endpoint not available"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/Error/InvalidFormatErrorTest.ts: -------------------------------------------------------------------------------- 1 | import {expect} from 'chai'; 2 | import {InvalidFormatError} from "../../src/Error/InvalidFormatError"; 3 | 4 | describe('InvalidFormatError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(InvalidFormatError.getTransportableHTTPError()).to.be.equal(400); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/Error/ResourceExistsErrorTest.ts: -------------------------------------------------------------------------------- 1 | import {expect} from 'chai'; 2 | import {ResourceExistsError} from "../../src/Error/ResourceExistsError"; 3 | 4 | describe('ResourceExistsError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(ResourceExistsError.getTransportableHTTPError()).to.be.equal(409); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/Error/ResourceNotAvailableErrorTest.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {ResourceNotAvailableError} from "../../src/Error/ResourceNotAvailableError"; 3 | 4 | describe('ResourceNotAvailableError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(ResourceNotAvailableError.getTransportableHTTPError()).to.be.equal(404); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /lib/Model/Metadata.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * User class 3 | */ 4 | export declare class Metadata { 5 | /** 6 | * To metadata 7 | * 8 | * @param array:{} 9 | * 10 | * @returns {string} 11 | */ 12 | static toMetadata(array: {}): string; 13 | /** 14 | * From metadata 15 | * 16 | * @param metadata 17 | * 18 | * @return {{}} 19 | */ 20 | static fromMetadata(metadata: string): any; 21 | } 22 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/node:latest 6 | 7 | steps: 8 | - checkout 9 | - run: 10 | name: Install dependencies 11 | command: | 12 | npm install 13 | 14 | - run: 15 | name: Run test scenarios 16 | command: | 17 | npm run test 18 | -------------------------------------------------------------------------------- /test/Error/UnsupportedContentTypeErrorTest.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {UnsupportedContentTypeError} from "../../src/Error/UnsupportedContentTypeError"; 3 | 4 | describe('UnsupportedContentTypeError()', () => { 5 | describe('', () => { 6 | it('Should return transportable http error', () => { 7 | expect(UnsupportedContentTypeError.getTransportableHTTPError()).to.be.equal(415); 8 | }); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /lib/Http/Client.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Client 3 | */ 4 | export declare abstract class Client { 5 | protected version: string; 6 | /** 7 | * Constructor 8 | * 9 | * @param version 10 | */ 11 | constructor(version: string); 12 | /** 13 | * Build an url parameters array by an object 14 | * 15 | * @param params 16 | * 17 | * @returns {string} 18 | */ 19 | static objectToUrlParameters(params: any): string; 20 | } 21 | -------------------------------------------------------------------------------- /lib/Error/ErrorWithMessage.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ErrorWithMessage = void 0; 4 | /** 5 | * ConnectError 6 | */ 7 | var ErrorWithMessage = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param message 12 | */ 13 | function ErrorWithMessage(message) { 14 | this.message = message; 15 | } 16 | return ErrorWithMessage; 17 | }()); 18 | exports.ErrorWithMessage = ErrorWithMessage; 19 | -------------------------------------------------------------------------------- /test/HttpHelper.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * HttpHelper class 3 | */ 4 | export class HttpHelper { 5 | 6 | /** 7 | * Get items by types 8 | * 9 | * @param httpTransportable 10 | * 11 | * @return {Object} 12 | */ 13 | static emulateHttpTransport(httpTransportable: any): any { 14 | let array = httpTransportable.toArray(); 15 | array = JSON.parse(JSON.stringify(array)); 16 | 17 | return httpTransportable.constructor.createFromArray(array); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/Http/Response.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Response 3 | */ 4 | export declare class Response { 5 | code: number; 6 | body: any; 7 | /** 8 | * Constructor 9 | * 10 | * @param code 11 | * @param body 12 | */ 13 | constructor(code: number, body: any); 14 | /** 15 | * Get code 16 | * 17 | * @return {number} 18 | */ 19 | getCode(): number; 20 | /** 21 | * Get body 22 | * 23 | * @return {any} 24 | */ 25 | getBody(): any; 26 | } 27 | -------------------------------------------------------------------------------- /lib/Transformer/ReadTransformer.d.ts: -------------------------------------------------------------------------------- 1 | import { Item } from "../Model/Item"; 2 | /** 3 | * Read Transformer 4 | */ 5 | export interface ReadTransformer { 6 | /** 7 | * The item should be converted by this transformer. 8 | * 9 | * @param item 10 | * 11 | * @return {boolean} 12 | */ 13 | isValidItem(item: Item): boolean; 14 | /** 15 | * Create object by item. 16 | * 17 | * @param item 18 | * 19 | * @return {any} 20 | */ 21 | fromItem(item: Item): any; 22 | } 23 | -------------------------------------------------------------------------------- /webpack.base.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = { 4 | entry: path.resolve(__dirname, 'src') + '/index.ts', 5 | output: {}, 6 | module: { 7 | rules: [ 8 | { 9 | test: /\.tsx?$/, 10 | loader: 'ts-loader', 11 | include: [ 12 | path.resolve(__dirname, "src"), 13 | ] 14 | } 15 | ] 16 | }, 17 | resolve: { 18 | extensions: [ '.tsx', '.ts', '.js', '.json' ] 19 | } 20 | }; -------------------------------------------------------------------------------- /src/Transformer/ReadTransformer.ts: -------------------------------------------------------------------------------- 1 | import {Item} from "../Model/Item"; 2 | /** 3 | * Read Transformer 4 | */ 5 | export interface ReadTransformer { 6 | 7 | /** 8 | * The item should be converted by this transformer. 9 | * 10 | * @param item 11 | * 12 | * @return {boolean} 13 | */ 14 | isValidItem(item: Item): boolean; 15 | 16 | /** 17 | * Create object by item. 18 | * 19 | * @param item 20 | * 21 | * @return {any} 22 | */ 23 | fromItem(item: Item): any; 24 | } 25 | -------------------------------------------------------------------------------- /lib/Error/UnsupportedContentTypeError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Unsupported content type error 4 | */ 5 | export declare class UnsupportedContentTypeError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * Unsupported content type 14 | * 15 | * @return {InvalidFormatError} 16 | */ 17 | static createUnsupportedContentTypeException(): UnsupportedContentTypeError; 18 | } 19 | -------------------------------------------------------------------------------- /lib/Http/RetryMap.d.ts: -------------------------------------------------------------------------------- 1 | import { Retry, RetryConfig } from "./Retry"; 2 | /** 3 | * Http class 4 | */ 5 | export declare class RetryMap { 6 | /** 7 | * Create from array 8 | */ 9 | static createFromArray(array: RetryConfig[]): RetryMap; 10 | private retries; 11 | /** 12 | * Add retry 13 | * 14 | * @param retry 15 | */ 16 | addRetry(retry: Retry): void; 17 | /** 18 | * Get retry 19 | * 20 | * @param url 21 | * @param method 22 | * 23 | * @returns {Retry} 24 | */ 25 | getRetry(url: string, method: string): Retry; 26 | } 27 | -------------------------------------------------------------------------------- /webpack.dev.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | const merge = require('webpack-merge'); 4 | const baseConfig = require('./webpack.base'); 5 | 6 | /** 7 | * Development configuration 8 | */ 9 | const browserConfig = merge(baseConfig, { 10 | devtool: 'source-map', 11 | target: 'web', 12 | mode: 'development', 13 | output: { 14 | path: path.resolve(__dirname, 'dist'), 15 | filename: 'apisearch.js', 16 | libraryTarget: 'umd', 17 | library: 'apisearch', 18 | libraryExport: 'default' 19 | } 20 | }); 21 | 22 | module.exports = [browserConfig]; -------------------------------------------------------------------------------- /webpack.min.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | const merge = require('webpack-merge'); 4 | const baseConfig = require('./webpack.base'); 5 | 6 | /** 7 | * Production configuration 8 | */ 9 | const browserConfig = merge(baseConfig, { 10 | devtool: 'source-map', 11 | mode: 'production', 12 | target: 'web', 13 | output: { 14 | path: path.resolve(__dirname, 'dist'), 15 | filename: 'apisearch.min.js', 16 | libraryTarget: 'umd', 17 | library: 'apisearch', 18 | libraryExport: 'default' 19 | } 20 | }); 21 | 22 | module.exports = [browserConfig]; -------------------------------------------------------------------------------- /lib/Cache/KeyValueCache.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Cache class 3 | */ 4 | export interface KeyValueCache { 5 | /** 6 | * Set cache element 7 | * 8 | * @param key 9 | * @param value 10 | * 11 | * @returns {void} 12 | */ 13 | set(key: string, value: any): void; 14 | /** 15 | * Get element from cache 16 | * 17 | * @param key 18 | * 19 | * @returns {any} 20 | */ 21 | get(key: string): any; 22 | /** 23 | * Deletes element from cache 24 | * 25 | * @param key 26 | */ 27 | del(key: string): any; 28 | /** 29 | * Clear cache 30 | */ 31 | clear(): any; 32 | } 33 | -------------------------------------------------------------------------------- /lib/Cache/NoCache.d.ts: -------------------------------------------------------------------------------- 1 | import { KeyValueCache } from "./KeyValueCache"; 2 | /** 3 | * Cache class 4 | */ 5 | export declare class NoCache implements KeyValueCache { 6 | /** 7 | * Set cache element 8 | * 9 | * @param key 10 | * @param value 11 | * 12 | * @returns {void} 13 | */ 14 | set(key: string, value: any): void; 15 | /** 16 | * Get element from cache 17 | * 18 | * @param key 19 | * 20 | * @returns {any} 21 | */ 22 | get(key: string): any; 23 | /** 24 | * Deletes element from cache 25 | * 26 | * @param key 27 | */ 28 | del(key: string): void; 29 | /** 30 | * Clear cache 31 | */ 32 | clear(): void; 33 | } 34 | -------------------------------------------------------------------------------- /src/Http/Response.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Response 3 | */ 4 | export class Response { 5 | 6 | code: number; 7 | body: any; 8 | 9 | /** 10 | * Constructor 11 | * 12 | * @param code 13 | * @param body 14 | */ 15 | constructor( 16 | code: number, 17 | body: any, 18 | ) { 19 | this.code = code; 20 | this.body = body; 21 | } 22 | 23 | /** 24 | * Get code 25 | * 26 | * @return {number} 27 | */ 28 | public getCode(): number { 29 | return this.code; 30 | } 31 | 32 | /** 33 | * Get body 34 | * 35 | * @return {any} 36 | */ 37 | public getBody(): any { 38 | return this.body; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Error/UnsupportedContentTypeError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Unsupported content type error 5 | */ 6 | export class UnsupportedContentTypeError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 415; 15 | } 16 | 17 | /** 18 | * Unsupported content type 19 | * 20 | * @return {InvalidFormatError} 21 | */ 22 | public static createUnsupportedContentTypeException() { 23 | return new UnsupportedContentTypeError("This content type is not accepted. Please use application/json"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lib/Http/HttpClient.d.ts: -------------------------------------------------------------------------------- 1 | import { Response } from "./Response"; 2 | /** 3 | * Http class 4 | */ 5 | export declare abstract class HttpClient { 6 | /** 7 | * Get 8 | * 9 | * @param url 10 | * @param method 11 | * @param credentials 12 | * @param parameters 13 | * @param data 14 | * 15 | * @return {Promise} 16 | */ 17 | abstract get(url: string, method: string, credentials: any, parameters: any, data: any): Promise; 18 | /** 19 | * Abort current request 20 | * And regenerate the cancellation token 21 | * 22 | * @param url 23 | * @param urlIsFormatted 24 | */ 25 | abstract abort(url: string, urlIsFormatted: boolean): any; 26 | } 27 | -------------------------------------------------------------------------------- /lib/Transformer/WriteTransformer.d.ts: -------------------------------------------------------------------------------- 1 | import { Item } from "../Model/Item"; 2 | import { ItemUUID } from "../Model/ItemUUID"; 3 | /** 4 | * Write Transformer 5 | */ 6 | export interface WriteTransformer { 7 | /** 8 | * Is an indexable object. 9 | * 10 | * @param object 11 | * 12 | * @return {boolean} 13 | */ 14 | isValidObject(object: any): boolean; 15 | /** 16 | * Create item by object. 17 | * 18 | * @param object 19 | * 20 | * @return {Item} 21 | */ 22 | toItem(object: any): Item; 23 | /** 24 | * Create item UUID by object. 25 | * 26 | * @param object 27 | * 28 | * @return {ItemUUID} 29 | */ 30 | toItemUUID(object: any): ItemUUID; 31 | } 32 | -------------------------------------------------------------------------------- /src/Transformer/WriteTransformer.ts: -------------------------------------------------------------------------------- 1 | import {Item} from "../Model/Item"; 2 | import {ItemUUID} from "../Model/ItemUUID"; 3 | /** 4 | * Write Transformer 5 | */ 6 | export interface WriteTransformer { 7 | 8 | /** 9 | * Is an indexable object. 10 | * 11 | * @param object 12 | * 13 | * @return {boolean} 14 | */ 15 | isValidObject(object: any): boolean; 16 | 17 | /** 18 | * Create item by object. 19 | * 20 | * @param object 21 | * 22 | * @return {Item} 23 | */ 24 | toItem(object): Item; 25 | 26 | /** 27 | * Create item UUID by object. 28 | * 29 | * @param object 30 | * 31 | * @return {ItemUUID} 32 | */ 33 | toItemUUID(object): ItemUUID; 34 | } 35 | -------------------------------------------------------------------------------- /lib/Http/TestClient.d.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from "./HttpClient"; 2 | import { Response } from "./Response"; 3 | export declare class TestClient implements HttpClient { 4 | calls: any[]; 5 | /** 6 | * Get 7 | * 8 | * @param url 9 | * @param method 10 | * @param credentials 11 | * @param parameters 12 | * @param data 13 | * 14 | * @return {Promise} 15 | */ 16 | get(url: string, method: string, credentials: any, parameters?: any, data?: any): Promise; 17 | /** 18 | * Abort current request 19 | * And regenerate the cancellation token 20 | * 21 | * @param url 22 | * @param urlIsFormatted 23 | */ 24 | abort(url: string, urlIsFormatted: boolean): void; 25 | } 26 | -------------------------------------------------------------------------------- /src/Http/Client.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Client 3 | */ 4 | export abstract class Client { 5 | protected version: string; 6 | 7 | /** 8 | * Constructor 9 | * 10 | * @param version 11 | */ 12 | constructor(version: string) { 13 | this.version = version.replace(/^\/*|\/*$/g, ""); 14 | } 15 | 16 | /** 17 | * Build an url parameters array by an object 18 | * 19 | * @param params 20 | * 21 | * @returns {string} 22 | */ 23 | public static objectToUrlParameters(params: any): string { 24 | 25 | const builtParams: string[] = []; 26 | 27 | for (const i in params) { 28 | builtParams.push(i + "=" + params[i]); 29 | } 30 | 31 | return builtParams.join("&"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lib/Error/UnknownError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.UnknownError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Connection error 8 | */ 9 | var UnknownError = /** @class */ (function (_super) { 10 | tslib_1.__extends(UnknownError, _super); 11 | function UnknownError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Unknown error 16 | * 17 | * @return this 18 | */ 19 | UnknownError.createUnknownError = function () { 20 | return new this("Unknown error."); 21 | }; 22 | return UnknownError; 23 | }(ErrorWithMessage_1.ErrorWithMessage)); 24 | exports.UnknownError = UnknownError; 25 | -------------------------------------------------------------------------------- /lib/Error/EventError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.EventError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * EventError 8 | */ 9 | var EventError = /** @class */ (function (_super) { 10 | tslib_1.__extends(EventError, _super); 11 | function EventError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | EventError.throwEndpointNotAvailable = function () { 20 | return new EventError("Endpoint not available"); 21 | }; 22 | return EventError; 23 | }(ErrorWithMessage_1.ErrorWithMessage)); 24 | exports.EventError = EventError; 25 | -------------------------------------------------------------------------------- /lib/Error/ConnectionError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ConnectionError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Connection error 8 | */ 9 | var ConnectionError = /** @class */ (function (_super) { 10 | tslib_1.__extends(ConnectionError, _super); 11 | function ConnectionError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | ConnectionError.getTransportableHTTPError = function () { 20 | return 500; 21 | }; 22 | return ConnectionError; 23 | }(ErrorWithMessage_1.ErrorWithMessage)); 24 | exports.ConnectionError = ConnectionError; 25 | -------------------------------------------------------------------------------- /lib/Http/Response.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Response = void 0; 4 | /** 5 | * Response 6 | */ 7 | var Response = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param code 12 | * @param body 13 | */ 14 | function Response(code, body) { 15 | this.code = code; 16 | this.body = body; 17 | } 18 | /** 19 | * Get code 20 | * 21 | * @return {number} 22 | */ 23 | Response.prototype.getCode = function () { 24 | return this.code; 25 | }; 26 | /** 27 | * Get body 28 | * 29 | * @return {any} 30 | */ 31 | Response.prototype.getBody = function () { 32 | return this.body; 33 | }; 34 | return Response; 35 | }()); 36 | exports.Response = Response; 37 | -------------------------------------------------------------------------------- /lib/Http/Client.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Client = void 0; 4 | /** 5 | * Client 6 | */ 7 | var Client = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param version 12 | */ 13 | function Client(version) { 14 | this.version = version.replace(/^\/*|\/*$/g, ""); 15 | } 16 | /** 17 | * Build an url parameters array by an object 18 | * 19 | * @param params 20 | * 21 | * @returns {string} 22 | */ 23 | Client.objectToUrlParameters = function (params) { 24 | var builtParams = []; 25 | for (var i in params) { 26 | builtParams.push(i + "=" + params[i]); 27 | } 28 | return builtParams.join("&"); 29 | }; 30 | return Client; 31 | }()); 32 | exports.Client = Client; 33 | -------------------------------------------------------------------------------- /lib/Error/ForbiddenError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Forbidden Error 4 | */ 5 | export declare class ForbiddenError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * App id is required 14 | * 15 | * @return {ForbiddenError} 16 | */ 17 | static createAppIdIsRequiredException(): ForbiddenError; 18 | /** 19 | * Index id is required 20 | * 21 | * @return {ForbiddenError} 22 | */ 23 | static createIndexIsRequiredException(): ForbiddenError; 24 | /** 25 | * Token is required 26 | * 27 | * @return {ForbiddenError} 28 | */ 29 | static createTokenIsRequiredException(): ForbiddenError; 30 | } 31 | -------------------------------------------------------------------------------- /lib/Error/ResourceExistsError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Resource exists error 4 | */ 5 | export declare class ResourceExistsError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * Index not available 14 | * 15 | * @return {InvalidFormatError} 16 | */ 17 | static indexAvailable(): ResourceExistsError; 18 | /** 19 | * Events not available 20 | * 21 | * @return {InvalidFormatError} 22 | */ 23 | static eventsIndexAvailable(): ResourceExistsError; 24 | /** 25 | * Logs not available 26 | * 27 | * @return {InvalidFormatError} 28 | */ 29 | static logsIndexAvailable(): ResourceExistsError; 30 | } 31 | -------------------------------------------------------------------------------- /lib/Cache/InMemoryCache.d.ts: -------------------------------------------------------------------------------- 1 | import { KeyValueCache } from "./KeyValueCache"; 2 | /** 3 | * Cache class 4 | */ 5 | export declare class InMemoryCache implements KeyValueCache { 6 | private cache; 7 | private size; 8 | /** 9 | * Constructor 10 | */ 11 | constructor(); 12 | /** 13 | * Set cache element 14 | * 15 | * @param key 16 | * @param value 17 | * 18 | * @returns {void} 19 | */ 20 | set(key: string, value: any): void; 21 | /** 22 | * Get element from cache 23 | * 24 | * @param key 25 | * 26 | * @returns {any} 27 | */ 28 | get(key: string): any | null; 29 | /** 30 | * Deletes element from cache 31 | * 32 | * @param key 33 | */ 34 | del(key: string): void; 35 | /** 36 | * Clear cache 37 | */ 38 | clear(): void; 39 | } 40 | -------------------------------------------------------------------------------- /src/Http/HttpClient.ts: -------------------------------------------------------------------------------- 1 | import {Response} from "./Response"; 2 | /** 3 | * Http class 4 | */ 5 | export abstract class HttpClient { 6 | 7 | /** 8 | * Get 9 | * 10 | * @param url 11 | * @param method 12 | * @param credentials 13 | * @param parameters 14 | * @param data 15 | * 16 | * @return {Promise} 17 | */ 18 | public abstract async get( 19 | url: string, 20 | method: string, 21 | credentials: any, 22 | parameters: any, 23 | data: any, 24 | ): Promise; 25 | 26 | /** 27 | * Abort current request 28 | * And regenerate the cancellation token 29 | * 30 | * @param url 31 | * @param urlIsFormatted 32 | */ 33 | public abstract abort( 34 | url: string, 35 | urlIsFormatted: boolean, 36 | ); 37 | } 38 | -------------------------------------------------------------------------------- /docker-compose/docker-compose-infra.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | 5 | elasticsearch: 6 | image: "docker.elastic.co/elasticsearch/elasticsearch:7.6.0" 7 | networks: [apisearch] 8 | ports: 9 | - 9200:9200 10 | environment: 11 | - "ES_JAVA_OPTS=-Xms256m -Xmx256m" 12 | - "discovery.type=single-node" 13 | - "action.auto_create_index=-apisearch*,+*" 14 | 15 | search-server: 16 | image: "apisearchio/search-server:commit-447d5c6" 17 | networks: [apisearch] 18 | ports: 19 | - 8000:8000 20 | environment: 21 | APISEARCH_GOD_TOKEN: 0e4d75ba-c640-44c1-a745-06ee51db4e93 22 | APISEARCH_ENABLED_PLUGINS: elasticsearch 23 | ELASTICSEARCH_HOST: elasticsearch 24 | ELASTICSEARCH_REFRESH_ON_WRITE: 1 25 | 26 | networks: 27 | apisearch: 28 | -------------------------------------------------------------------------------- /lib/Error/InvalidTokenError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Invalid token error 4 | */ 5 | export declare class InvalidTokenError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * Invalid token permissions 14 | * 15 | * @param tokenReference 16 | * 17 | * @return {InvalidTokenError} 18 | */ 19 | static createInvalidTokenPermissions(tokenReference: any): InvalidTokenError; 20 | /** 21 | * Invalid token permissions 22 | * 23 | * @param tokenReference 24 | * @param maxHitsPerQuery 25 | * 26 | * @return {InvalidTokenError} 27 | */ 28 | static createInvalidTokenMaxHitsPerQuery(tokenReference: any, maxHitsPerQuery: any): InvalidTokenError; 29 | } 30 | -------------------------------------------------------------------------------- /lib/Query/Range.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Aggregation constants 3 | */ 4 | export declare const RANGE_MINUS_INFINITE: any; 5 | export declare const RANGE_INFINITE: any; 6 | export declare const RANGE_SEPARATOR = ".."; 7 | /** 8 | * Filter class 9 | */ 10 | export declare class Range { 11 | /** 12 | * Strong to array 13 | * 14 | * @param string 15 | * 16 | * @returns {[number, number]} 17 | */ 18 | static stringToArray(string: string): [number, number]; 19 | /** 20 | * Array to string 21 | * 22 | * @param values 23 | * 24 | * @return {string} 25 | */ 26 | static arrayToString(values: [number, number]): string; 27 | /** 28 | * Create ranges 29 | * 30 | * @param from 31 | * @param to 32 | * @param incremental 33 | */ 34 | static createRanges(from: number, to: number, incremental: number): string[]; 35 | } 36 | -------------------------------------------------------------------------------- /test/Transformer/ProductReadTransformer.ts: -------------------------------------------------------------------------------- 1 | import {ReadTransformer} from "../../src/Transformer/ReadTransformer"; 2 | import {Product} from "./Product"; 3 | import {Item} from "../../src/Model/Item"; 4 | 5 | /** 6 | * Product ReadTransformer 7 | */ 8 | export class ProductReadTransformer implements ReadTransformer { 9 | 10 | /** 11 | * The item should be converted by this transformer. 12 | * 13 | * @param item 14 | * 15 | * @return {boolean} 16 | */ 17 | isValidItem(item:Item): boolean { 18 | return item.getType() === 'product'; 19 | } 20 | 21 | /** 22 | * Create object by item. 23 | * 24 | * @param item 25 | * 26 | * @return {any} 27 | */ 28 | fromItem(item:Item) : any { 29 | return new Product( 30 | item.getId(), 31 | item.get('sku'), 32 | item.get('name') 33 | ); 34 | } 35 | } -------------------------------------------------------------------------------- /lib/Model/User.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * User class 3 | */ 4 | export declare class User { 5 | private id; 6 | private attributes; 7 | /** 8 | * Construct 9 | * 10 | * @param id string 11 | * @param attributes Array 12 | */ 13 | constructor(id: string, attributes?: Object); 14 | /** 15 | * Return the user id 16 | * 17 | * @return {string} 18 | */ 19 | getId(): string; 20 | /** 21 | * Return array 22 | * 23 | * @returns {{}} 24 | */ 25 | getAttributes(): {}; 26 | /** 27 | * To array 28 | * 29 | * @returns {{id: string, attributes: {}}} 30 | */ 31 | toArray(): { 32 | id: string; 33 | attributes?: {}; 34 | }; 35 | /** 36 | * Create from array 37 | * 38 | * @param array 39 | * 40 | * @return User 41 | */ 42 | static createFromArray(array: any): User; 43 | } 44 | -------------------------------------------------------------------------------- /lib/Http/CacheClient.d.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from "./HttpClient"; 2 | import { Response } from "./Response"; 3 | /** 4 | * AxiosClient 5 | */ 6 | export declare class CacheClient implements HttpClient { 7 | private cache; 8 | private httpClient; 9 | private hits; 10 | constructor(httpClient: HttpClient); 11 | flushCache(): void; 12 | size(): number; 13 | getNumberOfHits(): number; 14 | /** 15 | * Get 16 | * 17 | * @param url 18 | * @param method 19 | * @param credentials 20 | * @param parameters 21 | * @param data 22 | * 23 | * @return {Promise} 24 | */ 25 | get(url: string, method: string, credentials: any, parameters?: any, data?: any): Promise; 26 | /** 27 | * Abort current request 28 | * And regenerate the cancellation token 29 | * 30 | * @param url 31 | * @param urlIsFormatted 32 | */ 33 | abort(url: string, urlIsFormatted: boolean): void; 34 | } 35 | -------------------------------------------------------------------------------- /lib/Config/Synonym.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Result class 3 | */ 4 | export declare class Synonym { 5 | private words; 6 | /** 7 | * Constructor 8 | * 9 | * @param words 10 | */ 11 | constructor(words: string[]); 12 | /** 13 | * get words 14 | * 15 | * @return {string[]} 16 | */ 17 | getWords(): string[]; 18 | /** 19 | * Create by words 20 | * 21 | * @param words 22 | * 23 | * @return {Synonym} 24 | */ 25 | static createbyWords(words: string[]): Synonym; 26 | /** 27 | * To array 28 | * 29 | * @return {{words: string[]}} 30 | */ 31 | toArray(): { 32 | words: string[]; 33 | }; 34 | /** 35 | * create from array 36 | * 37 | * @param array 38 | * 39 | * @returns {Synonym} 40 | */ 41 | static createFromArray(array: any): Synonym; 42 | /** 43 | * Expand 44 | * 45 | * @returns {string} 46 | */ 47 | expand(): string; 48 | } 49 | -------------------------------------------------------------------------------- /lib/Model/Coordinate.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Coordinate Type cast 3 | * @param coordinate 4 | */ 5 | export declare class Coordinate { 6 | private lat; 7 | private lon; 8 | /** 9 | * Constructor 10 | * 11 | * @param {number} lat 12 | * @param {number} lon 13 | */ 14 | constructor(lat: number, lon: number); 15 | /** 16 | * Get latitude 17 | * 18 | * @return float 19 | */ 20 | getLatitude(): number; 21 | /** 22 | * Get longitude 23 | * 24 | * @return float 25 | */ 26 | getLongitude(): number; 27 | /** 28 | * To array 29 | * 30 | * @return {{lat: number, lon: number}} 31 | */ 32 | toArray(): { 33 | lat: number; 34 | lon: number; 35 | }; 36 | /** 37 | * Create from array 38 | * 39 | * @param array 40 | * 41 | * @return Coordinate 42 | * 43 | * @throws InvalidFormatError 44 | */ 45 | static createFromArray(array: any): Coordinate; 46 | } 47 | -------------------------------------------------------------------------------- /lib/Cache/NoCache.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | /** 4 | * Cache class 5 | */ 6 | var NoCache = /** @class */ (function () { 7 | function NoCache() { 8 | } 9 | /** 10 | * Set cache element 11 | * 12 | * @param key 13 | * @param value 14 | * 15 | * @returns {void} 16 | */ 17 | NoCache.prototype.set = function (key, value) { 18 | // Empty 19 | }; 20 | /** 21 | * Get element from cache 22 | * 23 | * @param key 24 | * 25 | * @returns {any} 26 | */ 27 | NoCache.prototype.get = function (key) { 28 | return undefined; 29 | }; 30 | /** 31 | * Deletes element from cache 32 | * 33 | * @param key 34 | */ 35 | NoCache.prototype.del = function (key) { 36 | // Empty 37 | }; 38 | /** 39 | * Clear cache 40 | */ 41 | NoCache.prototype.clear = function () { 42 | // Empty 43 | }; 44 | return NoCache; 45 | }()); 46 | exports.NoCache = NoCache; 47 | -------------------------------------------------------------------------------- /lib/Model/AppUUID.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * AppUUID class 3 | */ 4 | export declare class AppUUID { 5 | private id; 6 | /** 7 | * Constructor 8 | * 9 | * @param id 10 | */ 11 | constructor(id: string); 12 | /** 13 | * Create by id 14 | * 15 | * @param id 16 | * 17 | * @returns {ItemUUID} 18 | */ 19 | static createById(id: string): AppUUID; 20 | /** 21 | * Return id 22 | * 23 | * @returns {string} 24 | */ 25 | getId(): string; 26 | /** 27 | * To array 28 | * 29 | * @returns {{id: *, type: *}} 30 | */ 31 | toArray(): { 32 | id: string; 33 | }; 34 | /** 35 | * Create from array 36 | * 37 | * @param array {{id:string, type:string}} 38 | * 39 | * @return {ItemUUID} 40 | */ 41 | static createFromArray(array: { 42 | id: string; 43 | }): AppUUID; 44 | /** 45 | * Compose unique id 46 | * 47 | * @returns {string} 48 | */ 49 | composedUUID(): string; 50 | } 51 | -------------------------------------------------------------------------------- /lib/Model/IndexUUID.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * IndexUUID class 3 | */ 4 | export declare class IndexUUID { 5 | private id; 6 | /** 7 | * Constructor 8 | * 9 | * @param id 10 | */ 11 | constructor(id: string); 12 | /** 13 | * Create by id 14 | * 15 | * @param id 16 | * 17 | * @returns {ItemUUID} 18 | */ 19 | static createById(id: string): IndexUUID; 20 | /** 21 | * Return id 22 | * 23 | * @returns {string} 24 | */ 25 | getId(): string; 26 | /** 27 | * To array 28 | * 29 | * @returns {{id: *, type: *}} 30 | */ 31 | toArray(): { 32 | id: string; 33 | }; 34 | /** 35 | * Create from array 36 | * 37 | * @param array {{id:string, type:string}} 38 | * 39 | * @return {ItemUUID} 40 | */ 41 | static createFromArray(array: { 42 | id: string; 43 | }): IndexUUID; 44 | /** 45 | * Compose unique id 46 | * 47 | * @returns {string} 48 | */ 49 | composedUUID(): string; 50 | } 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Marc Morera 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /src/Error/InvalidTokenError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Invalid token error 5 | */ 6 | export class InvalidTokenError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 401; 15 | } 16 | 17 | /** 18 | * Invalid token permissions 19 | * 20 | * @param tokenReference 21 | * 22 | * @return {InvalidTokenError} 23 | */ 24 | public static createInvalidTokenPermissions(tokenReference) { 25 | return new InvalidTokenError("Token " + tokenReference + "not valid"); 26 | } 27 | 28 | /** 29 | * Invalid token permissions 30 | * 31 | * @param tokenReference 32 | * @param maxHitsPerQuery 33 | * 34 | * @return {InvalidTokenError} 35 | */ 36 | public static createInvalidTokenMaxHitsPerQuery(tokenReference, maxHitsPerQuery) { 37 | return new InvalidTokenError("Token " + tokenReference + "not valid. Max " + maxHitsPerQuery + " hits allowed"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Error/ResourceExistsError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Resource exists error 5 | */ 6 | export class ResourceExistsError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 409; 15 | } 16 | 17 | /** 18 | * Index not available 19 | * 20 | * @return {InvalidFormatError} 21 | */ 22 | public static indexAvailable() { 23 | return new ResourceExistsError("Index exists and cannot be created again"); 24 | } 25 | 26 | /** 27 | * Events not available 28 | * 29 | * @return {InvalidFormatError} 30 | */ 31 | public static eventsIndexAvailable() { 32 | return new ResourceExistsError("Events index exists and cannot be created again"); 33 | } 34 | 35 | /** 36 | * Logs not available 37 | * 38 | * @return {InvalidFormatError} 39 | */ 40 | public static logsIndexAvailable() { 41 | return new ResourceExistsError("Logs index exists and cannot be created again"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Error/ForbiddenError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Forbidden Error 5 | */ 6 | export class ForbiddenError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 403; 15 | } 16 | 17 | /** 18 | * App id is required 19 | * 20 | * @return {ForbiddenError} 21 | */ 22 | public static createAppIdIsRequiredException() { 23 | return new ForbiddenError("AppId query parameter MUST be defined with a valid value"); 24 | } 25 | 26 | /** 27 | * Index id is required 28 | * 29 | * @return {ForbiddenError} 30 | */ 31 | public static createIndexIsRequiredException() { 32 | return new ForbiddenError("Index query parameter MUST be defined with a valid value"); 33 | } 34 | 35 | /** 36 | * Token is required 37 | * 38 | * @return {ForbiddenError} 39 | */ 40 | public static createTokenIsRequiredException() { 41 | return new ForbiddenError("Token query parameter MUST be defined with a valid value"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/Model/Metadata.test.ts: -------------------------------------------------------------------------------- 1 | import {Metadata} from "../../src/Model/Metadata"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('Metadata', () => { 6 | describe('.toMetadata', () => { 7 | it('should pass', () => { 8 | expect(Metadata.toMetadata({ 9 | 'id': 1, 10 | 'name': 'product', 11 | 'level': '5' 12 | })).to.be.equal('id##1~~name##product~~level##5'); 13 | }); 14 | }); 15 | 16 | describe('.fromMetadata', () => { 17 | it('should pass', () => { 18 | expect(Metadata.fromMetadata('id##1~~name##product~~level##5')).to.be.deep.equal({ 19 | 'id': '1', 20 | 'name': 'product', 21 | 'level': '5' 22 | }); 23 | 24 | expect(Metadata.fromMetadata('my-id')).to.be.deep.equal({ 25 | 'id': 'my-id', 26 | 'name': 'my-id', 27 | }); 28 | 29 | expect(Metadata.fromMetadata('name##product~~level##1')).to.be.equal(null); 30 | }); 31 | }); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /lib/Error/UnsupportedContentTypeError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.UnsupportedContentTypeError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Unsupported content type error 8 | */ 9 | var UnsupportedContentTypeError = /** @class */ (function (_super) { 10 | tslib_1.__extends(UnsupportedContentTypeError, _super); 11 | function UnsupportedContentTypeError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | UnsupportedContentTypeError.getTransportableHTTPError = function () { 20 | return 415; 21 | }; 22 | /** 23 | * Unsupported content type 24 | * 25 | * @return {InvalidFormatError} 26 | */ 27 | UnsupportedContentTypeError.createUnsupportedContentTypeException = function () { 28 | return new UnsupportedContentTypeError("This content type is not accepted. Please use application/json"); 29 | }; 30 | return UnsupportedContentTypeError; 31 | }(ErrorWithMessage_1.ErrorWithMessage)); 32 | exports.UnsupportedContentTypeError = UnsupportedContentTypeError; 33 | -------------------------------------------------------------------------------- /lib/Error/ResourceNotAvailableError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Resource not available error 4 | */ 5 | export declare class ResourceNotAvailableError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * Index not available 14 | * 15 | * @param resourceId 16 | * 17 | * @return {InvalidFormatError} 18 | */ 19 | static indexNotAvailable(resourceId: any): ResourceNotAvailableError; 20 | /** 21 | * Events not available 22 | * 23 | * @param resourceId 24 | * 25 | * @return {InvalidFormatError} 26 | */ 27 | static eventsIndexNotAvailable(resourceId: any): ResourceNotAvailableError; 28 | /** 29 | * Logs not available 30 | * 31 | * @param resourceId 32 | * 33 | * @return {InvalidFormatError} 34 | */ 35 | static logsIndexNotAvailable(resourceId: any): ResourceNotAvailableError; 36 | /** 37 | * Engine not available 38 | * 39 | * @param resourceId 40 | * 41 | * @return {InvalidFormatError} 42 | */ 43 | static engineNotAvailable(resourceId: any): ResourceNotAvailableError; 44 | } 45 | -------------------------------------------------------------------------------- /lib/Model/ItemUUID.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * ItemUUID class 3 | */ 4 | export declare class ItemUUID { 5 | private id; 6 | private type; 7 | /** 8 | * Constructor 9 | * 10 | * @param id 11 | * @param type 12 | */ 13 | constructor(id: string, type: string); 14 | /** 15 | * Create composed UUID 16 | * 17 | * @param composedUUID 18 | * 19 | * @returns {ItemUUID} 20 | */ 21 | static createByComposedUUID(composedUUID: string): ItemUUID; 22 | /** 23 | * Return id 24 | * 25 | * @returns {string} 26 | */ 27 | getId(): string; 28 | /** 29 | * Get type 30 | * 31 | * @returns {string} 32 | */ 33 | getType(): string; 34 | /** 35 | * To array 36 | * 37 | * @returns {{id: *, type: *}} 38 | */ 39 | toArray(): { 40 | id: string; 41 | type: string; 42 | }; 43 | /** 44 | * Create from array 45 | * 46 | * @param array {{id:string, type:string}} 47 | * 48 | * @return {ItemUUID} 49 | */ 50 | static createFromArray(array: { 51 | id: string; 52 | type: string; 53 | }): ItemUUID; 54 | /** 55 | * Compose unique id 56 | * 57 | * @returns {string} 58 | */ 59 | composedUUID(): string; 60 | } 61 | -------------------------------------------------------------------------------- /test/Query/ScoreStrategies.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { 3 | ScoreStrategies, 4 | AVG, 5 | SUM 6 | } from "../../src/Query/ScoreStrategies"; 7 | import {ScoreStrategy} from "../../src/Query/ScoreStrategy"; 8 | 9 | describe('Query/', () => { 10 | describe('ScoreStrategies', () => { 11 | describe('.createEmpty()', () => { 12 | let scoreStrategies = ScoreStrategies.createEmpty(); 13 | it('Should work properly', () => { 14 | expect(scoreStrategies.getScoreMode()).to.be.equal(SUM); 15 | expect(scoreStrategies.getScoreStrategies()).to.be.deep.equal([]); 16 | }); 17 | }); 18 | describe('with ScoreStrategy instances', () => { 19 | let scoreStrategies = ScoreStrategies.createEmpty(AVG); 20 | scoreStrategies.addScoreStrategy(ScoreStrategy.createDefault()); 21 | scoreStrategies.addScoreStrategy(ScoreStrategy.createDefault()); 22 | scoreStrategies.addScoreStrategy(ScoreStrategy.createDefault()); 23 | it('Should work properly', () => { 24 | expect(scoreStrategies.getScoreMode()).to.be.equal(AVG); 25 | expect(scoreStrategies.getScoreStrategies().length).to.be.equal(3); 26 | }); 27 | }); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /lib/Config/ImmutableConfig.d.ts: -------------------------------------------------------------------------------- 1 | import { Synonym } from "./Synonym"; 2 | /** 3 | * Result class 4 | */ 5 | export declare class ImmutableConfig { 6 | private language; 7 | private storeSearchableMetadata; 8 | private synonyms; 9 | /** 10 | * Constructor 11 | * 12 | * @param language 13 | * @param storeSearchableMetadata 14 | */ 15 | constructor(language?: string, storeSearchableMetadata?: boolean); 16 | /** 17 | * Get language 18 | * 19 | * @return {string} 20 | */ 21 | getLanguage(): string; 22 | /** 23 | * Should searchable metadata be stored 24 | * 25 | * @return {boolean} 26 | */ 27 | shouldSearchableMetadataBeStored(): boolean; 28 | /** 29 | * Add synonym 30 | * 31 | * @param synonym 32 | */ 33 | addSynonym(synonym: Synonym): void; 34 | /** 35 | * Get synonyms 36 | * 37 | * @return {Synonym[]} 38 | */ 39 | getSynonyms(): Synonym[]; 40 | /** 41 | * to array 42 | */ 43 | toArray(): { 44 | language: string; 45 | store_searchable_metadata: boolean; 46 | synonyms: { 47 | words: string[]; 48 | }[]; 49 | }; 50 | /** 51 | * Create from array 52 | */ 53 | static createFromArray(array: any): ImmutableConfig; 54 | } 55 | -------------------------------------------------------------------------------- /test/Config/Synonym.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Synonym} from "../../src/Config/Synonym"; 3 | 4 | describe('Config/', () => { 5 | describe('Synonym', () => { 6 | 7 | let words = ['a', 'b', 'c']; 8 | 9 | describe('.createByWords()', () => { 10 | let synonym = Synonym.createbyWords(words); 11 | it('should work properly', () => { 12 | expect(synonym.getWords()).to.be.deep.equal(words); 13 | }); 14 | }); 15 | 16 | describe('.expand()', () => { 17 | let synonym = Synonym.createbyWords(words); 18 | it('should work properly', () => { 19 | expect(synonym.expand()).to.be.deep.equal('a,b,c'); 20 | }); 21 | }); 22 | 23 | describe('.createFromArray()', () => { 24 | let synonym = Synonym.createFromArray({'words': ['a', 'b', 'c']}); 25 | it('should work properly', () => { 26 | expect(synonym.getWords()).to.be.deep.equal(words); 27 | }); 28 | }); 29 | 30 | describe('.createFromArray() empty', () => { 31 | let synonym = Synonym.createFromArray({}); 32 | it('should work properly', () => { 33 | expect(synonym.getWords()).to.be.deep.equal([]); 34 | }); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apisearch - Javascript Client 2 | 3 | ** Repository migrated to https://github.com/empathyco/apisearch-javascript-client ** 4 | 5 | This library is part of the Apisearch project. 6 | 7 | [![CircleCI](https://circleci.com/gh/apisearch-io/javascript-client.svg?style=svg)](https://circleci.com/gh/apisearch-io/javascript-client) 8 | [![Join the Slack](https://img.shields.io/badge/join%20us-on%20slack-blue.svg)](https://apisearch.slack.com) 9 | 10 | Apisearch is an open source search engine fully based on open source third party 11 | technologies. The project provides an *in crescendo* set of language 12 | integration libraries for her users, as well as some third party projects 13 | integration bundles, plugins, or javascript widgets. 14 | 15 | Some first steps for you! 16 | 17 | - [Go to DOCS](http://docs.apisearch.io) 18 | 19 | or 20 | 21 | - [Download and install Apisearch](https://docs.apisearch.io/#/download) 22 | - [Create your first application](https://docs.apisearch.io/#/first-example) 23 | 24 | Take a tour using these links. 25 | 26 | - [View a demo](http://apisearch.io) 27 | - [Join us on slack](https://apisearch.slack.com) - or [Get an invitation](https://apisearch-slack.herokuapp.com/) 28 | - [Twitter](https://twitter.com/apisearch_io) 29 | 30 | ...and remember give us a star on Github! The more stars we have, the further 31 | we'll arrive. 32 | -------------------------------------------------------------------------------- /test/Transformer/ProductWriteTransformer.ts: -------------------------------------------------------------------------------- 1 | import {WriteTransformer} from "../../src/Transformer/WriteTransformer"; 2 | import {Product} from "./Product"; 3 | import {Item} from "../../src/Model/Item"; 4 | import {ItemUUID} from "../../src/Model/ItemUUID"; 5 | 6 | /** 7 | * Product ReadTransformer 8 | */ 9 | export class ProductWriteTransformer implements WriteTransformer { 10 | 11 | /** 12 | * Is an indexable object. 13 | * 14 | * @param object 15 | * 16 | * @return {boolean} 17 | */ 18 | isValidObject(object:any): boolean { 19 | return object instanceof Product; 20 | } 21 | 22 | /** 23 | * Create item by object. 24 | * 25 | * @param object 26 | * 27 | * @return {Item} 28 | */ 29 | toItem(object): Item { 30 | return Item.create( 31 | this.toItemUUID(object), 32 | { 33 | 'name': object.name 34 | }, 35 | { 36 | 'sku': object.sku 37 | } 38 | ); 39 | } 40 | 41 | /** 42 | * Create item UUID by object. 43 | * 44 | * @param object 45 | * 46 | * @return {ItemUUID} 47 | */ 48 | toItemUUID(object): ItemUUID { 49 | return ItemUUID.createFromArray({ 50 | id: object.id, 51 | type: 'product' 52 | }) 53 | } 54 | } -------------------------------------------------------------------------------- /lib/Http/Retry.d.ts: -------------------------------------------------------------------------------- 1 | export declare const DEFAULT_MICROSECONDS_BETWEEN_RETRIES = 1000; 2 | export interface RetryConfig { 3 | url?: string; 4 | method?: string; 5 | retries?: number; 6 | microseconds_between_retries?: number; 7 | } 8 | /** 9 | * Http class 10 | */ 11 | export declare class Retry { 12 | /** 13 | * Create from array 14 | * 15 | * @param array 16 | * 17 | * @return {Retry} 18 | */ 19 | static createFromArray(array: RetryConfig): Retry; 20 | private url; 21 | private method; 22 | private retries; 23 | private microsecondsBetweenRetries; 24 | /** 25 | * Constructor 26 | * 27 | * @param url 28 | * @param method 29 | * @param retries 30 | * @param microsecondsBetweenRetries 31 | */ 32 | constructor(url: string, method: string, retries: number, microsecondsBetweenRetries: number); 33 | /** 34 | * Get url 35 | * 36 | * @return {string} 37 | */ 38 | getUrl(): string; 39 | /** 40 | * Get method 41 | * 42 | * @return {string} 43 | */ 44 | getMethod(): string; 45 | /** 46 | * Ge retries 47 | * 48 | * @return {number} 49 | */ 50 | getRetries(): number; 51 | /** 52 | * Get microseconds between retries 53 | * 54 | * @return {number} 55 | */ 56 | getMicrosecondsBetweenRetries(): number; 57 | } 58 | -------------------------------------------------------------------------------- /lib/Cache/InMemoryCache.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | var tslib_1 = require("tslib"); 4 | /** 5 | * Cache class 6 | */ 7 | var InMemoryCache = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | */ 11 | function InMemoryCache() { 12 | this.cache = {}; 13 | this.size = 0; 14 | this.cache = {}; 15 | this.size = 0; 16 | } 17 | /** 18 | * Set cache element 19 | * 20 | * @param key 21 | * @param value 22 | * 23 | * @returns {void} 24 | */ 25 | InMemoryCache.prototype.set = function (key, value) { 26 | var _a; 27 | this.cache = tslib_1.__assign({}, this.cache, (_a = {}, _a[key] = value, _a)); 28 | this.size = this.size + 1; 29 | }; 30 | /** 31 | * Get element from cache 32 | * 33 | * @param key 34 | * 35 | * @returns {any} 36 | */ 37 | InMemoryCache.prototype.get = function (key) { 38 | return this.cache[key]; 39 | }; 40 | /** 41 | * Deletes element from cache 42 | * 43 | * @param key 44 | */ 45 | InMemoryCache.prototype.del = function (key) { 46 | delete this.cache[key]; 47 | }; 48 | /** 49 | * Clear cache 50 | */ 51 | InMemoryCache.prototype.clear = function () { 52 | this.cache = {}; 53 | this.size = 0; 54 | }; 55 | return InMemoryCache; 56 | }()); 57 | exports.InMemoryCache = InMemoryCache; 58 | -------------------------------------------------------------------------------- /lib/Http/AxiosClient.d.ts: -------------------------------------------------------------------------------- 1 | import { Client } from "./Client"; 2 | import { HttpClient } from "./HttpClient"; 3 | import { Response } from "./Response"; 4 | /** 5 | * AxiosClient 6 | */ 7 | export declare class AxiosClient extends Client implements HttpClient { 8 | private host; 9 | private timeout; 10 | private overrideQueries; 11 | private abortControllers; 12 | /** 13 | * Constructor 14 | * 15 | * @param host 16 | * @param version 17 | * @param timeout 18 | * @param overrideQueries 19 | */ 20 | constructor(host: string, version: string, timeout: number, overrideQueries: boolean); 21 | /** 22 | * @param url 23 | * @param method 24 | * @param credentials 25 | * @param parameters 26 | * @param data 27 | */ 28 | get(url: string, method: string, credentials: any, parameters?: any, data?: any): Promise; 29 | /** 30 | * Abort current request 31 | * And regenerate the cancellation token 32 | * 33 | * @param url 34 | * @param urlIsFormatted 35 | */ 36 | abort(url: string, urlIsFormatted: boolean): void; 37 | /** 38 | * Generate a new cancellation token for a query 39 | * 40 | * @param url 41 | */ 42 | generateAbortController(url: string): void; 43 | /** 44 | * @param url 45 | * @param options 46 | * @param retries 47 | */ 48 | fetch(url: string, options: {}, retries: number): any; 49 | } 50 | -------------------------------------------------------------------------------- /test/Model/AppUUID.test.ts: -------------------------------------------------------------------------------- 1 | import {AppUUID} from "../../src/Model/AppUUID"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('AppUUID', () => { 6 | describe('()', () => { 7 | let appUUID = AppUUID.createById('1'); 8 | 9 | it('Should work properly', () => { 10 | expect(appUUID.getId()).to.be.equal('1'); 11 | expect(appUUID.composedUUID()).to.be.equal('1'); 12 | }); 13 | }); 14 | describe('.createFromArray()', () => { 15 | describe('with good params', () => { 16 | let appUUID = AppUUID.createFromArray({'id': '1'}); 17 | it('should work properly', () => { 18 | expect(appUUID.getId()).to.be.equal('1'); 19 | }); 20 | }); 21 | }); 22 | describe('().toArray()', () => { 23 | let appUUIDAsArray = {'id': '1'}; 24 | expect(AppUUID.createFromArray(appUUIDAsArray).toArray()).to.be.deep.equal(appUUIDAsArray); 25 | }); 26 | describe('error', () => { 27 | describe('with underslash', () => { 28 | let creation = function() { 29 | AppUUID.createFromArray({'id': 'a_b'}); 30 | }; 31 | it('should throw error', () => { 32 | expect(creation).to.throw(); 33 | }); 34 | }); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /src/Config/Synonym.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Result class 3 | */ 4 | export class Synonym { 5 | 6 | private words: string[]; 7 | 8 | /** 9 | * Constructor 10 | * 11 | * @param words 12 | */ 13 | constructor(words: string[]) { 14 | this.words = words; 15 | } 16 | 17 | /** 18 | * get words 19 | * 20 | * @return {string[]} 21 | */ 22 | public getWords(): string[] { 23 | return this.words; 24 | } 25 | 26 | /** 27 | * Create by words 28 | * 29 | * @param words 30 | * 31 | * @return {Synonym} 32 | */ 33 | public static createbyWords(words: string[]) { 34 | return new Synonym(words); 35 | } 36 | 37 | /** 38 | * To array 39 | * 40 | * @return {{words: string[]}} 41 | */ 42 | public toArray(): {words: string[]} { 43 | return { 44 | words: this.words, 45 | }; 46 | } 47 | 48 | /** 49 | * create from array 50 | * 51 | * @param array 52 | * 53 | * @returns {Synonym} 54 | */ 55 | public static createFromArray(array: any): Synonym { 56 | 57 | return new Synonym( 58 | array.words instanceof Object 59 | ? array.words 60 | : [], 61 | ); 62 | } 63 | 64 | /** 65 | * Expand 66 | * 67 | * @returns {string} 68 | */ 69 | public expand(): string { 70 | return this.words.join(","); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /test/Model/IndexUUID.test.ts: -------------------------------------------------------------------------------- 1 | import {IndexUUID} from "../../src/Model/IndexUUID"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('IndexUUID', () => { 6 | describe('()', () => { 7 | let indexUUID = IndexUUID.createById('1'); 8 | 9 | it('Should work properly', () => { 10 | expect(indexUUID.getId()).to.be.equal('1'); 11 | expect(indexUUID.composedUUID()).to.be.equal('1'); 12 | }); 13 | }); 14 | describe('.createFromArray()', () => { 15 | describe('with good params', () => { 16 | let indexUUID = IndexUUID.createFromArray({'id': '1'}); 17 | it('should work properly', () => { 18 | expect(indexUUID.getId()).to.be.equal('1'); 19 | }); 20 | }); 21 | }); 22 | describe('().toArray()', () => { 23 | let indexUUIDAsArray = {'id': '1'}; 24 | expect(IndexUUID.createFromArray(indexUUIDAsArray).toArray()).to.be.deep.equal(indexUUIDAsArray); 25 | }); 26 | describe('error', () => { 27 | describe('with underslash', () => { 28 | let creation = function() { 29 | IndexUUID.createFromArray({'id': 'a_b'}); 30 | }; 31 | it('should throw error', () => { 32 | expect(creation).to.throw(); 33 | }); 34 | }); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /lib/Config/Synonym.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Synonym = void 0; 4 | /** 5 | * Result class 6 | */ 7 | var Synonym = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param words 12 | */ 13 | function Synonym(words) { 14 | this.words = words; 15 | } 16 | /** 17 | * get words 18 | * 19 | * @return {string[]} 20 | */ 21 | Synonym.prototype.getWords = function () { 22 | return this.words; 23 | }; 24 | /** 25 | * Create by words 26 | * 27 | * @param words 28 | * 29 | * @return {Synonym} 30 | */ 31 | Synonym.createbyWords = function (words) { 32 | return new Synonym(words); 33 | }; 34 | /** 35 | * To array 36 | * 37 | * @return {{words: string[]}} 38 | */ 39 | Synonym.prototype.toArray = function () { 40 | return { 41 | words: this.words 42 | }; 43 | }; 44 | /** 45 | * create from array 46 | * 47 | * @param array 48 | * 49 | * @returns {Synonym} 50 | */ 51 | Synonym.createFromArray = function (array) { 52 | return new Synonym(array.words instanceof Object 53 | ? array.words 54 | : []); 55 | }; 56 | /** 57 | * Expand 58 | * 59 | * @returns {string} 60 | */ 61 | Synonym.prototype.expand = function () { 62 | return this.words.join(","); 63 | }; 64 | return Synonym; 65 | }()); 66 | exports.Synonym = Synonym; 67 | -------------------------------------------------------------------------------- /lib/Error/InvalidTokenError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.InvalidTokenError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Invalid token error 8 | */ 9 | var InvalidTokenError = /** @class */ (function (_super) { 10 | tslib_1.__extends(InvalidTokenError, _super); 11 | function InvalidTokenError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | InvalidTokenError.getTransportableHTTPError = function () { 20 | return 401; 21 | }; 22 | /** 23 | * Invalid token permissions 24 | * 25 | * @param tokenReference 26 | * 27 | * @return {InvalidTokenError} 28 | */ 29 | InvalidTokenError.createInvalidTokenPermissions = function (tokenReference) { 30 | return new InvalidTokenError("Token " + tokenReference + "not valid"); 31 | }; 32 | /** 33 | * Invalid token permissions 34 | * 35 | * @param tokenReference 36 | * @param maxHitsPerQuery 37 | * 38 | * @return {InvalidTokenError} 39 | */ 40 | InvalidTokenError.createInvalidTokenMaxHitsPerQuery = function (tokenReference, maxHitsPerQuery) { 41 | return new InvalidTokenError("Token " + tokenReference + "not valid. Max " + maxHitsPerQuery + " hits allowed"); 42 | }; 43 | return InvalidTokenError; 44 | }(ErrorWithMessage_1.ErrorWithMessage)); 45 | exports.InvalidTokenError = InvalidTokenError; 46 | -------------------------------------------------------------------------------- /test/Model/Coordinate.test.ts: -------------------------------------------------------------------------------- 1 | import {Coordinate} from "../../src/Model/Coordinate"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('Coordinate', () => { 6 | describe('()', () => { 7 | let coordinate = new Coordinate(1.20, 2.10); 8 | 9 | it('Should work properly', () => { 10 | expect(coordinate.getLatitude()).to.be.equal(1.20); 11 | expect(coordinate.getLongitude()).to.be.equal(2.10); 12 | }); 13 | }); 14 | 15 | describe('::createFromArray()', () => { 16 | let coordinate = Coordinate.createFromArray({ 17 | 'lat': 1.20, 18 | 'lon': 2.10 19 | }); 20 | 21 | it('Should work properly', () => { 22 | expect(coordinate.getLatitude()).to.be.equal(1.20); 23 | expect(coordinate.getLongitude()).to.be.equal(2.10); 24 | }); 25 | }); 26 | 27 | describe('::createFromArray() with exception', () => { 28 | it('Should throw exception', () => { 29 | expect(function() {Coordinate.createFromArray(null)}).to.throw(); 30 | }); 31 | }); 32 | 33 | describe('->toArray()', () => { 34 | let coordinateAsArray = { 35 | 'lat': 1.20, 36 | 'lon': 2.10 37 | }; 38 | 39 | it('Should work properly', () => { 40 | expect(Coordinate.createFromArray(coordinateAsArray).toArray()).to.be.deep.equal(coordinateAsArray); 41 | }); 42 | }); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /lib/Result/ResultAggregations.d.ts: -------------------------------------------------------------------------------- 1 | import { ResultAggregation } from "./ResultAggregation"; 2 | /** 3 | * ResultAggregation class 4 | */ 5 | export declare class ResultAggregations { 6 | private aggregations; 7 | private totalElements; 8 | /** 9 | * Constructor 10 | * 11 | * @param totalElements 12 | */ 13 | constructor(totalElements: number); 14 | /** 15 | * Add aggregation 16 | * 17 | * @param name 18 | * @param aggregation 19 | */ 20 | addAggregation(name: string, aggregation: ResultAggregation): void; 21 | /** 22 | * Get aggregations 23 | * 24 | * @returns {{}} 25 | */ 26 | getAggregations(): any; 27 | /** 28 | * Get aggregation 29 | * 30 | * @param name 31 | * 32 | * @returns {Aggregation|null} 33 | */ 34 | getAggregation(name: string): any; 35 | /** 36 | * Has not empty aggregation 37 | * 38 | * @param name 39 | * 40 | * @returns {boolean} 41 | */ 42 | hasNotEmptyAggregation(name: string): boolean; 43 | /** 44 | * Get total elements 45 | * 46 | * @return {number} 47 | */ 48 | getTotalElements(): number; 49 | /** 50 | * To array 51 | * 52 | * @return {{total_elements?: number, aggregations?: {}}} 53 | */ 54 | toArray(): { 55 | total_elements?: number; 56 | aggregations?: {}; 57 | }; 58 | /** 59 | * Create from array 60 | * 61 | * @param array 62 | * 63 | * @return {ResultAggregations} 64 | */ 65 | static createFromArray(array: any): ResultAggregations; 66 | } 67 | -------------------------------------------------------------------------------- /lib/Query/ScoreStrategies.d.ts: -------------------------------------------------------------------------------- 1 | import { ScoreStrategy } from "./ScoreStrategy"; 2 | /** 3 | * ScoreStrategies constants 4 | */ 5 | export declare const MULTIPLY = "multiply"; 6 | export declare const SUM = "sum"; 7 | export declare const AVG = "avg"; 8 | export declare const MAX = "max"; 9 | export declare const MIN = "min"; 10 | /** 11 | * ScoreStrategies 12 | */ 13 | export declare class ScoreStrategies { 14 | private scoreStrategies; 15 | private scoreMode; 16 | /** 17 | * Create empty 18 | * 19 | * @param scoreMode 20 | * 21 | * @return {ScoreStrategies} 22 | */ 23 | static createEmpty(scoreMode?: string): ScoreStrategies; 24 | /** 25 | * Add score strategy 26 | * 27 | * @param scoreStrategy 28 | * 29 | * @return {ScoreStrategies} 30 | */ 31 | addScoreStrategy(scoreStrategy: ScoreStrategy): ScoreStrategies; 32 | /** 33 | * Get score strategies 34 | * 35 | * @return {ScoreStrategy[]} 36 | */ 37 | getScoreStrategies(): ScoreStrategy[]; 38 | /** 39 | * Get score mode 40 | * 41 | * @return {string} 42 | */ 43 | getScoreMode(): string; 44 | /** 45 | * To array 46 | * 47 | * @return {{ 48 | * score_mode: string, 49 | * score_strategies: any 50 | * }} 51 | */ 52 | toArray(): { 53 | score_mode: string; 54 | score_strategies: any; 55 | }; 56 | /** 57 | * Create from array 58 | * 59 | * @param array 60 | * 61 | * @return {ScoreStrategies} 62 | */ 63 | static createFromArray(array: any): ScoreStrategies; 64 | } 65 | -------------------------------------------------------------------------------- /lib/Error/ForbiddenError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ForbiddenError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Forbidden Error 8 | */ 9 | var ForbiddenError = /** @class */ (function (_super) { 10 | tslib_1.__extends(ForbiddenError, _super); 11 | function ForbiddenError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | ForbiddenError.getTransportableHTTPError = function () { 20 | return 403; 21 | }; 22 | /** 23 | * App id is required 24 | * 25 | * @return {ForbiddenError} 26 | */ 27 | ForbiddenError.createAppIdIsRequiredException = function () { 28 | return new ForbiddenError("AppId query parameter MUST be defined with a valid value"); 29 | }; 30 | /** 31 | * Index id is required 32 | * 33 | * @return {ForbiddenError} 34 | */ 35 | ForbiddenError.createIndexIsRequiredException = function () { 36 | return new ForbiddenError("Index query parameter MUST be defined with a valid value"); 37 | }; 38 | /** 39 | * Token is required 40 | * 41 | * @return {ForbiddenError} 42 | */ 43 | ForbiddenError.createTokenIsRequiredException = function () { 44 | return new ForbiddenError("Token query parameter MUST be defined with a valid value"); 45 | }; 46 | return ForbiddenError; 47 | }(ErrorWithMessage_1.ErrorWithMessage)); 48 | exports.ForbiddenError = ForbiddenError; 49 | -------------------------------------------------------------------------------- /src/Error/ResourceNotAvailableError.ts: -------------------------------------------------------------------------------- 1 | import {ErrorWithMessage} from "./ErrorWithMessage"; 2 | 3 | /** 4 | * Resource not available error 5 | */ 6 | export class ResourceNotAvailableError extends ErrorWithMessage { 7 | 8 | /** 9 | * Get transportable http error 10 | * 11 | * @return {number} 12 | */ 13 | public static getTransportableHTTPError() { 14 | return 404; 15 | } 16 | 17 | /** 18 | * Index not available 19 | * 20 | * @param resourceId 21 | * 22 | * @return {InvalidFormatError} 23 | */ 24 | public static indexNotAvailable(resourceId) { 25 | return new ResourceNotAvailableError("Index not available - " + resourceId); 26 | } 27 | 28 | /** 29 | * Events not available 30 | * 31 | * @param resourceId 32 | * 33 | * @return {InvalidFormatError} 34 | */ 35 | public static eventsIndexNotAvailable(resourceId) { 36 | return new ResourceNotAvailableError("Events not available - " + resourceId); 37 | } 38 | 39 | /** 40 | * Logs not available 41 | * 42 | * @param resourceId 43 | * 44 | * @return {InvalidFormatError} 45 | */ 46 | public static logsIndexNotAvailable(resourceId) { 47 | return new ResourceNotAvailableError("Logs not available - " + resourceId); 48 | } 49 | 50 | /** 51 | * Engine not available 52 | * 53 | * @param resourceId 54 | * 55 | * @return {InvalidFormatError} 56 | */ 57 | public static engineNotAvailable(resourceId) { 58 | return new ResourceNotAvailableError("Engine not available - " + resourceId); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /lib/Error/ResourceExistsError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ResourceExistsError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Resource exists error 8 | */ 9 | var ResourceExistsError = /** @class */ (function (_super) { 10 | tslib_1.__extends(ResourceExistsError, _super); 11 | function ResourceExistsError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | ResourceExistsError.getTransportableHTTPError = function () { 20 | return 409; 21 | }; 22 | /** 23 | * Index not available 24 | * 25 | * @return {InvalidFormatError} 26 | */ 27 | ResourceExistsError.indexAvailable = function () { 28 | return new ResourceExistsError("Index exists and cannot be created again"); 29 | }; 30 | /** 31 | * Events not available 32 | * 33 | * @return {InvalidFormatError} 34 | */ 35 | ResourceExistsError.eventsIndexAvailable = function () { 36 | return new ResourceExistsError("Events index exists and cannot be created again"); 37 | }; 38 | /** 39 | * Logs not available 40 | * 41 | * @return {InvalidFormatError} 42 | */ 43 | ResourceExistsError.logsIndexAvailable = function () { 44 | return new ResourceExistsError("Logs index exists and cannot be created again"); 45 | }; 46 | return ResourceExistsError; 47 | }(ErrorWithMessage_1.ErrorWithMessage)); 48 | exports.ResourceExistsError = ResourceExistsError; 49 | -------------------------------------------------------------------------------- /src/Model/AppUUID.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | 3 | /** 4 | * AppUUID class 5 | */ 6 | export class AppUUID { 7 | 8 | private id: string; 9 | 10 | /** 11 | * Constructor 12 | * 13 | * @param id 14 | */ 15 | constructor(id: string) { 16 | if (id.indexOf('_') >= 0) { 17 | throw InvalidFormatError.appUUIDFormatNotValid(); 18 | } 19 | this.id = id; 20 | } 21 | 22 | /** 23 | * Create by id 24 | * 25 | * @param id 26 | * 27 | * @returns {ItemUUID} 28 | */ 29 | public static createById(id: string): AppUUID { 30 | return new AppUUID(id); 31 | } 32 | 33 | /** 34 | * Return id 35 | * 36 | * @returns {string} 37 | */ 38 | public getId(): string { 39 | return this.id; 40 | } 41 | 42 | /** 43 | * To array 44 | * 45 | * @returns {{id: *, type: *}} 46 | */ 47 | public toArray(): { 48 | id: string, 49 | } { 50 | return { 51 | id: this.id, 52 | }; 53 | } 54 | 55 | /** 56 | * Create from array 57 | * 58 | * @param array {{id:string, type:string}} 59 | * 60 | * @return {ItemUUID} 61 | */ 62 | public static createFromArray(array: {id: string}): AppUUID { 63 | array = JSON.parse(JSON.stringify(array)); 64 | return new AppUUID( 65 | array.id 66 | ); 67 | } 68 | 69 | /** 70 | * Compose unique id 71 | * 72 | * @returns {string} 73 | */ 74 | public composedUUID(): string { 75 | return this.id; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Model/Metadata.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * User class 3 | */ 4 | export class Metadata { 5 | 6 | /** 7 | * To metadata 8 | * 9 | * @param array:{} 10 | * 11 | * @returns {string} 12 | */ 13 | public static toMetadata(array: {}): string { 14 | array = JSON.parse(JSON.stringify(array)); 15 | const parts = []; 16 | for (const key in array) { 17 | parts.push(key + "##" + array[key]); 18 | } 19 | 20 | return parts.join("~~"); 21 | } 22 | 23 | /** 24 | * From metadata 25 | * 26 | * @param metadata 27 | * 28 | * @return {{}} 29 | */ 30 | public static fromMetadata(metadata: string): any { 31 | 32 | let values: any = {}; 33 | const splittedParts = metadata.split("~~"); 34 | let iterator = 0; 35 | let size = 0; 36 | let lastElement = null; 37 | 38 | for (const key in splittedParts) { 39 | const part = splittedParts[key]; 40 | const parts = part.split("##"); 41 | if (parts.length > 1) { 42 | lastElement = parts[1]; 43 | values[parts[0]] = lastElement; 44 | } else { 45 | lastElement = part; 46 | values[iterator++] = lastElement; 47 | } 48 | size++; 49 | } 50 | 51 | if (size == 1) { 52 | values = { 53 | id: lastElement, 54 | name: lastElement, 55 | }; 56 | } 57 | 58 | if (typeof values.id == "undefined") { 59 | return null; 60 | } 61 | 62 | return values; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Model/IndexUUID.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | 3 | /** 4 | * IndexUUID class 5 | */ 6 | export class IndexUUID { 7 | 8 | private id: string; 9 | 10 | /** 11 | * Constructor 12 | * 13 | * @param id 14 | */ 15 | constructor(id: string) { 16 | if (id.indexOf('_') >= 0) { 17 | throw InvalidFormatError.indexUUIDFormatNotValid(); 18 | } 19 | this.id = id; 20 | } 21 | 22 | /** 23 | * Create by id 24 | * 25 | * @param id 26 | * 27 | * @returns {ItemUUID} 28 | */ 29 | public static createById(id: string): IndexUUID { 30 | return new IndexUUID(id); 31 | } 32 | 33 | /** 34 | * Return id 35 | * 36 | * @returns {string} 37 | */ 38 | public getId(): string { 39 | return this.id; 40 | } 41 | 42 | /** 43 | * To array 44 | * 45 | * @returns {{id: *, type: *}} 46 | */ 47 | public toArray(): { 48 | id: string, 49 | } { 50 | return { 51 | id: this.id, 52 | }; 53 | } 54 | 55 | /** 56 | * Create from array 57 | * 58 | * @param array {{id:string, type:string}} 59 | * 60 | * @return {ItemUUID} 61 | */ 62 | public static createFromArray(array: {id: string}): IndexUUID { 63 | array = JSON.parse(JSON.stringify(array)); 64 | return new IndexUUID( 65 | array.id 66 | ); 67 | } 68 | 69 | /** 70 | * Compose unique id 71 | * 72 | * @returns {string} 73 | */ 74 | public composedUUID(): string { 75 | return this.id; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /lib/Result/Counter.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Aggregation class 3 | */ 4 | export declare class Counter { 5 | values: any; 6 | used: boolean; 7 | n: number; 8 | /** 9 | * Constructor 10 | * 11 | * @param values 12 | * @param used 13 | * @param n 14 | */ 15 | constructor(values: any, used: boolean, n: number); 16 | /** 17 | * Get id 18 | * 19 | * @return {string|null} 20 | */ 21 | getId(): string; 22 | /** 23 | * Get name 24 | * 25 | * @return {string|null} 26 | */ 27 | getName(): string; 28 | /** 29 | * Get slug 30 | * 31 | * @return {string|null} 32 | */ 33 | getSlug(): string; 34 | /** 35 | * Get level 36 | * 37 | * @return {number} 38 | */ 39 | getLevel(): number; 40 | /** 41 | * Get values 42 | * 43 | * @returns {{}} 44 | */ 45 | getValues(): any; 46 | /** 47 | * Is used 48 | * 49 | * @returns {boolean} 50 | */ 51 | isUsed(): boolean; 52 | /** 53 | * Get N 54 | * 55 | * @returns {number} 56 | */ 57 | getN(): number; 58 | /** 59 | * Create by active elements 60 | * 61 | * @param name 62 | * @param n 63 | * @param activeElements 64 | */ 65 | static createByActiveElements(name: string, n: number, activeElements: string[]): Counter; 66 | /** 67 | * To array 68 | * 69 | * @return {{}} 70 | */ 71 | toArray(): any; 72 | /** 73 | * Create from array 74 | * 75 | * @param array 76 | * 77 | * @return {Counter} 78 | */ 79 | static createFromArray(array: any): Counter; 80 | } 81 | -------------------------------------------------------------------------------- /lib/index.d.ts: -------------------------------------------------------------------------------- 1 | import Apisearch from "./Apisearch"; 2 | export default Apisearch; 3 | export * from "./Config/Config"; 4 | export * from "./Config/Synonym"; 5 | export * from "./Error/ConnectionError"; 6 | export * from "./Error/ErrorWithMessage"; 7 | export * from "./Error/EventError"; 8 | export * from "./Error/ForbiddenError"; 9 | export * from "./Error/InvalidFormatError"; 10 | export * from "./Error/InvalidTokenError"; 11 | export * from "./Error/ResourceExistsError"; 12 | export * from "./Error/ResourceNotAvailableError"; 13 | export * from "./Error/UnsupportedContentTypeError"; 14 | export * from "./Geo/LocationRange"; 15 | export * from "./Http/AxiosClient"; 16 | export * from "./Http/Client"; 17 | export * from "./Http/HttpClient"; 18 | export * from "./Http/Response"; 19 | export * from "./Http/CacheClient"; 20 | export * from "./Model/Changes"; 21 | export * from "./Model/Coordinate"; 22 | export * from "./Model/Item"; 23 | export * from "./Model/ItemUUID"; 24 | export * from "./Model/Metadata"; 25 | export * from "./Model/User"; 26 | export * from "./Query/Aggregation"; 27 | export * from "./Query/Filter"; 28 | export * from "./Query/Query"; 29 | export * from "./Query/Range"; 30 | export * from "./Query/ScoreStrategies"; 31 | export * from "./Query/ScoreStrategy"; 32 | export * from "./Query/SortBy"; 33 | export * from "./Repository/HttpRepository"; 34 | export * from "./Repository/Repository"; 35 | export * from "./Result/ResultAggregation"; 36 | export * from "./Result/ResultAggregations"; 37 | export * from "./Result/Counter"; 38 | export * from "./Result/Result"; 39 | export * from "./Transformer/ReadTransformer"; 40 | export * from "./Transformer/Transformer"; 41 | export * from "./Transformer/WriteTransformer"; 42 | -------------------------------------------------------------------------------- /lib/Model/AppUUID.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.AppUUID = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | /** 6 | * AppUUID class 7 | */ 8 | var AppUUID = /** @class */ (function () { 9 | /** 10 | * Constructor 11 | * 12 | * @param id 13 | */ 14 | function AppUUID(id) { 15 | if (id.indexOf('_') >= 0) { 16 | throw InvalidFormatError_1.InvalidFormatError.appUUIDFormatNotValid(); 17 | } 18 | this.id = id; 19 | } 20 | /** 21 | * Create by id 22 | * 23 | * @param id 24 | * 25 | * @returns {ItemUUID} 26 | */ 27 | AppUUID.createById = function (id) { 28 | return new AppUUID(id); 29 | }; 30 | /** 31 | * Return id 32 | * 33 | * @returns {string} 34 | */ 35 | AppUUID.prototype.getId = function () { 36 | return this.id; 37 | }; 38 | /** 39 | * To array 40 | * 41 | * @returns {{id: *, type: *}} 42 | */ 43 | AppUUID.prototype.toArray = function () { 44 | return { 45 | id: this.id 46 | }; 47 | }; 48 | /** 49 | * Create from array 50 | * 51 | * @param array {{id:string, type:string}} 52 | * 53 | * @return {ItemUUID} 54 | */ 55 | AppUUID.createFromArray = function (array) { 56 | array = JSON.parse(JSON.stringify(array)); 57 | return new AppUUID(array.id); 58 | }; 59 | /** 60 | * Compose unique id 61 | * 62 | * @returns {string} 63 | */ 64 | AppUUID.prototype.composedUUID = function () { 65 | return this.id; 66 | }; 67 | return AppUUID; 68 | }()); 69 | exports.AppUUID = AppUUID; 70 | -------------------------------------------------------------------------------- /lib/Model/Coordinate.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Coordinate = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | /** 6 | * Coordinate Type cast 7 | * @param coordinate 8 | */ 9 | var Coordinate = /** @class */ (function () { 10 | /** 11 | * Constructor 12 | * 13 | * @param {number} lat 14 | * @param {number} lon 15 | */ 16 | function Coordinate(lat, lon) { 17 | this.lat = lat; 18 | this.lon = lon; 19 | } 20 | /** 21 | * Get latitude 22 | * 23 | * @return float 24 | */ 25 | Coordinate.prototype.getLatitude = function () { 26 | return this.lat; 27 | }; 28 | /** 29 | * Get longitude 30 | * 31 | * @return float 32 | */ 33 | Coordinate.prototype.getLongitude = function () { 34 | return this.lon; 35 | }; 36 | /** 37 | * To array 38 | * 39 | * @return {{lat: number, lon: number}} 40 | */ 41 | Coordinate.prototype.toArray = function () { 42 | return { 43 | lat: this.lat, 44 | lon: this.lon 45 | }; 46 | }; 47 | /** 48 | * Create from array 49 | * 50 | * @param array 51 | * 52 | * @return Coordinate 53 | * 54 | * @throws InvalidFormatError 55 | */ 56 | Coordinate.createFromArray = function (array) { 57 | if (typeof array.lat == "undefined" || 58 | typeof array.lon == "undefined") { 59 | throw InvalidFormatError_1.InvalidFormatError.coordinateFormatNotValid(); 60 | } 61 | return new Coordinate(array.lat, array.lon); 62 | }; 63 | return Coordinate; 64 | }()); 65 | exports.Coordinate = Coordinate; 66 | -------------------------------------------------------------------------------- /lib/Http/RetryMap.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | var Retry_1 = require("./Retry"); 4 | /** 5 | * Http class 6 | */ 7 | var RetryMap = /** @class */ (function () { 8 | function RetryMap() { 9 | this.retries = {}; 10 | } 11 | /** 12 | * Create from array 13 | */ 14 | RetryMap.createFromArray = function (array) { 15 | var retryMap = new RetryMap(); 16 | for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { 17 | var retryConfig = array_1[_i]; 18 | retryMap.addRetry(Retry_1.Retry.createFromArray(retryConfig)); 19 | } 20 | return retryMap; 21 | }; 22 | /** 23 | * Add retry 24 | * 25 | * @param retry 26 | */ 27 | RetryMap.prototype.addRetry = function (retry) { 28 | this.retries[retry.getUrl() + "~~" + retry.getMethod()] = retry; 29 | }; 30 | /** 31 | * Get retry 32 | * 33 | * @param url 34 | * @param method 35 | * 36 | * @returns {Retry} 37 | */ 38 | RetryMap.prototype.getRetry = function (url, method) { 39 | if (this.retries[url + "~~" + method] instanceof Retry_1.Retry) { 40 | return this.retries[url + "~~" + method]; 41 | } 42 | if (this.retries["*~~" + method] instanceof Retry_1.Retry) { 43 | return this.retries["*~~" + method]; 44 | } 45 | if (this.retries[url + "~~*"] instanceof Retry_1.Retry) { 46 | return this.retries[url + "~~*"]; 47 | } 48 | if (this.retries["*~~*"] instanceof Retry_1.Retry) { 49 | return this.retries["*~~*"]; 50 | } 51 | return null; 52 | }; 53 | return RetryMap; 54 | }()); 55 | exports.RetryMap = RetryMap; 56 | -------------------------------------------------------------------------------- /lib/Model/IndexUUID.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.IndexUUID = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | /** 6 | * IndexUUID class 7 | */ 8 | var IndexUUID = /** @class */ (function () { 9 | /** 10 | * Constructor 11 | * 12 | * @param id 13 | */ 14 | function IndexUUID(id) { 15 | if (id.indexOf('_') >= 0) { 16 | throw InvalidFormatError_1.InvalidFormatError.indexUUIDFormatNotValid(); 17 | } 18 | this.id = id; 19 | } 20 | /** 21 | * Create by id 22 | * 23 | * @param id 24 | * 25 | * @returns {ItemUUID} 26 | */ 27 | IndexUUID.createById = function (id) { 28 | return new IndexUUID(id); 29 | }; 30 | /** 31 | * Return id 32 | * 33 | * @returns {string} 34 | */ 35 | IndexUUID.prototype.getId = function () { 36 | return this.id; 37 | }; 38 | /** 39 | * To array 40 | * 41 | * @returns {{id: *, type: *}} 42 | */ 43 | IndexUUID.prototype.toArray = function () { 44 | return { 45 | id: this.id 46 | }; 47 | }; 48 | /** 49 | * Create from array 50 | * 51 | * @param array {{id:string, type:string}} 52 | * 53 | * @return {ItemUUID} 54 | */ 55 | IndexUUID.createFromArray = function (array) { 56 | array = JSON.parse(JSON.stringify(array)); 57 | return new IndexUUID(array.id); 58 | }; 59 | /** 60 | * Compose unique id 61 | * 62 | * @returns {string} 63 | */ 64 | IndexUUID.prototype.composedUUID = function () { 65 | return this.id; 66 | }; 67 | return IndexUUID; 68 | }()); 69 | exports.IndexUUID = IndexUUID; 70 | -------------------------------------------------------------------------------- /lib/Model/Index.d.ts: -------------------------------------------------------------------------------- 1 | import { IndexUUID } from "./IndexUUID"; 2 | import { AppUUID } from "./AppUUID"; 3 | /** 4 | * Index class 5 | */ 6 | export declare class Index { 7 | private uuid; 8 | private appUUID; 9 | private isOK; 10 | private docCount; 11 | private size; 12 | /** 13 | * Constructor 14 | * 15 | * @param uuid 16 | * @param appUUID 17 | * @param isOK 18 | * @param docCount 19 | * @param size 20 | */ 21 | constructor(uuid: IndexUUID, appUUID: AppUUID, isOK?: boolean, docCount?: number, size?: string); 22 | /** 23 | * Get uuid 24 | * 25 | * @return {IndexUUID} 26 | */ 27 | getUUID(): IndexUUID; 28 | /** 29 | * Get app id 30 | * 31 | * @return {AppUUID} 32 | */ 33 | getAppUUID(): AppUUID; 34 | /** 35 | * Index is OK 36 | * 37 | * @return {boolean} 38 | */ 39 | isOk(): boolean; 40 | /** 41 | * Get doc count 42 | * 43 | * @return {number} 44 | */ 45 | getDocCount(): number; 46 | /** 47 | * get size 48 | * 49 | * @return {string} 50 | */ 51 | getSize(): string; 52 | /** 53 | * To array 54 | * 55 | * @returns {{id: string, attributes: {}}} 56 | */ 57 | toArray(): { 58 | uuid: any; 59 | app_id: any; 60 | is_ok: boolean; 61 | doc_count: number; 62 | size: string; 63 | }; 64 | /** 65 | * Create from array 66 | * 67 | * @param array 68 | * 69 | * @return User 70 | */ 71 | static createFromArray(array: { 72 | uuid: any; 73 | app_id: any; 74 | is_ok: boolean; 75 | doc_count: number; 76 | size: string; 77 | }): Index; 78 | } 79 | -------------------------------------------------------------------------------- /src/Model/Coordinate.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | 3 | /** 4 | * Coordinate Type cast 5 | * @param coordinate 6 | */ 7 | export class Coordinate { 8 | 9 | private lat: number; 10 | private lon: number; 11 | 12 | /** 13 | * Constructor 14 | * 15 | * @param {number} lat 16 | * @param {number} lon 17 | */ 18 | constructor(lat: number, 19 | lon: number) { 20 | this.lat = lat; 21 | this.lon = lon; 22 | } 23 | 24 | /** 25 | * Get latitude 26 | * 27 | * @return float 28 | */ 29 | public getLatitude(): number { 30 | return this.lat; 31 | } 32 | 33 | /** 34 | * Get longitude 35 | * 36 | * @return float 37 | */ 38 | public getLongitude(): number { 39 | return this.lon; 40 | } 41 | 42 | /** 43 | * To array 44 | * 45 | * @return {{lat: number, lon: number}} 46 | */ 47 | public toArray(): { 48 | lat: number, 49 | lon: number, 50 | } { 51 | return { 52 | lat: this.lat, 53 | lon: this.lon, 54 | }; 55 | } 56 | 57 | /** 58 | * Create from array 59 | * 60 | * @param array 61 | * 62 | * @return Coordinate 63 | * 64 | * @throws InvalidFormatError 65 | */ 66 | public static createFromArray(array: any) { 67 | 68 | if ( 69 | typeof array.lat == "undefined" || 70 | typeof array.lon == "undefined" 71 | ) { 72 | throw InvalidFormatError.coordinateFormatNotValid(); 73 | } 74 | 75 | return new Coordinate( 76 | array.lat, 77 | array.lon, 78 | ); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import Apisearch from "./Apisearch"; 2 | 3 | export default Apisearch; 4 | 5 | export * from "./Config/Config"; 6 | export * from "./Config/Synonym"; 7 | 8 | export * from "./Error/ConnectionError"; 9 | export * from "./Error/ErrorWithMessage"; 10 | export * from "./Error/EventError"; 11 | export * from "./Error/ForbiddenError"; 12 | export * from "./Error/InvalidFormatError"; 13 | export * from "./Error/InvalidTokenError"; 14 | export * from "./Error/ResourceExistsError"; 15 | export * from "./Error/ResourceNotAvailableError"; 16 | export * from "./Error/UnsupportedContentTypeError"; 17 | 18 | export * from "./Geo/LocationRange"; 19 | export * from "./Http/AxiosClient"; 20 | export * from "./Http/Client"; 21 | export * from "./Http/HttpClient"; 22 | export * from "./Http/Response"; 23 | export * from "./Http/CacheClient"; 24 | 25 | export * from "./Model/Changes"; 26 | export * from "./Model/Coordinate"; 27 | export * from "./Model/Item"; 28 | export * from "./Model/ItemUUID"; 29 | export * from "./Model/Metadata"; 30 | export * from "./Model/User"; 31 | 32 | export * from "./Query/Aggregation"; 33 | export * from "./Query/Filter"; 34 | export * from "./Query/Query"; 35 | export * from "./Query/Range"; 36 | export * from "./Query/ScoreStrategies"; 37 | export * from "./Query/ScoreStrategy"; 38 | export * from "./Query/SortBy"; 39 | 40 | export * from "./Repository/HttpRepository"; 41 | export * from "./Repository/Repository"; 42 | 43 | export * from "./Result/ResultAggregation"; 44 | export * from "./Result/ResultAggregations"; 45 | export * from "./Result/Counter"; 46 | export * from "./Result/Result"; 47 | 48 | export * from "./Transformer/ReadTransformer"; 49 | export * from "./Transformer/Transformer"; 50 | export * from "./Transformer/WriteTransformer"; 51 | -------------------------------------------------------------------------------- /src/Http/TestClient.ts: -------------------------------------------------------------------------------- 1 | import {Query} from "../Query/Query"; 2 | import {Result} from "../Result/Result"; 3 | import {HttpClient} from "./HttpClient"; 4 | import {Response} from "./Response"; 5 | 6 | export class TestClient implements HttpClient { 7 | 8 | public calls: any[] = []; 9 | 10 | /** 11 | * Get 12 | * 13 | * @param url 14 | * @param method 15 | * @param credentials 16 | * @param parameters 17 | * @param data 18 | * 19 | * @return {Promise} 20 | */ 21 | public async get( 22 | url: string, 23 | method: string, 24 | credentials: any, 25 | parameters: any = {}, 26 | data: any = {}, 27 | ): Promise { 28 | 29 | this.calls.push({ 30 | url, 31 | method, 32 | credentials, 33 | parameters, 34 | data, 35 | }); 36 | 37 | if (credentials.token === "error") { 38 | throw new Error("Error found"); 39 | } 40 | 41 | return new Promise( 42 | (resolve) => resolve(new Response( 43 | 200, 44 | (( 45 | method === "get" && 46 | url === "/" 47 | ) 48 | ? Result.createFromArray({ 49 | query: Query.createMatchAll(), 50 | }).toArray() 51 | : ""), 52 | )), 53 | ); 54 | } 55 | 56 | /** 57 | * Abort current request 58 | * And regenerate the cancellation token 59 | * 60 | * @param url 61 | * @param urlIsFormatted 62 | */ 63 | public abort( 64 | url: string, 65 | urlIsFormatted: boolean, 66 | ) 67 | { 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /lib/Model/Metadata.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Metadata = void 0; 4 | /** 5 | * User class 6 | */ 7 | var Metadata = /** @class */ (function () { 8 | function Metadata() { 9 | } 10 | /** 11 | * To metadata 12 | * 13 | * @param array:{} 14 | * 15 | * @returns {string} 16 | */ 17 | Metadata.toMetadata = function (array) { 18 | array = JSON.parse(JSON.stringify(array)); 19 | var parts = []; 20 | for (var key in array) { 21 | parts.push(key + "##" + array[key]); 22 | } 23 | return parts.join("~~"); 24 | }; 25 | /** 26 | * From metadata 27 | * 28 | * @param metadata 29 | * 30 | * @return {{}} 31 | */ 32 | Metadata.fromMetadata = function (metadata) { 33 | var values = {}; 34 | var splittedParts = metadata.split("~~"); 35 | var iterator = 0; 36 | var size = 0; 37 | var lastElement = null; 38 | for (var key in splittedParts) { 39 | var part = splittedParts[key]; 40 | var parts = part.split("##"); 41 | if (parts.length > 1) { 42 | lastElement = parts[1]; 43 | values[parts[0]] = lastElement; 44 | } 45 | else { 46 | lastElement = part; 47 | values[iterator++] = lastElement; 48 | } 49 | size++; 50 | } 51 | if (size == 1) { 52 | values = { 53 | id: lastElement, 54 | name: lastElement 55 | }; 56 | } 57 | if (typeof values.id == "undefined") { 58 | return null; 59 | } 60 | return values; 61 | }; 62 | return Metadata; 63 | }()); 64 | exports.Metadata = Metadata; 65 | -------------------------------------------------------------------------------- /test/Transformer/Transformer.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Transformer} from "../../src/Transformer/Transformer"; 3 | import {ProductReadTransformer} from "./ProductReadTransformer"; 4 | import {ProductWriteTransformer} from "./ProductWriteTransformer"; 5 | import {Product} from "./Product"; 6 | 7 | describe('Transformer/', () => { 8 | describe('Transformer', () => { 9 | describe('basic test', () => { 10 | let transformer = new Transformer(); 11 | transformer.addReadTransformer(new ProductReadTransformer()); 12 | transformer.addWriteTransformer(new ProductWriteTransformer()); 13 | 14 | it('Should work properly', () => { 15 | const product = new Product( 16 | 111, 17 | 9999, 18 | 'my-product' 19 | ); 20 | const item = transformer.toItem(product); 21 | expect(item.get('sku')).to.be.equal(9999); 22 | expect(item.get('name')).to.be.equal('my-product'); 23 | expect(item.getId()).to.be.equal(111); 24 | expect(transformer.toItems([product])[0]).to.be.deep.equal(item); 25 | const itemUUID = transformer.toItemUUID(product); 26 | expect(transformer.toItemUUIDs([product])[0]).to.be.deep.equal(itemUUID); 27 | expect(itemUUID.getId()).to.be.equal(111); 28 | expect(itemUUID.getType()).to.be.equal('product'); 29 | const returnedProduct = transformer.fromItem(item); 30 | expect(returnedProduct.sku).to.be.equal(9999); 31 | expect(returnedProduct.name).to.be.equal('my-product'); 32 | expect(returnedProduct.id).to.be.equal(111); 33 | }); 34 | 35 | }); 36 | }); 37 | }); -------------------------------------------------------------------------------- /lib/Config/Config.d.ts: -------------------------------------------------------------------------------- 1 | import { Synonym } from "./Synonym"; 2 | export declare const DEFAULT_SHARDS = 1; 3 | export declare const DEFAULT_REPLICAS = 0; 4 | /** 5 | * Result class 6 | */ 7 | export declare class Config { 8 | private language; 9 | private storeSearchableMetadata; 10 | private synonyms; 11 | private shards; 12 | private replicas; 13 | /** 14 | * Constructor 15 | * 16 | * @param language 17 | * @param storeSearchableMetadata 18 | * @param shards 19 | * @param replicas 20 | */ 21 | constructor(language?: string, storeSearchableMetadata?: boolean, shards?: number, replicas?: number); 22 | /** 23 | * Get language 24 | * 25 | * @return {string} 26 | */ 27 | getLanguage(): string; 28 | /** 29 | * Should searchable metadata be stored 30 | * 31 | * @return {boolean} 32 | */ 33 | shouldSearchableMetadataBeStored(): boolean; 34 | /** 35 | * Add synonym 36 | * 37 | * @param synonym 38 | */ 39 | addSynonym(synonym: Synonym): void; 40 | /** 41 | * Get synonyms 42 | * 43 | * @return {Synonym[]} 44 | */ 45 | getSynonyms(): Synonym[]; 46 | /** 47 | * Get shards 48 | * 49 | * @return {number} 50 | */ 51 | getShards(): number; 52 | /** 53 | * Get replicas 54 | * 55 | * @return {number} 56 | */ 57 | getReplicas(): number; 58 | /** 59 | * to array 60 | */ 61 | toArray(): { 62 | language: string; 63 | store_searchable_metadata: boolean; 64 | synonyms: { 65 | words: string[]; 66 | }[]; 67 | shards: number; 68 | replicas: number; 69 | }; 70 | /** 71 | * Create from array 72 | */ 73 | static createFromArray(array: any): Config; 74 | } 75 | -------------------------------------------------------------------------------- /lib/Transformer/Transformer.d.ts: -------------------------------------------------------------------------------- 1 | import { Item } from "../Model/Item"; 2 | import { ItemUUID } from "../Model/ItemUUID"; 3 | import { ReadTransformer } from "./ReadTransformer"; 4 | import { WriteTransformer } from "./WriteTransformer"; 5 | /** 6 | * Transformer 7 | */ 8 | export declare class Transformer { 9 | private readTransformers; 10 | private writeTransformers; 11 | /** 12 | * Add read transformer 13 | * 14 | * @param readTransformer 15 | */ 16 | addReadTransformer(readTransformer: ReadTransformer): void; 17 | /** 18 | * @return {boolean} 19 | */ 20 | hasReadTransformers(): boolean; 21 | /** 22 | * Add write transformer 23 | * 24 | * @param writeTransformer 25 | */ 26 | addWriteTransformer(writeTransformer: WriteTransformer): void; 27 | /** 28 | * Items to objects 29 | * 30 | * @param items 31 | * 32 | * @returns {any[]} 33 | */ 34 | fromItems(items: Item[]): any[]; 35 | /** 36 | * Item to object 37 | * 38 | * @param item 39 | * 40 | * @returns {any} 41 | */ 42 | fromItem(item: Item): any; 43 | /** 44 | * Objects to items 45 | * 46 | * @param objects 47 | * 48 | * @returns {Item[]} 49 | */ 50 | toItems(objects: any[]): Item[]; 51 | /** 52 | * Object to item 53 | * 54 | * @param object 55 | * 56 | * @returns {any} 57 | */ 58 | toItem(object: any): any; 59 | /** 60 | * Objects to items 61 | * 62 | * @param objects 63 | * 64 | * @returns {ItemUUID[]} 65 | */ 66 | toItemUUIDs(objects: any[]): ItemUUID[]; 67 | /** 68 | * Object to item 69 | * 70 | * @param object 71 | * 72 | * @returns {any} 73 | */ 74 | toItemUUID(object: any): any; 75 | } 76 | -------------------------------------------------------------------------------- /test/Model/User.test.ts: -------------------------------------------------------------------------------- 1 | import {User} from "../../src/Model/User"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('User', () => { 6 | describe('() Non empty attributes', () => { 7 | let user = new User('1', {'field': 'value'}); 8 | 9 | it('Should construct properly', () => { 10 | expect(user.getId()).to.be.equal('1'); 11 | expect(user.getAttributes()).to.be.deep.equal({'field': 'value'}); 12 | }); 13 | }); 14 | 15 | describe('() Empty attributes', () => { 16 | let user = new User('1'); 17 | 18 | it('Should construct properly', () => { 19 | expect(user.getId()).to.be.equal('1'); 20 | expect(user.getAttributes()).to.be.deep.equal({}); 21 | }); 22 | }); 23 | 24 | describe('.createFromArray()', () => { 25 | describe('with correct values', () => { 26 | let user = User.createFromArray({'id': '1', 'attributes': {'field': 'value'}}); 27 | 28 | it('Should construct properly', () => { 29 | expect(user.getId()).to.be.equal('1'); 30 | expect(user.getAttributes()).to.be.deep.equal({'field': 'value'}); 31 | }); 32 | }); 33 | 34 | describe('with bad values', () => { 35 | it('Should throw exception', () => { 36 | expect(function() {User.createFromArray({})}).to.throw(); 37 | }); 38 | }); 39 | }); 40 | 41 | describe('().toArray()', () => { 42 | let userAsArray = {'id': '1', 'attributes': {'field': 'value'}}; 43 | expect(User.createFromArray(userAsArray).toArray()).to.be.deep.equal(userAsArray); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /lib/Model/User.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.User = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | /** 6 | * User class 7 | */ 8 | var User = /** @class */ (function () { 9 | /** 10 | * Construct 11 | * 12 | * @param id string 13 | * @param attributes Array 14 | */ 15 | function User(id, attributes) { 16 | if (attributes === void 0) { attributes = {}; } 17 | this.id = id; 18 | this.attributes = attributes; 19 | } 20 | /** 21 | * Return the user id 22 | * 23 | * @return {string} 24 | */ 25 | User.prototype.getId = function () { 26 | return this.id; 27 | }; 28 | /** 29 | * Return array 30 | * 31 | * @returns {{}} 32 | */ 33 | User.prototype.getAttributes = function () { 34 | return this.attributes; 35 | }; 36 | /** 37 | * To array 38 | * 39 | * @returns {{id: string, attributes: {}}} 40 | */ 41 | User.prototype.toArray = function () { 42 | var array = { 43 | id: this.id 44 | }; 45 | if (Object.keys(this.attributes).length > 0) { 46 | array.attributes = this.attributes; 47 | } 48 | return array; 49 | }; 50 | /** 51 | * Create from array 52 | * 53 | * @param array 54 | * 55 | * @return User 56 | */ 57 | User.createFromArray = function (array) { 58 | if (array == null || 59 | typeof array.id == "undefined" || 60 | array.id == null) { 61 | throw InvalidFormatError_1.InvalidFormatError.userFormatNotValid(); 62 | } 63 | var attributes = typeof array.attributes === typeof {} 64 | ? array.attributes 65 | : {}; 66 | return new User(array.id, attributes); 67 | }; 68 | return User; 69 | }()); 70 | exports.User = User; 71 | -------------------------------------------------------------------------------- /lib/Http/Retry.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.DEFAULT_MICROSECONDS_BETWEEN_RETRIES = 1000; 4 | /** 5 | * Http class 6 | */ 7 | var Retry = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param url 12 | * @param method 13 | * @param retries 14 | * @param microsecondsBetweenRetries 15 | */ 16 | function Retry(url, method, retries, microsecondsBetweenRetries) { 17 | this.url = url; 18 | this.method = method; 19 | this.retries = retries; 20 | this.microsecondsBetweenRetries = microsecondsBetweenRetries; 21 | } 22 | /** 23 | * Create from array 24 | * 25 | * @param array 26 | * 27 | * @return {Retry} 28 | */ 29 | Retry.createFromArray = function (array) { 30 | return new Retry(array.url ? array.url : "*", array.method ? array.method : "*", array.retries ? array.retries : 0, array.microseconds_between_retries 31 | ? array.microseconds_between_retries 32 | : exports.DEFAULT_MICROSECONDS_BETWEEN_RETRIES); 33 | }; 34 | /** 35 | * Get url 36 | * 37 | * @return {string} 38 | */ 39 | Retry.prototype.getUrl = function () { 40 | return this.url; 41 | }; 42 | /** 43 | * Get method 44 | * 45 | * @return {string} 46 | */ 47 | Retry.prototype.getMethod = function () { 48 | return this.method; 49 | }; 50 | /** 51 | * Ge retries 52 | * 53 | * @return {number} 54 | */ 55 | Retry.prototype.getRetries = function () { 56 | return this.retries; 57 | }; 58 | /** 59 | * Get microseconds between retries 60 | * 61 | * @return {number} 62 | */ 63 | Retry.prototype.getMicrosecondsBetweenRetries = function () { 64 | return this.microsecondsBetweenRetries; 65 | }; 66 | return Retry; 67 | }()); 68 | exports.Retry = Retry; 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "apisearch", 3 | "version": "0.3.19", 4 | "description": "Javascript client for Apisearch.", 5 | "main": "lib/index.js", 6 | "types": "lib/index.d.ts", 7 | "scripts": { 8 | "dev": "webpack --config ./webpack.dev.js --progress --colors --watch", 9 | "build:lib": "tsc -d --outDir lib -m commonjs", 10 | "build:dev": "webpack --config ./webpack.dev.js", 11 | "build:min": "webpack --config ./webpack.min.js", 12 | "dist": "npm t; rm -rf ./dist/*; npm run build:lib && npm run build:dev && npm run build:min", 13 | "dist-no-test": "rm -rf ./dist/*; npm run build:lib && npm run build:dev && npm run build:min", 14 | "fix": "tslint -c tslint.json --fix 'src/**/*.ts'", 15 | "test:unit": "mocha --timeout 10000 --recursive --require ts-node/register test/**/*.ts test/*.ts", 16 | "test": "npm run test:unit" 17 | }, 18 | "license": "MIT", 19 | "author": "Marc Morera ", 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/apisearch-io/javascript-client.git" 23 | }, 24 | "bugs": { 25 | "url": "https://github.com/apisearch-io/javascript-client/issues" 26 | }, 27 | "homepage": "https://github.com/apisearch-io/javascript-client", 28 | "keywords": [ 29 | "es6", 30 | "search-engine", 31 | "apisearch", 32 | "webpack4", 33 | "node", 34 | "javascript-client" 35 | ], 36 | "devDependencies": { 37 | "@types/chai": "^4.1", 38 | "@types/mocha": "^5.2", 39 | "@types/node": "^10.14", 40 | "@types/sinon": "^7.0", 41 | "chai": "^4.2", 42 | "mocha": "^9.1.3", 43 | "sinon": "^7.3", 44 | "ts-loader": "^9.2.6", 45 | "ts-node": "^8.4", 46 | "tslint": "^5.14", 47 | "typescript": "^3.6", 48 | "webpack": "^5.53.0", 49 | "webpack-cli": "^4.9.1", 50 | "webpack-merge": "^4.2" 51 | }, 52 | "dependencies": { 53 | "axios": ">=0.21.1", 54 | "ts-md5": "^1.2.4" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /lib/Model/Changes.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * filter constants 3 | */ 4 | export declare const TYPE_VALUE = 1; 5 | export declare const TYPE_LITERAL = 4; 6 | export declare const TYPE_ARRAY_ELEMENT_UPDATE = 8; 7 | export declare const TYPE_ARRAY_ELEMENT_ADD = 16; 8 | export declare const TYPE_ARRAY_ELEMENT_DELETE = 32; 9 | export declare const TYPE_ARRAY_EXPECTS_ELEMENT = 24; 10 | export declare const TYPE_ARRAY = 56; 11 | /** 12 | * Changes Type cast 13 | * @param Changes 14 | */ 15 | export declare class Changes { 16 | /** 17 | * Changes 18 | * 19 | * @type {Array} 20 | */ 21 | changes: any[]; 22 | /** 23 | * Add new change 24 | * 25 | * @param field 26 | * @param value 27 | * @param type 28 | */ 29 | addChange(field: string, value: string, type?: number): void; 30 | /** 31 | * Update element from list 32 | * 33 | * @param field 34 | * @param condition 35 | * @param value 36 | * @param type 37 | */ 38 | updateElementFromList(field: string, condition: string, value: string, type: number): void; 39 | /** 40 | * Add element in list 41 | * 42 | * @param field 43 | * @param value 44 | * @param type 45 | */ 46 | addElementInList(field: string, value: string, type: number): void; 47 | /** 48 | * Delete element from list 49 | * 50 | * @param field 51 | * @param condition 52 | */ 53 | deleteElementFromList(field: string, condition: string): void; 54 | /** 55 | * Get changes 56 | * 57 | * @returns {[]} 58 | */ 59 | getChanges(): any[]; 60 | /** 61 | * Create 62 | * 63 | * @returns {Changes} 64 | */ 65 | static create(): Changes; 66 | /** 67 | * To array 68 | * 69 | * @returns {[]} 70 | */ 71 | toArray(): Object; 72 | /** 73 | * Create from array 74 | * 75 | * @param array 76 | * 77 | * @returns {Changes} 78 | */ 79 | static createFromArray(array: any): Changes; 80 | } 81 | -------------------------------------------------------------------------------- /test/Query/Range.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Range} from "../../src/Query/Range"; 3 | import { 4 | RANGE_MINUS_INFINITE, 5 | RANGE_INFINITE 6 | } from '../../src/Query/Range'; 7 | 8 | describe('Query/', () => { 9 | describe('Range', () => { 10 | describe('.stringToArray()', () => { 11 | [ 12 | ['2..5', [2, 5]], 13 | ['..10', [RANGE_MINUS_INFINITE, 10]], 14 | ['0..', [0, RANGE_INFINITE]], 15 | ['2..', [2, RANGE_INFINITE]], 16 | ['..', [RANGE_MINUS_INFINITE, RANGE_INFINITE]] 17 | ].forEach(function(element:[string, [number, number]]) { 18 | it('Should work with entry ' + element[0], () => { 19 | expect(Range.stringToArray(element[0])).to.be.deep.equal(element[1]); 20 | }); 21 | }); 22 | }); 23 | 24 | describe('.arrayToString()', () => { 25 | [ 26 | [[2, 6], '2..6'], 27 | [[RANGE_MINUS_INFINITE, 10], '..10'], 28 | [[2, RANGE_INFINITE], '2..'], 29 | [[RANGE_MINUS_INFINITE, RANGE_INFINITE], '..'], 30 | ].forEach(function(element:[[number, number], string]) { 31 | it('Should work with entry ' + element[0], () => { 32 | expect(Range.arrayToString(element[0])).to.be.equal(element[1]); 33 | }); 34 | }); 35 | }); 36 | 37 | describe('.createRanges()', () => { 38 | [ 39 | [1, 7, 2, ['1..3', '3..5', '5..7']], 40 | [0, 4, 2, ['0..2', '2..4']], 41 | ].forEach(function(element:[number, number, number, string[]]) { 42 | it('Should work with entry ' + element[0] + ',' + element[1] + ',' + element[2], () => { 43 | expect(Range.createRanges(element[0], element[1], element[2])).to.be.deep.equal(element[3]); 44 | }); 45 | }); 46 | }); 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /src/Model/User.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | 3 | /** 4 | * User class 5 | */ 6 | export class User { 7 | 8 | private id: string; 9 | private attributes: {}; 10 | 11 | /** 12 | * Construct 13 | * 14 | * @param id string 15 | * @param attributes Array 16 | */ 17 | constructor(id: string, 18 | attributes: Object = {}) { 19 | this.id = id; 20 | this.attributes = attributes; 21 | } 22 | 23 | /** 24 | * Return the user id 25 | * 26 | * @return {string} 27 | */ 28 | public getId(): string { 29 | return this.id; 30 | } 31 | 32 | /** 33 | * Return array 34 | * 35 | * @returns {{}} 36 | */ 37 | public getAttributes(): {} { 38 | return this.attributes; 39 | } 40 | 41 | /** 42 | * To array 43 | * 44 | * @returns {{id: string, attributes: {}}} 45 | */ 46 | public toArray(): { 47 | id: string, 48 | attributes?: {}, 49 | } { 50 | const array: { 51 | id: string, 52 | attributes?: {}, 53 | } = { 54 | id: this.id, 55 | }; 56 | 57 | if (Object.keys(this.attributes).length > 0) { 58 | array.attributes = this.attributes; 59 | } 60 | 61 | return array; 62 | } 63 | 64 | /** 65 | * Create from array 66 | * 67 | * @param array 68 | * 69 | * @return User 70 | */ 71 | public static createFromArray(array: any) { 72 | if ( 73 | array == null || 74 | typeof array.id == "undefined" || 75 | array.id == null 76 | ) { 77 | throw InvalidFormatError.userFormatNotValid(); 78 | } 79 | 80 | const attributes = typeof array.attributes === typeof {} 81 | ? array.attributes 82 | : {}; 83 | 84 | return new User( 85 | array.id, 86 | attributes, 87 | ); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /test/Model/ItemUUID.test.ts: -------------------------------------------------------------------------------- 1 | import {ItemUUID} from "../../src/Model/ItemUUID"; 2 | import { expect } from 'chai'; 3 | 4 | describe('Model/', () => { 5 | describe('ItemUUID', () => { 6 | describe('()', () => { 7 | let itemUUID = new ItemUUID('1', 'product'); 8 | 9 | it('Should work properly', () => { 10 | expect(itemUUID.getId()).to.be.equal('1'); 11 | expect(itemUUID.getType()).to.be.equal('product'); 12 | }); 13 | }); 14 | describe('.createByComposedUUID()', () => { 15 | describe('with good params', () => { 16 | let itemUUID = ItemUUID.createByComposedUUID('1~product'); 17 | it('should work properly', () => { 18 | expect(itemUUID.getId()).to.be.equal('1'); 19 | expect(itemUUID.getType()).to.be.equal('product'); 20 | }); 21 | }); 22 | 23 | describe('with bad params', () => { 24 | expect(function() {ItemUUID.createByComposedUUID('')}).to.throw(); 25 | expect(function() {ItemUUID.createByComposedUUID('1')}).to.throw(); 26 | expect(function() {ItemUUID.createByComposedUUID('product')}).to.throw(); 27 | expect(function() {ItemUUID.createByComposedUUID('1~product~xxx')}).to.throw(); 28 | }); 29 | }); 30 | describe('.createFromArray()', () => { 31 | describe('with good params', () => { 32 | let itemUUID = ItemUUID.createFromArray({'id': '1', 'type': 'product'}); 33 | it('should work properly', () => { 34 | expect(itemUUID.getId()).to.be.equal('1'); 35 | expect(itemUUID.getType()).to.be.equal('product'); 36 | }); 37 | }); 38 | }); 39 | describe('().toArray()', () => { 40 | let itemUUIDAsArray = {'id': '1', 'type': 'product'}; 41 | expect(ItemUUID.createFromArray(itemUUIDAsArray).toArray()).to.be.deep.equal(itemUUIDAsArray); 42 | }); 43 | 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /src/Query/Range.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Aggregation constants 3 | */ 4 | export const RANGE_MINUS_INFINITE = null; 5 | export const RANGE_INFINITE = null; 6 | export const RANGE_SEPARATOR = ".."; 7 | 8 | /** 9 | * Filter class 10 | */ 11 | export class Range { 12 | 13 | /** 14 | * Strong to array 15 | * 16 | * @param string 17 | * 18 | * @returns {[number, number]} 19 | */ 20 | public static stringToArray(string: string): [number, number] { 21 | const parts = string.split(RANGE_SEPARATOR); 22 | const from = parts[0]; 23 | const to = parts[1]; 24 | let finalFrom: number = RANGE_MINUS_INFINITE; 25 | let finalTo: number = RANGE_INFINITE; 26 | 27 | if (from != "") { 28 | finalFrom = parseInt(from); 29 | } 30 | 31 | if (to != "") { 32 | finalTo = parseInt(to); 33 | } 34 | 35 | return [finalFrom, finalTo]; 36 | 37 | } 38 | 39 | /** 40 | * Array to string 41 | * 42 | * @param values 43 | * 44 | * @return {string} 45 | */ 46 | public static arrayToString(values: [number, number]): string { 47 | const finalValues: [string, string] = ["", ""]; 48 | if (values[0] != RANGE_MINUS_INFINITE) { 49 | finalValues[0] = String(values[0]); 50 | } 51 | 52 | if (values[1] != RANGE_INFINITE) { 53 | finalValues[1] = String(values[1]); 54 | } 55 | 56 | return finalValues.join(RANGE_SEPARATOR); 57 | } 58 | 59 | /** 60 | * Create ranges 61 | * 62 | * @param from 63 | * @param to 64 | * @param incremental 65 | */ 66 | public static createRanges(from: number, 67 | to: number, 68 | incremental: number): string[] { 69 | const ranges = []; 70 | let nextTo; 71 | while (from < to) { 72 | nextTo = from + incremental; 73 | ranges.push(from + RANGE_SEPARATOR + nextTo); 74 | from = nextTo; 75 | } 76 | 77 | return ranges; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /lib/Http/TestClient.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.TestClient = void 0; 4 | var tslib_1 = require("tslib"); 5 | var Query_1 = require("../Query/Query"); 6 | var Result_1 = require("../Result/Result"); 7 | var Response_1 = require("./Response"); 8 | var TestClient = /** @class */ (function () { 9 | function TestClient() { 10 | this.calls = []; 11 | } 12 | /** 13 | * Get 14 | * 15 | * @param url 16 | * @param method 17 | * @param credentials 18 | * @param parameters 19 | * @param data 20 | * 21 | * @return {Promise} 22 | */ 23 | TestClient.prototype.get = function (url, method, credentials, parameters, data) { 24 | if (parameters === void 0) { parameters = {}; } 25 | if (data === void 0) { data = {}; } 26 | return tslib_1.__awaiter(this, void 0, void 0, function () { 27 | return tslib_1.__generator(this, function (_a) { 28 | this.calls.push({ 29 | url: url, 30 | method: method, 31 | credentials: credentials, 32 | parameters: parameters, 33 | data: data 34 | }); 35 | if (credentials.token === "error") { 36 | throw new Error("Error found"); 37 | } 38 | return [2 /*return*/, new Promise(function (resolve) { return resolve(new Response_1.Response(200, ((method === "get" && 39 | url === "/") 40 | ? Result_1.Result.createFromArray({ 41 | query: Query_1.Query.createMatchAll() 42 | }).toArray() 43 | : ""))); })]; 44 | }); 45 | }); 46 | }; 47 | /** 48 | * Abort current request 49 | * And regenerate the cancellation token 50 | * 51 | * @param url 52 | * @param urlIsFormatted 53 | */ 54 | TestClient.prototype.abort = function (url, urlIsFormatted) { 55 | }; 56 | return TestClient; 57 | }()); 58 | exports.TestClient = TestClient; 59 | -------------------------------------------------------------------------------- /lib/Model/ItemUUID.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ItemUUID = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | /** 6 | * ItemUUID class 7 | */ 8 | var ItemUUID = /** @class */ (function () { 9 | /** 10 | * Constructor 11 | * 12 | * @param id 13 | * @param type 14 | */ 15 | function ItemUUID(id, type) { 16 | this.id = id; 17 | this.type = type; 18 | } 19 | /** 20 | * Create composed UUID 21 | * 22 | * @param composedUUID 23 | * 24 | * @returns {ItemUUID} 25 | */ 26 | ItemUUID.createByComposedUUID = function (composedUUID) { 27 | var parts = composedUUID.split("~"); 28 | if (2 != parts.length) { 29 | throw InvalidFormatError_1.InvalidFormatError.composedItemUUIDNotValid(); 30 | } 31 | return new ItemUUID(parts[0], parts[1]); 32 | }; 33 | /** 34 | * Return id 35 | * 36 | * @returns {string} 37 | */ 38 | ItemUUID.prototype.getId = function () { 39 | return this.id; 40 | }; 41 | /** 42 | * Get type 43 | * 44 | * @returns {string} 45 | */ 46 | ItemUUID.prototype.getType = function () { 47 | return this.type; 48 | }; 49 | /** 50 | * To array 51 | * 52 | * @returns {{id: *, type: *}} 53 | */ 54 | ItemUUID.prototype.toArray = function () { 55 | return { 56 | id: this.id, 57 | type: this.type 58 | }; 59 | }; 60 | /** 61 | * Create from array 62 | * 63 | * @param array {{id:string, type:string}} 64 | * 65 | * @return {ItemUUID} 66 | */ 67 | ItemUUID.createFromArray = function (array) { 68 | array = JSON.parse(JSON.stringify(array)); 69 | return new ItemUUID(array.id, array.type); 70 | }; 71 | /** 72 | * Compose unique id 73 | * 74 | * @returns {string} 75 | */ 76 | ItemUUID.prototype.composedUUID = function () { 77 | return this.id + "~" + this.type; 78 | }; 79 | return ItemUUID; 80 | }()); 81 | exports.ItemUUID = ItemUUID; 82 | -------------------------------------------------------------------------------- /lib/Query/Range.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Range = exports.RANGE_SEPARATOR = exports.RANGE_INFINITE = exports.RANGE_MINUS_INFINITE = void 0; 4 | /** 5 | * Aggregation constants 6 | */ 7 | exports.RANGE_MINUS_INFINITE = null; 8 | exports.RANGE_INFINITE = null; 9 | exports.RANGE_SEPARATOR = ".."; 10 | /** 11 | * Filter class 12 | */ 13 | var Range = /** @class */ (function () { 14 | function Range() { 15 | } 16 | /** 17 | * Strong to array 18 | * 19 | * @param string 20 | * 21 | * @returns {[number, number]} 22 | */ 23 | Range.stringToArray = function (string) { 24 | var parts = string.split(exports.RANGE_SEPARATOR); 25 | var from = parts[0]; 26 | var to = parts[1]; 27 | var finalFrom = exports.RANGE_MINUS_INFINITE; 28 | var finalTo = exports.RANGE_INFINITE; 29 | if (from != "") { 30 | finalFrom = parseInt(from); 31 | } 32 | if (to != "") { 33 | finalTo = parseInt(to); 34 | } 35 | return [finalFrom, finalTo]; 36 | }; 37 | /** 38 | * Array to string 39 | * 40 | * @param values 41 | * 42 | * @return {string} 43 | */ 44 | Range.arrayToString = function (values) { 45 | var finalValues = ["", ""]; 46 | if (values[0] != exports.RANGE_MINUS_INFINITE) { 47 | finalValues[0] = String(values[0]); 48 | } 49 | if (values[1] != exports.RANGE_INFINITE) { 50 | finalValues[1] = String(values[1]); 51 | } 52 | return finalValues.join(exports.RANGE_SEPARATOR); 53 | }; 54 | /** 55 | * Create ranges 56 | * 57 | * @param from 58 | * @param to 59 | * @param incremental 60 | */ 61 | Range.createRanges = function (from, to, incremental) { 62 | var ranges = []; 63 | var nextTo; 64 | while (from < to) { 65 | nextTo = from + incremental; 66 | ranges.push(from + exports.RANGE_SEPARATOR + nextTo); 67 | from = nextTo; 68 | } 69 | return ranges; 70 | }; 71 | return Range; 72 | }()); 73 | exports.Range = Range; 74 | -------------------------------------------------------------------------------- /src/Model/ItemUUID.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | 3 | /** 4 | * ItemUUID class 5 | */ 6 | export class ItemUUID { 7 | 8 | private id: string; 9 | private type: string; 10 | 11 | /** 12 | * Constructor 13 | * 14 | * @param id 15 | * @param type 16 | */ 17 | constructor(id: string, 18 | type: string) { 19 | this.id = id; 20 | this.type = type; 21 | } 22 | 23 | /** 24 | * Create composed UUID 25 | * 26 | * @param composedUUID 27 | * 28 | * @returns {ItemUUID} 29 | */ 30 | public static createByComposedUUID(composedUUID: string): ItemUUID { 31 | const parts = composedUUID.split("~"); 32 | if (2 != parts.length) { 33 | throw InvalidFormatError.composedItemUUIDNotValid(); 34 | } 35 | 36 | return new ItemUUID(parts[0], parts[1]); 37 | } 38 | 39 | /** 40 | * Return id 41 | * 42 | * @returns {string} 43 | */ 44 | public getId(): string { 45 | return this.id; 46 | } 47 | 48 | /** 49 | * Get type 50 | * 51 | * @returns {string} 52 | */ 53 | public getType(): string { 54 | return this.type; 55 | } 56 | 57 | /** 58 | * To array 59 | * 60 | * @returns {{id: *, type: *}} 61 | */ 62 | public toArray(): { 63 | id: string, 64 | type: string, 65 | } { 66 | return { 67 | id: this.id, 68 | type: this.type, 69 | }; 70 | } 71 | 72 | /** 73 | * Create from array 74 | * 75 | * @param array {{id:string, type:string}} 76 | * 77 | * @return {ItemUUID} 78 | */ 79 | public static createFromArray(array: {id: string, type: string}): ItemUUID { 80 | array = JSON.parse(JSON.stringify(array)); 81 | return new ItemUUID( 82 | array.id, 83 | array.type, 84 | ); 85 | } 86 | 87 | /** 88 | * Compose unique id 89 | * 90 | * @returns {string} 91 | */ 92 | public composedUUID(): string { 93 | return `${this.id}~${this.type}`; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /lib/Error/ResourceNotAvailableError.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ResourceNotAvailableError = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ErrorWithMessage_1 = require("./ErrorWithMessage"); 6 | /** 7 | * Resource not available error 8 | */ 9 | var ResourceNotAvailableError = /** @class */ (function (_super) { 10 | tslib_1.__extends(ResourceNotAvailableError, _super); 11 | function ResourceNotAvailableError() { 12 | return _super !== null && _super.apply(this, arguments) || this; 13 | } 14 | /** 15 | * Get transportable http error 16 | * 17 | * @return {number} 18 | */ 19 | ResourceNotAvailableError.getTransportableHTTPError = function () { 20 | return 404; 21 | }; 22 | /** 23 | * Index not available 24 | * 25 | * @param resourceId 26 | * 27 | * @return {InvalidFormatError} 28 | */ 29 | ResourceNotAvailableError.indexNotAvailable = function (resourceId) { 30 | return new ResourceNotAvailableError("Index not available - " + resourceId); 31 | }; 32 | /** 33 | * Events not available 34 | * 35 | * @param resourceId 36 | * 37 | * @return {InvalidFormatError} 38 | */ 39 | ResourceNotAvailableError.eventsIndexNotAvailable = function (resourceId) { 40 | return new ResourceNotAvailableError("Events not available - " + resourceId); 41 | }; 42 | /** 43 | * Logs not available 44 | * 45 | * @param resourceId 46 | * 47 | * @return {InvalidFormatError} 48 | */ 49 | ResourceNotAvailableError.logsIndexNotAvailable = function (resourceId) { 50 | return new ResourceNotAvailableError("Logs not available - " + resourceId); 51 | }; 52 | /** 53 | * Engine not available 54 | * 55 | * @param resourceId 56 | * 57 | * @return {InvalidFormatError} 58 | */ 59 | ResourceNotAvailableError.engineNotAvailable = function (resourceId) { 60 | return new ResourceNotAvailableError("Engine not available - " + resourceId); 61 | }; 62 | return ResourceNotAvailableError; 63 | }(ErrorWithMessage_1.ErrorWithMessage)); 64 | exports.ResourceNotAvailableError = ResourceNotAvailableError; 65 | -------------------------------------------------------------------------------- /src/Http/CacheClient.ts: -------------------------------------------------------------------------------- 1 | import {HttpClient} from "./HttpClient"; 2 | import {Response} from "./Response"; 3 | import {Md5} from "ts-md5"; 4 | 5 | /** 6 | * AxiosClient 7 | */ 8 | export class CacheClient implements HttpClient { 9 | 10 | private cache: {[key: string]: Response} = {}; 11 | private httpClient: HttpClient; 12 | private hits: number = 0; 13 | 14 | constructor(httpClient: HttpClient) { 15 | this.httpClient = httpClient; 16 | } 17 | 18 | public flushCache() { 19 | this.cache = {}; 20 | } 21 | 22 | public size() { 23 | return Object.keys(this.cache).length; 24 | } 25 | 26 | public getNumberOfHits() { 27 | return this.hits; 28 | } 29 | 30 | /** 31 | * Get 32 | * 33 | * @param url 34 | * @param method 35 | * @param credentials 36 | * @param parameters 37 | * @param data 38 | * 39 | * @return {Promise} 40 | */ 41 | public async get( 42 | url: string, 43 | method: string, 44 | credentials: any, 45 | parameters: any = {}, 46 | data: any = {}, 47 | ): Promise { 48 | if (method !== 'get') { 49 | return this.httpClient.get( 50 | url, 51 | method, 52 | credentials, 53 | parameters, 54 | data 55 | ); 56 | } 57 | 58 | const cacheUID = Md5.hashStr(JSON.stringify({ 59 | 'u': url, 60 | 'c': credentials, 61 | 'p': parameters, 62 | 'd': data, 63 | })).toString(); 64 | 65 | if (!this.cache[cacheUID]) { 66 | this.cache[cacheUID] = await this.httpClient.get( 67 | url, 68 | method, 69 | credentials, 70 | parameters, 71 | data, 72 | ); 73 | } else { 74 | this.httpClient.abort(url, false); 75 | this.hits++; 76 | } 77 | 78 | return this.cache[cacheUID]; 79 | } 80 | 81 | /** 82 | * Abort current request 83 | * And regenerate the cancellation token 84 | * 85 | * @param url 86 | * @param urlIsFormatted 87 | */ 88 | public abort( 89 | url: string, 90 | urlIsFormatted: boolean, 91 | ) 92 | { 93 | 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /test/Result/ResultAggregations.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {ResultAggregation} from "../../src/Result/ResultAggregation"; 3 | import {ResultAggregations} from "../../src/Result/ResultAggregations"; 4 | 5 | describe('Result/', () => { 6 | describe('Aggregations', () => { 7 | describe('() with errors', () => { 8 | let aggregations = new ResultAggregations(100); 9 | aggregations.addAggregation('name', new ResultAggregation('name', 0, 10, ['1'])); 10 | aggregations.addAggregation('name2', new ResultAggregation('name2', 0, 10, [])); 11 | 12 | it('Should work properly', () => { 13 | expect(Object.keys(aggregations.getAggregations()).length).to.be.equal(2); 14 | expect(aggregations.getTotalElements()).to.be.equal(100); 15 | expect(aggregations.getAggregation('name') instanceof ResultAggregation).to.be.equal(true); 16 | expect(aggregations.getAggregation('name').getName()).to.be.equal('name'); 17 | expect(aggregations.getAggregation('name2') instanceof ResultAggregation).to.be.equal(true); 18 | expect(aggregations.getAggregation('name2').getName()).to.be.equal('name2'); 19 | expect(aggregations.hasNotEmptyAggregation('name')).to.be.equal(true); 20 | expect(aggregations.hasNotEmptyAggregation('name2')).to.be.equal(false); 21 | }); 22 | }); 23 | 24 | describe('.toArray() & createFromArray() default values', () => { 25 | it('Should work properly', () => { 26 | expect(ResultAggregations.createFromArray({}).toArray()).to.be.deep.equal({}); 27 | }); 28 | }); 29 | 30 | describe('.toArray() & createFromArray() default values', () => { 31 | let aggregationsAsArray = { 32 | 'total_elements': 100, 33 | 'aggregations': { 34 | 'name': { 35 | 'name': 'name' 36 | }, 37 | 'name2': { 38 | 'name': 'name2' 39 | } 40 | } 41 | }; 42 | 43 | it('Should work properly', () => { 44 | expect(ResultAggregations.createFromArray(aggregationsAsArray).toArray()).to.be.deep.equal(aggregationsAsArray); 45 | }); 46 | }); 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /lib/Query/Filter.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * filter constants 3 | */ 4 | export declare const FILTER_MUST_ALL = 4; 5 | export declare const FILTER_MUST_ALL_WITH_LEVELS = 5; 6 | export declare const FILTER_AT_LEAST_ONE = 8; 7 | export declare const FILTER_EXCLUDE = 16; 8 | export declare const FILTER_PROMOTE = 32; 9 | export declare const FILTER_TYPE_FIELD = "field"; 10 | export declare const FILTER_TYPE_RANGE = "range"; 11 | export declare const FILTER_TYPE_DATE_RANGE = "date_range"; 12 | export declare const FILTER_TYPE_GEO = "geo"; 13 | export declare const FILTER_TYPE_QUERY = "query"; 14 | /** 15 | * Filter class 16 | */ 17 | export declare class Filter { 18 | private field; 19 | private values; 20 | private applicationType; 21 | private filterType; 22 | private filterTerms; 23 | /** 24 | * Constructor 25 | * 26 | * @param field 27 | * @param values 28 | * @param applicationType 29 | * @param filterType 30 | * @param filterTerms 31 | */ 32 | private constructor(); 33 | /** 34 | * Get field 35 | * 36 | * @returns {string} 37 | */ 38 | getField(): string; 39 | /** 40 | * Get values 41 | * 42 | * @returns {any} 43 | */ 44 | getValues(): any; 45 | /** 46 | * Has value 47 | * 48 | * @param value 49 | * 50 | * @returns {boolean} 51 | */ 52 | hasValue(value: any): boolean; 53 | /** 54 | * getApplicationType 55 | * 56 | * @returns {number} 57 | */ 58 | getApplicationType(): number; 59 | /** 60 | * Get filter type 61 | * 62 | * @return {string} 63 | */ 64 | getFilterType(): string; 65 | /** 66 | * Get filter type 67 | * 68 | * @return {{}} 69 | */ 70 | getFilterTerms(): string[]; 71 | /** 72 | * Create 73 | * 74 | * @param field 75 | * @param values 76 | * @param applicationType 77 | * @param filterType 78 | * @param filterTerms 79 | * 80 | * @return {Filter} 81 | */ 82 | static create(field: string, values: any, applicationType: number, filterType: string, filterTerms?: string[]): Filter; 83 | /** 84 | * To array 85 | * 86 | * @returns {Array} 87 | */ 88 | toArray(): any; 89 | /** 90 | * Create from array 91 | * 92 | * @param array 93 | * 94 | * @returns {Filter} 95 | */ 96 | static createFromArray(array: any): Filter; 97 | } 98 | -------------------------------------------------------------------------------- /lib/Result/ResultAggregation.d.ts: -------------------------------------------------------------------------------- 1 | import { Counter } from "./Counter"; 2 | /** 3 | * ResultAggregation class 4 | */ 5 | export declare class ResultAggregation { 6 | private name; 7 | private counters; 8 | private applicationType; 9 | private totalElements; 10 | private activeElements; 11 | private highestActiveElement; 12 | private metadata; 13 | /** 14 | * @param name 15 | * @param applicationType 16 | * @param totalElements 17 | * @param activeElements 18 | * @param metadata 19 | */ 20 | constructor(name: string, applicationType: number, totalElements: number, activeElements: any[], metadata?: any); 21 | /** 22 | * Add counter 23 | * 24 | * @param name 25 | * @param counter 26 | */ 27 | addCounter(name: string, counter: number): void; 28 | /** 29 | * Get name 30 | * 31 | * @return {string} 32 | */ 33 | getName(): string; 34 | /** 35 | * Get counter 36 | * 37 | * @return {any} 38 | */ 39 | getCounters(): any; 40 | /** 41 | * 42 | */ 43 | getMetadata(): any; 44 | /** 45 | * Return if the aggregation belongs to a filter. 46 | * 47 | * @return {boolean} 48 | */ 49 | isFilter(): boolean; 50 | /** 51 | * Aggregation has levels. 52 | * 53 | * @return {boolean} 54 | */ 55 | hasLevels(): boolean; 56 | /** 57 | * Get counter by name 58 | * 59 | * @param name 60 | * 61 | * @return {null} 62 | */ 63 | getCounter(name: any): Counter; 64 | /** 65 | * Get all elements 66 | * 67 | * @return {{}} 68 | */ 69 | getAllElements(): any; 70 | /** 71 | * Get total elements 72 | * 73 | * @return {number} 74 | */ 75 | getTotalElements(): number; 76 | /** 77 | * Get active elements 78 | * 79 | * @return {any} 80 | */ 81 | getActiveElements(): any; 82 | /** 83 | * Clean results by level and remove all levels higher than the lowest. 84 | */ 85 | cleanCountersByLevel(): void; 86 | /** 87 | * Is empty 88 | * 89 | * @returns {boolean} 90 | */ 91 | isEmpty(): boolean; 92 | /** 93 | * To array 94 | * 95 | * @return {any} 96 | */ 97 | toArray(): any; 98 | /** 99 | * Create from array 100 | * 101 | * @param array 102 | */ 103 | static createFromArray(array: any): ResultAggregation; 104 | } 105 | -------------------------------------------------------------------------------- /lib/Config/ImmutableConfig.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | var Synonym_1 = require("./Synonym"); 4 | /** 5 | * Result class 6 | */ 7 | var ImmutableConfig = /** @class */ (function () { 8 | /** 9 | * Constructor 10 | * 11 | * @param language 12 | * @param storeSearchableMetadata 13 | */ 14 | function ImmutableConfig(language, storeSearchableMetadata) { 15 | if (language === void 0) { language = null; } 16 | if (storeSearchableMetadata === void 0) { storeSearchableMetadata = true; } 17 | this.synonyms = []; 18 | this.language = language; 19 | this.storeSearchableMetadata = storeSearchableMetadata; 20 | } 21 | /** 22 | * Get language 23 | * 24 | * @return {string} 25 | */ 26 | ImmutableConfig.prototype.getLanguage = function () { 27 | return this.language; 28 | }; 29 | /** 30 | * Should searchable metadata be stored 31 | * 32 | * @return {boolean} 33 | */ 34 | ImmutableConfig.prototype.shouldSearchableMetadataBeStored = function () { 35 | return this.storeSearchableMetadata; 36 | }; 37 | /** 38 | * Add synonym 39 | * 40 | * @param synonym 41 | */ 42 | ImmutableConfig.prototype.addSynonym = function (synonym) { 43 | this.synonyms.push(synonym); 44 | }; 45 | /** 46 | * Get synonyms 47 | * 48 | * @return {Synonym[]} 49 | */ 50 | ImmutableConfig.prototype.getSynonyms = function () { 51 | return this.synonyms; 52 | }; 53 | /** 54 | * to array 55 | */ 56 | ImmutableConfig.prototype.toArray = function () { 57 | return { 58 | language: this.language, 59 | store_searchable_metadata: this.storeSearchableMetadata, 60 | synonyms: this.synonyms.map(function (synonym) { return synonym.toArray(); }) 61 | }; 62 | }; 63 | /** 64 | * Create from array 65 | */ 66 | ImmutableConfig.createFromArray = function (array) { 67 | var immutableConfig = new ImmutableConfig(array.language ? array.language : null, typeof array.store_searchable_metadata == "boolean" 68 | ? array.store_searchable_metadata 69 | : true); 70 | if (array.synonyms instanceof Array && 71 | array.synonyms.length > 0) { 72 | immutableConfig.synonyms = array.synonyms.map(function (synonym) { return Synonym_1.Synonym.createFromArray(synonym); }); 73 | } 74 | return immutableConfig; 75 | }; 76 | return ImmutableConfig; 77 | }()); 78 | exports.ImmutableConfig = ImmutableConfig; 79 | -------------------------------------------------------------------------------- /test/Model/Index.test.ts: -------------------------------------------------------------------------------- 1 | import {IndexUUID} from "../../src/Model/IndexUUID"; 2 | import {Index} from "../../src/Model/Index"; 3 | import { expect } from 'chai'; 4 | import {AppUUID} from "../../src/Model/AppUUID"; 5 | 6 | describe('Model/', () => { 7 | describe('Index', () => { 8 | describe('createFromArray()', () => { 9 | let index = Index.createFromArray({ 10 | 'uuid': { 11 | 'id': 'testId' 12 | }, 13 | 'app_id': { 14 | 'id': 'testAppId' 15 | }, 16 | 'is_ok': true, 17 | 'doc_count': 10, 18 | 'size': '1kb' 19 | }); 20 | 21 | it('Should work properly', () => { 22 | expect(index.getUUID().getId()).to.be.equal('testId'); 23 | expect(index.getAppUUID().getId()).to.be.equal('testAppId'); 24 | expect(index.isOk()).to.be.true; 25 | expect(index.getDocCount()).to.be.equal(10); 26 | expect(index.getSize()).to.be.equal('1kb'); 27 | }); 28 | }); 29 | describe('()', () => { 30 | let index = new Index( 31 | IndexUUID.createById('testId'), 32 | AppUUID.createById('testAppId'), 33 | true, 34 | 10, 35 | '1kb' 36 | ); 37 | 38 | it('Should work properly', () => { 39 | expect(index.getUUID().getId()).to.be.equal('testId'); 40 | expect(index.getAppUUID().getId()).to.be.equal('testAppId'); 41 | expect(index.isOk()).to.be.true; 42 | expect(index.getDocCount()).to.be.equal(10); 43 | expect(index.getSize()).to.be.equal('1kb'); 44 | let indexAsArray = index.toArray(); 45 | expect(indexAsArray.uuid.id).to.be.equal('testId'); 46 | expect(indexAsArray.app_id.id).to.be.equal('testAppId'); 47 | expect(indexAsArray.is_ok).to.be.true; 48 | expect(indexAsArray.doc_count).to.be.equal(10); 49 | expect(indexAsArray.size).to.be.equal('1kb'); 50 | }); 51 | }); 52 | describe('defaults', () => { 53 | let index = new Index( 54 | IndexUUID.createById('testId'), 55 | AppUUID.createById('testAppId') 56 | ); 57 | 58 | it('Should work properly', () => { 59 | expect(index.isOk()).to.be.false; 60 | expect(index.getDocCount()).to.be.equal(0); 61 | expect(index.getSize()).to.be.equal('0kb'); 62 | }); 63 | }); 64 | }); 65 | }); 66 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | var tslib_1 = require("tslib"); 4 | var Apisearch_1 = require("./Apisearch"); 5 | exports["default"] = Apisearch_1["default"]; 6 | tslib_1.__exportStar(require("./Config/Config"), exports); 7 | tslib_1.__exportStar(require("./Config/Synonym"), exports); 8 | tslib_1.__exportStar(require("./Error/ConnectionError"), exports); 9 | tslib_1.__exportStar(require("./Error/ErrorWithMessage"), exports); 10 | tslib_1.__exportStar(require("./Error/EventError"), exports); 11 | tslib_1.__exportStar(require("./Error/ForbiddenError"), exports); 12 | tslib_1.__exportStar(require("./Error/InvalidFormatError"), exports); 13 | tslib_1.__exportStar(require("./Error/InvalidTokenError"), exports); 14 | tslib_1.__exportStar(require("./Error/ResourceExistsError"), exports); 15 | tslib_1.__exportStar(require("./Error/ResourceNotAvailableError"), exports); 16 | tslib_1.__exportStar(require("./Error/UnsupportedContentTypeError"), exports); 17 | tslib_1.__exportStar(require("./Geo/LocationRange"), exports); 18 | tslib_1.__exportStar(require("./Http/AxiosClient"), exports); 19 | tslib_1.__exportStar(require("./Http/Client"), exports); 20 | tslib_1.__exportStar(require("./Http/HttpClient"), exports); 21 | tslib_1.__exportStar(require("./Http/Response"), exports); 22 | tslib_1.__exportStar(require("./Http/CacheClient"), exports); 23 | tslib_1.__exportStar(require("./Model/Changes"), exports); 24 | tslib_1.__exportStar(require("./Model/Coordinate"), exports); 25 | tslib_1.__exportStar(require("./Model/Item"), exports); 26 | tslib_1.__exportStar(require("./Model/ItemUUID"), exports); 27 | tslib_1.__exportStar(require("./Model/Metadata"), exports); 28 | tslib_1.__exportStar(require("./Model/User"), exports); 29 | tslib_1.__exportStar(require("./Query/Aggregation"), exports); 30 | tslib_1.__exportStar(require("./Query/Filter"), exports); 31 | tslib_1.__exportStar(require("./Query/Query"), exports); 32 | tslib_1.__exportStar(require("./Query/Range"), exports); 33 | tslib_1.__exportStar(require("./Query/ScoreStrategies"), exports); 34 | tslib_1.__exportStar(require("./Query/ScoreStrategy"), exports); 35 | tslib_1.__exportStar(require("./Query/SortBy"), exports); 36 | tslib_1.__exportStar(require("./Repository/HttpRepository"), exports); 37 | tslib_1.__exportStar(require("./Repository/Repository"), exports); 38 | tslib_1.__exportStar(require("./Result/ResultAggregation"), exports); 39 | tslib_1.__exportStar(require("./Result/ResultAggregations"), exports); 40 | tslib_1.__exportStar(require("./Result/Counter"), exports); 41 | tslib_1.__exportStar(require("./Result/Result"), exports); 42 | tslib_1.__exportStar(require("./Transformer/ReadTransformer"), exports); 43 | tslib_1.__exportStar(require("./Transformer/Transformer"), exports); 44 | tslib_1.__exportStar(require("./Transformer/WriteTransformer"), exports); 45 | -------------------------------------------------------------------------------- /lib/Query/Aggregation.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Aggregation constants 3 | */ 4 | export declare const AGGREGATION_SORT_BY_COUNT_ASC: string[]; 5 | export declare const AGGREGATION_SORT_BY_COUNT_DESC: string[]; 6 | export declare const AGGREGATION_SORT_BY_NAME_ASC: string[]; 7 | export declare const AGGREGATION_SORT_BY_NAME_DESC: string[]; 8 | export declare const AGGREGATION_NO_LIMIT = 0; 9 | /** 10 | * Aggregation class 11 | */ 12 | export declare class Aggregation { 13 | private name; 14 | private field; 15 | private applicationType; 16 | private filterType; 17 | private subgroup; 18 | private sort; 19 | private limit; 20 | private promoted; 21 | /** 22 | * Construct 23 | * 24 | * @param name 25 | * @param field 26 | * @param applicationType 27 | * @param filterType 28 | * @param subgroup 29 | * @param sort 30 | * @param limit 31 | * @param promoted 32 | */ 33 | private constructor(); 34 | /** 35 | * Get name 36 | * 37 | * @returns {string} 38 | */ 39 | getName(): string; 40 | /** 41 | * Get field 42 | * 43 | * @returns {string} 44 | */ 45 | getField(): string; 46 | /** 47 | * getApplicationType 48 | * 49 | * @returns {number} 50 | */ 51 | getApplicationType(): number; 52 | /** 53 | * Get filter type 54 | * 55 | * @return {string} 56 | */ 57 | getFilterType(): string; 58 | /** 59 | * Get subgroup 60 | * 61 | * @return {[]} 62 | */ 63 | getSubgroup(): string[]; 64 | /** 65 | * Get sort 66 | * 67 | * @return {[]} 68 | */ 69 | getSort(): string[]; 70 | /** 71 | * Get limit 72 | * 73 | * @return {number} 74 | */ 75 | getLimit(): number; 76 | /** 77 | * Get promoted 78 | * 79 | * @return {[]} 80 | */ 81 | getPromoted(): string[]; 82 | /** 83 | * Create 84 | * 85 | * @param name 86 | * @param field 87 | * @param applicationType 88 | * @param filterType 89 | * @param subgroup 90 | * @param sort 91 | * @param limit 92 | * @param promoted 93 | * 94 | * @returns {Aggregation} 95 | */ 96 | static create(name: string, field: string, applicationType: number, filterType: string, subgroup?: string[], sort?: string[], limit?: number, promoted?: string[]): Aggregation; 97 | /** 98 | * To array 99 | * 100 | * @returns {Array} 101 | */ 102 | toArray(): any; 103 | /** 104 | * Create from array 105 | * 106 | * @param array 107 | * 108 | * @returns {Aggregation} 109 | */ 110 | static createFromArray(array: any): Aggregation; 111 | } 112 | -------------------------------------------------------------------------------- /lib/Http/CacheClient.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.CacheClient = void 0; 4 | var tslib_1 = require("tslib"); 5 | var ts_md5_1 = require("ts-md5"); 6 | /** 7 | * AxiosClient 8 | */ 9 | var CacheClient = /** @class */ (function () { 10 | function CacheClient(httpClient) { 11 | this.cache = {}; 12 | this.hits = 0; 13 | this.httpClient = httpClient; 14 | } 15 | CacheClient.prototype.flushCache = function () { 16 | this.cache = {}; 17 | }; 18 | CacheClient.prototype.size = function () { 19 | return Object.keys(this.cache).length; 20 | }; 21 | CacheClient.prototype.getNumberOfHits = function () { 22 | return this.hits; 23 | }; 24 | /** 25 | * Get 26 | * 27 | * @param url 28 | * @param method 29 | * @param credentials 30 | * @param parameters 31 | * @param data 32 | * 33 | * @return {Promise} 34 | */ 35 | CacheClient.prototype.get = function (url, method, credentials, parameters, data) { 36 | if (parameters === void 0) { parameters = {}; } 37 | if (data === void 0) { data = {}; } 38 | return tslib_1.__awaiter(this, void 0, void 0, function () { 39 | var cacheUID, _a, _b; 40 | return tslib_1.__generator(this, function (_c) { 41 | switch (_c.label) { 42 | case 0: 43 | if (method !== 'get') { 44 | return [2 /*return*/, this.httpClient.get(url, method, credentials, parameters, data)]; 45 | } 46 | cacheUID = ts_md5_1.Md5.hashStr(JSON.stringify({ 47 | 'u': url, 48 | 'c': credentials, 49 | 'p': parameters, 50 | 'd': data 51 | })).toString(); 52 | if (!!this.cache[cacheUID]) return [3 /*break*/, 2]; 53 | _a = this.cache; 54 | _b = cacheUID; 55 | return [4 /*yield*/, this.httpClient.get(url, method, credentials, parameters, data)]; 56 | case 1: 57 | _a[_b] = _c.sent(); 58 | return [3 /*break*/, 3]; 59 | case 2: 60 | this.httpClient.abort(url, false); 61 | this.hits++; 62 | _c.label = 3; 63 | case 3: return [2 /*return*/, this.cache[cacheUID]]; 64 | } 65 | }); 66 | }); 67 | }; 68 | /** 69 | * Abort current request 70 | * And regenerate the cancellation token 71 | * 72 | * @param url 73 | * @param urlIsFormatted 74 | */ 75 | CacheClient.prototype.abort = function (url, urlIsFormatted) { 76 | }; 77 | return CacheClient; 78 | }()); 79 | exports.CacheClient = CacheClient; 80 | -------------------------------------------------------------------------------- /lib/Apisearch.d.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from "./Http/HttpClient"; 2 | import { Coordinate } from "./Model/Coordinate"; 3 | import { ItemUUID } from "./Model/ItemUUID"; 4 | import { Query } from "./Query/Query"; 5 | import { SortBy } from "./Query/SortBy"; 6 | import { HttpRepository } from "./Repository/HttpRepository"; 7 | import { Result } from "./Result/Result"; 8 | /** 9 | * Apisearch class 10 | */ 11 | export default class Apisearch { 12 | /** 13 | * Constructor 14 | * 15 | * @param config 16 | * 17 | * @return {HttpRepository} 18 | */ 19 | static createRepository(config: { 20 | app_id: string; 21 | index_id: string; 22 | token: string; 23 | options: { 24 | endpoint: string; 25 | api_version?: string; 26 | timeout?: number; 27 | override_queries?: boolean; 28 | http_client?: HttpClient; 29 | use_cache?: boolean; 30 | }; 31 | }): HttpRepository; 32 | /** 33 | * Ensure the Repository configuration is valid 34 | * 35 | * @param config 36 | */ 37 | static ensureRepositoryConfigIsValid(config: any): void; 38 | /** 39 | * Ensure the value is not undefined 40 | * 41 | * @param param 42 | * @param name 43 | */ 44 | static ensureIsDefined(param: any, name: string): void; 45 | /** 46 | * Created located 47 | * 48 | * @param coordinate 49 | * @param queryText 50 | * @param page 51 | * @param size 52 | * 53 | * @returns {Query} 54 | */ 55 | static createQueryLocated(coordinate: Coordinate, queryText: string, page?: number, size?: number): Query; 56 | /** 57 | * Create 58 | * 59 | * @param queryText 60 | * @param page 61 | * @param size 62 | * 63 | * @returns {Query} 64 | */ 65 | static createQuery(queryText: string, page?: number, size?: number): Query; 66 | /** 67 | * Create match all 68 | * 69 | * @return {Query} 70 | */ 71 | static createQueryMatchAll(): Query; 72 | /** 73 | * Create by UUID 74 | * 75 | * @param uuid 76 | * 77 | * @return {Query} 78 | */ 79 | static createQueryByUUID(uuid: ItemUUID): Query; 80 | /** 81 | * Create by UUIDs 82 | * 83 | * @param uuids 84 | * 85 | * @return {Query} 86 | */ 87 | static createQueryByUUIDs(...uuids: ItemUUID[]): Query; 88 | /** 89 | * Create empty result 90 | * 91 | * @return {Result} 92 | */ 93 | static createEmptyResult(): Result; 94 | /** 95 | * Create empty sortby 96 | * 97 | * @return {SortBy} 98 | */ 99 | static createEmptySortBy(): SortBy; 100 | /** 101 | * Create empty sortby 102 | * 103 | * @return {SortBy} 104 | */ 105 | static createEmptyScoreStrategy(): SortBy; 106 | } 107 | -------------------------------------------------------------------------------- /test/Geo/CoordinateAndDistance.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Coordinate} from "../../src/Model/Coordinate"; 3 | import {LocationRange} from "../../src/Geo/LocationRange"; 4 | import {CoordinateAndDistance} from "../../src/Geo/LocationRange"; 5 | 6 | describe('Geo/', () => { 7 | describe('CoordinateAndDistance', () => { 8 | describe('.toFilterObject()', () => { 9 | let coordinateAndDistance = new CoordinateAndDistance( 10 | new Coordinate(0.0, 1.1), 11 | '10km' 12 | ); 13 | 14 | it('Should work properly', () => { 15 | expect(coordinateAndDistance.toFilterObject()).to.be.deep.equal({ 16 | 'coordinate': { 17 | 'lat': 0.0, 18 | 'lon': 1.1 19 | }, 20 | 'distance': '10km' 21 | }); 22 | }); 23 | }); 24 | 25 | describe('.fromFilterObject()', () => { 26 | let coordinateAndDistanceAsArray = { 27 | 'coordinate': { 28 | 'lat': 2.1, 29 | 'lon': 1.1 30 | }, 31 | 'distance': '10km' 32 | }; 33 | let coordinateAndDistance = CoordinateAndDistance.fromFilterObject(coordinateAndDistanceAsArray); 34 | 35 | it('Should work properly', () => { 36 | expect(coordinateAndDistance.toFilterObject()).to.be.deep.equal(coordinateAndDistanceAsArray); 37 | }); 38 | }); 39 | 40 | describe('.toArray()', () => { 41 | let coordinateAndDistance = new CoordinateAndDistance( 42 | new Coordinate(0.0, 1.1), 43 | '10km' 44 | ); 45 | 46 | it('Should work properly', () => { 47 | expect(coordinateAndDistance.toArray()).to.be.deep.equal({ 48 | 'type': 'CoordinateAndDistance', 49 | 'data': { 50 | 'coordinate': { 51 | 'lat': 0.0, 52 | 'lon': 1.1 53 | }, 54 | 'distance': '10km' 55 | } 56 | }); 57 | }); 58 | }); 59 | 60 | describe('.createFromArray', () => { 61 | let coordinateAndDistanceAsArray = { 62 | 'type': 'CoordinateAndDistance', 63 | 'data': { 64 | 'coordinate': { 65 | 'lat': 0.0, 66 | 'lon': 1.1 67 | }, 68 | 'distance': '10km' 69 | } 70 | }; 71 | let coordinateAndDistance = LocationRange.createFromArray(coordinateAndDistanceAsArray); 72 | 73 | it('Should work properly', () => { 74 | expect(coordinateAndDistance.toArray()).to.be.deep.equal(coordinateAndDistanceAsArray); 75 | }); 76 | }); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /lib/Model/Index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Index = void 0; 4 | var InvalidFormatError_1 = require("../Error/InvalidFormatError"); 5 | var IndexUUID_1 = require("./IndexUUID"); 6 | var AppUUID_1 = require("./AppUUID"); 7 | /** 8 | * Index class 9 | */ 10 | var Index = /** @class */ (function () { 11 | /** 12 | * Constructor 13 | * 14 | * @param uuid 15 | * @param appUUID 16 | * @param isOK 17 | * @param docCount 18 | * @param size 19 | */ 20 | function Index(uuid, appUUID, isOK, docCount, size) { 21 | if (isOK === void 0) { isOK = false; } 22 | if (docCount === void 0) { docCount = 0; } 23 | if (size === void 0) { size = '0kb'; } 24 | this.uuid = uuid; 25 | this.appUUID = appUUID; 26 | this.isOK = isOK; 27 | this.docCount = docCount; 28 | this.size = size; 29 | } 30 | /** 31 | * Get uuid 32 | * 33 | * @return {IndexUUID} 34 | */ 35 | Index.prototype.getUUID = function () { 36 | return this.uuid; 37 | }; 38 | /** 39 | * Get app id 40 | * 41 | * @return {AppUUID} 42 | */ 43 | Index.prototype.getAppUUID = function () { 44 | return this.appUUID; 45 | }; 46 | /** 47 | * Index is OK 48 | * 49 | * @return {boolean} 50 | */ 51 | Index.prototype.isOk = function () { 52 | return this.isOK; 53 | }; 54 | /** 55 | * Get doc count 56 | * 57 | * @return {number} 58 | */ 59 | Index.prototype.getDocCount = function () { 60 | return this.docCount; 61 | }; 62 | /** 63 | * get size 64 | * 65 | * @return {string} 66 | */ 67 | Index.prototype.getSize = function () { 68 | return this.size; 69 | }; 70 | /** 71 | * To array 72 | * 73 | * @returns {{id: string, attributes: {}}} 74 | */ 75 | Index.prototype.toArray = function () { 76 | return { 77 | uuid: this.uuid.toArray(), 78 | app_id: this.appUUID.toArray(), 79 | is_ok: this.isOK, 80 | doc_count: this.docCount, 81 | size: this.size 82 | }; 83 | }; 84 | /** 85 | * Create from array 86 | * 87 | * @param array 88 | * 89 | * @return User 90 | */ 91 | Index.createFromArray = function (array) { 92 | if (typeof array.uuid == "undefined" || 93 | typeof array.app_id == "undefined") { 94 | throw InvalidFormatError_1.InvalidFormatError.indexFormatNotValid(); 95 | } 96 | return new Index(IndexUUID_1.IndexUUID.createFromArray(array.uuid), AppUUID_1.AppUUID.createFromArray(array.app_id), (typeof array.is_ok == "undefined" ? false : array.is_ok), (typeof array.doc_count == "undefined" ? 0 : array.doc_count), (typeof array.size == "undefined" ? '0kb' : array.size)); 97 | }; 98 | return Index; 99 | }()); 100 | exports.Index = Index; 101 | -------------------------------------------------------------------------------- /src/Query/ScoreStrategies.ts: -------------------------------------------------------------------------------- 1 | import {ScoreStrategy} from "./ScoreStrategy"; 2 | /** 3 | * ScoreStrategies constants 4 | */ 5 | export const MULTIPLY = 'multiply'; 6 | export const SUM = 'sum'; 7 | export const AVG = 'avg'; 8 | export const MAX = 'max'; 9 | export const MIN = 'min'; 10 | 11 | /** 12 | * ScoreStrategies 13 | */ 14 | export class ScoreStrategies { 15 | 16 | private scoreStrategies: ScoreStrategy[] = []; 17 | private scoreMode : string; 18 | 19 | /** 20 | * Create empty 21 | * 22 | * @param scoreMode 23 | * 24 | * @return {ScoreStrategies} 25 | */ 26 | public static createEmpty(scoreMode: string = SUM) : ScoreStrategies { 27 | let scoreStrategies = new ScoreStrategies; 28 | scoreStrategies.scoreMode = scoreMode; 29 | 30 | return scoreStrategies; 31 | } 32 | 33 | /** 34 | * Add score strategy 35 | * 36 | * @param scoreStrategy 37 | * 38 | * @return {ScoreStrategies} 39 | */ 40 | public addScoreStrategy(scoreStrategy: ScoreStrategy) : ScoreStrategies { 41 | this.scoreStrategies.push(scoreStrategy); 42 | 43 | return this; 44 | } 45 | 46 | /** 47 | * Get score strategies 48 | * 49 | * @return {ScoreStrategy[]} 50 | */ 51 | public getScoreStrategies() : ScoreStrategy[] { 52 | return this.scoreStrategies; 53 | } 54 | 55 | /** 56 | * Get score mode 57 | * 58 | * @return {string} 59 | */ 60 | public getScoreMode() : string { 61 | return this.scoreMode; 62 | } 63 | 64 | /** 65 | * To array 66 | * 67 | * @return {{ 68 | * score_mode: string, 69 | * score_strategies: any 70 | * }} 71 | */ 72 | public toArray(): { 73 | score_mode: string, 74 | score_strategies: any 75 | } { 76 | let scoreStrategiesAsArray = []; 77 | for (const i in this.scoreStrategies) { 78 | scoreStrategiesAsArray.push(this.scoreStrategies[i].toArray()); 79 | } 80 | 81 | return { 82 | score_mode: this.scoreMode, 83 | score_strategies: scoreStrategiesAsArray 84 | }; 85 | } 86 | 87 | /** 88 | * Create from array 89 | * 90 | * @param array 91 | * 92 | * @return {ScoreStrategies} 93 | */ 94 | public static createFromArray(array: any): ScoreStrategies { 95 | array = JSON.parse(JSON.stringify(array)); 96 | const scoreStrategies = (typeof array.score_mode != "undefined") 97 | ? ScoreStrategies.createEmpty(array.score_mode) 98 | : ScoreStrategies.createEmpty(); 99 | 100 | scoreStrategies.scoreStrategies = []; 101 | for (const i in array.score_strategies) { 102 | scoreStrategies 103 | .scoreStrategies 104 | .push(ScoreStrategy.createFromArray(array.score_strategies[i])); 105 | } 106 | 107 | return scoreStrategies; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /test/Geo/Square.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Square} from "../../src/Geo/LocationRange"; 3 | import {LocationRange} from "../../src/Geo/LocationRange"; 4 | import {Coordinate} from "../../src/Model/Coordinate"; 5 | 6 | describe('Geo/', () => { 7 | describe('Square', () => { 8 | describe('.toFilterObject()', () => { 9 | let square = new Square( 10 | new Coordinate(0.0, 1.1), 11 | new Coordinate(2.0, 1.1) 12 | ); 13 | 14 | it('Should work properly', () => { 15 | expect(square.toFilterObject()).to.be.deep.equal({ 16 | 'top_left': { 17 | 'lat': 0.0, 18 | 'lon': 1.1 19 | }, 20 | 'bottom_right': { 21 | 'lat': 2.0, 22 | 'lon': 1.1 23 | } 24 | }); 25 | }); 26 | }); 27 | 28 | describe('.fromFilterObject()', () => { 29 | let squareAsArray = { 30 | 'top_left': { 31 | 'lat': 0.0, 32 | 'lon': 1.1 33 | }, 34 | 'bottom_right': { 35 | 'lat': 2.0, 36 | 'lon': 1.1 37 | } 38 | }; 39 | let square = Square.fromFilterObject(squareAsArray); 40 | 41 | it('Should work properly', () => { 42 | expect(square.toFilterObject()).to.be.deep.equal(squareAsArray); 43 | }); 44 | }); 45 | 46 | describe('.toArray()', () => { 47 | let square = new Square( 48 | new Coordinate(0.0, 1.1), 49 | new Coordinate(2.0, 1.1) 50 | ); 51 | 52 | it('Should work properly', () => { 53 | expect(square.toArray()).to.be.deep.equal({ 54 | 'type': 'Square', 55 | 'data': { 56 | 'top_left': { 57 | 'lat': 0.0, 58 | 'lon': 1.1 59 | }, 60 | 'bottom_right': { 61 | 'lat': 2.0, 62 | 'lon': 1.1 63 | } 64 | } 65 | }); 66 | }); 67 | }); 68 | 69 | describe('.createFromArray', () => { 70 | let squareAsArray = { 71 | 'type': 'Square', 72 | 'data': { 73 | 'top_left': { 74 | 'lat': 0.0, 75 | 'lon': 1.1 76 | }, 77 | 'bottom_right': { 78 | 'lat': 2.0, 79 | 'lon': 1.1 80 | } 81 | } 82 | }; 83 | let square = LocationRange.createFromArray(squareAsArray); 84 | 85 | it('Should work properly', () => { 86 | expect(square.toArray()).to.be.deep.equal(squareAsArray); 87 | }); 88 | }); 89 | }); 90 | }); 91 | -------------------------------------------------------------------------------- /test/Config/Config.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Synonym} from "../../src/Config/Synonym"; 3 | import { 4 | Config, DEFAULT_SHARDS, 5 | DEFAULT_REPLICAS 6 | } from "../../src/Config/Config"; 7 | 8 | describe('Config/', () => { 9 | describe('Config', () => { 10 | 11 | describe('.create()', () => { 12 | let config = new Config('es', true); 13 | it('should work properly', () => { 14 | expect(config.getLanguage()).to.be.equal('es'); 15 | expect(config.shouldSearchableMetadataBeStored()).to.be.true; 16 | }) 17 | }); 18 | 19 | describe('.create() with null language', () => { 20 | let config = new Config(null, true); 21 | it('should work properly', () => { 22 | expect(config.getLanguage()).to.be.null; 23 | expect(config.shouldSearchableMetadataBeStored()).to.be.true; 24 | }) 25 | }); 26 | 27 | describe('.create() with searchable metadata store false', () => { 28 | let config = new Config('es', false); 29 | it('should work properly', () => { 30 | expect(config.shouldSearchableMetadataBeStored()).to.be.false; 31 | config = Config.createFromArray({ 32 | 'store_searchable_metadata': false 33 | }); 34 | expect(config.shouldSearchableMetadataBeStored()).to.be.false; 35 | }) 36 | }); 37 | 38 | describe('.addSynonym()', () => { 39 | let config = new Config(null, true); 40 | config.addSynonym(Synonym.createbyWords(['a', 'b'])); 41 | config.addSynonym(Synonym.createbyWords(['b', 'c'])); 42 | 43 | it('should work properly', () => { 44 | expect(config.getSynonyms()).to.be.deep.equal([ 45 | Synonym.createbyWords(['a', 'b']), 46 | Synonym.createbyWords(['b', 'c']) 47 | ]); 48 | }) 49 | }); 50 | 51 | describe('.testDefaultsValues()', () => { 52 | let config = Config.createFromArray({}); 53 | it('should work properly', () => { 54 | expect(config.getLanguage()).to.be.null; 55 | expect(config.shouldSearchableMetadataBeStored()).to.be.true; 56 | expect(config.getSynonyms()).to.be.deep.equal([]); 57 | expect(config.getShards()).to.be.equal(DEFAULT_SHARDS); 58 | expect(config.getReplicas()).to.be.equal(DEFAULT_REPLICAS); 59 | }) 60 | }); 61 | 62 | describe('http transport', () => { 63 | let configAsArray = { 64 | 'language': 'es', 65 | 'store_searchable_metadata': false, 66 | 'synonyms': [ 67 | {'words': ['a', 'b']}, 68 | {'words': ['b', 'c']}, 69 | ], 70 | 'shards': 5, 71 | 'replicas': 8 72 | }; 73 | 74 | it('should work properly', () => { 75 | expect(Config.createFromArray(configAsArray).toArray()).to.be.deep.equal(configAsArray); 76 | }) 77 | }); 78 | }); 79 | }); 80 | -------------------------------------------------------------------------------- /lib/Query/ScoreStrategies.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ScoreStrategies = exports.MIN = exports.MAX = exports.AVG = exports.SUM = exports.MULTIPLY = void 0; 4 | var ScoreStrategy_1 = require("./ScoreStrategy"); 5 | /** 6 | * ScoreStrategies constants 7 | */ 8 | exports.MULTIPLY = 'multiply'; 9 | exports.SUM = 'sum'; 10 | exports.AVG = 'avg'; 11 | exports.MAX = 'max'; 12 | exports.MIN = 'min'; 13 | /** 14 | * ScoreStrategies 15 | */ 16 | var ScoreStrategies = /** @class */ (function () { 17 | function ScoreStrategies() { 18 | this.scoreStrategies = []; 19 | } 20 | /** 21 | * Create empty 22 | * 23 | * @param scoreMode 24 | * 25 | * @return {ScoreStrategies} 26 | */ 27 | ScoreStrategies.createEmpty = function (scoreMode) { 28 | if (scoreMode === void 0) { scoreMode = exports.SUM; } 29 | var scoreStrategies = new ScoreStrategies; 30 | scoreStrategies.scoreMode = scoreMode; 31 | return scoreStrategies; 32 | }; 33 | /** 34 | * Add score strategy 35 | * 36 | * @param scoreStrategy 37 | * 38 | * @return {ScoreStrategies} 39 | */ 40 | ScoreStrategies.prototype.addScoreStrategy = function (scoreStrategy) { 41 | this.scoreStrategies.push(scoreStrategy); 42 | return this; 43 | }; 44 | /** 45 | * Get score strategies 46 | * 47 | * @return {ScoreStrategy[]} 48 | */ 49 | ScoreStrategies.prototype.getScoreStrategies = function () { 50 | return this.scoreStrategies; 51 | }; 52 | /** 53 | * Get score mode 54 | * 55 | * @return {string} 56 | */ 57 | ScoreStrategies.prototype.getScoreMode = function () { 58 | return this.scoreMode; 59 | }; 60 | /** 61 | * To array 62 | * 63 | * @return {{ 64 | * score_mode: string, 65 | * score_strategies: any 66 | * }} 67 | */ 68 | ScoreStrategies.prototype.toArray = function () { 69 | var scoreStrategiesAsArray = []; 70 | for (var i in this.scoreStrategies) { 71 | scoreStrategiesAsArray.push(this.scoreStrategies[i].toArray()); 72 | } 73 | return { 74 | score_mode: this.scoreMode, 75 | score_strategies: scoreStrategiesAsArray 76 | }; 77 | }; 78 | /** 79 | * Create from array 80 | * 81 | * @param array 82 | * 83 | * @return {ScoreStrategies} 84 | */ 85 | ScoreStrategies.createFromArray = function (array) { 86 | array = JSON.parse(JSON.stringify(array)); 87 | var scoreStrategies = (typeof array.score_mode != "undefined") 88 | ? ScoreStrategies.createEmpty(array.score_mode) 89 | : ScoreStrategies.createEmpty(); 90 | scoreStrategies.scoreStrategies = []; 91 | for (var i in array.score_strategies) { 92 | scoreStrategies 93 | .scoreStrategies 94 | .push(ScoreStrategy_1.ScoreStrategy.createFromArray(array.score_strategies[i])); 95 | } 96 | return scoreStrategies; 97 | }; 98 | return ScoreStrategies; 99 | }()); 100 | exports.ScoreStrategies = ScoreStrategies; 101 | -------------------------------------------------------------------------------- /lib/Error/InvalidFormatError.d.ts: -------------------------------------------------------------------------------- 1 | import { ErrorWithMessage } from "./ErrorWithMessage"; 2 | /** 3 | * Class InvalidFormatError 4 | */ 5 | export declare class InvalidFormatError extends ErrorWithMessage { 6 | /** 7 | * Get transportable http error 8 | * 9 | * @return {number} 10 | */ 11 | static getTransportableHTTPError(): number; 12 | /** 13 | * Item representation not valid 14 | * 15 | * @return {InvalidFormatError} 16 | */ 17 | static itemRepresentationNotValid(): InvalidFormatError; 18 | /** 19 | * Item UUID representation not valid 20 | * 21 | * @return {InvalidFormatError} 22 | */ 23 | static itemUUIDRepresentationNotValid(): InvalidFormatError; 24 | /** 25 | * Create Composed UUID bad format. 26 | * 27 | * @return {InvalidFormatError} 28 | */ 29 | static composedItemUUIDNotValid(): InvalidFormatError; 30 | /** 31 | * Create Query sorted by distance without coordinate. 32 | * 33 | * @return {InvalidFormatError} 34 | */ 35 | static querySortedByDistanceWithoutCoordinate(): InvalidFormatError; 36 | /** 37 | * Query representation not valid 38 | * 39 | * @return {InvalidFormatError} 40 | */ 41 | static queryFormatNotValid(): InvalidFormatError; 42 | /** 43 | * Coordinate representation not valid 44 | * 45 | * @return {InvalidFormatError} 46 | */ 47 | static coordinateFormatNotValid(): InvalidFormatError; 48 | /** 49 | * Config representation not valid 50 | * 51 | * @return {InvalidFormatError} 52 | */ 53 | static configFormatNotValid(): InvalidFormatError; 54 | /** 55 | * Token representation not valid 56 | * 57 | * @return {InvalidFormatError} 58 | */ 59 | static tokenFormatNotValid(): InvalidFormatError; 60 | /** 61 | * Index format not valid. 62 | * 63 | * @return {InvalidFormatError} 64 | */ 65 | static indexFormatNotValid(): InvalidFormatError; 66 | /** 67 | * IndexUUI format not valid. 68 | * 69 | * @return {InvalidFormatError} 70 | */ 71 | static indexUUIDFormatNotValid(): InvalidFormatError; 72 | /** 73 | * App format not valid. 74 | * 75 | * @return {InvalidFormatError} 76 | */ 77 | static appUUIDFormatNotValid(): InvalidFormatError; 78 | /** 79 | * Campaign representation not valid 80 | * 81 | * @return {InvalidFormatError} 82 | */ 83 | static campaignFormatNotValid(): InvalidFormatError; 84 | /** 85 | * Changes representation not valid 86 | * 87 | * @return {InvalidFormatError} 88 | */ 89 | static changesFormatNotValid(): InvalidFormatError; 90 | /** 91 | * Boost clause representation not valid 92 | * 93 | * @return {InvalidFormatError} 94 | */ 95 | static boostClauseFormatNotValid(): InvalidFormatError; 96 | /** 97 | * token uuid representation not valid 98 | * 99 | * @return {InvalidFormatError} 100 | */ 101 | static tokenUUIDFormatNotValid(): InvalidFormatError; 102 | /** 103 | * User representation not valid 104 | * 105 | * @return {InvalidFormatError} 106 | */ 107 | static userFormatNotValid(): InvalidFormatError; 108 | } 109 | -------------------------------------------------------------------------------- /src/Model/Index.ts: -------------------------------------------------------------------------------- 1 | import {InvalidFormatError} from "../Error/InvalidFormatError"; 2 | import {IndexUUID} from "./IndexUUID"; 3 | import {AppUUID} from "./AppUUID"; 4 | 5 | /** 6 | * Index class 7 | */ 8 | export class Index { 9 | 10 | private uuid: IndexUUID; 11 | private appUUID: AppUUID; 12 | private isOK: boolean; 13 | private docCount: number; 14 | private size: string; 15 | 16 | /** 17 | * Constructor 18 | * 19 | * @param uuid 20 | * @param appUUID 21 | * @param isOK 22 | * @param docCount 23 | * @param size 24 | */ 25 | constructor( 26 | uuid: IndexUUID, 27 | appUUID: AppUUID, 28 | isOK=false, 29 | docCount=0, 30 | size='0kb' 31 | ) { 32 | this.uuid = uuid; 33 | this.appUUID = appUUID; 34 | this.isOK = isOK; 35 | this.docCount = docCount; 36 | this.size = size; 37 | } 38 | 39 | /** 40 | * Get uuid 41 | * 42 | * @return {IndexUUID} 43 | */ 44 | public getUUID() : IndexUUID { 45 | return this.uuid; 46 | } 47 | 48 | /** 49 | * Get app id 50 | * 51 | * @return {AppUUID} 52 | */ 53 | public getAppUUID() : AppUUID { 54 | return this.appUUID; 55 | } 56 | 57 | /** 58 | * Index is OK 59 | * 60 | * @return {boolean} 61 | */ 62 | public isOk() : boolean { 63 | return this.isOK; 64 | } 65 | 66 | /** 67 | * Get doc count 68 | * 69 | * @return {number} 70 | */ 71 | public getDocCount() : number { 72 | return this.docCount; 73 | } 74 | 75 | /** 76 | * get size 77 | * 78 | * @return {string} 79 | */ 80 | public getSize() : string { 81 | return this.size; 82 | } 83 | 84 | /** 85 | * To array 86 | * 87 | * @returns {{id: string, attributes: {}}} 88 | */ 89 | public toArray(): { 90 | uuid: any, 91 | app_id: any, 92 | is_ok: boolean, 93 | doc_count: number, 94 | size: string 95 | } { 96 | return { 97 | uuid: this.uuid.toArray(), 98 | app_id: this.appUUID.toArray(), 99 | is_ok: this.isOK, 100 | doc_count: this.docCount, 101 | size: this.size 102 | }; 103 | } 104 | 105 | /** 106 | * Create from array 107 | * 108 | * @param array 109 | * 110 | * @return User 111 | */ 112 | public static createFromArray(array: { 113 | uuid: any, 114 | app_id: any, 115 | is_ok: boolean, 116 | doc_count: number, 117 | size: string 118 | }) { 119 | if ( 120 | typeof array.uuid == "undefined" || 121 | typeof array.app_id == "undefined" 122 | ) { 123 | throw InvalidFormatError.indexFormatNotValid(); 124 | } 125 | 126 | return new Index( 127 | IndexUUID.createFromArray(array.uuid), 128 | AppUUID.createFromArray(array.app_id), 129 | (typeof array.is_ok == "undefined" ? false : array.is_ok), 130 | (typeof array.doc_count == "undefined" ? 0 : array.doc_count), 131 | (typeof array.size == "undefined" ? '0kb' : array.size) 132 | ); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/Model/Changes.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * filter constants 3 | */ 4 | export const TYPE_VALUE = 1; 5 | export const TYPE_LITERAL = 4; 6 | export const TYPE_ARRAY_ELEMENT_UPDATE = 8; 7 | export const TYPE_ARRAY_ELEMENT_ADD = 16; 8 | export const TYPE_ARRAY_ELEMENT_DELETE = 32; 9 | export const TYPE_ARRAY_EXPECTS_ELEMENT = 24; 10 | export const TYPE_ARRAY = 56; 11 | 12 | /** 13 | * Changes Type cast 14 | * @param Changes 15 | */ 16 | export class Changes { 17 | 18 | /** 19 | * Changes 20 | * 21 | * @type {Array} 22 | */ 23 | public changes: any[] = []; 24 | 25 | /** 26 | * Add new change 27 | * 28 | * @param field 29 | * @param value 30 | * @param type 31 | */ 32 | public addChange(field: string, 33 | value: string, 34 | type: number = TYPE_VALUE) { 35 | this.changes.push({ 36 | field, 37 | type, 38 | value, 39 | }); 40 | } 41 | 42 | /** 43 | * Update element from list 44 | * 45 | * @param field 46 | * @param condition 47 | * @param value 48 | * @param type 49 | */ 50 | public updateElementFromList(field: string, 51 | condition: string, 52 | value: string, 53 | type: number) { 54 | this.changes.push({ 55 | field, 56 | type: type | TYPE_ARRAY_ELEMENT_UPDATE, 57 | condition, 58 | value, 59 | }); 60 | } 61 | 62 | /** 63 | * Add element in list 64 | * 65 | * @param field 66 | * @param value 67 | * @param type 68 | */ 69 | public addElementInList(field: string, 70 | value: string, 71 | type: number) { 72 | this.changes.push({ 73 | field, 74 | type: type | TYPE_ARRAY_ELEMENT_ADD, 75 | value, 76 | }); 77 | } 78 | 79 | /** 80 | * Delete element from list 81 | * 82 | * @param field 83 | * @param condition 84 | */ 85 | public deleteElementFromList(field: string, 86 | condition: string) { 87 | this.changes.push({ 88 | field, 89 | type: TYPE_ARRAY_ELEMENT_DELETE, 90 | condition, 91 | }); 92 | } 93 | 94 | /** 95 | * Get changes 96 | * 97 | * @returns {[]} 98 | */ 99 | public getChanges(): any[] { 100 | return this.changes; 101 | } 102 | 103 | /** 104 | * Create 105 | * 106 | * @returns {Changes} 107 | */ 108 | public static create(): Changes { 109 | return new Changes(); 110 | } 111 | 112 | /** 113 | * To array 114 | * 115 | * @returns {[]} 116 | */ 117 | public toArray(): Object { 118 | return JSON.parse(JSON.stringify(this.changes)); 119 | } 120 | 121 | /** 122 | * Create from array 123 | * 124 | * @param array 125 | * 126 | * @returns {Changes} 127 | */ 128 | public static createFromArray(array: any): Changes { 129 | array = JSON.parse(JSON.stringify(array)); 130 | const changes = Changes.create(); 131 | changes.changes = array; 132 | 133 | return changes; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Result/ResultAggregations.ts: -------------------------------------------------------------------------------- 1 | import {ResultAggregation} from "./ResultAggregation"; 2 | 3 | /** 4 | * ResultAggregation class 5 | */ 6 | export class ResultAggregations { 7 | 8 | private aggregations: any = {}; 9 | private totalElements: number; 10 | 11 | /** 12 | * Constructor 13 | * 14 | * @param totalElements 15 | */ 16 | constructor(totalElements: number) { 17 | this.totalElements = totalElements; 18 | } 19 | 20 | /** 21 | * Add aggregation 22 | * 23 | * @param name 24 | * @param aggregation 25 | */ 26 | public addAggregation( 27 | name: string, 28 | aggregation: ResultAggregation, 29 | ) { 30 | this.aggregations[name] = aggregation; 31 | } 32 | 33 | /** 34 | * Get aggregations 35 | * 36 | * @returns {{}} 37 | */ 38 | public getAggregations(): any { 39 | return this.aggregations; 40 | } 41 | 42 | /** 43 | * Get aggregation 44 | * 45 | * @param name 46 | * 47 | * @returns {Aggregation|null} 48 | */ 49 | public getAggregation(name: string) { 50 | return this.aggregations[name] instanceof ResultAggregation 51 | ? this.aggregations[name] 52 | : null; 53 | } 54 | 55 | /** 56 | * Has not empty aggregation 57 | * 58 | * @param name 59 | * 60 | * @returns {boolean} 61 | */ 62 | public hasNotEmptyAggregation(name: string): boolean { 63 | const aggregation = this.getAggregation(name); 64 | return (aggregation instanceof ResultAggregation) && 65 | (!aggregation.isEmpty()); 66 | } 67 | 68 | /** 69 | * Get total elements 70 | * 71 | * @return {number} 72 | */ 73 | public getTotalElements(): number { 74 | return this.totalElements; 75 | } 76 | 77 | /** 78 | * To array 79 | * 80 | * @return {{total_elements?: number, aggregations?: {}}} 81 | */ 82 | public toArray(): {total_elements?: number, aggregations?: {}} { 83 | const aggregationCollection = {}; 84 | for (const i in this.aggregations) { 85 | aggregationCollection[i] = this.aggregations[i].toArray(); 86 | } 87 | 88 | const array: {total_elements?: number, aggregations?: {}} = {}; 89 | 90 | if (this.totalElements > 0) { 91 | array.total_elements = this.totalElements; 92 | } 93 | 94 | if (Object.keys(aggregationCollection).length > 0) { 95 | array.aggregations = aggregationCollection; 96 | } 97 | 98 | return array; 99 | } 100 | 101 | /** 102 | * Create from array 103 | * 104 | * @param array 105 | * 106 | * @return {ResultAggregations} 107 | */ 108 | public static createFromArray(array: any) { 109 | const aggregations = new ResultAggregations( 110 | typeof array.total_elements === "number" 111 | ? array.total_elements 112 | : 0, 113 | ); 114 | 115 | if (typeof array.aggregations === typeof {}) { 116 | for (const i in array.aggregations) { 117 | aggregations.addAggregation( 118 | i, 119 | ResultAggregation.createFromArray(array.aggregations[i]), 120 | ); 121 | } 122 | } 123 | 124 | return aggregations; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /lib/Result/ResultAggregations.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.ResultAggregations = void 0; 4 | var ResultAggregation_1 = require("./ResultAggregation"); 5 | /** 6 | * ResultAggregation class 7 | */ 8 | var ResultAggregations = /** @class */ (function () { 9 | /** 10 | * Constructor 11 | * 12 | * @param totalElements 13 | */ 14 | function ResultAggregations(totalElements) { 15 | this.aggregations = {}; 16 | this.totalElements = totalElements; 17 | } 18 | /** 19 | * Add aggregation 20 | * 21 | * @param name 22 | * @param aggregation 23 | */ 24 | ResultAggregations.prototype.addAggregation = function (name, aggregation) { 25 | this.aggregations[name] = aggregation; 26 | }; 27 | /** 28 | * Get aggregations 29 | * 30 | * @returns {{}} 31 | */ 32 | ResultAggregations.prototype.getAggregations = function () { 33 | return this.aggregations; 34 | }; 35 | /** 36 | * Get aggregation 37 | * 38 | * @param name 39 | * 40 | * @returns {Aggregation|null} 41 | */ 42 | ResultAggregations.prototype.getAggregation = function (name) { 43 | return this.aggregations[name] instanceof ResultAggregation_1.ResultAggregation 44 | ? this.aggregations[name] 45 | : null; 46 | }; 47 | /** 48 | * Has not empty aggregation 49 | * 50 | * @param name 51 | * 52 | * @returns {boolean} 53 | */ 54 | ResultAggregations.prototype.hasNotEmptyAggregation = function (name) { 55 | var aggregation = this.getAggregation(name); 56 | return (aggregation instanceof ResultAggregation_1.ResultAggregation) && 57 | (!aggregation.isEmpty()); 58 | }; 59 | /** 60 | * Get total elements 61 | * 62 | * @return {number} 63 | */ 64 | ResultAggregations.prototype.getTotalElements = function () { 65 | return this.totalElements; 66 | }; 67 | /** 68 | * To array 69 | * 70 | * @return {{total_elements?: number, aggregations?: {}}} 71 | */ 72 | ResultAggregations.prototype.toArray = function () { 73 | var aggregationCollection = {}; 74 | for (var i in this.aggregations) { 75 | aggregationCollection[i] = this.aggregations[i].toArray(); 76 | } 77 | var array = {}; 78 | if (this.totalElements > 0) { 79 | array.total_elements = this.totalElements; 80 | } 81 | if (Object.keys(aggregationCollection).length > 0) { 82 | array.aggregations = aggregationCollection; 83 | } 84 | return array; 85 | }; 86 | /** 87 | * Create from array 88 | * 89 | * @param array 90 | * 91 | * @return {ResultAggregations} 92 | */ 93 | ResultAggregations.createFromArray = function (array) { 94 | var aggregations = new ResultAggregations(typeof array.total_elements === "number" 95 | ? array.total_elements 96 | : 0); 97 | if (typeof array.aggregations === typeof {}) { 98 | for (var i in array.aggregations) { 99 | aggregations.addAggregation(i, ResultAggregation_1.ResultAggregation.createFromArray(array.aggregations[i])); 100 | } 101 | } 102 | return aggregations; 103 | }; 104 | return ResultAggregations; 105 | }()); 106 | exports.ResultAggregations = ResultAggregations; 107 | -------------------------------------------------------------------------------- /src/Config/Config.ts: -------------------------------------------------------------------------------- 1 | import {Synonym} from "./Synonym"; 2 | 3 | export const DEFAULT_SHARDS = 1; 4 | export const DEFAULT_REPLICAS = 0; 5 | 6 | /** 7 | * Result class 8 | */ 9 | export class Config { 10 | 11 | private language: string; 12 | private storeSearchableMetadata: boolean; 13 | private synonyms: Synonym[] = []; 14 | private shards: number; 15 | private replicas: number; 16 | 17 | /** 18 | * Constructor 19 | * 20 | * @param language 21 | * @param storeSearchableMetadata 22 | * @param shards 23 | * @param replicas 24 | */ 25 | constructor( 26 | language: string = null, 27 | storeSearchableMetadata: boolean = true, 28 | shards: number = DEFAULT_SHARDS, 29 | replicas: number = DEFAULT_REPLICAS 30 | ) { 31 | this.language = language; 32 | this.storeSearchableMetadata = storeSearchableMetadata; 33 | this.shards = shards; 34 | this.replicas = replicas; 35 | } 36 | 37 | /** 38 | * Get language 39 | * 40 | * @return {string} 41 | */ 42 | public getLanguage(): string { 43 | return this.language; 44 | } 45 | 46 | /** 47 | * Should searchable metadata be stored 48 | * 49 | * @return {boolean} 50 | */ 51 | public shouldSearchableMetadataBeStored(): boolean { 52 | return this.storeSearchableMetadata; 53 | } 54 | 55 | /** 56 | * Add synonym 57 | * 58 | * @param synonym 59 | */ 60 | public addSynonym(synonym: Synonym) { 61 | this.synonyms.push(synonym); 62 | } 63 | 64 | /** 65 | * Get synonyms 66 | * 67 | * @return {Synonym[]} 68 | */ 69 | public getSynonyms(): Synonym[] { 70 | return this.synonyms; 71 | } 72 | 73 | /** 74 | * Get shards 75 | * 76 | * @return {number} 77 | */ 78 | public getShards(): number { 79 | return this.shards; 80 | } 81 | 82 | /** 83 | * Get replicas 84 | * 85 | * @return {number} 86 | */ 87 | public getReplicas(): number { 88 | return this.replicas; 89 | } 90 | 91 | /** 92 | * to array 93 | */ 94 | public toArray() { 95 | return { 96 | language: this.language, 97 | store_searchable_metadata: this.storeSearchableMetadata, 98 | synonyms: this.synonyms.map((synonym) => synonym.toArray()), 99 | shards: this.shards, 100 | replicas: this.replicas 101 | }; 102 | } 103 | 104 | /** 105 | * Create from array 106 | */ 107 | public static createFromArray(array: any): Config { 108 | const config = new Config( 109 | array.language ? array.language : null, 110 | typeof array.store_searchable_metadata == "boolean" 111 | ? array.store_searchable_metadata 112 | : true, 113 | ); 114 | 115 | if ( 116 | array.synonyms instanceof Array && 117 | array.synonyms.length > 0 118 | ) { 119 | config.synonyms = array.synonyms.map((synonym) => Synonym.createFromArray(synonym)); 120 | } 121 | 122 | config.shards = typeof array.shards== "number" 123 | ? array.shards 124 | : DEFAULT_SHARDS; 125 | 126 | config.replicas = typeof array.replicas== "number" 127 | ? array.replicas 128 | : DEFAULT_REPLICAS; 129 | 130 | return config; 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /lib/Model/Changes.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Changes = exports.TYPE_ARRAY = exports.TYPE_ARRAY_EXPECTS_ELEMENT = exports.TYPE_ARRAY_ELEMENT_DELETE = exports.TYPE_ARRAY_ELEMENT_ADD = exports.TYPE_ARRAY_ELEMENT_UPDATE = exports.TYPE_LITERAL = exports.TYPE_VALUE = void 0; 4 | /** 5 | * filter constants 6 | */ 7 | exports.TYPE_VALUE = 1; 8 | exports.TYPE_LITERAL = 4; 9 | exports.TYPE_ARRAY_ELEMENT_UPDATE = 8; 10 | exports.TYPE_ARRAY_ELEMENT_ADD = 16; 11 | exports.TYPE_ARRAY_ELEMENT_DELETE = 32; 12 | exports.TYPE_ARRAY_EXPECTS_ELEMENT = 24; 13 | exports.TYPE_ARRAY = 56; 14 | /** 15 | * Changes Type cast 16 | * @param Changes 17 | */ 18 | var Changes = /** @class */ (function () { 19 | function Changes() { 20 | /** 21 | * Changes 22 | * 23 | * @type {Array} 24 | */ 25 | this.changes = []; 26 | } 27 | /** 28 | * Add new change 29 | * 30 | * @param field 31 | * @param value 32 | * @param type 33 | */ 34 | Changes.prototype.addChange = function (field, value, type) { 35 | if (type === void 0) { type = exports.TYPE_VALUE; } 36 | this.changes.push({ 37 | field: field, 38 | type: type, 39 | value: value 40 | }); 41 | }; 42 | /** 43 | * Update element from list 44 | * 45 | * @param field 46 | * @param condition 47 | * @param value 48 | * @param type 49 | */ 50 | Changes.prototype.updateElementFromList = function (field, condition, value, type) { 51 | this.changes.push({ 52 | field: field, 53 | type: type | exports.TYPE_ARRAY_ELEMENT_UPDATE, 54 | condition: condition, 55 | value: value 56 | }); 57 | }; 58 | /** 59 | * Add element in list 60 | * 61 | * @param field 62 | * @param value 63 | * @param type 64 | */ 65 | Changes.prototype.addElementInList = function (field, value, type) { 66 | this.changes.push({ 67 | field: field, 68 | type: type | exports.TYPE_ARRAY_ELEMENT_ADD, 69 | value: value 70 | }); 71 | }; 72 | /** 73 | * Delete element from list 74 | * 75 | * @param field 76 | * @param condition 77 | */ 78 | Changes.prototype.deleteElementFromList = function (field, condition) { 79 | this.changes.push({ 80 | field: field, 81 | type: exports.TYPE_ARRAY_ELEMENT_DELETE, 82 | condition: condition 83 | }); 84 | }; 85 | /** 86 | * Get changes 87 | * 88 | * @returns {[]} 89 | */ 90 | Changes.prototype.getChanges = function () { 91 | return this.changes; 92 | }; 93 | /** 94 | * Create 95 | * 96 | * @returns {Changes} 97 | */ 98 | Changes.create = function () { 99 | return new Changes(); 100 | }; 101 | /** 102 | * To array 103 | * 104 | * @returns {[]} 105 | */ 106 | Changes.prototype.toArray = function () { 107 | return JSON.parse(JSON.stringify(this.changes)); 108 | }; 109 | /** 110 | * Create from array 111 | * 112 | * @param array 113 | * 114 | * @returns {Changes} 115 | */ 116 | Changes.createFromArray = function (array) { 117 | array = JSON.parse(JSON.stringify(array)); 118 | var changes = Changes.create(); 119 | changes.changes = array; 120 | return changes; 121 | }; 122 | return Changes; 123 | }()); 124 | exports.Changes = Changes; 125 | -------------------------------------------------------------------------------- /test/Query/Filter.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import {Filter} from "../../src/Query/Filter"; 3 | import { 4 | FILTER_MUST_ALL, 5 | FILTER_AT_LEAST_ONE, 6 | FILTER_TYPE_GEO, 7 | FILTER_TYPE_FIELD 8 | } from "../../src/Query/Filter" 9 | 10 | describe('Query/', () => { 11 | describe('Filter', () => { 12 | describe('.create() All attributes defined', () => { 13 | let filter = Filter.create( 14 | 'field', 15 | [1, 2, 3], 16 | FILTER_MUST_ALL, 17 | FILTER_TYPE_GEO, 18 | ['a', 'b'] 19 | ); 20 | 21 | it('Should have right values', () => { 22 | expect(filter.getField()).to.be.equal('field'); 23 | expect(filter.getValues()).to.be.deep.equal([1, 2, 3]); 24 | expect(filter.getApplicationType()).to.be.equal(FILTER_MUST_ALL); 25 | expect(filter.getFilterType()).to.be.equal(FILTER_TYPE_GEO); 26 | expect(filter.getFilterTerms()).to.be.deep.equal(['a', 'b']); 27 | }); 28 | }); 29 | describe('.create() Default attributes', () => { 30 | let filter = Filter.create( 31 | 'field', 32 | [1, 2, 3], 33 | FILTER_MUST_ALL, 34 | FILTER_TYPE_GEO 35 | ); 36 | 37 | it('Should have right values', () => { 38 | expect(filter.getField()).to.be.equal('field'); 39 | expect(filter.getValues()).to.be.deep.equal([1, 2, 3]); 40 | expect(filter.getApplicationType()).to.be.equal(FILTER_MUST_ALL); 41 | expect(filter.getFilterType()).to.be.equal(FILTER_TYPE_GEO); 42 | expect(filter.getFilterTerms()).to.be.deep.equal([]); 43 | }); 44 | }); 45 | describe('.toArray() All attributes defined', () => { 46 | let filterAsArray = { 47 | 'field': 'field', 48 | 'values': [1, 2, 3], 49 | 'application_type': FILTER_MUST_ALL, 50 | 'filter_type': FILTER_TYPE_GEO, 51 | 'filter_terms': ['a', 'b'] 52 | }; 53 | 54 | it('Should have all values', () => { 55 | expect(Filter.createFromArray(filterAsArray).toArray()).to.be.deep.equal(filterAsArray); 56 | }); 57 | }); 58 | describe('.toArray() Default attributes', () => { 59 | let filterAsArray = { 60 | 'field': 'uuid.type', 61 | 'values': [], 62 | 'application_type': FILTER_AT_LEAST_ONE, 63 | 'filter_type': FILTER_TYPE_FIELD, 64 | 'filter_terms': [], 65 | }; 66 | 67 | it('Should have all values', () => { 68 | expect(Filter.createFromArray(filterAsArray).toArray()).to.be.deep.equal({}); 69 | }); 70 | }); 71 | 72 | describe('.createFromArray() Default attributes', () => { 73 | let filterAsArray = {}; 74 | let filter = Filter.createFromArray(filterAsArray); 75 | 76 | it('Should have all values', () => { 77 | expect(filter.toArray()).to.be.deep.equal({}); 78 | expect(filter.getField()).to.be.equal('uuid.type'); 79 | expect(filter.getValues()).to.be.deep.equal([]); 80 | expect(filter.getApplicationType()).to.be.equal(FILTER_AT_LEAST_ONE); 81 | expect(filter.getFilterType()).to.be.equal(FILTER_TYPE_FIELD); 82 | expect(filter.getFilterTerms()).to.be.deep.equal([]); 83 | }); 84 | }); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /lib/Result/Counter.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | exports.__esModule = true; 3 | exports.Counter = void 0; 4 | var Metadata_1 = require("../Model/Metadata"); 5 | /** 6 | * Aggregation class 7 | */ 8 | var Counter = /** @class */ (function () { 9 | /** 10 | * Constructor 11 | * 12 | * @param values 13 | * @param used 14 | * @param n 15 | */ 16 | function Counter(values, used, n) { 17 | this.values = values; 18 | this.used = used; 19 | this.n = n; 20 | } 21 | /** 22 | * Get id 23 | * 24 | * @return {string|null} 25 | */ 26 | Counter.prototype.getId = function () { 27 | return typeof this.values.id == "string" 28 | ? this.values.id 29 | : null; 30 | }; 31 | /** 32 | * Get name 33 | * 34 | * @return {string|null} 35 | */ 36 | Counter.prototype.getName = function () { 37 | return typeof this.values.name == "string" 38 | ? this.values.name 39 | : null; 40 | }; 41 | /** 42 | * Get slug 43 | * 44 | * @return {string|null} 45 | */ 46 | Counter.prototype.getSlug = function () { 47 | return typeof this.values.slug == "string" 48 | ? this.values.slug 49 | : null; 50 | }; 51 | /** 52 | * Get level 53 | * 54 | * @return {number} 55 | */ 56 | Counter.prototype.getLevel = function () { 57 | return typeof this.values.level == "number" 58 | ? this.values.level 59 | : 0; 60 | }; 61 | /** 62 | * Get values 63 | * 64 | * @returns {{}} 65 | */ 66 | Counter.prototype.getValues = function () { 67 | return this.values; 68 | }; 69 | /** 70 | * Is used 71 | * 72 | * @returns {boolean} 73 | */ 74 | Counter.prototype.isUsed = function () { 75 | return this.used; 76 | }; 77 | /** 78 | * Get N 79 | * 80 | * @returns {number} 81 | */ 82 | Counter.prototype.getN = function () { 83 | return this.n; 84 | }; 85 | /** 86 | * Create by active elements 87 | * 88 | * @param name 89 | * @param n 90 | * @param activeElements 91 | */ 92 | Counter.createByActiveElements = function (name, n, activeElements) { 93 | var values = Metadata_1.Metadata.fromMetadata(name); 94 | if (values == null) { 95 | return null; 96 | } 97 | var i = activeElements.length; 98 | var inActiveElements = false; 99 | while (i--) { 100 | if (activeElements[i] == values.id) { 101 | inActiveElements = true; 102 | } 103 | } 104 | return new Counter(values, inActiveElements, n); 105 | }; 106 | /** 107 | * To array 108 | * 109 | * @return {{}} 110 | */ 111 | Counter.prototype.toArray = function () { 112 | var values = { 113 | values: this.values, 114 | n: this.n 115 | }; 116 | if (this.used === true) { 117 | values.used = true; 118 | } 119 | return values; 120 | }; 121 | /** 122 | * Create from array 123 | * 124 | * @param array 125 | * 126 | * @return {Counter} 127 | */ 128 | Counter.createFromArray = function (array) { 129 | array = JSON.parse(JSON.stringify(array)); 130 | return new Counter(array.values, (typeof array.used == "boolean") 131 | ? array.used 132 | : false, array.n); 133 | }; 134 | return Counter; 135 | }()); 136 | exports.Counter = Counter; 137 | --------------------------------------------------------------------------------