11import { KeyValueCache } from "./Cache/KeyValueCache" ;
22import { NoCache } from "./Cache/NoCache" ;
33import { AxiosClient } from "./Http/AxiosClient" ;
4+ import { HttpClient } from "./Http/HttpClient" ;
45import { RetryMap } from "./Http/RetryMap" ;
56import { Coordinate } from "./Model/Coordinate" ;
67import { ItemUUID } from "./Model/ItemUUID" ;
7- import { Query } from "./Query/Query" ;
88import { QUERY_DEFAULT_PAGE } from "./Query/Query" ;
99import { QUERY_DEFAULT_SIZE } from "./Query/Query" ;
10+ import { Query } from "./Query/Query" ;
1011import { SortBy } from "./Query/SortBy" ;
1112import { HttpRepository } from "./Repository/HttpRepository" ;
12- import { Repository } from "./Repository/Repository" ;
13- import { ResultAggregations } from "./Result/ResultAggregations" ;
1413import { Result } from "./Result/Result" ;
14+ import { ResultAggregations } from "./Result/ResultAggregations" ;
1515import { Transformer } from "./Transformer/Transformer" ;
1616
1717/**
@@ -24,41 +24,48 @@ export default class Apisearch {
2424 *
2525 * @param config
2626 *
27- * @returns { Repository }
27+ * @return { HttpRepository }
2828 */
29- public static createRepository (
30- config : {
31- app_id : string ,
32- index_id : string ,
33- token : string ,
34- options : {
35- endpoint ?: string ,
36- api_version ?: string ,
37- timeout ?: number ,
38- override_queries ?: boolean ,
39- cache ?: KeyValueCache ,
40- } ,
41- }
42- ) : Repository {
29+ public static createRepository ( config : {
30+ app_id : string ,
31+ index_id : string ,
32+ token : string ,
33+ options : {
34+ endpoint : string ,
35+ api_version ?: string ,
36+ timeout ?: number ,
37+ override_queries ?: boolean ,
38+ cache ?: KeyValueCache ,
39+ http_client ?: HttpClient ,
40+ } ,
41+ } ) : HttpRepository {
42+
43+ Apisearch . ensureIsDefined ( config . app_id , "app_id" ) ;
44+ Apisearch . ensureIsDefined ( config . index_id , "index_id" ) ;
45+ Apisearch . ensureIsDefined ( config . token , "token" ) ;
46+ Apisearch . ensureIsDefined ( config . options . endpoint , "options.endpoint" ) ;
47+
4348 config . options = {
4449 api_version : "v1" ,
4550 cache : new NoCache ( ) ,
46- timeout : 10000 ,
51+ timeout : 5000 ,
4752 override_queries : true ,
4853 ...config . options ,
4954 } ;
5055
5156 /**
5257 * Client
5358 */
54- const httpClient = new AxiosClient (
55- config . options . endpoint ,
56- config . options . api_version ,
57- config . options . timeout ,
58- new RetryMap ( ) ,
59- config . options . override_queries ,
60- config . options . cache ,
61- ) ;
59+ const httpClient = typeof config . options . http_client !== "undefined"
60+ ? config . options . http_client
61+ : new AxiosClient (
62+ config . options . endpoint ,
63+ config . options . api_version ,
64+ config . options . timeout ,
65+ new RetryMap ( ) ,
66+ config . options . override_queries ,
67+ config . options . cache ,
68+ ) ;
6269
6370 return new HttpRepository (
6471 httpClient ,
@@ -69,6 +76,18 @@ export default class Apisearch {
6976 ) ;
7077 }
7178
79+ /**
80+ * Ensure the value is not undefined
81+ *
82+ * @param param
83+ * @param name
84+ */
85+ public static ensureIsDefined ( param , name ) {
86+ if ( typeof param === "undefined" ) {
87+ throw new TypeError ( `${ name } parameter must be defined.` ) ;
88+ }
89+ }
90+
7291 /**
7392 * Created located
7493 *
0 commit comments