Browse Source

Merge pull request #8015 from abpframework/feat/8014

Improved proxy generated code to avoid TS Lint errors
pull/8029/head
Mehmet Erim 6 years ago
committed by GitHub
parent
commit
454c14b719
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template
  2. 16
      npm/ng-packs/packages/schematics/src/models/method.ts
  3. 6
      npm/ng-packs/packages/schematics/src/utils/enum.ts
  4. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  5. 4
      npm/ng-packs/packages/schematics/src/utils/model.ts
  6. 9
      npm/ng-packs/packages/schematics/src/utils/prop.ts

2
npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template

@ -13,7 +13,7 @@ export class <%= name %>Service {
method: '<%= body.method %>',<%
if (body.responseType === 'string') { %>
responseType: 'text',<% } %>
url: `/<%= body.url %>`,<%
url: <%= body.url %>,<%
if (body.params.length) { %>
params: { <%= body.params.join(', ') %> },<% }
if (body.body) { %>

16
npm/ng-packs/packages/schematics/src/models/method.ts

@ -1,5 +1,6 @@
import { eBindingSourceId, eMethodModifier } from '../enums';
import { camel } from '../utils';
import { isValidProp } from '../utils/prop';
import { camel } from '../utils/text';
import { ParameterInBody } from './api-definition';
import { Property } from './model';
import { Omissible } from './util';
@ -44,12 +45,16 @@ export class Body {
const { bindingSourceId, descriptorName, jsonName, name, nameOnMethod } = param;
const camelName = camel(name);
const paramName = jsonName || camelName;
const value = descriptorName ? `${descriptorName}.${paramName}` : nameOnMethod;
const value = descriptorName
? isValidProp(paramName)
? `${descriptorName}.${paramName}`
: `${descriptorName}['${paramName}']`
: nameOnMethod;
switch (bindingSourceId) {
case eBindingSourceId.Model:
case eBindingSourceId.Query:
this.params.push(`${paramName}: ${value}`);
this.params.push(paramName === value ? value : `${paramName}: ${value}`);
break;
case eBindingSourceId.Body:
this.body = value;
@ -65,6 +70,11 @@ export class Body {
constructor(options: BodyOptions) {
Object.assign(this, options);
this.setUrlQuotes();
}
private setUrlQuotes() {
this.url = /{/.test(this.url) ? `\`/${this.url}\`` : `'/${this.url}'`;
}
}

6
npm/ng-packs/packages/schematics/src/utils/enum.ts

@ -3,6 +3,7 @@ import { Exception } from '../enums';
import { Type } from '../models';
import { interpolate } from './common';
import { parseNamespace } from './namespace';
import { isValidProp } from './prop';
export interface EnumGeneratorParams {
targetPath: string;
@ -32,7 +33,10 @@ export function createImportRefToEnumMapper({ solution, types }: EnumGeneratorPa
throw new SchematicsException(interpolate(Exception.NoTypeDefinition, ref));
const namespace = parseNamespace(solution, ref);
const members = enumNames!.map((key, i) => ({ key, value: enumValues[i] }));
const members = enumNames!.map((key, i) => ({
key: isValidProp(key) ? key : `'${key}'`,
value: enumValues[i],
}));
return {
namespace,

1
npm/ng-packs/packages/schematics/src/utils/index.ts

@ -10,6 +10,7 @@ export * from './import';
export * from './model';
export * from './namespace';
export * from './path';
export * from './prop';
export * from './rule';
export * from './service';
export * from './source';

4
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -8,6 +8,7 @@ import {
} from './generics';
import { parseNamespace } from './namespace';
import { relativePathToModel } from './path';
import { isValidProp } from './prop';
import { camel } from './text';
import { parseGenerics } from './tree';
import {
@ -134,7 +135,8 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP
genericsCollector.reset();
typeDef.properties?.forEach(prop => {
const name = prop.jsonName || camel(prop.name);
let name = prop.jsonName || camel(prop.name);
name = isValidProp(name) ? name : `'${name}'`;
const type = simplifyType(prop.typeSimple);
const refs = parseType(prop.type).reduce(
(acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()),

9
npm/ng-packs/packages/schematics/src/utils/prop.ts

@ -0,0 +1,9 @@
export const isValidProp = (name: string) => {
try {
new Function('return {}.' + name);
} catch (_) {
return false;
}
return true;
};
Loading…
Cancel
Save