-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: otter sdk training - model extension
- Loading branch information
Showing
13 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.reviver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<R extends typeof reviveMyModel>(baseRevive: R) { | ||
const reviver = <T extends MyModel = MyModel>(data: any, dictionaries?: any) => { | ||
const revivedData = baseRevive<MyModelCoreIfy<T>>(data, dictionaries); | ||
/* Set the value of your new fields here */ | ||
// EXAMPLE: revivedData.myNewField = 'fake value'; | ||
return revivedData; | ||
}; | ||
|
||
return reviver; | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/flight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 extends IgnoreEnum<MyModel>> = T & { | ||
/* Add your additional fields here */ | ||
}; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/flight/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './flight'; | ||
export * from './flight.reviver'; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/core/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Export your core models here | ||
export * from './flight'; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/exercise/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './base'; | ||
export * from './core'; |
56 changes: 56 additions & 0 deletions
56
apps/showcase/src/assets/trainings/sdk/steps/model-extension/instructions.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<p> | ||
There are certain cases in which you want to be able to extend your SDK models, and therefore ensure that revivers are generated. | ||
</p> | ||
<p> | ||
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 <code>Flight</code> type. | ||
</p> | ||
<p> | ||
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 <code>openapitools.json</code>). | ||
Ensure that the global property <code>allowModelExtension</code> has been set to <b>true</b>. This will guarantee that the revivers of the | ||
base models are generated, which is essential for the following exercise. | ||
</p> | ||
|
||
<h3>Exercise</h3> | ||
<p> | ||
Extensions of base models (located in the <b>base</b> folder) are created in the <b>core</b> folder. | ||
</p> | ||
<p> | ||
First, create the type <code>FlightCoreIfy</code> which extends <code>Flight</code>, imported from the <b>base</b> folder. | ||
You can do this using the template file <code>flight.ts</code>. | ||
</p> | ||
<p> | ||
<i> | ||
Note: The naming convention requires the core model to contain the suffix <code>CoreIfy</code>. You can find more information on core models in the | ||
<a href="https://github.com/AmadeusITGroup/otter/blob/main/docs/api-sdk/SDK_MODELS_HIERARCHY.md" target="_blank">SDK models hierarchy documentation</a>. | ||
</i> | ||
</p> | ||
<p> | ||
Then, you can create the reviver for this new core model using the existing template file <code>flight.reviver.ts</code>. This reviver will extend the | ||
reviver of the base <code>Flight</code> model, which should exist since we have ensured this during the prerequisites of the exercise. | ||
</p> | ||
<p> | ||
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 <code>base/flight/index.ts</code> by following this template: | ||
</p> | ||
<pre class="w-100 bg-body-tertiary px-5 pre-whitespace"> | ||
<code> | ||
// 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<BaseModel>; | ||
export const reviveMyModel = reviveMyModelFactory(baseReviver); | ||
export type { MyModel as BaseMyModel }; | ||
</code> | ||
</pre> | ||
<p> | ||
<i> | ||
<u>Hint:</u> <code>MyModel</code> should be replaced by <code>Flight</code> throughout these template files. | ||
</i> | ||
</p> | ||
<p> | ||
Don't forget to check out the solution of this exercise! | ||
</p> |
7 changes: 7 additions & 0 deletions
7
apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/base/flight/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BaseModel>; | ||
export const reviveFlight = reviveFlightFactory(baseReviver); | ||
export type { BaseModel as BaseFlight }; |
19 changes: 19 additions & 0 deletions
19
...ase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.reviver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<R extends typeof reviveFlight>(baseRevive: R) { | ||
const reviver = <T extends Flight = Flight>(data: any, dictionaries?: any) => { | ||
const revivedData = baseRevive<FlightCoreIfy<T>>(data, dictionaries); | ||
/* Set the value of your new fields here */ | ||
revivedData.id = 'sampleIdValue'; | ||
return revivedData; | ||
}; | ||
|
||
return reviver; | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/flight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Flight } from '../../base/flight/flight'; | ||
import type { IgnoreEnum } from '@ama-sdk/core'; | ||
|
||
export type FlightCoreIfy<T extends IgnoreEnum<Flight>> = T & { | ||
id: string; | ||
}; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/flight/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './flight'; | ||
export * from './flight.reviver'; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/core/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Export your core models here | ||
export * from './flight'; |
2 changes: 2 additions & 0 deletions
2
apps/showcase/src/assets/trainings/sdk/steps/model-extension/solution/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './base'; | ||
export * from './core'; |