Skip to content

Commit 73356a7

Browse files
authored
Merge pull request #103 from apisearch-io/feature/added-custom-transformation-function
Added custom transformation function
2 parents 0f44201 + 6b346af commit 73356a7

20 files changed

+71
-830
lines changed

.circleci/config.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@ jobs:
33
build:
44
docker:
55
- image: circleci/node:latest
6-
- image: docker.elastic.co/elasticsearch/elasticsearch:8.12.1
7-
environment:
8-
- "ES_JAVA_OPTS=-Xms256m -Xmx256m"
9-
- "discovery.type=single-node"
10-
- "action.auto_create_index=-apisearch*,+*"
11-
- "xpack.security.enabled=false"
12-
- "indices.id_field_data.enabled=true"
13-
14-
- image: apisearchio/search-server:commit-3aa3b8f6
15-
environment:
16-
APISEARCH_GOD_TOKEN: 0e4d75ba-c640-44c1-a745-06ee51db4e93
17-
APISEARCH_ENABLED_PLUGINS: elasticsearch
18-
ELASTICSEARCH_HOST: localhost
19-
ELASTICSEARCH_REFRESH_ON_WRITE: 1
206

217
steps:
228
- checkout
@@ -25,13 +11,6 @@ jobs:
2511
command: |
2612
npm install
2713
28-
- run:
29-
name: Wait services to be running
30-
command: |
31-
sudo apt-get install wait-for-it
32-
wait-for-it localhost:9200
33-
wait-for-it localhost:8000
34-
3514
- run:
3615
name: Run test scenarios
3716
command: |

dist/apisearch.js

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Repository/HttpRepository.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,32 @@ var HttpRepository = /** @class */ (function (_super) {
147147
*/
148148
HttpRepository.prototype.query = function (query) {
149149
return tslib_1.__awaiter(this, void 0, void 0, function () {
150-
var response, response_3, result;
151-
return tslib_1.__generator(this, function (_a) {
152-
switch (_a.label) {
150+
var response, response_3, result, _a, _b;
151+
return tslib_1.__generator(this, function (_c) {
152+
switch (_c.label) {
153153
case 0:
154-
_a.trys.push([0, 2, , 3]);
154+
_c.trys.push([0, 2, , 3]);
155155
return [4 /*yield*/, this.httpClient.get("/" + this.appId + "/indices/" + this.indexId, "get", this.getCredentials(), {
156156
query: JSON.stringify(query.toArray())
157157
.replace(/&/g, "%26")
158158
}, {})];
159159
case 1:
160-
response = _a.sent();
160+
response = _c.sent();
161161
return [3 /*break*/, 3];
162162
case 2:
163-
response_3 = _a.sent();
163+
response_3 = _c.sent();
164164
throw HttpRepository.createErrorFromResponse(response_3);
165165
case 3:
166166
result = Result_1.Result.createFromArray(response.getBody());
167-
return [2 /*return*/, this.applyTransformersToResult(result)];
167+
result = this.applyTransformersToResult(result);
168+
if (!(typeof globalThis !== "undefined" &&
169+
typeof globalThis.apisearchItemsTransformation === "function")) return [3 /*break*/, 5];
170+
_b = (_a = result).withItems;
171+
return [4 /*yield*/, globalThis.apisearchItemsTransformation(result.getItems())];
172+
case 4:
173+
_b.apply(_a, [_c.sent()]);
174+
_c.label = 5;
175+
case 5: return [2 /*return*/, result];
168176
}
169177
});
170178
});

lib/Result/Result.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export declare class Result {
5454
* @return {Item[]}
5555
*/
5656
getItems(): Item[];
57+
/**
58+
* @param items
59+
*/
60+
withItems(items: Item[]): void;
5761
/**
5862
* Get items grouped by types
5963
*

lib/Result/Result.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ var Result = /** @class */ (function () {
7373
Result.prototype.getItems = function () {
7474
return this.items;
7575
};
76+
/**
77+
* @param items
78+
*/
79+
Result.prototype.withItems = function (items) {
80+
this.items = items;
81+
};
7682
/**
7783
* Get items grouped by types
7884
*

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apisearch",
3-
"version": "0.3.18",
3+
"version": "0.3.19",
44
"description": "Javascript client for Apisearch.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
@@ -13,8 +13,7 @@
1313
"dist-no-test": "rm -rf ./dist/*; npm run build:lib && npm run build:dev && npm run build:min",
1414
"fix": "tslint -c tslint.json --fix 'src/**/*.ts'",
1515
"test:unit": "mocha --timeout 10000 --recursive --require ts-node/register test/**/*.ts test/*.ts",
16-
"test:functional": "mocha --timeout 10000 --recursive --require ts-node/register test/Functional/*/*.ts",
17-
"test": "npm run test:unit && npm run test:functional"
16+
"test": "npm run test:unit"
1817
},
1918
"license": "MIT",
2019
"author": "Marc Morera <yuhu@mmoreram.com>",

src/Repository/HttpRepository.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,20 @@ export class HttpRepository extends Repository {
168168
} catch (response) {
169169
throw HttpRepository.createErrorFromResponse(response);
170170
}
171-
const result = Result.createFromArray(response.getBody());
171+
let result = Result.createFromArray(response.getBody());
172+
result = this.applyTransformersToResult(result);
173+
174+
/**
175+
* Custom transformation function
176+
*/
177+
if (
178+
typeof globalThis !== "undefined" &&
179+
typeof globalThis.apisearchItemsTransformation === "function"
180+
) {
181+
result.withItems(await globalThis.apisearchItemsTransformation(result.getItems()));
182+
}
172183

173-
return this.applyTransformersToResult(result);
184+
return result;
174185
}
175186

176187
/**

0 commit comments

Comments
 (0)