Skip to content

Commit

Permalink
Fixed the way a repository is built from Apisearch (#12)
Browse files Browse the repository at this point in the history
- Added some tests under Apisearch class
  • Loading branch information
mmoreram authored Jul 12, 2018
1 parent 68d591e commit 1a2a08e
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 50 deletions.
49 changes: 33 additions & 16 deletions dist/apisearch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/apisearch.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/apisearch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/apisearch.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apisearch",
"version": "0.2.4",
"version": "0.2.5",
"description": "Javascript client for Apisearch.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -9,7 +9,7 @@
"build:dev": "webpack --config ./webpack.dev.js --display-error-details",
"build:min": "webpack --config ./webpack.min.js --display-error-details",
"dist": "npm t; rm -rf ./dist/*; npm run build:dev && npm run build:min",
"test": "mocha --recursive --require ts-node/register test/*/*.ts",
"test": "mocha --recursive --require ts-node/register test/**/*.ts test/*.ts",
"fix": "tslint -c tslint.json --fix 'src/**/*.ts'",
"test:functional": "mocha --recursive --require ts-node/register test/Functional/*/*.ts"
},
Expand Down
73 changes: 46 additions & 27 deletions src/Apisearch.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {KeyValueCache} from "./Cache/KeyValueCache";
import {NoCache} from "./Cache/NoCache";
import {AxiosClient} from "./Http/AxiosClient";
import {HttpClient} from "./Http/HttpClient";
import {RetryMap} from "./Http/RetryMap";
import {Coordinate} from "./Model/Coordinate";
import {ItemUUID} from "./Model/ItemUUID";
import {Query} from "./Query/Query";
import {QUERY_DEFAULT_PAGE} from "./Query/Query";
import {QUERY_DEFAULT_SIZE} from "./Query/Query";
import {Query} from "./Query/Query";
import {SortBy} from "./Query/SortBy";
import {HttpRepository} from "./Repository/HttpRepository";
import {Repository} from "./Repository/Repository";
import {ResultAggregations} from "./Result/ResultAggregations";
import {Result} from "./Result/Result";
import {ResultAggregations} from "./Result/ResultAggregations";
import {Transformer} from "./Transformer/Transformer";

/**
Expand All @@ -24,41 +24,48 @@ export default class Apisearch {
*
* @param config
*
* @returns {Repository}
* @return {HttpRepository}
*/
public static createRepository(
config: {
app_id: string,
index_id: string,
token: string,
options: {
endpoint?: string,
api_version?: string,
timeout?: number,
override_queries?: boolean,
cache?: KeyValueCache,
},
}
): Repository {
public static createRepository(config: {
app_id: string,
index_id: string,
token: string,
options: {
endpoint: string,
api_version?: string,
timeout?: number,
override_queries?: boolean,
cache?: KeyValueCache,
http_client?: HttpClient,
},
}): HttpRepository {

Apisearch.ensureIsDefined(config.app_id, "app_id");
Apisearch.ensureIsDefined(config.index_id, "index_id");
Apisearch.ensureIsDefined(config.token, "token");
Apisearch.ensureIsDefined(config.options.endpoint, "options.endpoint");

config.options = {
api_version: "v1",
cache: new NoCache(),
timeout: 10000,
timeout: 5000,
override_queries: true,
...config.options,
};

/**
* Client
*/
const httpClient = new AxiosClient(
config.options.endpoint,
config.options.api_version,
config.options.timeout,
new RetryMap(),
config.options.override_queries,
config.options.cache,
);
const httpClient = typeof config.options.http_client !== "undefined"
? config.options.http_client
: new AxiosClient(
config.options.endpoint,
config.options.api_version,
config.options.timeout,
new RetryMap(),
config.options.override_queries,
config.options.cache,
);

return new HttpRepository(
httpClient,
Expand All @@ -69,6 +76,18 @@ export default class Apisearch {
);
}

/**
* Ensure the value is not undefined
*
* @param param
* @param name
*/
public static ensureIsDefined(param, name) {
if (typeof param === "undefined") {
throw new TypeError(`${name} parameter must be defined.`);
}
}

/**
* Created located
*
Expand Down
2 changes: 1 addition & 1 deletion src/Result/Result.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Item} from "../Model/Item";
import {ResultAggregation} from "../Result/ResultAggregation";
import {Query} from "../Query/Query";
import {ResultAggregation} from "../Result/ResultAggregation";
import {ResultAggregations} from "./ResultAggregations";
/**
* Result class
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ export * from "./Result/Result";

export * from "./Transformer/ReadTransformer";
export * from "./Transformer/Transformer";
export * from "./Transformer/WriteTransformer";
export * from "./Transformer/WriteTransformer";
65 changes: 65 additions & 0 deletions test/Apisearch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect } from 'chai';
import Apisearch from "../src/Apisearch";
import {Product} from "./Transformer/Product";
import {Item} from "../src/Model/Item";
import {ItemUUID} from "../src/Model/ItemUUID";
import {Changes} from "../src/Model/Changes";
import {Query} from "../src/Query/Query";
import {ImmutableConfig} from "../src/Config/ImmutableConfig";
import {Config} from "../src/Config/Config";
import {TestClient} from "../src/Http/TestClient";
import {ProductReadTransformer} from "./Transformer/ProductReadTransformer";

describe('Apisearch', () => {

const client = new TestClient();
const repository = Apisearch.createRepository({
"app_id": "aaaa",
"index_id": 'bbbb',
"token": 'cccc',
"options": {
"endpoint": 'http://blabla',
"http_client": client,
}
});
repository.getTransformer().addReadTransformer(new ProductReadTransformer());

describe('Initial build', () => {
it('Should create properly', () => {
expect(typeof repository.getTransformer()).to.be.equal("object");
});
});

describe('Calls will work properly', () => {
repository.addObject(new Product('1', '1', 'name'));
repository.addObject(new Product('2', '2', 'name'));
repository.addItem(Item.create(ItemUUID.createByComposedUUID('3~product')));
repository.addItems([
Item.create(ItemUUID.createByComposedUUID('4~product')),
Item.create(ItemUUID.createByComposedUUID('5~product'))
]);
repository.deleteItem(ItemUUID.createByComposedUUID('6~product'));
repository.deleteItems([
ItemUUID.createByComposedUUID('7~product'),
ItemUUID.createByComposedUUID('8~product')
]);
repository.deleteObject(new Product('9', 'lalala', 'prod4'));
repository.deleteObject(new Product('10', 'lalala', 'prod5'));

it('Should take properly all available repository calls', () => {
return Promise.all([
repository.flush(100, true),
repository.flush(3),
repository.query(Query.createMatchAll()),
repository.updateItems(Query.createMatchAll(), Changes.create()),
repository.createIndex(new ImmutableConfig()),
repository.deleteIndex(),
repository.resetIndex(),
repository.checkIndex(),
repository.configureIndex(new Config())
]).then(() => {
expect(client.calls.length).to.be.equal(9);
})
});
});
});
6 changes: 6 additions & 0 deletions test/Repository/HttpRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ describe('Repository/', () => {
Item.create(ItemUUID.createByComposedUUID('6~product')),
]);

await repository
.flush(20, true)
.then(_ => {
expect(client.calls.length).to.be.equal(0);
});

await repository
.flush(2)
.then(_ => {
Expand Down

0 comments on commit 1a2a08e

Please sign in to comment.