diff --git a/apps/showcase/.eslintrc.js b/apps/showcase/.eslintrc.js index 46ccc6a5f7..a9ef502884 100644 --- a/apps/showcase/.eslintrc.js +++ b/apps/showcase/.eslintrc.js @@ -3,6 +3,7 @@ module.exports = { 'root': true, + 'ignorePatterns': ['/src/assets/trainings/sdk'], 'overrides': [ { 'files': [ diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.reviver.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.reviver.ts new file mode 100644 index 0000000000..83b27de741 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.reviver.ts @@ -0,0 +1,20 @@ +/* Replace MyModel with the name of your model */ +import type { MyModel } from '../../base/my-model/my-model'; +import type { reviveMyModel } from '../../base/my-model/my-model.reviver'; +import type { MyModelCoreIfy } from './my-model'; + +/** + * Generate reviver for MyModel core model + * + * @param baseRevive + */ +export function reviveMyModelFactory(baseRevive: R) { + const reviver = (data: any, dictionaries?: any) => { + const revivedData = baseRevive>(data, dictionaries); + /* Set the value of your new fields here */ + // EXAMPLE: revivedData.myNewField = 'fake value'; + return revivedData; + }; + + return reviver; +} diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.ts new file mode 100644 index 0000000000..792bb5064c --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.ts @@ -0,0 +1,7 @@ +/* Replace MyModel with the name of your model */ +import type { MyModel } from '../../base/my-model/my-model'; +import type { IgnoreEnum } from '@ama-sdk/core'; + +export type MyModelCoreIfy> = T & { + /* Add your additional fields here */ +}; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/index.ts new file mode 100644 index 0000000000..e3d7e78403 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/index.ts @@ -0,0 +1,2 @@ +export * from './flight'; +export * from './flight.reviver'; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/index.ts new file mode 100644 index 0000000000..026280ae72 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/index.ts @@ -0,0 +1,2 @@ +// Export your core models here +export * from './flight'; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/index.ts new file mode 100644 index 0000000000..4b8d9646e5 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/index.ts @@ -0,0 +1,2 @@ +export * from './base'; +export * from './core'; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/instructions.html b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/instructions.html new file mode 100644 index 0000000000..c63ab33b2f --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/instructions.html @@ -0,0 +1,56 @@ +

+ There are certain cases in which you want to be able to extend your SDK models, and therefore ensure that revivers are generated. +

+

+ Let's continue with the use case of the previous exercise. In order to keep track of the user's current booking, it would be useful to generate an ID. + To do this, we can create a new model which extends the previously generated Flight type. +

+

+ Before beginning the exercise, we need to make sure that the existing API was generated with model extension in the previous steps. + To verify this, check out the configuration used to generate the API (either the command line or the file openapitools.json). + Ensure that the global property allowModelExtension has been set to true. This will guarantee that the revivers of the + base models are generated, which is essential for the following exercise. +

+ +

Exercise

+

+ Extensions of base models (located in the base folder) are created in the core folder. +

+

+ First, create the type FlightCoreIfy which extends Flight, imported from the base folder. + You can do this using the template file flight.ts. +

+

+ + Note: The naming convention requires the core model to contain the suffix CoreIfy. You can find more information on core models in the + SDK models hierarchy documentation. + +

+

+ Then, you can create the reviver for this new core model using the existing template file flight.reviver.ts. This reviver will extend the + reviver of the base Flight model, which should exist since we have ensured this during the prerequisites of the exercise. +

+

+ Once the core model and its reviver are created, we can go back to the base model to update the exported models and revivers. + You can do so in the file base/flight/index.ts by following this template: +

+
+  
+// in the models/base/my-model/index.ts
+import { MyModelCoreIfy, reviveMyModelFactory } from '../../core/my-model';
+import type { MyModel as BaseModel } from './my-model';
+import { reviveMyModel as baseReviver } from './my-model.reviver';
+
+export type MyModel = MyModelCoreIfy;
+export const reviveMyModel = reviveMyModelFactory(baseReviver);
+export type { MyModel as BaseMyModel };
+  
+
+

+ + Hint: MyModel should be replaced by Flight throughout these template files. + +

+

+ Don't forget to check out the solution of this exercise! +

diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/base/flight/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/base/flight/index.ts new file mode 100644 index 0000000000..0fc3dd5419 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/base/flight/index.ts @@ -0,0 +1,7 @@ +import { FlightCoreIfy, reviveFlightFactory } from '../../core/flight'; +import type { Flight as BaseModel } from './flight'; +import { reviveFlight as baseReviver } from './flight.reviver'; + +export type Flight = FlightCoreIfy; +export const reviveFlight = reviveFlightFactory(baseReviver); +export type { BaseModel as BaseFlight }; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.reviver.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.reviver.ts new file mode 100644 index 0000000000..689cad384a --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.reviver.ts @@ -0,0 +1,19 @@ +import type { Flight } from '../../base/flight/flight'; +import type { reviveFlight } from '../../base/flight/flight.reviver'; +import type { FlightCoreIfy } from './flight'; + +/** + * Generate reviver for Flight core model + * + * @param baseRevive + */ +export function reviveFlightFactory(baseRevive: R) { + const reviver = (data: any, dictionaries?: any) => { + const revivedData = baseRevive>(data, dictionaries); + /* Set the value of your new fields here */ + revivedData.id = 'sampleIdValue'; + return revivedData; + }; + + return reviver; +} diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.ts new file mode 100644 index 0000000000..541814a0a8 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.ts @@ -0,0 +1,6 @@ +import type { Flight } from '../../base/flight/flight'; +import type { IgnoreEnum } from '@ama-sdk/core'; + +export type FlightCoreIfy> = T & { + id: string; +}; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/index.ts new file mode 100644 index 0000000000..e3d7e78403 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/index.ts @@ -0,0 +1,2 @@ +export * from './flight'; +export * from './flight.reviver'; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/index.ts new file mode 100644 index 0000000000..026280ae72 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/index.ts @@ -0,0 +1,2 @@ +// Export your core models here +export * from './flight'; diff --git a/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/index.ts b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/index.ts new file mode 100644 index 0000000000..4b8d9646e5 --- /dev/null +++ b/apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/index.ts @@ -0,0 +1,2 @@ +export * from './base'; +export * from './core';