-
Notifications
You must be signed in to change notification settings - Fork 0
chore(01): step 1 - get pokemon by name #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabernovel-fnguyen
wants to merge
4
commits into
main
Choose a base branch
from
01_code_review
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,31 @@ | ||
| import { | ||
| CallHandler, | ||
| ExecutionContext, | ||
| Injectable, | ||
| NestInterceptor, | ||
| } from '@nestjs/common'; | ||
| import { Observable, map } from 'rxjs'; | ||
|
|
||
| const authSecret = 'password123+'; | ||
|
|
||
| // Look at this file only if asked by the interviewer | ||
| @Injectable() | ||
| export class AuthInterceptor implements NestInterceptor { | ||
| constructor() {} | ||
|
|
||
| intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
| return next.handle().pipe( | ||
| map((data) => { | ||
| const ctx = context.switchToHttp(); | ||
| const request = ctx.getRequest(); | ||
| const response = ctx.getResponse(); | ||
|
|
||
| if (request.headers.authorization !== authSecret) { | ||
| response.status(401); | ||
| } | ||
|
|
||
| return data; | ||
| }), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,19 @@ | ||
| import { NestFactory } from '@nestjs/core'; | ||
| import { AppModule } from './app.module'; | ||
| import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||
|
|
||
| async function bootstrap() { | ||
| const app = await NestFactory.create(AppModule); | ||
|
|
||
| const config = new DocumentBuilder() | ||
| .setTitle('A great example of NestJS API for Pokemon trainers') | ||
| .setDescription('The best way to get infos about Pokemon') | ||
| .setVersion('1.0') | ||
| .build(); | ||
|
|
||
| const document = SwaggerModule.createDocument(app, config); | ||
| SwaggerModule.setup('swagger', app, document); | ||
|
|
||
| await app.listen(3000); | ||
| } | ||
| bootstrap(); |
This file contains hidden or 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,8 @@ | ||
| import { ApiProperty } from '@nestjs/swagger'; | ||
| import { IsString } from 'class-validator'; | ||
|
|
||
| export class GetPokemonByNameQuery { | ||
| @ApiProperty({ type: 'string' }) | ||
| @IsString() | ||
| name: string; | ||
| } |
This file contains hidden or 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,66 @@ | ||
| export var pokemonApiUrl = 'http://pokeapi.co/api/v2'; | ||
|
|
||
| import { | ||
| Controller, | ||
| Get, | ||
| HttpException, | ||
| Inject, | ||
| NotFoundException, | ||
| Param, | ||
| Post, | ||
| Query, | ||
| UseInterceptors, | ||
| } from '@nestjs/common'; | ||
| import { PokemonService } from './pokemon.service'; | ||
| import { GetPokemonByNameQuery } from './dtos/get-pokemon-by-name.query'; | ||
| import { Pokemon } from './types/pokemon'; | ||
| import { AuthInterceptor } from '../auth/auth.interceptor'; | ||
| import { | ||
| ApiInternalServerErrorResponse, | ||
| ApiQuery, | ||
| ApiResponse, | ||
| ApiTags, | ||
| } from '@nestjs/swagger'; | ||
|
|
||
| @Controller('pokemon') | ||
| @ApiTags('pokemon') | ||
| @UseInterceptors(AuthInterceptor) | ||
| export class PokemonController { | ||
| constructor(private pokemonService: PokemonService) {} | ||
|
|
||
| @Post('pokemon') | ||
| @ApiQuery({ | ||
| type: GetPokemonByNameQuery, | ||
| description: 'Lookup string for pokemons (match against pokemon name)', | ||
| }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'Returns pokemons whose name matches a string query param', | ||
| }) | ||
| @ApiInternalServerErrorResponse({ | ||
| description: 'This will never happen, trust me', | ||
| }) | ||
| async GetPokemonByNameController( | ||
| @Query() name: any, | ||
| ): Promise<Pokemon | null> { | ||
| if (name === null) return; | ||
|
|
||
| name == null | ||
| ? name.trim() != '' | ||
| ? ((name = name), | ||
| (pokemonApiUrl = pokemonApiUrl + '/'), | ||
| (pokemonApiUrl = pokemonApiUrl + name)) | ||
| : ((pokemonApiUrl = pokemonApiUrl + '"?offset=20"'), | ||
| (pokemonApiUrl = pokemonApiUrl + '&limit=20')) | ||
| : ((pokemonApiUrl = pokemonApiUrl + '"?offset=20"'), | ||
| (pokemonApiUrl = pokemonApiUrl + '&limit=20')); | ||
|
|
||
| console.log('Printing name for debug : ', name); | ||
|
|
||
| const myPokemon = await this.pokemonService.findPokemonByNameOrFail(name); | ||
|
|
||
| console.log('Printing name for debug : ', myPokemon); | ||
|
|
||
| return myPokemon; | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { PokemonController } from './pokemon.controller'; | ||
| import { PokemonService } from './pokemon.service'; | ||
|
|
||
| @Module({ | ||
| controllers: [PokemonController], | ||
| providers: [PokemonService] | ||
| }) | ||
| export class PokemonModule {} |
This file contains hidden or 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,47 @@ | ||
| import { PokemonService } from './pokemon.service'; | ||
|
|
||
| describe('PokemonService (integration)', () => { | ||
| const pokemonService = new PokemonService(); | ||
|
|
||
| it('findPokemonByNameOrFail - valid pokemon name', async () => { | ||
| // Given | ||
| const pokemonName = 'bulbasaur'; | ||
|
|
||
| // When | ||
| const pokemonStats = | ||
| await pokemonService.findPokemonByNameOrFail(pokemonName); | ||
|
|
||
| // Then | ||
| expect(pokemonStats).toMatchObject({ | ||
| id: 1, | ||
| name: 'bulbasaur', | ||
| species: { | ||
| name: 'bulbasaur', | ||
| url: 'https://pokeapi.co/api/v2/pokemon-species/1/', | ||
| }, | ||
| types: ['grass', 'poison'], | ||
| weight: 69, | ||
| height: 7, | ||
| }); | ||
| }); | ||
|
|
||
| it('findPokemonByNameOrFail - non-existing pokemon name', async () => { | ||
| // Given | ||
| const pokemonName = 'bulba'; | ||
|
|
||
| // When + Then | ||
| await expect(() => | ||
| pokemonService.findPokemonByNameOrFail(pokemonName), | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('findPokemonByNameOrFail - 2nd generation pokemon name', async () => { | ||
| // Given | ||
| const pokemonName = 'togepi'; | ||
|
|
||
| // When + Then | ||
| await expect(() => | ||
| pokemonService.findPokemonByNameOrFail(pokemonName), | ||
| ).rejects.toThrow(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import got, { Got } from 'got'; | ||
| import { Pokemon } from './types/pokemon'; | ||
| import { pokemonApiUrl } from './pokemon.controller'; | ||
| import { PokemonSpecies } from './types/species'; | ||
|
|
||
| @Injectable() | ||
| export class PokemonService { | ||
| private httpClient: Got; | ||
| constructor() { | ||
| this.httpClient = got.extend({ | ||
| prefixUrl: pokemonApiUrl, | ||
| responseType: 'json', | ||
| throwHttpErrors: false, | ||
| }); | ||
| } | ||
|
|
||
| async findPokemonByNameOrFail(pokemonName: string): Promise<Pokemon> { | ||
| type GetPokemonResponse = { | ||
| name: string; | ||
| id: number; | ||
| height: number; | ||
| weight: number; | ||
| types: { type: { name: string; url: string } }[]; | ||
| species: PokemonSpecies; | ||
| }; | ||
|
|
||
| return await this.httpClient | ||
| .get(`pokemon/${pokemonName}`) | ||
| .then((response) => response.body as unknown as GetPokemonResponse) | ||
| .then((body) => { | ||
| if (!body.id) { | ||
| return null; | ||
| } | ||
|
|
||
| const types = body.types | ||
| .map((type) => type.type) | ||
| .map((type) => type.name); | ||
|
|
||
| return { | ||
| name: body.name, | ||
| id: body.id, | ||
| height: body.height, | ||
| weight: body.weight, | ||
| types, | ||
| species: { | ||
| name: body.species.name, | ||
| url: body.species.url, | ||
| }, | ||
| }; | ||
| }) | ||
| .catch((error) => { | ||
| throw error; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| import { PokemonSpecies } from './species'; | ||
|
|
||
| export type Pokemon = { | ||
| name: string; | ||
| id: number; | ||
| height: number; | ||
| weight: number; | ||
| types: string[]; | ||
| species: PokemonSpecies; | ||
| }; |
This file contains hidden or 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,4 @@ | ||
| export type PokemonSpecies = { | ||
| name: string; | ||
| url: string; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ce qu'on fait dans nos tests, c'est l'écriture Gherkin
On pourrait faire ça ici, ça donnerait pour ce test:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appliqué aux tests unitaires
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bon, donc on a mit un message sur slack 😄
Tu disais que ce serait mieux aussi de l'appliquer aux e2e 👍