Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add the capability to clear dictionary field #2059

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,11 +22,18 @@ Example of a reviver:
```typescript
export function reviveMyModel<T extends MyModel = MyModel>(data: any, dictionaries?: any): T | undefined {
if (!data) { return ; }
data.myField = reviverMyFiedType<MyFiedType>(data.myField, dictionaries);
data.myField = reviverMyFieldType<MyFieldType>(data.myField, dictionaries);
return data as T;
}
```

> [!TIP]
> The reviver can help remove dictionary fields from a revived object thanks to the `clearDictionaryFields` option:
>
> ```typescript
> reviveMyModel<MyModel>(data, undefined, { clearDictionaryFields: true });
> ```

## 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
15 changes: 15 additions & 0 deletions packages/@ama-sdk/core/src/fwk/Reviver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ export interface ReviverOptions {
* @default console
*/
logger?: Logger;

/**
* Determine if the fields from dictionary should be removed from the given object.
* This options affects dictionary fields only if no dictionary is provided to the reviver function
* @default false
* @example Clear Dictionary fields from Data object
* ```typescript
* // Revived Data:
* const revivedData = reviveMyModel<MyModel>(data, myDictionary);
*
* // Remove dictionary from revived data :
* reviveMyModel(revivedData, undefined, { clearDictionaryFields: true });
* ```
*/
clearDictionaryFields?: boolean;
}

/** Reviver type */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function revive{{classname}}<T extends {{classname}} = {{classname}}>(dat
{{#x-map-name}}
{{#x-field-is-revived}}
if (data.{{x-map-name}}) {
data.{{x-map-name}} = reviveMap<{{x-field-type}}>(data.{{x-map-name}}, null, revive{{x-field-type}}, options);
data.{{x-map-name}} = options?.clearDictionaryFields ? undefined : reviveMap<{{x-field-type}}>(data.{{x-map-name}}, null, revive{{x-field-type}}, options);
}
{{/x-field-is-revived}}
{{/x-map-name}}
Expand All @@ -101,7 +101,7 @@ export function revive{{classname}}<T extends {{classname}} = {{classname}}>(dat
{{#x-field-name}}
{{#x-field-is-revived}}
if (data.{{x-field-name}}) {
data.{{x-field-name}} = revive{{x-field-type}}(data.{{x-field-name}}, undefined, options);
data.{{x-field-name}} = options?.clearDictionaryFields ? undefined : revive{{x-field-type}}(data.{{x-field-name}}, undefined, options);
}
{{/x-field-is-revived}}
{{/x-field-name}}
Expand Down
Loading