Skip to content

Commit ab41286

Browse files
committed
Merge branch 'next-38358/auto-imported-from-github' into 'trunk'
NEXT-38358 - Allow admin-search to get aborted by new request See merge request shopware/6/product/platform!14801
2 parents 98d1965 + 3bd06c1 commit ab41286

File tree

5 files changed

+112
-5
lines changed

5 files changed

+112
-5
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Allow admin-search to get aborted by new request
3+
issue: NEXT-38358
4+
author: Benjamin Wittwer
5+
author_email: benjamin.wittwer@a-k-f.de
6+
author_github: akf-bw
7+
---
8+
# Administration
9+
* Changed `elastic` function in `search.api.service` to abort the previous search on function call
10+
* Changed `searchQuery` function in `search.api.service` to abort the previous search on function call

src/Administration/Resources/app/administration/src/core/factory/http.factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function storeSessionExpiredInterceptor(client) {
330330
'FRAMEWORK__STORE_SHOP_SECRET_INVALID',
331331
];
332332

333-
if (response.status === 403 && errorCodes.includes(code)) {
333+
if (response?.status === 403 && errorCodes.includes(code)) {
334334
if (typeof config.storeSessionRequestRetries === 'number') {
335335
config.storeSessionRequestRetries += 1;
336336
} else {

src/Administration/Resources/app/administration/src/core/service/api/search.api.service.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { CanceledError } from 'axios';
12
import ApiService from '../api.service';
23

34
/**
4-
* Gateway for the API end point 'product'
5+
* Gateway for the API end point 'search'
56
* @class
67
* @extends ApiService
78
*/
89
class SearchApiService extends ApiService {
10+
searchAbortController = null;
11+
912
constructor(httpClient, loginService, apiEndpoint = '_admin') {
1013
super(httpClient, loginService, apiEndpoint);
1114
this.name = 'searchService';
@@ -14,10 +17,25 @@ class SearchApiService extends ApiService {
1417
elastic(term, entities, limit, additionalHeaders = {}) {
1518
const headers = this.getBasicHeaders(additionalHeaders);
1619

20+
if (this.searchAbortController && !this.searchAbortController.signal.aborted) {
21+
this.searchAbortController.abort();
22+
}
23+
24+
this.searchAbortController = new AbortController();
25+
1726
return this.httpClient
18-
.post(`${this.getApiBasePath()}/es-search`, { term, limit, entities }, { headers })
27+
.post(`${this.getApiBasePath()}/es-search`, { term, limit, entities }, {
28+
headers,
29+
signal: this.searchAbortController.signal,
30+
})
1931
.then((response) => {
2032
return ApiService.handleResponse(response);
33+
})
34+
.catch((error) => {
35+
if (error instanceof CanceledError) {
36+
return {};
37+
}
38+
throw error;
2139
});
2240
}
2341

@@ -35,10 +53,25 @@ class SearchApiService extends ApiService {
3553
}
3654
});
3755

56+
if (this.searchAbortController && !this.searchAbortController.signal.aborted) {
57+
this.searchAbortController.abort();
58+
}
59+
60+
this.searchAbortController = new AbortController();
61+
3862
return this.httpClient
39-
.post(`${this.getApiBasePath()}/search`, queries, { headers })
63+
.post(`${this.getApiBasePath()}/search`, queries, {
64+
headers,
65+
signal: this.searchAbortController.signal,
66+
})
4067
.then((response) => {
4168
return ApiService.handleResponse(response);
69+
})
70+
.catch((error) => {
71+
if (error instanceof CanceledError) {
72+
return {};
73+
}
74+
throw error;
4275
});
4376
}
4477
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import createLoginService from 'src/core/service/login.service';
2+
import createHTTPClient from 'src/core/factory/http.factory';
3+
import MockAdapter from 'axios-mock-adapter';
4+
import SearchApiService from './search.api.service';
5+
6+
function getSearchApiService() {
7+
const client = createHTTPClient();
8+
const clientMock = new MockAdapter(client);
9+
const loginService = createLoginService(client, Shopware.Context.api);
10+
11+
const searchApiService = new SearchApiService(client, loginService);
12+
return { searchApiService, clientMock };
13+
}
14+
15+
describe('searchApiService', () => {
16+
it('is registered correctly', async () => {
17+
const { searchApiService } = getSearchApiService();
18+
expect(searchApiService).toBeInstanceOf(SearchApiService);
19+
});
20+
21+
it('is request elastic send correctly', async () => {
22+
const { searchApiService, clientMock } = getSearchApiService();
23+
24+
clientMock.onPost('/_admin/es-search')
25+
.reply(
26+
200,
27+
{ data: 'foo' },
28+
);
29+
30+
const response = await searchApiService.elastic('bar', [], 10);
31+
32+
expect(response.data).toBe('foo');
33+
});
34+
35+
it('is request searchQuery send correctly', async () => {
36+
const { searchApiService, clientMock } = getSearchApiService();
37+
38+
clientMock.onPost('/_admin/search')
39+
.reply(
40+
200,
41+
{ data: 'foo' },
42+
);
43+
44+
const response = await searchApiService.searchQuery({});
45+
46+
expect(response.data).toBe('foo');
47+
});
48+
49+
it('is request aborted correctly', async () => {
50+
const { searchApiService, clientMock } = getSearchApiService();
51+
52+
clientMock.onPost('/_admin/search')
53+
.reply(
54+
200,
55+
{ data: 'foo' },
56+
);
57+
58+
const response = searchApiService.searchQuery({});
59+
searchApiService.searchAbortController.abort();
60+
61+
expect(await response).toEqual({});
62+
expect(searchApiService.searchAbortController).toBeInstanceOf(AbortController);
63+
expect(searchApiService.searchAbortController.signal.aborted).toBeTruthy();
64+
});
65+
});

src/Administration/Resources/app/administration/src/meta/baseline.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ const missingTests = [
214214
'src/core/service/api/recommendations.api.service.js',
215215
'src/core/service/api/sales-channel.api.service.js',
216216
'src/core/service/api/scheduled-task.api.service.ts',
217-
'src/core/service/api/search.api.service.js',
218217
'src/core/service/api/seo-url-template.api.service.js',
219218
'src/core/service/api/seo-url.api.service.js',
220219
'src/core/service/api/snippet-set.api.service.js',

0 commit comments

Comments
 (0)