Skip to content
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

Code review & reflections #10

Closed
wants to merge 1 commit into from
Closed

Code review & reflections #10

wants to merge 1 commit into from

Conversation

s373r
Copy link

@s373r s373r commented Feb 15, 2022

A draft PR to be able to add GitHub comments at the current code

Here we go 🚀

PS. NOT FOR MERGE

Copy link
Author

@s373r s373r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ynhhoJ, I made a quick look, added some comments

```
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed a blank line

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@@ -10,4 +11,4 @@ yarn ts-node ./examples/searchAuthors.ts [author name] [page number] [items limi

```powershell
yarn ts-node ./examples/searchBooksBySeries.ts [book name] [page number] [items limit count]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To run examples, we need to install ts-node (a dev dependency), so will be great to have a note about that

For example, npm install --include dev (or yarn variant, ideally both)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, what about using npm scripts (package.json scripts section) here instead of full commands?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i just added scripts for paginated API and without pagination

@@ -1,3 +1,4 @@
<!--- -->
### Search book by name
Commands:
```powershell
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general comment:

In common case, better to use ```sh for shell commands since we do not have here any PowerShell specific stuff

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@@ -1,3 +1,4 @@

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a lock file*, please commit it

  • package-lock.json or yarn.lock

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

{
"name": "flibusta-api",
"version": "0.2.0",
"main": "index.js",
"main": "index.js" ,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not have a main script here since we have not build artifacts (e.g. js bundle)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed main, maybe i will add it back later

@@ -22,6 +23,7 @@ class FlibustaAPI {
this.GetAuthors = new GetAuthors(axiosController);
}

//
getBooksByName(name: string, page = 0, limit = 50) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an API design question (here and below)

Preamble:
With the current design, I as an API user, need to make other API calls if a page is not last.
I'd like to get items (books) with one call.

What about encapsulate that kind of logic into internals?

Pagination is a good thing as an option but should we use it as default? I see, it heavily depends on internal logic but we can make a more friendly interface

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tip!
Add two different APIs, for paginated result and just raw items.

}

// NOTE: 584 книг
const [booksItemsCountAsString] = stringMatch;
const booksCountOnlyNumbers = StringUtils.getNumbersFromString(booksItemsCountAsString);

return parseInt(booksCountOnlyNumbers, 10);
return parseInt(booksCountOnlyNumbers, 10); //
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other places, we use modular variant: Number.parseInt(), I guess here we have to do it as well (for consistency reasons)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, added it!

@@ -89,14 +93,14 @@ abstract class FlibustaAPIHelper {
const stringMatch = StringUtils.getStringMatches(booksOrTranslationsAsString, regexRule);

if (isNil(stringMatch)) {
return FlibustaAPIHelper.NIL_RESULT;
return FlibustaAPIHelper.NIL_RESULT; //
}

// NOTE: 584 книг
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an unnecessary comment

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed!

import AxiosController from './axiosController';
import { PagesInformation } from '../types/pagesInformation';
import { HTMLElement, parse, Node } from 'node-html-parser';
import { isEmpty, isNil, parseInt } from 'lodash';
import Author from '../types/authors';
import Book from '../types/book';
import StringUtils from './utils/string';
import StringUtils from './utils/string'; //

abstract class FlibustaAPIHelper {
private static NIL_RESULT = -1;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a pure C thing :)

Since we have a strong type-system in TS, I'd like to suggest use Nullable-like result type

Some reflections -- it is easy to forget to check result value with some magic constant but if it will be a nullable value we have to do that on a type basis

My personal opinion regarding interface design in general: if the type system is strongly typed out of the box we need to use it to protect API users

Another point, any checks with magic constants is a runtime thing but type checking is a compiling (read transpiling) time thing

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented nullable type instead of "magic constant", thx

@@ -1,16 +1,18 @@
//
import AxiosController from './axiosController';
import { PagesInformation } from '../types/pagesInformation';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general optional comment regarding this module cohesion

I'd like to suggest taking a look at the page model conception (https://testcafe.io/documentation/402826/guides/concepts/page-model)

It helps reduce dependencies in code by moving page-related things into separate classes.
I see you use the raw HTML parsing approach -- maybe for the next projects, I propose to play around Testcase framework. It can run a browser in headless mode and interact with pages as a user would do

QA automation guys usually use this approach to test any kind of frontends (not only sites, even native desktop applications). Since here we have a classical scraping task, we can use it too

This was referenced Feb 16, 2022
@pvlrmnnk
Copy link

Dude, where are your tests? Your code is weak against unpredictable layout changes.

@ynhhoJ
Copy link
Owner

ynhhoJ commented Feb 19, 2022

@pvlrmnnk , i created an issue for this #4 , i will try to add ASAP

@ynhhoJ
Copy link
Owner

ynhhoJ commented Feb 20, 2022

изображение

Just added tests for Flibusta API. Help is still wanted for 100% coverage or advices how tests should be written. I have small experience on work with it.

#4

@ynhhoJ ynhhoJ closed this Feb 23, 2022
@pvlrmnnk
Copy link

You've made great progress. Thank you for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants