Skip to content

Commit 6a7febb

Browse files
committed
Add fetchTags()
1 parent 558434f commit 6a7febb

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const { NekosiaAPI } = require('nekosia.js');
7373
})();
7474
```
7575

76-
7776
### IP-based Sessions
7877
In this example, we used an IP-based session. What does this mean? Thanks to this solution, a user with a specific IP address will not encounter duplicate images when selecting them randomly.
7978

@@ -115,6 +114,16 @@ const { NekosiaAPI } = require('nekosia.js');
115114
https://github.com/Nekosia-API/nekosia.js/tree/main/examples
116115

117116

117+
## Tags
118+
```js
119+
const { NekosiaAPI } = require('nekosia.js');
120+
121+
(async () => {
122+
console.log(await NekosiaAPI.fetchTags()); // Simply returns all available tags, etc.
123+
})();
124+
```
125+
126+
118127
## Versions
119128
```js
120129
const { NekosiaVersion } = require('nekosia.js');

examples/fetchTags.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { NekosiaAPI } = require('../index.js');
2+
3+
(async () => {
4+
const json = await NekosiaAPI.fetchTags();
5+
console.log(json);
6+
})();

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ class NekosiaAPI {
4343

4444
async fetchImages(options = {}) {
4545
if (!Array.isArray(options.tags) || options.tags.length === 0) {
46-
throw new Error('`tags` must be a non-empty array for the nothing category');
46+
throw new Error('`tags` must be a non-empty array');
47+
}
48+
49+
if (Array.isArray(options.blacklistedTags)) {
50+
throw new Error('Unexpected `blacklistedTags` in `fetchImages()`, use `blacklist` instead');
4751
}
4852

4953
return this.fetchCategoryImages('nothing', {
@@ -55,6 +59,10 @@ class NekosiaAPI {
5559
});
5660
}
5761

62+
async fetchTags() {
63+
return this.makeHttpRequest(`${API_URL}/tags`);
64+
}
65+
5866
async fetchById(id) {
5967
if (!id) throw new Error('`id` parameter is required');
6068

test/api.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('NekosiaAPI (API Tests)', () => {
7070
});
7171

7272
it('should throw an error if additionalTagsArray is empty', async () => {
73-
await expect(NekosiaAPI.fetchImages([])).rejects.toThrow('`tags` must be a non-empty array for the nothing category');
73+
await expect(NekosiaAPI.fetchImages([])).rejects.toThrow('`tags` must be a non-empty array');
7474
});
7575

7676
it('should return an error response for invalid count parameter', async () => {

test/integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('NekosiaAPI', () => {
5858

5959
describe('fetchImages', () => {
6060
it('should throw an error if additionalTags is empty', async () => {
61-
await expect(NekosiaAPI.fetchImages({})).rejects.toThrow('`tags` must be a non-empty array for the nothing category');
61+
await expect(NekosiaAPI.fetchImages({})).rejects.toThrow('`tags` must be a non-empty array');
6262
});
6363

6464
it('should correctly call fetchImages with additionalTags', async () => {

types/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ declare module 'nekosia.js' {
371371
attribution: ImageAttribution;
372372
}
373373

374+
/**
375+
* GET https://api.nekosia.cat/api/v1/tags
376+
*/
377+
interface TagsResponse {
378+
status: number,
379+
success: boolean,
380+
tags: string[],
381+
anime: string[],
382+
characters: string[]
383+
}
384+
374385
/**
375386
* Nekosia API class, containing methods for fetching images.
376387
* All methods are asynchronous and return a Promise resolving to an `ImageResponse`.
@@ -407,6 +418,12 @@ declare module 'nekosia.js' {
407418
*/
408419
static fetchImages(options?: FetchImagesOptions): Promise<ImageResponse>;
409420

421+
/**
422+
* Fetches the latest array with tags, anime titles, and characters
423+
* @returns A Promise resolving to an `ImageResponse`.
424+
*/
425+
static fetchTags(id: string): Promise<TagsResponse>;
426+
410427
/**
411428
* Fetches an image by its identifier.
412429
* @param id - The image identifier (e.g., `66ae26a07886f165901e8a3f`).

0 commit comments

Comments
 (0)