From d2651e8f4c148f6f944342c1039b005f9d7e3817 Mon Sep 17 00:00:00 2001 From: Jack Willis-Craig <4758854+szdc@users.noreply.github.com> Date: Sun, 3 Feb 2019 18:37:12 +1000 Subject: [PATCH] feat: add method to retrieve information about stickers (#86) --- README.md | 34 +++++++++++ src/index.ts | 18 ++++++ src/types/index.d.ts | 1 + src/types/sticker.d.ts | 48 +++++++++++++++ test/sticker.spec.ts | 105 +++++++++++++++++++++++++++++++++ test/testdata/getSticker.json | 25 ++++++++ test/testdata/getStickers.json | 42 +++++++++++++ 7 files changed, 273 insertions(+) create mode 100644 src/types/sticker.d.ts create mode 100644 test/sticker.spec.ts create mode 100644 test/testdata/getSticker.json create mode 100644 test/testdata/getStickers.json diff --git a/README.md b/README.md index ec500e4..71e0afa 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ const api = new TikTokAPI(params, { signURL }); * [.listPostsInHashtag(params)](#listpostsinhashtagparams) * [.listForYouFeed([params])](#listforyoufeedparams) * [.listFollowingFeed([params])](#listfollowingfeedparams) +* [.getSticker(id)](#getstickerid) +* [.getStickers([ids])](#getstickersids) * [.joinLiveStream(id)](#joinlivestreamid) * [.leaveLiveStream(id)](#leavelivestreamid) * [.canStartLiveStream()](#canstartlivestream) @@ -472,6 +474,38 @@ api.listFollowingFeed() See the [feed types](src/types/feed.d.ts) for the complete request/response objects. +#### .getSticker(id) + +Gets information about a sticker/effect. + +```javascript +api.getSticker('') + .then(res => console.log(res.data.sticker_infos)) + .catch(console.log); + +// Outputs: +// [{ id: '100000', name: 'cloned', owner_nickname: 'Effect Assistant', ...}] + +``` + +See the [sticker types](src/types/sticker.d.ts) for the complete response object. + +#### .getStickers([ids]) + +Gets information about many stickers/effects. + +```javascript +api.getStickers(['', '']) + .then(res => console.log(res.data.sticker_infos)) + .catch(console.log); + +// Outputs: +// [{ id: '100000', name: 'cloned', owner_nickname: 'Effect Assistant', ...}, ...] + +``` + +See the [sticker types](src/types/sticker.d.ts) for the complete response object. + #### .joinLiveStream(id) Joins a live stream. diff --git a/src/index.ts b/src/index.ts index 5004844..7f8763e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -408,6 +408,24 @@ export default class TikTokAPI { }), }) + /** + * Gets information about a sticker/effect. + * + * @param stickerId + */ + getSticker = (stickerId: string) => this.getStickers([stickerId]); + + /** + * Gets information about many stickers/effects. + * + * @param stickerIds + */ + getStickers = (stickerIds: string[]) => this.request.get('aweme/v1/sticker/detail/', { + params: { + sticker_ids: stickerIds.join(','), + }, + }) + /** * Joins a live stream. * diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 6a3b135..ac32876 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -13,6 +13,7 @@ export * from './post'; export * from './qr-code'; export * from './request'; export * from './search'; +export * from './sticker'; export * from './tag'; export * from './user'; export * from './video'; diff --git a/src/types/sticker.d.ts b/src/types/sticker.d.ts new file mode 100644 index 0000000..ded13db --- /dev/null +++ b/src/types/sticker.d.ts @@ -0,0 +1,48 @@ +import { + BaseResponseData, + Media, +} from './request'; + +export interface GetStickersRequest { + /** A list of sticker ids to get information about */ + sticker_ids: string; +} + +export interface GetStickersResponse extends BaseResponseData { + sticker_infos: Sticker[]; +} + +export interface Sticker { + /** ??? */ + children: []; + + /** A description of the sticker */ + desc: string; + + /** The ID of the sticker */ + effect_id: string; + + /** The icon associated with the sticker */ + icon_url: Media; + + /** The ID of the sticker */ + id: string; + + /** True if the current user has favorited the sticker */ + is_favorite: boolean; + + /** The name of the sticker */ + name: string; + + /** The ID the user that owns the sticker (empty if owned by the Effect Assistant) */ + owner_id: string; + + /** The nickname of the owner, e.g. "Effect Assistant" */ + owner_nickname: string; + + /** ??? */ + tags: any[]; + + /** The total number of posts using this sticker */ + user_count: number; +} diff --git a/test/sticker.spec.ts b/test/sticker.spec.ts new file mode 100644 index 0000000..b2d25f1 --- /dev/null +++ b/test/sticker.spec.ts @@ -0,0 +1,105 @@ +import MockAdapter from 'axios-mock-adapter'; +import { assert } from 'chai'; +import { describe, it } from 'mocha'; + +import TikTokAPI, { GetStickersResponse } from '../src'; +import { + loadTestData, + mockConfig, + mockParams, +} from './util'; + +describe('#getSticker()', () => { + it('a successful response should match the interface', async () => { + const api = new TikTokAPI(mockParams, mockConfig); + const mock = new MockAdapter(api.request); + + mock + .onGet(new RegExp('aweme/v1/sticker/detail/\?.*')) + .reply(200, loadTestData('getSticker.json'), {}); + + const res = await api.getSticker('100000'); + const expected: GetStickersResponse = { + extra: { + now: 1000000000000, + }, + sticker_infos: [ + { + children: [], + desc: '', + effect_id: '100000', + icon_url: { + url_list: [ + 'http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/111111', + ], + }, + id: '100000', + is_favorite: false, + name: 'sticker1', + owner_id: '', + owner_nickname: 'Effect Assistant', + tags: [], + user_count: 10000, + }, + ], + status_code: 0, + }; + assert.deepStrictEqual(res.data, expected); + }); +}); + +describe('#getStickers()', () => { + it('a successful response should match the interface', async () => { + const api = new TikTokAPI(mockParams, mockConfig); + const mock = new MockAdapter(api.request); + + mock + .onGet(new RegExp('aweme/v1/sticker/detail/\?.*')) + .reply(200, loadTestData('getStickers.json'), {}); + + const res = await api.getStickers(['100000', '100001']); + const expected: GetStickersResponse = { + extra: { + now: 1000000000000, + }, + sticker_infos: [ + { + children: [], + desc: '', + effect_id: '100000', + icon_url: { + url_list: [ + 'http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/111111', + ], + }, + id: '100000', + is_favorite: false, + name: 'sticker1', + owner_id: '', + owner_nickname: 'Effect Assistant', + tags: [], + user_count: 10000, + }, + { + children: [], + desc: '', + effect_id: '100001', + icon_url: { + url_list: [ + 'http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/222222', + ], + }, + id: '100001', + is_favorite: false, + name: 'sticker2', + owner_id: '', + owner_nickname: 'Effect Assistant', + tags: [], + user_count: 10001, + }, + ], + status_code: 0, + }; + assert.deepStrictEqual(res.data, expected); + }); +}); diff --git a/test/testdata/getSticker.json b/test/testdata/getSticker.json new file mode 100644 index 0000000..5ca9475 --- /dev/null +++ b/test/testdata/getSticker.json @@ -0,0 +1,25 @@ +{ + "extra": { + "now": 1000000000000 + }, + "sticker_infos": [ + { + "children": [], + "desc": "", + "effect_id": "100000", + "icon_url": { + "url_list": [ + "http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/111111" + ] + }, + "id": "100000", + "is_favorite": false, + "name": "sticker1", + "owner_id": "", + "owner_nickname": "Effect Assistant", + "tags": [], + "user_count": 10000 + } + ], + "status_code": 0 +} diff --git a/test/testdata/getStickers.json b/test/testdata/getStickers.json new file mode 100644 index 0000000..82c5599 --- /dev/null +++ b/test/testdata/getStickers.json @@ -0,0 +1,42 @@ +{ + "extra": { + "now": 1000000000000 + }, + "sticker_infos": [ + { + "children": [], + "desc": "", + "effect_id": "100000", + "icon_url": { + "url_list": [ + "http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/111111" + ] + }, + "id": "100000", + "is_favorite": false, + "name": "sticker1", + "owner_id": "", + "owner_nickname": "Effect Assistant", + "tags": [], + "user_count": 10000 + }, + { + "children": [], + "desc": "", + "effect_id": "100001", + "icon_url": { + "url_list": [ + "http://sf-tk-sg.ibytedtos.com/obj/ies.fe.effect.alisg/222222" + ] + }, + "id": "100001", + "is_favorite": false, + "name": "sticker2", + "owner_id": "", + "owner_nickname": "Effect Assistant", + "tags": [], + "user_count": 10001 + } + ], + "status_code": 0 +}