Comparing version 0.1.2 to 0.2.0
@@ -7,6 +7,13 @@ const Request = require("./Request") | ||
class Images extends Request { | ||
constructor({link, dinky, query = null}) { | ||
super({dinky, link, query, path: "images"}) | ||
constructor({link, dinky}) { | ||
super({dinky, link, path: "images"}) | ||
} | ||
/** | ||
* Takes an image by given ID. | ||
* | ||
* @param {number} id – image ID on Derpibooru | ||
* | ||
* @return {Promise<object>} | ||
*/ | ||
async id(id) { | ||
@@ -13,0 +20,0 @@ this._path.push(id) |
@@ -8,3 +8,3 @@ const Query = require("./Query") | ||
/** | ||
* Creates a new Request instance | ||
* The Request object helps to describe request to Derpibooru API. | ||
* | ||
@@ -14,4 +14,6 @@ * @param {object} options | ||
*/ | ||
constructor({dinky, link, path/* , query = {} */}) { | ||
constructor({dinky, link, path}) { | ||
/** | ||
* @prop {Function} __link – request handler taken from link helper | ||
* | ||
* @private | ||
@@ -22,8 +24,22 @@ */ | ||
/** | ||
* @prop {Dinky} _dinky – current Dinky.js inststance | ||
* | ||
* @protected | ||
*/ | ||
this._dinky = dinky // Current Dinky.js inststance | ||
this._query = new Query(/* query */) // e. g. URLSearchParams | ||
this._path = [path] // A path to where request will be sent | ||
this._dinky = dinky | ||
/** | ||
* @prop {Query} _query – query search params | ||
* | ||
* @protected | ||
*/ | ||
this._query = new Query() | ||
/** | ||
* @prop {string[]} _path – a path where the request will be sent | ||
* | ||
* @protected | ||
*/ | ||
this._path = [path] | ||
this.exec = this.exec.bind(this) | ||
@@ -33,2 +49,4 @@ } | ||
/** | ||
* Sets images ordering to ascending | ||
* | ||
* @public | ||
@@ -43,2 +61,4 @@ */ | ||
/** | ||
* Sets images ordering to descending | ||
* | ||
* @public | ||
@@ -45,0 +65,0 @@ */ |
@@ -62,2 +62,28 @@ const waterfall = require("./util/waterfall") | ||
/** | ||
* Sets the MINIMAL score of requested images | ||
* | ||
* @param {number} value – a value of minimal socre | ||
* | ||
* @return {Search} | ||
*/ | ||
minScore(value) { | ||
this._query.set("min_score", value) | ||
return this | ||
} | ||
/** | ||
* Sets the MAXIMAL score of requested images | ||
* | ||
* @param {number} value – a value of maximal socre | ||
* | ||
* @return {Search} | ||
*/ | ||
maxScore(value) { | ||
this._query.set("max_score", value) | ||
return this | ||
} | ||
/** | ||
* If been called, the API will return random image | ||
@@ -103,2 +129,14 @@ * | ||
const min = this._query.get("min_score") | ||
// eslint-disable-next-line no-restricted-globals | ||
if (min != null && isNaN(min)) { | ||
throw new TypeError("You must specify minimal score as a number.") | ||
} | ||
const max = this._query.get("max_score") | ||
// eslint-disable-next-line no-restricted-globals | ||
if (max != null && isNaN(max)) { | ||
throw new TypeError("You must specify maximal score as a number.") | ||
} | ||
return super.exec() | ||
@@ -105,0 +143,0 @@ } |
@@ -6,8 +6,7 @@ const {parse, format} = require("url") | ||
const partial = require("./partial") | ||
const right = require("./partialRight") | ||
const waterfall = require("./waterfall") | ||
const partial = require("./partial") | ||
const setProtocol = require("./setProtocol") | ||
const setProtocol = url => url.replace(/^(https?:\/\/)?/, "https://") | ||
const available = ["trixiebooru.org", "derpibooru.org"] | ||
@@ -17,2 +16,11 @@ const joined = available.join(", ") | ||
/** | ||
* Creates a link for given Derpibooru's host. Optionally takes an API key. | ||
* | ||
* @param {object} options – link options | ||
* @param {string} options.url - Derpibooru API hostname | ||
* @param {string} [options.key = null] – your personal API key | ||
* taken from your account settings | ||
* | ||
* @return {Function} | ||
* | ||
* @api private | ||
@@ -23,6 +31,16 @@ */ | ||
return async function request(pathname, search) { | ||
/** | ||
* Sent a request with given params | ||
* | ||
* @param {string[]} pathname | ||
* @param {dinky.Query} search | ||
* | ||
* @return {object | object[]} | ||
* | ||
* @api private | ||
*/ | ||
return async function request(path, search) { | ||
if (!available.includes(url.hostname)) { | ||
throw new Error( | ||
`Dinky can sends requests only to these hosts: ${joined}` | ||
`Dinky can send requests only to these hosts: ${joined}` | ||
) | ||
@@ -35,6 +53,4 @@ } | ||
pathname = `${pathname.join("/").replace(/\/{2,}/g, "/")}.json` | ||
const pathname = `${path.join("/").replace(/\/{2,}/g, "/")}.json` | ||
const address = format({...url, pathname, search: search.toString()}) | ||
const send = partial(fetch, address, {method: "get"}) | ||
@@ -41,0 +57,0 @@ |
{ | ||
"name": "dinky.js", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "A JavaScript client for Derpibooru API", | ||
@@ -34,10 +34,10 @@ "main": "lib/Dinky.js", | ||
"@octetstream/eslint-config": "3.0.0", | ||
"ava": "1.2.0", | ||
"ava": "1.2.1", | ||
"codecov": "3.1.0", | ||
"eslint": "5.12.1", | ||
"eslint": "5.13.0", | ||
"eslint-plugin-ava": "5.1.1", | ||
"fetch-mock": "7.3.0", | ||
"husky": "1.3.1", | ||
"lint-staged": "8.1.1", | ||
"nyc": "13.1.0", | ||
"lint-staged": "8.1.3", | ||
"nyc": "13.2.0", | ||
"proxyquire": "2.1.0", | ||
@@ -44,0 +44,0 @@ "sinon": "7.2.3" |
@@ -107,2 +107,14 @@ # dinky.js | ||
##### `minScore(value) -> {Search}` | ||
Sets the **minimal** score of requested images. | ||
- **{number}** value – a value of minimal socre | ||
##### `maxScore(value) -> {Search}` | ||
Sets the **maximal** score of requested images | ||
- **{number}** value – a value of maximal socre | ||
##### `random() -> {Promise<object>}` | ||
@@ -122,7 +134,7 @@ | ||
Sets results ordering to ascending | ||
Sets images ordering to ascending | ||
##### `descending() -> {Request}` | ||
Sets results ordering to descending | ||
Sets images ordering to descending | ||
@@ -151,2 +163,2 @@ ##### `page(offset) -> {Request}` | ||
* Derpibooru API documentation can be found here: [trixiebooru.org/pages/api](https://trixiebooru.org/pages/api) | ||
* Derpibooru API documentation can be found here: [derpibooru.org/pages/api](https://derpibooru.org/pages/api) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17955
18
464
162