From 02e6e9afee45686a3032938d2a8858bdfe33543c Mon Sep 17 00:00:00 2001 From: Kilian Panot Date: Thu, 29 Aug 2024 18:27:16 +0900 Subject: [PATCH] feat(sdk): add the capability to clear dictionary field --- docs/api-sdk/SDK_MODELS_HIERARCHY.md | 9 ++++++++- packages/@ama-sdk/core/src/fwk/Reviver.ts | 15 +++++++++++++++ .../typescriptFetch/model/reviver.mustache | 4 ++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/api-sdk/SDK_MODELS_HIERARCHY.md b/docs/api-sdk/SDK_MODELS_HIERARCHY.md index 9764abe6fd..50b6a24be0 100644 --- a/docs/api-sdk/SDK_MODELS_HIERARCHY.md +++ b/docs/api-sdk/SDK_MODELS_HIERARCHY.md @@ -22,11 +22,18 @@ Example of a reviver: ```typescript export function reviveMyModel(data: any, dictionaries?: any): T | undefined { if (!data) { return ; } - data.myField = reviverMyFiedType(data.myField, dictionaries); + data.myField = reviverMyFieldType(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(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. diff --git a/packages/@ama-sdk/core/src/fwk/Reviver.ts b/packages/@ama-sdk/core/src/fwk/Reviver.ts index dfeb99b1c8..ba78c0591e 100644 --- a/packages/@ama-sdk/core/src/fwk/Reviver.ts +++ b/packages/@ama-sdk/core/src/fwk/Reviver.ts @@ -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(data, myDictionary); + * + * // Remove dictionary from revived data : + * reviveMyModel(revivedData, undefined, { clearDictionaryFields: true }); + * ``` + */ + clearDictionaryFields?: boolean; } /** Reviver type */ diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache index 779dc3a658..da5d066466 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache @@ -90,7 +90,7 @@ export function revive{{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}} @@ -101,7 +101,7 @@ export function revive{{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}}