diff --git a/.all-contributorsrc b/.all-contributorsrc index 98bd9b6..0fb0186 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -55,6 +55,15 @@ "contributions": [ "code" ] + }, + { + "login": "maple3142", + "name": "maple", + "avatar_url": "https://avatars1.githubusercontent.com/u/9370547?v=4", + "profile": "https://blog.maple3142.net/", + "contributions": [ + "code" + ] } ], "repoType": "github", diff --git a/readme.md b/readme.md index 85222dd..fd60422 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ # pixiv-app-api [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) -[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) > Promise base pixiv API client @@ -80,6 +80,22 @@ See examples. #### nextQuery(): string; +#### makeIterable(resp?: Object): AsyncIterable + +
+ +```js +const json = await pixiv.searchIllust('艦これ10000users入り') +let ar = [] +for await (const r of pixiv.makeIterable(json)){ + ar = ar.concat(r.illusts) + await sleep(1000) // if the request rate is too high, pixiv might ban you +} +console.log(ar.length) +``` + +
+ #### userDetail(id: ID, params?: Object): Promise; #### userIllusts(id: ID, params?: Object): Promise; @@ -528,6 +544,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds Cake
Cake

💻 ⚠️ Ade Firman Fauzi
Ade Firman Fauzi

💻 yeti2018
yeti2018

💻 + maple
maple

💻 diff --git a/src/index.test.ts b/src/index.test.ts index 6fa99fb..6fcc46d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -133,3 +133,10 @@ test('not camelcaseKeys', async () => { const json = await pixiv.userIllusts(userId, { userId: 2957827 }) expect({}.hasOwnProperty.call(json, 'next_url')).toBe(false) }) + +test('makeIterable', async () => { + expect.assertions(1) + const json = await pixiv.userIllusts(userId, { userId: 2957827 }) + const iterable = pixiv.makeIterable(json) + expect({}.hasOwnProperty.call(iterable, Symbol.asyncIterator)).toBe(true) +}) diff --git a/src/index.ts b/src/index.ts index 73f3a74..38f6ebe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,6 +114,23 @@ class PixivApp { return url.parse(this.nextUrl, true).params } + makeIterable(resp: object): AsyncIterable { + // eslint-disable-next-line @typescript-eslint/no-this-alias + const self = this + const nextUrl = this.camelcaseKeys ? 'nextUrl' : 'next_url' + return { + async *[Symbol.asyncIterator]() { + yield resp + // eslint-disable-next-line require-atomic-updates + while (resp[nextUrl]) { + // eslint-disable-next-line require-atomic-updates, no-await-in-loop + resp = await self.fetch(resp[nextUrl]) + yield resp + } + } + } + } + userDetail(id: number, params = {}) { params = { user_id: id,