Merge pull request #5264 from abpframework/fix/5258

Remote service name is resolved from api-name parameter in proxy generation
pull/5268/head
Bunyamin Coskuner 4 years ago committed by GitHub
commit 235263f8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,10 +19,10 @@ import {
createImportRefToEnumMapper,
createProxyConfigReader,
createProxyConfigWriterCreator,
createRootNamespaceGetter,
EnumGeneratorParams,
generateProxyConfigJson,
getEnumNamesFromImports,
getRootNamespace,
interpolate,
ModelGeneratorParams,
removeDefaultPlaceholders,
@ -37,9 +37,10 @@ export default function(schema: GenerateProxySchema) {
return chain([
async (tree: Tree, _context: SchematicContext) => {
const source = await resolveProject(tree, params.source!);
const getRootNamespace = createRootNamespaceGetter(params);
const solution = await getRootNamespace(tree);
const target = await resolveProject(tree, params.target!);
const solution = getRootNamespace(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition);
const readProxyConfig = createProxyConfigReader(targetPath);
const createProxyConfigWriter = createProxyConfigWriterCreator(targetPath);

@ -5,34 +5,40 @@
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"description": "Backend module name",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"source": {
"alias": "s",
"description": "Angular project to resolve root namespace & API definition URL from",
"api-name": {
"description": "Backend api name, a.k.a. remoteServiceName",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter Angular project name to resolve root namespace & API definition URL from. (default: workspace \"defaultProject\")"
"x-prompt": "Please enter backend api name, a.k.a. remoteServiceName. (default: \"default\")"
},
"target": {
"alias": "t",
"description": "Angular project to generate code in",
"source": {
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 3
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}
},
"required": []

@ -5,7 +5,6 @@
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module name",
"type": "string",
"$default": {
@ -14,23 +13,30 @@
},
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"api-name": {
"description": "Backend api name, a.k.a. remoteServiceName",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Please enter backend api name, a.k.a. remoteServiceName. (default: \"default\")"
},
"source": {
"alias": "s",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
"index": 2
},
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
"index": 3
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}

@ -5,7 +5,6 @@
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module name",
"type": "string",
"$default": {
@ -14,23 +13,30 @@
},
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"api-name": {
"description": "Backend api name, a.k.a. remoteServiceName",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Please enter backend api name, a.k.a. remoteServiceName. (default: \"default\")"
},
"source": {
"alias": "s",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
"index": 2
},
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
"index": 3
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}

@ -5,7 +5,6 @@
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module name",
"type": "string",
"$default": {
@ -14,23 +13,30 @@
},
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"api-name": {
"description": "Backend api name, a.k.a. remoteServiceName",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Please enter backend api name, a.k.a. remoteServiceName. (default: \"default\")"
},
"source": {
"alias": "s",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
"index": 2
},
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
"index": 3
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}

@ -11,6 +11,6 @@ export const enum Exception {
NoTypeDefinition = '[Type Definition Not Found] There is no type definition for "{0}".',
NoWorkspace = '[Workspace Not Found] Make sure you are running schematics at the root directory of your workspace and it has an angular.json file.',
NoEnvironment = '[Environment Not Found] An environment file cannot be located in "{0}" project.',
NoApiUrl = '[API URL Not Found] Cannot resolve API URL for "{1}" module from "{0}" project.',
NoRootNamespace = '[Root Namespace Not Found] Cannot resolve root namespace for "{1}" module from "{0}" project.',
NoApiUrl = '[API URL Not Found] Cannot resolve API URL for "{1}" remote service name from "{0}" project.',
NoRootNamespace = '[Root Namespace Not Found] Cannot resolve root namespace for "{1}" api from "{0}" project.',
}

@ -4,6 +4,11 @@ export interface GenerateProxySchema {
*/
module?: string;
/**
* Backend api name, a.k.a. remoteServiceName
*/
['api-name']?: string;
/**
* Source Angular project for API definition URL & root namespace resolution
*/

@ -1,4 +1,3 @@
import { strings } from '@angular-devkit/core';
import { SchematicsException, Tree } from '@angular-devkit/schematics';
import got from 'got';
import {
@ -15,11 +14,11 @@ import { interpolate } from './common';
import { readEnvironment, resolveProject } from './workspace';
export function createApiDefinitionGetter(params: GenerateProxySchema) {
const moduleName = strings.camelize(params.module || 'app');
const apiName = params['api-name'] || 'default';
return async (host: Tree) => {
const source = await resolveProject(host, params.source!);
const sourceUrl = getSourceUrl(host, source, moduleName);
const sourceUrl = getSourceUrl(host, source, apiName);
return await getApiDefinition(sourceUrl);
};
}
@ -36,7 +35,7 @@ async function getApiDefinition(sourceUrl: string) {
}));
} catch ({ response }) {
// handle redirects
if (response.statusCode >= 400 || !response?.body)
if (!response?.body || response.statusCode >= 400)
throw new SchematicsException(Exception.NoApi);
body = response.body;
@ -45,48 +44,49 @@ async function getApiDefinition(sourceUrl: string) {
return body;
}
export function getRootNamespace(tree: Tree, project: Project, moduleName: string) {
const environmentExpr = readEnvironment(tree, project.definition);
export function createRootNamespaceGetter(params: GenerateProxySchema) {
const apiName = params['api-name'] || 'default';
if (!environmentExpr)
throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name));
return async (tree: Tree) => {
const project = await resolveProject(tree, params.source!);
const environmentExpr = readEnvironment(tree, project.definition);
let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
moduleName,
'rootNamespace',
]);
if (!environmentExpr)
throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name));
if (!assignment)
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
'default',
apiName,
'rootNamespace',
]);
if (!assignment)
throw new SchematicsException(interpolate(Exception.NoRootNamespace, project.name, moduleName));
if (!assignment)
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
'default',
'rootNamespace',
]);
return assignment.replace(/[`'"]/g, '');
if (!assignment)
throw new SchematicsException(interpolate(Exception.NoRootNamespace, project.name, apiName));
return assignment.replace(/[`'"]/g, '');
};
}
export function getSourceUrl(tree: Tree, project: Project, moduleName: string) {
export function getSourceUrl(tree: Tree, project: Project, apiName: string) {
const environmentExpr = readEnvironment(tree, project.definition);
if (!environmentExpr)
throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name));
let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
moduleName,
'url',
]);
let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', apiName, 'url']);
if (!assignment)
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', 'default', 'url']);
if (!assignment)
throw new SchematicsException(interpolate(Exception.NoApiUrl, project.name, moduleName));
throw new SchematicsException(interpolate(Exception.NoApiUrl, project.name, apiName));
return assignment.replace(/[`'"]/g, '');
}

Loading…
Cancel
Save