From 6e07f5ae798c364c938385f8ec3bf46438493ac4 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Fri, 7 Apr 2023 19:03:54 +0300 Subject: [PATCH] expose rest service options on generated proxy services as parameter. --- npm/ng-packs/package.json | 6 ++--- .../packages/core/src/lib/models/rest.ts | 1 + .../core/src/lib/services/rest.service.ts | 9 +++++++- .../__name@kebab__.service.ts.template | 2 +- .../packages/schematics/src/utils/service.ts | 22 ++++++++++++------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/npm/ng-packs/package.json b/npm/ng-packs/package.json index 7f1bdd633b..5d95c63991 100644 --- a/npm/ng-packs/package.json +++ b/npm/ng-packs/package.json @@ -34,8 +34,8 @@ "build:schematics": "cd scripts && yarn && yarn build:schematics && cd ..", "dev:schematics": "tsc -p packages/schematics/tsconfig.json -w", "mock:schematics": "cd scripts/mock-schematic && yarn && yarn start", - "debug:schematics": "./node_modules/.bin/ng g ./packages/schematics/src/collection.json:proxy-add --module __default --apiName __default --source __default --target __default --url https://localhost:44305 --serviceType application", - "debug:schematics-dist": "./node_modules/.bin/ng g ./dist/packages/schematics/collection.json:proxy-add --module __default --apiName __default --source __default --target __default --url http://localhost:4300 --service-type application", + "debug:schematics": "./node_modules/.bin/ng g ./packages/schematics/src/collection.json:proxy-add --module __default --apiName __default --source __default --target __default --url https://localhost:44305 --serviceType application --entryPoint __default ", + "debug:schematics-dist": "./node_modules/.bin/ng g ./dist/packages/schematics/collection.json:proxy-add --module __default --apiName __default --source __default --target __default --url http://localhost:4300 --service-type application --entryPoint __default", "ci": "yarn affected:lint && yarn affected:build && yarn affected:test", "lerna": "lerna", "migrate-nx": "yarn nx migrate --run-migrations", @@ -139,4 +139,4 @@ "npx prettier --write --config .prettierrc " ] } -} \ No newline at end of file +} diff --git a/npm/ng-packs/packages/core/src/lib/models/rest.ts b/npm/ng-packs/packages/core/src/lib/models/rest.ts index c5517ffa98..74cc3407f0 100644 --- a/npm/ng-packs/packages/core/src/lib/models/rest.ts +++ b/npm/ng-packs/packages/core/src/lib/models/rest.ts @@ -4,6 +4,7 @@ export namespace Rest { export type Config = Partial<{ apiName: string; skipHandleError: boolean; + skipAddingHeader: boolean; observe: Observe; httpParamEncoder?: HttpParameterCodec; }>; diff --git a/npm/ng-packs/packages/core/src/lib/services/rest.service.ts b/npm/ng-packs/packages/core/src/lib/services/rest.service.ts index e9a6e89851..6b865cede9 100644 --- a/npm/ng-packs/packages/core/src/lib/services/rest.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/rest.service.ts @@ -2,6 +2,7 @@ import { HttpClient, HttpParameterCodec, HttpParams, HttpRequest } from '@angula import { Inject, Injectable } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; +import { ExternalHttpClient } from '../clients/http.client'; import { ABP } from '../models/common'; import { Rest } from '../models/rest'; import { CORE_OPTIONS } from '../tokens/options.token'; @@ -16,6 +17,7 @@ export class RestService { constructor( @Inject(CORE_OPTIONS) protected options: ABP.Root, protected http: HttpClient, + protected externalHttp: ExternalHttpClient, protected environment: EnvironmentService, protected httpErrorReporter: HttpErrorReporterService, ) {} @@ -39,7 +41,9 @@ export class RestService { const { method, params, ...options } = request; const { observe = Rest.Observe.Body, skipHandleError } = config; const url = this.removeDuplicateSlashes(api + request.url); - return this.http + + const httpClient: HttpClient = this.getHttpClient(config.skipAddingHeader); + return httpClient .request(method, url, { observe, ...(params && { @@ -49,6 +53,9 @@ export class RestService { } as any) .pipe(catchError(err => (skipHandleError ? throwError(err) : this.handleError(err)))); } + private getHttpClient(isExternal: boolean) { + return isExternal ? this.http : this.externalHttp; + } private getParams(params: Rest.Params, encoder?: HttpParameterCodec): HttpParams { const filteredParams = Object.entries(params).reduce((acc, [key, value]) => { diff --git a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template b/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template index 1e1cc4564a..aae3b43471 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template +++ b/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template @@ -25,7 +25,7 @@ export class <%= name %>Service { if (body.body) { %> body: <%= body.body %>,<% } %> }, - { apiName: this.apiName });<% } %> + { apiName: this.apiName,...config });<% } %> constructor(private restService: RestService) {} } diff --git a/npm/ng-packs/packages/schematics/src/utils/service.ts b/npm/ng-packs/packages/schematics/src/utils/service.ts index 28ce82e3f7..0b633a8dd5 100644 --- a/npm/ng-packs/packages/schematics/src/utils/service.ts +++ b/npm/ng-packs/packages/schematics/src/utils/service.ts @@ -22,7 +22,7 @@ import { } from './type'; import { eBindingSourceId } from '../enums'; import { camelizeHyphen } from './text'; -import {VOLO_REMOTE_STREAM_CONTENT} from "../constants"; +import { VOLO_REMOTE_STREAM_CONTENT } from '../constants'; export function serializeParameters(parameters: Property[]) { return parameters.map(p => p.name + p.optional + ': ' + p.type + p.default, '').join(', '); @@ -39,9 +39,12 @@ export function createControllerToServiceMapper({ const name = controller.controllerName; const namespace = parseNamespace(solution, controller.type); const actions = Object.values(controller.actions); - const typeWithoutIRemoteStreamContent = getTypesWithoutIRemoteStreamContent(types) - const imports = actions.reduce(createActionToImportsReducer(solution, typeWithoutIRemoteStreamContent, namespace), []); - imports.push(new Import({ path: '@abp/ng.core', specifiers: ['RestService'] })); + const typeWithoutIRemoteStreamContent = getTypesWithoutIRemoteStreamContent(types); + const imports = actions.reduce( + createActionToImportsReducer(solution, typeWithoutIRemoteStreamContent, namespace), + [], + ); + imports.push(new Import({ path: '@abp/ng.core', specifiers: ['RestService', 'Rest'] })); imports.push(new Import({ path: '@angular/core', specifiers: ['Injectable'] })); sortImports(imports); const methods = actions.map(mapActionToMethod); @@ -51,9 +54,9 @@ export function createControllerToServiceMapper({ } function getTypesWithoutIRemoteStreamContent(types: Record) { - const newType = {...types} - delete newType[VOLO_REMOTE_STREAM_CONTENT] - return newType + const newType = { ...types }; + delete newType[VOLO_REMOTE_STREAM_CONTENT]; + return newType; } function sortMethods(methods: Method[]) { methods.sort((a, b) => (a.signature.name > b.signature.name ? 1 : -1)); @@ -76,7 +79,7 @@ export function createActionToBodyMapper() { return ({ httpMethod, parameters, returnValue, url }: Action) => { const responseType = adaptType(returnValue.typeSimple); const responseTypeWithNamespace = returnValue.typeSimple; - const body = new Body({ method: httpMethod, responseType, url ,responseTypeWithNamespace}); + const body = new Body({ method: httpMethod, responseType, url, responseTypeWithNamespace }); parameters.forEach(body.registerActionParameter); @@ -90,6 +93,8 @@ export function createActionToSignatureMapper() { return (action: Action) => { const signature = new Signature({ name: getMethodNameFromAction(action) }); const versionParameter = getVersionParameter(action); + const restConfig = new Property({ name: 'config', type: 'Partial' }); + restConfig.setOptional(true); const parameters = [ ...action.parametersOnMethod, ...(versionParameter ? [versionParameter] : []), @@ -101,6 +106,7 @@ export function createActionToSignatureMapper() { parameter.setOptional(p.isOptional); return parameter; }); + signature.parameters.push(restConfig); return signature; };