Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
Add support for customized host
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Mar 15, 2021
1 parent 26259b1 commit 938778d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "10"
- "12"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# future-diary-sdk

[![](https://badges.greenkeeper.io/future-diary/future-diary-sdk.svg)](https://greenkeeper.io/)
[![](https://img.shields.io/travis/future-diary/future-diary-sdk/master.svg)](https://travis-ci.org/future-diary/future-diary-sdk)
[![](https://img.shields.io/npm/v/future-diary-sdk.svg)](https://www.npmjs.com/package/future-diary-sdk)
[![](https://img.shields.io/npm/l/future-diary-sdk.svg)](https://github.com/future-diary/future-diary-sdk/blob/master/LICENSE)
[![](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
Expand All @@ -20,6 +18,7 @@ $ npm i future-diary-sdk
const Future = require('future-diary-sdk');

const f = new Future({
host: 'weilairiji.com',
username: '',
password: ''
});
Expand Down
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ const urlencode = require('urlencode');

class FutureDiary {
constructor(opt) {
this.host = 'weilairiji.com';
this.host = opt.host || 'weilairiji.com';
this.username = opt.username || '';
this.password = opt.password || '';
this.endpoint = `http://${this.host}`;
}

async get(uri, params) {
async get(uri, parameters) {
const url = this.endpoint + '/api' + uri + '.json';
const {body} = await got.get(url, {
encoding: null,
auth: `${this.username}:${this.password}`,
query: params
query: parameters
});
const result = iconv.decode(body, 'gb2312');
return FutureDiary._parseJson(result);
}

async post(uri, params) {
async post(uri, parameters) {
const url = this.endpoint + '/api' + uri + '.json';
const {body} = await got.post(url, {
encoding: null,
auth: `${this.username}:${this.password}`,
headers: {'content-type': 'application/x-www-form-urlencoded; charset=gb2312'},
from: true,
body: urlencode.stringify(params, {charset: 'gb2312'})
body: urlencode.stringify(parameters, {charset: 'gb2312'})
});
const result = iconv.decode(body, 'gb2312');
return FutureDiary._parseJson(result);
}

static _parseJson(str) {
const result = str
.replace(/"reply_to_status_id"="/g, `"reply_to_status_id":"`)
.replace(/"reply_to_user_id"="/g, `"reply_to_user_id":"`);
static _parseJson(string) {
const result = string
.replace(/"reply_to_status_id"="/g, '"reply_to_status_id":"')
.replace(/"reply_to_user_id"="/g, '"reply_to_user_id":"');
try {
return JSON.parse(result);
} catch (err) {
} catch {
return result;
}
}
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.0.5",
"description": "Future Diary SDK for Node.js",
"main": "index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo"
},
Expand All @@ -18,7 +21,7 @@
"homepage": "https://github.com/future-diary/future-diary-sdk#readme",
"devDependencies": {
"ava": "^1.0.0-beta.7",
"xo": "^0.22.0"
"xo": "^0.38.2"
},
"dependencies": {
"got": "^9.0.0",
Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const test = require('ava');
const Future = require('.');

const {
FD_HOST: host,
FD_USERNAME: username,
FD_PASSWORD: password
} = process.env;

const f = new Future({username, password});
const f = new Future({host, username, password});

test('test fd.get() without query', async t => {
const result = await f.get('/statuses/user_timeline');
Expand Down

0 comments on commit 938778d

Please sign in to comment.