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 for retrieving a user's QR code
Browse files Browse the repository at this point in the history
  • Loading branch information
szdc committed Nov 24, 2018
1 parent be81cfe commit 09271f0
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const api = new TikTokAPI(params, { signURL });
* [.loginWithEmail(email, password)](#loginwithemailemail-password)
* [.getUser(id)](#getuserid)
* [.searchUsers(params)](#searchusersparams)
* [.getQRCode(id, [schemaType])](#getqrcodeid-schematype)
* [.listPosts(params)](#listpostsparams)
* [.listFollowers(params)](#listfollowersparams)
* [.listFollowing(params)](#listfollowingparams)
Expand Down Expand Up @@ -123,6 +124,22 @@ api.searchUsers({

See the [search types](src/types/search.d.ts) for the complete request/response objects.

#### .getQRCode(id, [schemaType])

Gets the QR code for a user.

```javascript
api.getQRCode('<user_id>')
.then(res => console.log(res.data.qrcode_url.url_list[0]))
.catch(console.log);

// Outputs:
// 'http://p16.muscdn.com/img/musically-qrcode/1111111111111111111~c5_720x720.image'

```

See the [QR code types](src/types/qr-code.d.ts) for the complete request/response objects.

#### .listPosts(params)

Lists a user's posts.
Expand Down
25 changes: 25 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ export default class TikTokAPI {
},
})

/**
* Gets the QR code image for a user.
*
* @param userId
* @param schemaType
* @returns {AxiosPromise<QRCodeResponse | BaseResponseData>}
*/
getQRCode = (userId: string, schemaType = 4) =>
this.request.post<API.QRCodeResponse | API.BaseResponseData>(
'aweme/v1/fancy/qrcode/info/',
qs.stringify(<API.QRCodeRequest>{
schema_type: schemaType,
object_id: userId,
}),
{
headers: {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
params: {
js_sdk_version: '',
app_type: 'normal',
},
},
)

/**
* Lists a user's posts.
*
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 @@ -10,6 +10,7 @@ export * from './live-stream';
export * from './login';
export * from './music';
export * from './post';
export * from './qr-code';
export * from './request';
export * from './search';
export * from './tag';
Expand Down
20 changes: 20 additions & 0 deletions src/types/qr-code.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BaseResponseData } from './request';

export interface QRCodeRequest {
/** The internal version to use; currently 4 */
schema_type: number;

/** The ID of the user to get a QR code for */
object_id: string;
}

export interface QRCodeResponse extends BaseResponseData {
/** Contains a link to the QR code */
qrcode_url: {
/** An in-app link to the QR code */
uri: string;

/** Contains a public link to the QR code image (first element in array) */
url_list: string[];
};
}
37 changes: 37 additions & 0 deletions test/qr-code.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import MockAdapter from 'axios-mock-adapter';
import { assert } from 'chai';
import { describe, it } from 'mocha';

import TikTokAPI, { QRCodeResponse } from '../src';
import {
loadTestData,
mockConfig,
mockParams,
} from './util';

describe('#getQRCode()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
const userId = '9999999999999999999';

mock
.onPost(new RegExp('aweme/v1/fancy/qrcode/info/\?.*'))
.reply(200, loadTestData('getQRCode.json'), {});

const res = await api.getQRCode(userId);
const expected: QRCodeResponse = {
extra: {
now: 1000000000000,
},
qrcode_url: {
uri: 'musically-qrcode/1111111111111111111',
url_list: [
'http://p16.muscdn.com/img/musically-qrcode/1111111111111111111~c5_720x720.image',
],
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
12 changes: 12 additions & 0 deletions test/testdata/getQRCode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extra": {
"now": 1000000000000
},
"qrcode_url": {
"uri": "musically-qrcode/1111111111111111111",
"url_list": [
"http://p16.muscdn.com/img/musically-qrcode/1111111111111111111~c5_720x720.image"
]
},
"status_code": 0
}

0 comments on commit 09271f0

Please sign in to comment.