diff --git a/npm/ng-packs/packages/schematics/src/models/index.ts b/npm/ng-packs/packages/schematics/src/models/index.ts index 7cff6a3477..dd4e177353 100644 --- a/npm/ng-packs/packages/schematics/src/models/index.ts +++ b/npm/ng-packs/packages/schematics/src/models/index.ts @@ -1,4 +1,5 @@ export * from './api-definition'; export * from './import'; +export * from './method'; export * from './project'; export * from './util'; diff --git a/npm/ng-packs/packages/schematics/src/models/method.ts b/npm/ng-packs/packages/schematics/src/models/method.ts new file mode 100644 index 0000000000..51cbaa27bd --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/models/method.ts @@ -0,0 +1,85 @@ +import { strings } from '@angular-devkit/core'; +import { eBindingSourceId, eMethodModifier } from '../enums'; +import { ParameterInBody } from './api-definition'; +import { Omissible } from './util'; + +export class Method { + body: Body; + signature: Signature; + + constructor(options: MethodOptions) { + Object.assign(this, options); + } +} + +export type MethodOptions = Method; + +export class Signature { + generics = ''; + modifier = eMethodModifier.Public; + name: string; + parameters: Parameter[] = []; + returnType = ''; + + constructor(options: SignatureOptions) { + Object.assign(this, options); + } +} + +export type SignatureOptions = Omissible< + Signature, + 'generics' | 'modifier' | 'parameters' | 'returnType' +>; + +export class Body { + body?: string; + method: string; + params: string[] = []; + requestType = 'any'; + responseType: string; + url: string; + + registerActionParameter = (param: ParameterInBody) => { + let { bindingSourceId, descriptorName, name, nameOnMethod } = param; + name = strings.camelize(name); + const value = descriptorName ? `${descriptorName}.${name}` : nameOnMethod; + + switch (bindingSourceId) { + case eBindingSourceId.Model: + case eBindingSourceId.Query: + this.params.push(`${name}: ${value}`); + break; + case eBindingSourceId.Body: + this.body = value; + break; + case eBindingSourceId.Path: + const regex = new RegExp('{' + name + '}', 'g'); + this.url = this.url.replace(regex, '${' + value + '}'); + break; + default: + break; + } + }; + + constructor(options: BodyOptions) { + Object.assign(this, options); + } +} + +export type BodyOptions = Omissible< + Omit
, + 'params' | 'requestType' +>; + +export class Parameter { + name: string; + type: string; + default: string = ''; // convert actual value with JSON.stringify if not null + optional: '' | '?' = ''; + + constructor(options: ParameterOptions) { + Object.assign(this, options); + } +} + +export type ParameterOptions = Omissible