-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat: repo setup, adds tag and file api wrapper #1
Conversation
641d86a
to
808a358
Compare
808a358
to
3309644
Compare
3309644
to
5f4033b
Compare
I am not familiar with |
@@ -0,0 +1,47 @@ | |||
**[@ethersphere/bee-js](../README.md)** |
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.
Commiting generated files in a repo may not be a best practice in general, because it's possible that the source and the generated files are out of sync, it generates lot of noise etc.
Let's discuss if there is a good reason to do this.
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.
the .gitignore is still missing, so every generated files coould be pushed
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.
I agree it is not good idea to have the generated docs in the repo but I figured for the time being (before we have external docs) it's OK.
Fixed the .gitignore
missing though, sorry for that
export default class Bee { | ||
public readonly url: string | ||
|
||
constructor (url: string) { |
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.
In Typescript it's possible to write this in a shorter form:
constructor(readonly url: string) {}
Then you don't need neither the variable declaration in the class, neither the assignment in the constructor body. Let's discuss if we want to use this kind of idiom (called parameter properties)
options?: OptionsUpload | ||
): Promise<string> { | ||
return ( | ||
await axios({ |
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.
All axios
calls could be wrapped in a function that checks if there were errors. Then returning .data
would be safe (it could even be made typesafe :) and all errors could be returned in a well-defined and documented way as well.
For example this current function can either throw an exception from axios code, or return undefined (in case of reference is not defined on data) or return the reference itself, which happens to be a string returned by Bee but that's a coincidence here, because you expect 'application/octet-stream` so it could be even a random byte array that would return garbage string data.
const BEE_URL: string = process.env.BEE_URL || 'http://bee-0.localhost' | ||
|
||
describe('Bee class', () => { | ||
let bee: Bee |
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.
I suggest considering using immutable classes. One benefit of that is easier testing and not having to use let
at all :)
In this case that would mean we could have only one Bee
instance set up and we wouldn't need a before
block. It doesn't seem like a huge win here, but in general it makes tests easier to understand if they are stateless in my experience.
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.
Also I would utilize this practice in every case, the reference object initializations should happen with const
keyword, except well justified cases, for example on browser or node backed service initialization according to the running environment.
bee = new Bee(BEE_URL) | ||
}) | ||
it('should give proper bee URL', () => { | ||
expect(bee.url).to.equal(BEE_URL) |
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.
This doesn't seem like to be a very useful test to me :)
In general tests are supposed to test when there is a logical condition in the code where depending on the inputs the flow of the program changes. Here it is not case.
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.
Also the Bee class doesn't have any functionality which could include the already existing wrapper API methods, and so after it could be tested with these functions if it integrates well.
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.
Sure, I'll remove this test. I just needed something to test the github actions and forgot to remove.
const tag = await Tag.createTag(`${BEE_URL}/tags`) | ||
await File.upload(`${BEE_URL}/files`, file, { tag: tag.uid }) | ||
|
||
await sleep(2000) |
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.
sleep
and other time related functions are usually considered a red flag in tests. They make tests non-deterministic and may cause errors that are hard to reproduce and fix (see flaky tests).
In this case this seems to be an integration test that are waiting for an output from an external process. In that case it may be justifiable but then it's better to check repeatedly until a timeout. For that purpose we might define our own test helper functions.
* | ||
* @param length | ||
*/ | ||
export function randomBuffer (length: number): Buffer { |
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.
Another problem with this random function (besides the safety issue that you correctly pointed out) is that it's not deterministic. This makes it problematic when using it in tests because every test run will be unique and unreproducible.
Usually it's a better approach to use a hardcoded random seed and using a pseudo-random generator seeded with that number. That way tests become reproducible.
* feat: added browser target to the release bundle * ci: create release branch when merged to master
Features
TODO:
utils/data.ts
andutils/data.browser.ts
substitution for something more sensible