expose rest service options on generated proxy services as parameter.

pull/16207/head
Mahmut Gundogdu 3 years ago
parent feb02a8918
commit 6e07f5ae79

@ -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 "
]
}
}
}

@ -4,6 +4,7 @@ export namespace Rest {
export type Config = Partial<{
apiName: string;
skipHandleError: boolean;
skipAddingHeader: boolean;
observe: Observe;
httpParamEncoder?: HttpParameterCodec;
}>;

@ -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<R>(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]) => {

@ -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) {}
}

@ -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<string, Type>) {
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<Rest.Config>' });
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;
};

Loading…
Cancel
Save