diff --git a/generators/client/files.js b/generators/client/files.js index 67277495..f9703b25 100644 --- a/generators/client/files.js +++ b/generators/client/files.js @@ -165,6 +165,7 @@ const vueFiles = { 'core/ribbon/ribbon.vue', 'core/ribbon/ribbon.component.ts', 'shared/date/filters.ts', + 'shared/query/params.ts', 'shared/sort/jhi-sort-indicator.component.ts', 'shared/sort/jhi-sort-indicator.vue', 'shared/sort/sorts.ts', diff --git a/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs b/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs new file mode 100644 index 00000000..877e58a6 --- /dev/null +++ b/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs @@ -0,0 +1,13 @@ +export default function buildQueryParams(queryParams) { + if (Array.isArray(queryParams) && queryParams.length > 0) { + let paramString = ''; + for (const param of queryParams) { + if (paramString.length > 0) { + paramString += '&'; + } + paramString += param; + } + return `${paramString}`; + } + return ''; +} diff --git a/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs b/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs index eac26a0d..d15529ae 100644 --- a/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs +++ b/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs @@ -7,7 +7,8 @@ export default function buildPaginationQueryOpts(paginationQuery) { } sorts += 'sort=' + paginationQuery.sort[idx]; } - return `${sorts}&page=${paginationQuery.page}&size=${paginationQuery.size}`; + const pagination = `page=${paginationQuery.page}&size=${paginationQuery.size}`; + return sorts.length > 0 ? `${sorts}&${pagination}` : `${pagination}`; } return ''; } diff --git a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs index 46243599..84ad998c 100755 --- a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs +++ b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs @@ -1,4 +1,5 @@ import axios from 'axios'; +import buildQueryParams from '@/shared/query/params'; <% if (pagination !== 'no') { %> import buildPaginationQueryOpts from '@/shared/sort/sorts'; <% } %> @@ -32,6 +33,18 @@ export default class <%= entityAngularName %>Service { }); } + public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise[]> { + return new Promise[]>((resolve, reject) => { + let queryString = `${buildQueryParams(queryParams)}`<% if (pagination !== 'no') { %> + `${buildPaginationQueryOpts(paginationQuery)}` <% } %> + if (queryString.length > 0) { + queryString = `?${queryString}` + } + axios.get(`${baseApiUrl}${queryString}`).then(function (res) { + resolve(res.data); + }).catch(err => { reject(err); }); + }); + } + public retrieve(<% if (pagination !== 'no') { %>paginationQuery?: any<% } %>) : Promise { return new Promise((resolve, reject) => { axios.get(baseApiUrl<% if (pagination !== 'no') { %> + `?${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { @@ -39,6 +52,7 @@ export default class <%= entityAngularName %>Service { }).catch(err => { reject(err); }); }); } + <%_ if (!readOnly) { _%> diff --git a/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs b/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs index 00373bfd..3d0e5458 100644 --- a/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs +++ b/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs @@ -262,6 +262,15 @@ describe('Service Tests', () => { }); }); + it('should return a filtered list of <%= entityAngularName %>', async () => { + const returnedFromService = Object.assign({}, elemDefault); + mockedAxios.get.mockReturnValue(Promise.resolve({ data: [returnedFromService] })); + + return service.query([]<% if (pagination !== 'no') { %>, { sort: {}, page: 0, size: 10 }<% } %>).then((res) => { + expect(res).toContainEqual(elemDefault); + }); + }); + <%_ if (!readOnly) { _%> it('should delete a <%= entityAngularName %>', async () => { mockedAxios.delete.mockReturnValue(Promise.resolve({ok: true}));