From baa1206ce2351ee27b8dd7f56f825ee9f1d6e38d Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 2 Sep 2020 10:54:11 +0300 Subject: [PATCH 1/4] feat: update proxy command schema --- .../schematics/src/commands/api/index.ts | 4 ++-- .../src/commands/proxy-add/schema.json | 16 +++++++++++----- .../src/commands/proxy-refresh/schema.json | 16 +++++++++++----- .../src/commands/proxy-remove/schema.json | 16 +++++++++++----- .../src/models/generate-proxy-schema.ts | 7 +++++++ 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/index.ts b/npm/ng-packs/packages/schematics/src/commands/api/index.ts index 646f2170ea..7e73b1285f 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/api/index.ts @@ -10,7 +10,7 @@ import { url, } from '@angular-devkit/schematics'; import { Exception } from '../../enums'; -import { GenerateProxySchema, ServiceGeneratorParams } from '../../models'; +import { GenerateApiSchema, ServiceGeneratorParams } from '../../models'; import { applyWithOverwrite, buildDefaultPath, @@ -31,7 +31,7 @@ import { } from '../../utils'; import * as cases from '../../utils/text'; -export default function(schema: GenerateProxySchema) { +export default function(schema: GenerateApiSchema) { const params = removeDefaultPlaceholders(schema); const moduleName = strings.camelize(params.module || 'app'); diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy-add/schema.json b/npm/ng-packs/packages/schematics/src/commands/proxy-add/schema.json index f9e762c597..c817882730 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy-add/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/proxy-add/schema.json @@ -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\")" } diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy-refresh/schema.json b/npm/ng-packs/packages/schematics/src/commands/proxy-refresh/schema.json index f9e762c597..c817882730 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy-refresh/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/proxy-refresh/schema.json @@ -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\")" } diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy-remove/schema.json b/npm/ng-packs/packages/schematics/src/commands/proxy-remove/schema.json index f9e762c597..c817882730 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy-remove/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/proxy-remove/schema.json @@ -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\")" } diff --git a/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts b/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts index 59c4e6adab..4e58f355b1 100644 --- a/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts +++ b/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts @@ -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 */ @@ -14,3 +19,5 @@ export interface GenerateProxySchema { */ target?: string; } + +export type GenerateApiSchema = Omit; From 65b170e6ef5156b2558241b214ee8af0a6b607eb Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 2 Sep 2020 11:05:08 +0300 Subject: [PATCH 2/4] revert: use same schema in api command --- npm/ng-packs/packages/schematics/src/commands/api/index.ts | 4 ++-- .../packages/schematics/src/models/generate-proxy-schema.ts | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/index.ts b/npm/ng-packs/packages/schematics/src/commands/api/index.ts index 7e73b1285f..646f2170ea 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/api/index.ts @@ -10,7 +10,7 @@ import { url, } from '@angular-devkit/schematics'; import { Exception } from '../../enums'; -import { GenerateApiSchema, ServiceGeneratorParams } from '../../models'; +import { GenerateProxySchema, ServiceGeneratorParams } from '../../models'; import { applyWithOverwrite, buildDefaultPath, @@ -31,7 +31,7 @@ import { } from '../../utils'; import * as cases from '../../utils/text'; -export default function(schema: GenerateApiSchema) { +export default function(schema: GenerateProxySchema) { const params = removeDefaultPlaceholders(schema); const moduleName = strings.camelize(params.module || 'app'); diff --git a/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts b/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts index 4e58f355b1..5e305923c4 100644 --- a/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts +++ b/npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts @@ -19,5 +19,3 @@ export interface GenerateProxySchema { */ target?: string; } - -export type GenerateApiSchema = Omit; From d547b0b616db143167082d37bbf2273c53a11f5a Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 2 Sep 2020 11:05:46 +0300 Subject: [PATCH 3/4] feat: add api-name parameter to api schema.json --- .../schematics/src/commands/api/schema.json | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/schema.json b/npm/ng-packs/packages/schematics/src/commands/api/schema.json index 4411418c34..db58ed73ce 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/api/schema.json @@ -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": [] From 932e0f7f2b14bc1ff1648b8c632a85f0ff65fbef Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 2 Sep 2020 11:20:57 +0300 Subject: [PATCH 4/4] fix: use api-name parameter to resolve remote service --- .../schematics/src/commands/api/index.ts | 7 +-- .../schematics/src/enums/exception.ts | 4 +- .../packages/schematics/src/utils/source.ts | 52 +++++++++---------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/index.ts b/npm/ng-packs/packages/schematics/src/commands/api/index.ts index 646f2170ea..5254f816b6 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/api/index.ts @@ -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); diff --git a/npm/ng-packs/packages/schematics/src/enums/exception.ts b/npm/ng-packs/packages/schematics/src/enums/exception.ts index 98c92493d6..b267ad6674 100644 --- a/npm/ng-packs/packages/schematics/src/enums/exception.ts +++ b/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}".', 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.', } diff --git a/npm/ng-packs/packages/schematics/src/utils/source.ts b/npm/ng-packs/packages/schematics/src/utils/source.ts index d555e2ec1f..e1e965744f 100644 --- a/npm/ng-packs/packages/schematics/src/utils/source.ts +++ b/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 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, ''); }