Skip to content

Commit e0fb697

Browse files
authored
Improved the way to check configuration (#14)
* Added some tests
1 parent 96392c5 commit e0fb697

18 files changed

+190
-58
lines changed

dist/apisearch.js

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Apisearch.d.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { KeyValueCache } from "./Cache/KeyValueCache";
2+
import { HttpClient } from "./Http/HttpClient";
23
import { Coordinate } from "./Model/Coordinate";
34
import { ItemUUID } from "./Model/ItemUUID";
45
import { Query } from "./Query/Query";
56
import { SortBy } from "./Query/SortBy";
6-
import { Repository } from "./Repository/Repository";
7+
import { HttpRepository } from "./Repository/HttpRepository";
78
import { Result } from "./Result/Result";
89
/**
910
* Apisearch class
@@ -14,20 +15,34 @@ export default class Apisearch {
1415
*
1516
* @param config
1617
*
17-
* @returns {Repository}
18+
* @return {HttpRepository}
1819
*/
1920
static createRepository(config: {
2021
app_id: string;
2122
index_id: string;
2223
token: string;
2324
options: {
24-
endpoint?: string;
25+
endpoint: string;
2526
api_version?: string;
2627
timeout?: number;
2728
override_queries?: boolean;
2829
cache?: KeyValueCache;
30+
http_client?: HttpClient;
2931
};
30-
}): Repository;
32+
}): HttpRepository;
33+
/**
34+
* Ensure the Repository configuration is valid
35+
*
36+
* @param config
37+
*/
38+
static ensureRepositoryConfigIsValid(config: any): void;
39+
/**
40+
* Ensure the value is not undefined
41+
*
42+
* @param param
43+
* @param name
44+
*/
45+
static ensureIsDefined(param: any, name: string): void;
3146
/**
3247
* Created located
3348
*

lib/Apisearch.js

+38-13
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var Query_2 = require("./Query/Query");
1616
var Query_3 = require("./Query/Query");
1717
var SortBy_1 = require("./Query/SortBy");
1818
var HttpRepository_1 = require("./Repository/HttpRepository");
19-
var ResultAggregations_1 = require("./Result/ResultAggregations");
2019
var Result_1 = require("./Result/Result");
20+
var ResultAggregations_1 = require("./Result/ResultAggregations");
2121
var Transformer_1 = require("./Transformer/Transformer");
2222
/**
2323
* Apisearch class
@@ -30,16 +30,41 @@ var Apisearch = /** @class */ (function () {
3030
*
3131
* @param config
3232
*
33-
* @returns {Repository}
33+
* @return {HttpRepository}
3434
*/
3535
Apisearch.createRepository = function (config) {
36-
config.options = __assign({ api_version: "v1", cache: new NoCache_1.NoCache(), timeout: 10000, override_queries: true }, config.options);
36+
Apisearch.ensureRepositoryConfigIsValid(config);
37+
config.options = __assign({ api_version: "v1", cache: new NoCache_1.NoCache(), timeout: 5000, override_queries: true }, config.options);
3738
/**
3839
* Client
3940
*/
40-
var httpClient = new AxiosClient_1.AxiosClient(config.options.endpoint, config.options.api_version, config.options.timeout, new RetryMap_1.RetryMap(), config.options.override_queries, config.options.cache);
41+
var httpClient = typeof config.options.http_client !== "undefined"
42+
? config.options.http_client
43+
: new AxiosClient_1.AxiosClient(config.options.endpoint, config.options.api_version, config.options.timeout, new RetryMap_1.RetryMap(), config.options.override_queries, config.options.cache);
4144
return new HttpRepository_1.HttpRepository(httpClient, config.app_id, config.index_id, config.token, new Transformer_1.Transformer());
4245
};
46+
/**
47+
* Ensure the Repository configuration is valid
48+
*
49+
* @param config
50+
*/
51+
Apisearch.ensureRepositoryConfigIsValid = function (config) {
52+
Apisearch.ensureIsDefined(config.app_id, "app_id");
53+
Apisearch.ensureIsDefined(config.index_id, "index_id");
54+
Apisearch.ensureIsDefined(config.token, "token");
55+
Apisearch.ensureIsDefined(config.options.endpoint, "options.endpoint");
56+
};
57+
/**
58+
* Ensure the value is not undefined
59+
*
60+
* @param param
61+
* @param name
62+
*/
63+
Apisearch.ensureIsDefined = function (param, name) {
64+
if (typeof param === "undefined") {
65+
throw new TypeError(name + " parameter must be defined.");
66+
}
67+
};
4368
/**
4469
* Created located
4570
*
@@ -51,9 +76,9 @@ var Apisearch = /** @class */ (function () {
5176
* @returns {Query}
5277
*/
5378
Apisearch.createQueryLocated = function (coordinate, queryText, page, size) {
54-
if (page === void 0) { page = Query_2.QUERY_DEFAULT_PAGE; }
55-
if (size === void 0) { size = Query_3.QUERY_DEFAULT_SIZE; }
56-
return Query_1.Query.createLocated(coordinate, queryText, page, size);
79+
if (page === void 0) { page = Query_1.QUERY_DEFAULT_PAGE; }
80+
if (size === void 0) { size = Query_2.QUERY_DEFAULT_SIZE; }
81+
return Query_3.Query.createLocated(coordinate, queryText, page, size);
5782
};
5883
/**
5984
* Create
@@ -65,17 +90,17 @@ var Apisearch = /** @class */ (function () {
6590
* @returns {Query}
6691
*/
6792
Apisearch.createQuery = function (queryText, page, size) {
68-
if (page === void 0) { page = Query_2.QUERY_DEFAULT_PAGE; }
69-
if (size === void 0) { size = Query_3.QUERY_DEFAULT_SIZE; }
70-
return Query_1.Query.create(queryText, page, size);
93+
if (page === void 0) { page = Query_1.QUERY_DEFAULT_PAGE; }
94+
if (size === void 0) { size = Query_2.QUERY_DEFAULT_SIZE; }
95+
return Query_3.Query.create(queryText, page, size);
7196
};
7297
/**
7398
* Create match all
7499
*
75100
* @return {Query}
76101
*/
77102
Apisearch.createQueryMatchAll = function () {
78-
return Query_1.Query.createMatchAll();
103+
return Query_3.Query.createMatchAll();
79104
};
80105
/**
81106
* Create by UUID
@@ -85,7 +110,7 @@ var Apisearch = /** @class */ (function () {
85110
* @return {Query}
86111
*/
87112
Apisearch.createQueryByUUID = function (uuid) {
88-
return Query_1.Query.createByUUID(uuid);
113+
return Query_3.Query.createByUUID(uuid);
89114
};
90115
/**
91116
* Create by UUIDs
@@ -99,7 +124,7 @@ var Apisearch = /** @class */ (function () {
99124
for (var _i = 0; _i < arguments.length; _i++) {
100125
uuids[_i] = arguments[_i];
101126
}
102-
return Query_1.Query.createByUUIDs.apply(Query_1.Query, uuids);
127+
return Query_3.Query.createByUUIDs.apply(Query_3.Query, uuids);
103128
};
104129
/**
105130
* Create empty result

lib/Cache/InMemoryCache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ var InMemoryCache = /** @class */ (function () {
3030
* @returns {void}
3131
*/
3232
InMemoryCache.prototype.set = function (key, value) {
33+
var _a;
3334
this.cache = __assign({}, this.cache, (_a = {}, _a[key] = value, _a));
3435
this.size = this.size + 1;
35-
var _a;
3636
};
3737
/**
3838
* Get element from cache

lib/Http/AxiosClient.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
3232
function step(op) {
3333
if (f) throw new TypeError("Generator is already executing.");
3434
while (_) try {
35-
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
36-
if (y = 0, t) op = [0, t.value];
35+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
36+
if (y = 0, t) op = [op[0] & 2, t.value];
3737
switch (op[0]) {
3838
case 0: case 1: t = op; break;
3939
case 4: _.label++; return { value: op[1], done: false };
@@ -95,8 +95,8 @@ var AxiosClient = /** @class */ (function (_super) {
9595
if (parameters === void 0) { parameters = {}; }
9696
if (data === void 0) { data = {}; }
9797
return __awaiter(this, void 0, void 0, function () {
98-
var _this = this;
9998
var that;
99+
var _this = this;
100100
return __generator(this, function (_a) {
101101
that = this;
102102
url = url.replace(/^\/*|\/*$/g, "");

lib/Http/TestClient.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
1414
function step(op) {
1515
if (f) throw new TypeError("Generator is already executing.");
1616
while (_) try {
17-
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18-
if (y = 0, t) op = [0, t.value];
17+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18+
if (y = 0, t) op = [op[0] & 2, t.value];
1919
switch (op[0]) {
2020
case 0: case 1: t = op; break;
2121
case 4: _.label++; return { value: op[1], done: false };

0 commit comments

Comments
 (0)