diff --git a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template b/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template index 1d77599e7e..78fb27c33d 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template +++ b/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) { %> diff --git a/npm/ng-packs/packages/schematics/src/models/method.ts b/npm/ng-packs/packages/schematics/src/models/method.ts index 782128540c..96db013c8f 100644 --- a/npm/ng-packs/packages/schematics/src/models/method.ts +++ b/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}'`; } } diff --git a/npm/ng-packs/packages/schematics/src/utils/enum.ts b/npm/ng-packs/packages/schematics/src/utils/enum.ts index c01b086ec5..5ff644db82 100644 --- a/npm/ng-packs/packages/schematics/src/utils/enum.ts +++ b/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, diff --git a/npm/ng-packs/packages/schematics/src/utils/index.ts b/npm/ng-packs/packages/schematics/src/utils/index.ts index e6df05b2e2..a33218d1ef 100644 --- a/npm/ng-packs/packages/schematics/src/utils/index.ts +++ b/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'; diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index 9c2b7cfae8..96856fdbe4 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/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()), diff --git a/npm/ng-packs/packages/schematics/src/utils/prop.ts b/npm/ng-packs/packages/schematics/src/utils/prop.ts new file mode 100644 index 0000000000..0c78de7b14 --- /dev/null +++ b/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; +};