diff --git a/npm/ng-packs/apps/dev-app/src/environments/environment.ts b/npm/ng-packs/apps/dev-app/src/environments/environment.ts index 2bf42cf7dc..d8066be1cc 100644 --- a/npm/ng-packs/apps/dev-app/src/environments/environment.ts +++ b/npm/ng-packs/apps/dev-app/src/environments/environment.ts @@ -21,5 +21,8 @@ export const environment = { default: { url: 'https://localhost:44305', }, + saas: { + url: 'https://localhost:44305', + }, }, } as Config.Environment; diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy/index.ts b/npm/ng-packs/packages/schematics/src/commands/proxy/index.ts index 675b268d10..45f3997176 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/proxy/index.ts @@ -1,15 +1,29 @@ +import { strings } from '@angular-devkit/core'; import { chain, SchematicContext, Tree } from '@angular-devkit/schematics'; +import { API_DEFINITION_ENDPOINT } from '../../constants'; +import { ApiDefinition } from '../../models'; import { getSourceJson, getSourceUrl, resolveProject } from '../../utils'; import type { Schema as GenerateProxySchema } from './schema'; export default function(params: GenerateProxySchema) { + const moduleName = strings.camelize(params.module || 'app'); + return chain([ async (tree: Tree, _context: SchematicContext) => { - const project = await resolveProject(tree, params.module!); - const url = params.source || getSourceUrl(tree, project.definition) + '/api/abp/api-definition'; - const data = await getSourceJson(url); + const source = await resolveProject(tree, params.source!); + const url = getSourceUrl(tree, source, moduleName); + const data: ApiDefinition = await getSourceJson(url + API_DEFINITION_ENDPOINT); + + const services = Object.entries(data.modules).map(([name, def]) => [ + name, + Object.values(def.controllers).map( + ({controllerName, actions}) => [controllerName, Object.keys(actions)] + ), + ]); + + const defs = services.filter(([name]) => name === moduleName); - console.log(Object.keys(data.types)); + console.log(defs); return chain([]); }, ]); diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy/schema.json b/npm/ng-packs/packages/schematics/src/commands/proxy/schema.json index 492b52dad7..8ba6708836 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/proxy/schema.json @@ -6,36 +6,29 @@ "properties": { "module": { "alias": "m", - "description": "The name of the module to generate code for", + "description": "The name of the backend module to generate code for", "type": "string", "$default": { "$source": "argv", "index": 0 }, - "x-prompt": "Please enter name of the module you wish to generate proxies for. (default: \"app\")" + "x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")" }, "source": { "alias": "s", - "description": "The URL to get API definitions from", + "description": "Angular project to resolve API definition URL from", "type": "string", "$default": { "$source": "argv", "index": 1 }, - "x-prompt": "Plese enter URL to get API definitions from. (default: resolved from environment)" + "x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")" }, - "project": { - "alias": "p", - "description": "The project to place the generated code in", + "destination": { + "alias": "d", + "description": "Angular project to place the generated code in", "type": "string", - "x-prompt": "Please enter the project to place proxies in. (default: workspace \"defaultProject\")" - }, - "path": { - "description": "The path to place the generated code at", - "type": "string", - "format": "path", - "default": "", - "visible": false + "x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")" } }, "required": [] diff --git a/npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts b/npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts index 4e41904a46..0227b17e69 100644 --- a/npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts +++ b/npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts @@ -1,21 +1,16 @@ export interface Schema { /** - * The name of the module to generate code for + * Angular project to place the generated code in */ - module?: string; + destination?: string; /** - * The project to place the generated code in + * The name of the backend module to generate code for */ - project?: string; - - /** - * The path to place the generated code at - */ - path?: string; + module?: string; /** - * The URL to get API configuration from + * Angular project to resolve API definition URL from */ source?: string; } diff --git a/npm/ng-packs/packages/schematics/src/constants/api.ts b/npm/ng-packs/packages/schematics/src/constants/api.ts new file mode 100644 index 0000000000..fdd0c05ded --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/constants/api.ts @@ -0,0 +1 @@ +export const API_DEFINITION_ENDPOINT = '/api/abp/api-definition'; diff --git a/npm/ng-packs/packages/schematics/src/constants/index.ts b/npm/ng-packs/packages/schematics/src/constants/index.ts new file mode 100644 index 0000000000..b1c13e7340 --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/constants/index.ts @@ -0,0 +1 @@ +export * from './api'; diff --git a/npm/ng-packs/packages/schematics/src/enums/exception.ts b/npm/ng-packs/packages/schematics/src/enums/exception.ts index 234457f037..92f73b7ef4 100644 --- a/npm/ng-packs/packages/schematics/src/enums/exception.ts +++ b/npm/ng-packs/packages/schematics/src/enums/exception.ts @@ -1,8 +1,9 @@ export const enum Exception { FileNotFound = '[File Not Found] There is no file at "{0}" path.', InvalidWorkspace = '[Invalid Workspace] The angular.json should be a valid JSON file.', - NoApi = '[API Not Available] Please double-check the source url and make sure your application is up and running.', + NoApi = '[API Not Available] Please double-check the URL in the source project environment and make sure your application is up and running.', NoProject = '[Project Not Found] Either define a default project in your workspace or specify the project name in schematics options.', NoWorkspace = '[Workspace Not Found] Make sure you are running schematics at the root directory of your workspace and it has an angular.json file.', - RequiredApiUrl = '[API URL Required] API URL cannot be resolved. Please re-run the schematics and enter the URL to get API definitions from.', + 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.', } diff --git a/npm/ng-packs/packages/schematics/src/models/index.ts b/npm/ng-packs/packages/schematics/src/models/index.ts index a4e9855273..4b97b395c6 100644 --- a/npm/ng-packs/packages/schematics/src/models/index.ts +++ b/npm/ng-packs/packages/schematics/src/models/index.ts @@ -1 +1,2 @@ export * from './api-definition'; +export * from './project'; diff --git a/npm/ng-packs/packages/schematics/src/models/project.ts b/npm/ng-packs/packages/schematics/src/models/project.ts new file mode 100644 index 0000000000..f4794719aa --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/models/project.ts @@ -0,0 +1,6 @@ +import { workspaces } from '@angular-devkit/core'; + +export interface Project { + name: string; + definition: workspaces.ProjectDefinition; +} diff --git a/npm/ng-packs/packages/schematics/src/utils/source.ts b/npm/ng-packs/packages/schematics/src/utils/source.ts index b1411b6bca..cc1d45cebd 100644 --- a/npm/ng-packs/packages/schematics/src/utils/source.ts +++ b/npm/ng-packs/packages/schematics/src/utils/source.ts @@ -1,8 +1,9 @@ -import type { workspaces } from '@angular-devkit/core'; import { SchematicsException, Tree } from '@angular-devkit/schematics'; import got from 'got'; import { Exception } from '../enums'; +import { Project } from '../models'; import { getAssignedPropertyFromObjectliteral } from './ast'; +import { interpolate } from './common'; import { readEnvironment } from './workspace'; export async function getSourceJson(url: string) { @@ -14,28 +15,33 @@ export async function getSourceJson(url: string) { searchParams: { includeTypes: true }, https: { rejectUnauthorized: false }, })); - } catch (err) { + } catch ({ response }) { // handle redirects - try { - ({ response: { body } } = err); - if (!body.types) throw Error(''); - } catch (_) { - throw new SchematicsException(Exception.NoApi); - } + if (response?.body && response.statusCode < 400) return response.body; + + throw new SchematicsException(Exception.NoApi); } return body; } -export function getSourceUrl(tree: Tree, projectDefinition: workspaces.ProjectDefinition) { - const environmentExpr = readEnvironment(tree, projectDefinition); - let assignment: string | undefined; +export function getSourceUrl(tree: Tree, project: Project, moduleName: string) { + const environmentExpr = readEnvironment(tree, project.definition); + + if (!environmentExpr) + throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name)); - if (environmentExpr) { + let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [ + 'apis', + moduleName, + 'url', + ]); + + if (!assignment) assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', 'default', 'url']); - } - if (!assignment) throw new SchematicsException(Exception.RequiredApiUrl); + if (!assignment) + throw new SchematicsException(interpolate(Exception.NoApiUrl, project.name, moduleName)); return assignment.replace(/[`'"]/g, ''); } diff --git a/npm/ng-packs/packages/schematics/src/utils/workspace.ts b/npm/ng-packs/packages/schematics/src/utils/workspace.ts index ccae346835..1bdbc7181f 100644 --- a/npm/ng-packs/packages/schematics/src/utils/workspace.ts +++ b/npm/ng-packs/packages/schematics/src/utils/workspace.ts @@ -2,6 +2,7 @@ import type { experimental, workspaces } from '@angular-devkit/core'; import { SchematicsException } from '@angular-devkit/schematics'; import type { Tree } from '@angular-devkit/schematics'; import { Exception } from '../enums'; +import { Project } from '../models'; import { getWorkspace, ProjectType } from './angular'; import { findEnvironmentExpression } from './ast'; import { readFileInTree } from './common'; @@ -37,14 +38,10 @@ export function readWorkspaceSchema(tree: Tree) { export async function resolveProject( tree: Tree, name: string, -): Promise<{ name: string; definition: workspaces.ProjectDefinition }> { +): Promise { + name = name || readWorkspaceSchema(tree).defaultProject!; const workspace = await getWorkspace(tree); - let definition = workspace.projects.get(name); - - if (!definition) { - name = readWorkspaceSchema(tree).defaultProject!; - definition = workspace.projects.get(name); - } + const definition = workspace.projects.get(name); if (!definition) throw new SchematicsException(Exception.NoProject);