-
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.
- Loading branch information
Showing
7 changed files
with
206 additions
and
6 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
21 changes: 21 additions & 0 deletions
21
apps/showcase/src/assets/trainings/sdk/steps/plugins/exercise/app.component.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,21 @@ | ||
<table class="table"> | ||
<caption align="top"> | ||
First 10 pets with the status <b>available</b> from the Swagger Petstore | ||
</caption> | ||
<thead> | ||
<tr> | ||
<th scope="col">#</th> | ||
<th scope="col">Name</th> | ||
<th scope="col">Status</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@for (pet of pets(); track pet.id; let index = $index) { | ||
<tr> | ||
<td>{{ index + 1 }}</td> | ||
<td>{{ pet.name }}</td> | ||
<td>{{ pet.status }}</td> | ||
</tr> | ||
} | ||
</tbody> | ||
</table> |
28 changes: 28 additions & 0 deletions
28
apps/showcase/src/assets/trainings/sdk/steps/plugins/exercise/app.component.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,28 @@ | ||
import { Component, inject, signal } from '@angular/core'; | ||
import { type Pet, PetApi } from 'sdk'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
standalone: true, | ||
imports: [], | ||
templateUrl: './app.component.html', | ||
styleUrl: './app.component.scss' | ||
}) | ||
export class AppComponent { | ||
/** Title of the application */ | ||
public title = 'tutorial-app'; | ||
|
||
private readonly petStoreApi = inject(PetApi); | ||
private readonly petsWritable = signal<Pet[]>([]); | ||
public readonly pets = this.petsWritable.asReadonly(); | ||
|
||
constructor() { | ||
void this.setPets(); | ||
} | ||
|
||
public async setPets() { | ||
/* Get the first 10 pets whose status is 'available' */ | ||
const availablePets = await this.petStoreApi.findPetsByStatus({status: 'available'}); | ||
this.petsWritable.set(availablePets.slice(0, 10)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
apps/showcase/src/assets/trainings/sdk/steps/plugins/exercise/app.config.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,46 @@ | ||
import { ApiFetchClient } from '@ama-sdk/client-fetch'; | ||
import { PluginRunner, RequestOptions, RequestPlugin } from '@ama-sdk/core'; | ||
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; | ||
import { Pet, PetApi } from 'sdk'; | ||
import { routes } from './app.routes'; | ||
|
||
class MockInterceptRequest implements RequestPlugin { | ||
public load(): PluginRunner<RequestOptions, RequestOptions> { | ||
return { | ||
transform: async () => { | ||
// TODO Create a mock response of type Pet[] | ||
// TODO Store the stringified object in a Blob and create an object URL | ||
// TODO Replace the original base path with the URL of the Blob | ||
const basePath = '/target your blob resource/'; | ||
|
||
return { | ||
method: 'GET', | ||
headers: new Headers(), | ||
basePath | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
|
||
// TODO Add your plugin to the Api Fetch Client | ||
function petApiFactory() { | ||
const apiFetchClient = new ApiFetchClient( | ||
{ | ||
basePath: 'https://petstore3.swagger.io/api/v3', | ||
requestPlugins: [], | ||
replyPlugins: [], | ||
fetchPlugins: [] | ||
} | ||
); | ||
return new PetApi(apiFetchClient); | ||
} | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideZoneChangeDetection({ eventCoalescing: true }), | ||
provideRouter(routes), | ||
{provide: PetApi, useFactory: petApiFactory} | ||
] | ||
}; |
27 changes: 27 additions & 0 deletions
27
apps/showcase/src/assets/trainings/sdk/steps/plugins/instructions.md
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,27 @@ | ||
Thanks to the `@ama-sdk/core` plugins, you can intercept the API requests and responses to transform their data or | ||
perform actions such as authentication, logging, etc. | ||
|
||
These plugins will be applied before sending all the API requests and after the reception of the responses. | ||
|
||
### Objective | ||
In this tutorial, you will configure the `PetApi` to create a plugin whose purpose is to intercept the request before it is sent and serve a mocked response instead. | ||
|
||
### Pre-requisite | ||
- The package `@ama-sdk/core` needs to be installed (which has already been done for you). | ||
|
||
### Exercise | ||
Create your own plugin that implements the `RequestPlugin` interface to mock the request plugin. | ||
The exercise already contains the beginning of the code to inspire you. Here are some hints to help you as well: | ||
- First, create a mock response of type `Pet[]` from the SDK. | ||
- This object should be stringified and used to create a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob" target="_blank">Blob</a>. | ||
- Use the created `Blob` object to replace the base path (**hint**: check out the method <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static" target="_blank">URL.createObjectURL()</a>). | ||
|
||
If implemented correctly, you should now see your mock response in the table of available pets, instead of the response from the Swagger Petstore. | ||
|
||
> [!NOTE] | ||
> Don't forget to check the exercise solution! | ||
> [!TIP] | ||
> The Otter framework provides several mock intercept plugins, including a mock intercept request plugin, which you can look further into if wanted to compare to this exercise. | ||
> For more information, you can find all the <a href="https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core#available-plugins" target="_blank">project plugins</a> | ||
> in the @ama-sdk/core package of the Otter framework. |
45 changes: 45 additions & 0 deletions
45
apps/showcase/src/assets/trainings/sdk/steps/plugins/solution/app.config.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,45 @@ | ||
import { ApiFetchClient } from '@ama-sdk/client-fetch'; | ||
import { PluginRunner, RequestOptions, RequestPlugin } from '@ama-sdk/core'; | ||
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; | ||
import { Pet, PetApi } from 'sdk'; | ||
import { routes } from './app.routes'; | ||
|
||
class MockInterceptRequest implements RequestPlugin { | ||
public load(): PluginRunner<RequestOptions, RequestOptions> { | ||
return { | ||
transform: async () => { | ||
const mockData: Pet[] = [{ name : "mockPetName", photoUrls: ["mockPhotoUrl"], status: "available"}]; | ||
const text = JSON.stringify(mockData); | ||
const blob = new Blob([text], { type: 'application/json' }); | ||
const basePath = URL.createObjectURL(blob); | ||
|
||
return { | ||
method: 'GET', | ||
basePath, | ||
headers: new Headers() | ||
}; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
function petApiFactory() { | ||
const apiFetchClient = new ApiFetchClient( | ||
{ | ||
basePath: 'https://petstore3.swagger.io/api/v3', | ||
requestPlugins: [new MockInterceptRequest()], | ||
replyPlugins: [], | ||
fetchPlugins: [] | ||
} | ||
); | ||
return new PetApi(apiFetchClient); | ||
} | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideZoneChangeDetection({ eventCoalescing: true }), | ||
provideRouter(routes), | ||
{provide: PetApi, useFactory: petApiFactory} | ||
] | ||
}; |
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