Skip to content

Commit

Permalink
Add fetchTags()
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek committed Dec 11, 2024
1 parent 558434f commit 6a7febb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const { NekosiaAPI } = require('nekosia.js');
})();
```


### IP-based Sessions
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.

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


## Tags
```js
const { NekosiaAPI } = require('nekosia.js');

(async () => {
console.log(await NekosiaAPI.fetchTags()); // Simply returns all available tags, etc.
})();
```


## Versions
```js
const { NekosiaVersion } = require('nekosia.js');
Expand Down
6 changes: 6 additions & 0 deletions examples/fetchTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { NekosiaAPI } = require('../index.js');

(async () => {
const json = await NekosiaAPI.fetchTags();
console.log(json);
})();
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class NekosiaAPI {

async fetchImages(options = {}) {
if (!Array.isArray(options.tags) || options.tags.length === 0) {
throw new Error('`tags` must be a non-empty array for the nothing category');
throw new Error('`tags` must be a non-empty array');
}

if (Array.isArray(options.blacklistedTags)) {
throw new Error('Unexpected `blacklistedTags` in `fetchImages()`, use `blacklist` instead');
}

return this.fetchCategoryImages('nothing', {
Expand All @@ -55,6 +59,10 @@ class NekosiaAPI {
});
}

async fetchTags() {
return this.makeHttpRequest(`${API_URL}/tags`);
}

async fetchById(id) {
if (!id) throw new Error('`id` parameter is required');

Expand Down
2 changes: 1 addition & 1 deletion test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('NekosiaAPI (API Tests)', () => {
});

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

it('should return an error response for invalid count parameter', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('NekosiaAPI', () => {

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

it('should correctly call fetchImages with additionalTags', async () => {
Expand Down
17 changes: 17 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ declare module 'nekosia.js' {
attribution: ImageAttribution;
}

/**
* GET https://api.nekosia.cat/api/v1/tags
*/
interface TagsResponse {
status: number,
success: boolean,
tags: string[],
anime: string[],
characters: string[]
}

/**
* Nekosia API class, containing methods for fetching images.
* All methods are asynchronous and return a Promise resolving to an `ImageResponse`.
Expand Down Expand Up @@ -407,6 +418,12 @@ declare module 'nekosia.js' {
*/
static fetchImages(options?: FetchImagesOptions): Promise<ImageResponse>;

/**
* Fetches the latest array with tags, anime titles, and characters
* @returns A Promise resolving to an `ImageResponse`.
*/
static fetchTags(id: string): Promise<TagsResponse>;

/**
* Fetches an image by its identifier.
* @param id - The image identifier (e.g., `66ae26a07886f165901e8a3f`).
Expand Down

0 comments on commit 6a7febb

Please sign in to comment.