Skip to content

Commit

Permalink
Merge pull request #19 from maple3142/master
Browse files Browse the repository at this point in the history
add a utility function makeIterable
  • Loading branch information
akameco authored Sep 27, 2019
2 parents 75a61fe + c0a9cc1 commit 9b833ef
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 18 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -80,6 +80,22 @@ See examples.

#### nextQuery(): string;

#### makeIterable(resp?: Object): AsyncIterable<Object>

<details>

```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)
```

</details>

#### userDetail(id: ID, params?: Object): Promise<Object>;

#### userIllusts(id: ID, params?: Object): Promise<Object>;
Expand Down Expand Up @@ -528,6 +544,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
<td align="center"><a href="https://github.com/najimi"><img src="https://avatars3.githubusercontent.com/u/2237174?v=4" width="100px;" alt="Cake"/><br /><sub><b>Cake</b></sub></a><br /><a href="https://github.com/akameco/pixiv-app-api/commits?author=najimi" title="Code">💻</a> <a href="https://github.com/akameco/pixiv-app-api/commits?author=najimi" title="Tests">⚠️</a></td>
<td align="center"><a href="https://www.linkedin.com/in/adefirmanf/"><img src="https://avatars0.githubusercontent.com/u/23324722?v=4" width="100px;" alt="Ade Firman Fauzi"/><br /><sub><b>Ade Firman Fauzi</b></sub></a><br /><a href="https://github.com/akameco/pixiv-app-api/commits?author=adefirmanf" title="Code">💻</a></td>
<td align="center"><a href="https://www.linkedin.com/in/jiefenghe/"><img src="https://avatars0.githubusercontent.com/u/4796423?v=4" width="100px;" alt="yeti2018"/><br /><sub><b>yeti2018</b></sub></a><br /><a href="https://github.com/akameco/pixiv-app-api/commits?author=yeti2018" title="Code">💻</a></td>
<td align="center"><a href="https://blog.maple3142.net/"><img src="https://avatars1.githubusercontent.com/u/9370547?v=4" width="100px;" alt="maple"/><br /><sub><b>maple</b></sub></a><br /><a href="https://github.com/akameco/pixiv-app-api/commits?author=maple3142" title="Code">💻</a></td>
</tr>
</table>

Expand Down
7 changes: 7 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ class PixivApp {
return url.parse(this.nextUrl, true).params
}

makeIterable(resp: object): AsyncIterable<object> {
// 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,
Expand Down

0 comments on commit 9b833ef

Please sign in to comment.