fix: use api-name parameter to resolve remote service

pull/5264/head
Arman Ozak 5 years ago
parent d547b0b616
commit 932e0f7f2b

@ -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);

@ -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.',
}

@ -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