Browse Source

Merge pull request #12330 from abpframework/feat/11969

proxy generation api versioning imp
pull/12395/head
Mahmut Gundogdu 4 years ago
committed by GitHub
parent
commit
c5502dbba4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      npm/ng-packs/packages/schematics/src/models/method.ts
  2. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  3. 4
      npm/ng-packs/packages/schematics/src/utils/methods.ts
  4. 44
      npm/ng-packs/packages/schematics/src/utils/service.ts
  5. 3
      npm/ng-packs/packages/schematics/src/utils/text.ts

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

@ -1,5 +1,6 @@
import { eBindingSourceId, eMethodModifier } from '../enums';
import { camel } from '../utils/text';
import { camel, camelizeHyphen } from '../utils/text';
import { getParamName } from '../utils/methods';
import { ParameterInBody } from './api-definition';
import { Property } from './model';
import { Omissible } from './util';
@ -46,16 +47,17 @@ export class Body {
const { bindingSourceId, descriptorName, jsonName, name, nameOnMethod } = param;
const camelName = camel(name);
const paramName = jsonName || camelName;
const value = descriptorName
? shouldQuote(paramName)
let value = camelizeHyphen(nameOnMethod);
if (descriptorName) {
value = shouldQuote(paramName)
? `${descriptorName}['${paramName}']`
: `${descriptorName}.${paramName}`
: nameOnMethod;
: `${descriptorName}.${paramName}`;
}
switch (bindingSourceId) {
case eBindingSourceId.Model:
case eBindingSourceId.Query:
this.params.push(paramName === value ? value : `${paramName}: ${value}`);
this.params.push(paramName === value ? value : `${getParamName(paramName)}: ${value}`);
break;
case eBindingSourceId.Body:
this.body = value;

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

@ -7,6 +7,7 @@ export * from './enum';
export * from './file';
export * from './generics';
export * from './import';
export * from './methods';
export * from './model';
export * from './namespace';
export * from './path';

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

@ -0,0 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const shouldQuote = require('should-quote');
export const getParamName = (paramName: string) =>
shouldQuote(paramName) ? `["${paramName}"]` : paramName;

44
npm/ng-packs/packages/schematics/src/utils/service.ts

@ -20,6 +20,8 @@ import {
createTypesToImportsReducer,
removeTypeModifiers,
} from './type';
import { eBindingSourceId } from '../enums';
import { camelizeHyphen } from './text';
export function serializeParameters(parameters: Property[]) {
return parameters.map(p => p.name + p.optional + ': ' + p.type + p.default, '').join(', ');
@ -79,8 +81,12 @@ export function createActionToSignatureMapper() {
return (action: Action) => {
const signature = new Signature({ name: getMethodNameFromAction(action) });
signature.parameters = action.parametersOnMethod.map(p => {
const versionParameter = getVersionParameter(action);
const parameters = [
...action.parametersOnMethod,
...(versionParameter ? [versionParameter] : []),
];
signature.parameters = parameters.map(p => {
const type = adaptType(p.typeSimple);
const parameter = new Property({ name: p.name, type });
parameter.setDefault(p.defaultValue);
@ -96,6 +102,40 @@ function getMethodNameFromAction(action: Action): string {
return action.uniqueName.split('Async')[0];
}
function getVersionParameter(action: Action) {
const versionParameter = action.parameters.find(
p =>
(p.name == 'apiVersion' && p.bindingSourceId == eBindingSourceId.Path) ||
(p.name == 'api-version' && p.bindingSourceId == eBindingSourceId.Query),
);
const bestVersion = findBestApiVersion(action);
return versionParameter && bestVersion
? {
...versionParameter,
name: camelizeHyphen(versionParameter.name),
defaultValue: `"${bestVersion}"`,
}
: null;
}
// Implementation of https://github.com/abpframework/abp/commit/c3f77c1229508279015054a9b4f5586404a88a14#diff-a4dbf6be9a1aa21d8294f11047774949363ee6b601980bf3225e8046c0748c9eR101
function findBestApiVersion(action: Action) {
/*
TODO: Implement configuredVersion when js proxies implemented
let configuredVersion = null;
if (action.supportedVersions.includes(configuredVersion)) {
return configuredVersion;
}
*/
if (!action.supportedVersions?.length) {
// TODO: return configuredVersion if exists or '1.0'
return '1.0';
}
//TODO: Ensure to get the latest version!
return action.supportedVersions[action.supportedVersions.length - 1];
}
function createActionToImportsReducer(
solution: string,
types: Record<string, Type>,

3
npm/ng-packs/packages/schematics/src/utils/text.ts

@ -55,3 +55,6 @@ function isUpperCase(str = '') {
function toLowerCase(str = '') {
return str.toLowerCase();
}
export function camelizeHyphen(str: string) {
return str.replace(/-([a-z])/g, g => g[1].toUpperCase());
}

Loading…
Cancel
Save