Skip to content

Commit

Permalink
feat: otter sdk training - model extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sdo-1A committed Nov 6, 2024
1 parent 36e5eb3 commit 5ab818f
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/showcase/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module.exports = {
'root': true,
'ignorePatterns': ['/src/assets/trainings/sdk'],
'overrides': [
{
'files': [
Expand Down
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;
}
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 */
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './flight';
export * from './flight.reviver';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Export your core models here
export * from './flight';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './base';
export * from './core';
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>
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 };
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;
}
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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './flight';
export * from './flight.reviver';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Export your core models here
export * from './flight';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './base';
export * from './core';

0 comments on commit 5ab818f

Please sign in to comment.