Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
feat: add method to retrieve information about stickers (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
szdc committed Feb 3, 2019
1 parent a6f06c9 commit d2651e8
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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('<sticker_id>')
.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(['<sticker_id>', '<sticker_id>'])
.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.
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<API.GetStickersResponse>('aweme/v1/sticker/detail/', {
params: <API.GetStickersRequest>{
sticker_ids: stickerIds.join(','),
},
})

/**
* Joins a live stream.
*
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
48 changes: 48 additions & 0 deletions src/types/sticker.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
105 changes: 105 additions & 0 deletions test/sticker.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
25 changes: 25 additions & 0 deletions test/testdata/getSticker.json
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions test/testdata/getStickers.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d2651e8

Please sign in to comment.