Browse Source

fix: use api-name parameter to resolve remote service

pull/5264/head
Arman Ozak 6 years ago
parent
commit
932e0f7f2b
  1. 7
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  2. 4
      npm/ng-packs/packages/schematics/src/enums/exception.ts
  3. 52
      npm/ng-packs/packages/schematics/src/utils/source.ts

7
npm/ng-packs/packages/schematics/src/commands/api/index.ts

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

4
npm/ng-packs/packages/schematics/src/enums/exception.ts

@ -11,6 +11,6 @@ export const enum Exception {
NoTypeDefinition = '[Type Definition Not Found] There is no type definition for "{0}".', 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.', 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.', 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.', 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}" module from "{0}" project.', NoRootNamespace = '[Root Namespace Not Found] Cannot resolve root namespace for "{1}" api from "{0}" project.',
} }

52
npm/ng-packs/packages/schematics/src/utils/source.ts

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

Loading…
Cancel
Save