diff --git a/npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template b/npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template index 8200590bdf..1d6c9eb6ae 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template +++ b/npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template @@ -1,8 +1,6 @@ -import { RestService } from '@abp/ng.core'; -import { Injectable } from '@angular/core';<% -for (let _import of imports) { %> -import type { <%= _import.specifiers.join(', ') %> } from '<%= _import.path %>';<% } %> - +<% for (let _import of imports) { +%><%= _import.keyword %> { <%= _import.specifiers.join(', ') %> } from '<%= _import.path %>'; +<% } %> @Injectable({ providedIn: 'root', }) diff --git a/npm/ng-packs/packages/schematics/src/utils/service.ts b/npm/ng-packs/packages/schematics/src/utils/service.ts index 6bb5eff555..927e881f4e 100644 --- a/npm/ng-packs/packages/schematics/src/utils/service.ts +++ b/npm/ng-packs/packages/schematics/src/utils/service.ts @@ -16,11 +16,29 @@ export function createControllerToServiceMapper(solution: string, apiName: strin const namespace = parseNamespace(solution, controller.type); const actions = Object.values(controller.actions); const imports = actions.reduce(createActionToImportsReducer(solution, namespace), []); + imports.push(new Import({ path: '@abp/ng.core', specifiers: ['RestService'] })); + imports.push(new Import({ path: '@angular/core', specifiers: ['Injectable'] })); + sortImports(imports); const methods = actions.map(mapActionToMethod); + sortMethods(methods); return new Service({ apiName, imports, methods, name, namespace }); }; } +function sortImports(imports: Import[]) { + imports.sort((a, b) => + removeRelative(a) > removeRelative(b) ? 1 : a.keyword > b.keyword ? 1 : -1, + ); +} + +function removeRelative(importDef: Import) { + return importDef.path.replace(/\.\.\//g, ''); +} + +function sortMethods(methods: Method[]) { + methods.sort((a, b) => (a.signature.name > b.signature.name ? 1 : -1)); +} + export function createActionToImportsReducer(solution: string, namespace: string) { const mapTypeToImport = createTypeToImportMapper(solution, namespace); @@ -41,6 +59,7 @@ export function createActionToImportsReducer(solution: string, namespace: string ...new Set([...existingImport.specifiers, ...def.specifiers]), ].sort(); }); + return imports; }; }