Skip to content

Commit

Permalink
feat(sdk-schematics): expose models without dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Aug 15, 2024
1 parent 75fb9ea commit 7623af1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
9 changes: 8 additions & 1 deletion docs/api-sdk/SDK_MODELS_HIERARCHY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ The purpose of the revivers is to convert JSON parsed object to the model the re
Example of a reviver:

```typescript
export function reviveMyModel<T extends MyModel = MyModel>(data: any, dictionaries?: any): T | undefined {
export function reviveMyModel<T extends MyModelNoDictionary = MyModel>(data: any, dictionaries?: any): T | undefined {
if (!data) { return ; }
data.myField = reviverMyFiedType<MyFiedType>(data.myField, dictionaries);
return data as T;
}
```

> [!TIP]
> The reviver can help to remove the dictionary fields from a revived object when used without dictionary:
>
> ```typescript
> reviveMyModel<MyModelNoDictionary>(data);
> ```
## Core models (and base models extension)
To be able to use the core models in different applications, the base models must be extended the same way it is done in the generated SDKs, based on Swagger specification using the same definitions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type {{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#up
export interface {{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}RequestData {
{{#allParams}}
/** {{#description}}{{{description}}}{{/description}}{{^description}}{{#isArray}}List of {{#plurialize}}{{#noArrayInType}}{{{dataType}}}{{/noArrayInType}}{{/plurialize}}{{/isArray}}{{/description}} */
'{{baseName}}'{{^required}}?{{/required}}{{#required}}{{#defaultValue}}?{{/defaultValue}}{{/required}}: {{#isEnum}}{{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#uppercaseFirst}}{{paramName}}{{/uppercaseFirst}}Enum{{#isArray}}[]{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isBodyParam}}{{{baseType}}}{{#isArray}}[]{{/isArray}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/isEnum}};
'{{baseName}}'{{^required}}?{{/required}}{{#required}}{{#defaultValue}}?{{/defaultValue}}{{/required}}: {{#isEnum}}{{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#uppercaseFirst}}{{paramName}}{{/uppercaseFirst}}Enum{{#isArray}}[]{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isBodyParam}}{{{baseType}}}{{#keepRevivers}}NoDictionary{{/keepRevivers}}{{#isArray}}[]{{/isArray}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/isEnum}};
{{/allParams}}
}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
{{#model}}
{{#description}}
/**
* {{{description}}}
* {{{description}}}{{#keepRevivers}}
* Note: This interface does not expose the dictionary field that can be part of the revived object.{{/keepRevivers}}
*/
{{/description}}
{{^isEnum}}
{{^oneOf}}
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{^isEnum}}
{{^oneOf}}
export interface {{classname}}{{#keepRevivers}}NoDictionary{{/keepRevivers}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#vars}}
{{#description}}
/** {{{description}}} */
Expand All @@ -49,23 +50,7 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{/isEnum}}
{{/isArray}}
{{/description}}
{{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-field-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-field-name}}{{^required}}?{{/required}}: {{x-field-type}};
{{/x-field-name}}
{{/vendorExtensions}}
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-map-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-map-name}}{{^required}}?{{/required}}: { [key: string]: {{x-field-type}} | undefined };
{{/x-map-name}}
{{/vendorExtensions}}
{{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#keepRevivers}}NoDictionary{{/keepRevivers}}{{/isEnum}};
{{/vars}}
}
{{/oneOf}}
Expand Down Expand Up @@ -95,4 +80,40 @@ export type {{classname}} = {{#trimPipe}}{{#allowableValues}}{{#values}}'{{.}}'
{{/isEnum}}
{{/model}}
{{/models}}

{{#keepRevivers}}
{{#models}}
{{#model}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{^isEnum}}
{{^oneOf}}
export interface {{classname}} extends {{classname}}NoDictionary {
{{#vars}}
/** @inheritdoc */
{{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{#vendorExtensions}}
{{#x-field-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-field-name}}{{^required}}?{{/required}}: {{x-field-type}};
{{/x-field-name}}
{{/vendorExtensions}}
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-map-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-map-name}}{{^required}}?{{/required}}: { [key: string]: {{x-field-type}} | undefined };
{{/x-map-name}}
{{/vendorExtensions}}
{{/vars}}
{{/oneOf}}
{{/isEnum}}
}
{{/model}}
{{/models}}
{{/keepRevivers}}
{{/noUnusedImport}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
{{#noUnusedImport}}
{{#noEmptyLines}}
import { {{classname}} } from './{{#kebabCase}}{{classname}}{{/kebabCase}}';
import { {{classname}}, {{classname}}NoDictionary } from './{{#kebabCase}}{{classname}}{{/kebabCase}}';
{{#imports}}
import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
import { revive{{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
Expand All @@ -23,12 +23,12 @@ import { utils, reviveArray, reviveMap, type ReviverOptions, reviveDictionarized
{{#model}}
{{^isEnum}}
{{^isEnumRef}}
export function revive{{classname}}<T extends {{classname}} = {{classname}}>(data: undefined, dictionaries?: any, options?: ReviverOptions): undefined;
export function revive{{classname}}<T extends {{classname}}NoDictionary = {{classname}}>(data: undefined, dictionaries?: any, options?: ReviverOptions): undefined;
export function revive{{classname}}(data: {{classname}}, dictionaries?: any, options?: ReviverOptions): {{classname}} {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}};
export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): {{classname}} {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined;
export function revive{{classname}}<T extends {{classname}}>(data: T, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}};
export function revive{{classname}}<T extends {{classname}}>(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined;
export function revive{{classname}}<T extends {{classname}} = {{classname}}>(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined {
export function revive{{classname}}<T extends {{classname}}NoDictionary>(data: T, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}};
export function revive{{classname}}<T extends {{classname}}NoDictionary>(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined;
export function revive{{classname}}<T extends {{classname}}NoDictionary = {{classname}}>(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined {
if (!data) { return ; }
{{#noEmptyLines}}
{{#vendorExtensions}}
Expand Down

0 comments on commit 7623af1

Please sign in to comment.