octet-stream / dinky

JavaScript bindings for Philomena JSON API. Supports sites such as Derpibooru and Furbooru.
https://www.npmjs.com/package/dinky.js
MIT License
7 stars 3 forks source link

Search random image without specifying tags throws invalid json error. #1

Closed Veetaha closed 5 years ago

Veetaha commented 5 years ago

When you run

await dinky().search().random()

it throws

FetchError: invalid json response body at 
https://derpibooru.org/search.json?random_image=true reason: 
Unexpected end of JSON input

Its because this request results in an empty body.

https://derpibooru.org/search.json?random_image=true

You should set q=* parameter when there are no tags specified in order to resolve the issue.

octet-stream commented 5 years ago

Hello, thanks for the feedback. Sure I can add default search params for .random() :)

octet-stream commented 5 years ago

Fixed #1 in v0.7.1

Veetaha commented 5 years ago

@octet-stream Thanks ))

Veetaha commented 5 years ago

@octet-stream Wait, it still crashes when you pass an empty array dinky.search([]).random() . I suppose it's because your internal function when called flat([[]]) returns [undefined] which gets stored in search._query map under "q" key and your condition isn't triggered.

    const params = this._query.get("q")
    if (isArray(params) && params.length > 0) {
      this._query.set("q", params.join(","))
    } else if (this._query.has("random_image")) {
      // Add wildcard when searching for random image,
      // but no tags has been set
      this._query.set("q", "*")
    }
octet-stream commented 5 years ago

@Veetaha The question is why do you need to call .search() with empty array?

Veetaha commented 5 years ago

Because there is no prerequisite and this may happen when the user specifies no tags. According to the null object pattern this should work as if I haven't passed any tag at all.

octet-stream commented 5 years ago

When user specify no tags, the .tags() method (which is used in Search constructor when you call Dinky#search() with anything but undefined) expect list argument to be undefined when not tags has been set. So you must call this method without any arguments.

octet-stream commented 5 years ago

But I will take a look why flat() returns [undefined], anyway :)

octet-stream commented 5 years ago

Yeah, of course it crashed on flat() since I have no tests for that utility, lol.

octet-stream commented 5 years ago

And because I didn't expect this use case.

octet-stream commented 5 years ago

Okay, try v0.7.2 Hope this helps.

Veetaha commented 5 years ago

@octet-stream Yeah, great, thanks a lot!

octet-stream commented 5 years ago

Glad to hear that. Do not be shy to re-open this issue, if you still have related problems. Or just open a new one. Thanks again for the feedback.