Permalink
Browse files

feat: Switch methods to use v3

  • Loading branch information...
crookedneighbor committed May 19, 2016
1 parent d174535 commit 44f9cd3df3dac7c7875e0af437d7cd2e86bb9b91
View
@@ -11,7 +11,7 @@ let Habitica = require('habitica');
let api = new Habitica({
uuid: 'your-habitica.com-user-id',
token: 'your-habitica.com-api-token',
endpoint: 'custom-url', // defaults to https://habitica.com/api/v2
endpoint: 'custom-url', // defaults to https://habitica.com/api/v3
});
```
View
@@ -47,10 +47,10 @@ export default class {
confirmPassword: password
}
let user = await this._connection.post(
'register',
{send: creds}
)
let {data: user} = await this._connection.post(
'user/auth/local/register',
{send: creds}
)
this._connection.setCredentials({
uuid: user._id,
@@ -79,14 +79,14 @@ export default class {
username: usernameEmail,
password
}
let creds = await this._connection.post(
'user/auth/local',
let {data: creds} = await this._connection.post(
'user/auth/local/login',
{send: loginCreds}
)
this._connection.setCredentials({
uuid: creds.id,
token: creds.token
token: creds.apiToken
})
return creds
View
@@ -36,7 +36,7 @@ export default class {
// })
// ```
async get (path) {
let content = await this._connection.get('content')
let {data: content} = await this._connection.get('content')
if (path) {
let nestedContent = get(content, path)
@@ -71,7 +71,7 @@ export default class {
// })
// ```
async getKeys (path) {
let content = await this._connection.get('content')
let {data: content} = await this._connection.get('content')
if (path) {
let nestedContent = get(content, path)
@@ -98,7 +98,7 @@ export default class {
// })
// ```
async getUserPaths () {
let paths = await this._connection.get('content/paths')
let {data: paths} = await this._connection.get('models/user/paths')
return paths
}
View
@@ -16,7 +16,7 @@ module.exports = class {
//
// Set up your instance of Habitica.
//
// The endpoint will default to https://habitica.com/api/v2 if not provided.
// The endpoint will default to https://habitica.com/api/v3 if not provided.
//
// ```js
// let Habitica = require('habitica')
@@ -98,7 +98,7 @@ module.exports = class {
// api.setCredentials({
// uuid: 'new-user-id',
// token: 'new-api-token',
// endpoint: 'http://localhost:3000/api/v2'
// endpoint: 'http://localhost:3000/api/v3'
// })
// ```
setCredentials (creds) {
View
@@ -5,7 +5,7 @@ export default class {
constructor (options) {
this._uuid = options.uuid
this._token = options.token
this._endpoint = options.endpoint || 'https://habitica.com/api/v2'
this._endpoint = options.endpoint || 'https://habitica.com/api/v3'
this.delete = this.del
}
@@ -23,9 +23,15 @@ export default class {
}
setCredentials (creds = {}) {
this._uuid = creds.uuid || this._uuid
this._token = creds.token || this._token
this._endpoint = creds.endpoint || this._endpoint
if (creds.uuid !== undefined) {
this._uuid = creds.uuid
}
if (creds.token !== undefined) {
this._token = creds.token
}
if (creds.endpoint !== undefined) {
this._endpoint = creds.endpoint
}
}
get (route, options = {}) {
View
@@ -31,13 +31,13 @@ export default class {
// })
// ```
async get (id) {
let url = 'user/tags'
let url = 'tags'
if (id) {
url += `/${id}`
}
let tags = await this._connection.get(url)
let {data: tags} = await this._connection.get(url)
return tags
}
@@ -55,8 +55,8 @@ export default class {
// })
// ```
async post (tagBody) {
let tags = await this._connection.post(
'user/tags',
let {data: tags} = await this._connection.post(
'tags',
{send: tagBody}
)
@@ -80,8 +80,8 @@ export default class {
if (!id) throw new IME.MissingArgumentError('Tag id is required')
if (!tagBody) throw new IME.MissingArgumentError('Tag body is required')
let tag = await this._connection.put(
`user/tags/${id}`,
let {data: tag} = await this._connection.put(
`tags/${id}`,
{ send: tagBody }
)
@@ -101,7 +101,7 @@ export default class {
async del (id) {
if (!id) throw new IME.MissingArgumentError('Tag id is required')
let remainingTags = await this._connection.del(`user/tags/${id}`)
let {data: remainingTags} = await this._connection.del(`tags/${id}`)
return remainingTags
}
View
@@ -1,7 +1,6 @@
// Task
// checkmark box
// Get em, check em, level up!
import filter from 'lodash/filter'
import {INTERNAL_MODULE_ERRORS as IME} from './lib/errors'
export default class {
@@ -31,13 +30,15 @@ export default class {
// })
// ```
async get (id) {
let url = 'user/tasks'
let url = 'tasks'
if (id) {
url += `/${id}`
} else {
url += '/user'
}
let tasks = await this._connection.get(url)
let {data: tasks} = await this._connection.get(url)
return tasks
}
@@ -52,7 +53,7 @@ export default class {
// })
// ```
async getDailys () {
return this._filterTasksByType('daily')
return this._filterTasksByType('dailys')
}
// # task.getRewards()
@@ -65,7 +66,7 @@ export default class {
// })
// ```
async getRewards () {
return this._filterTasksByType('reward')
return this._filterTasksByType('rewards')
}
// # task.getHabits()
@@ -78,7 +79,7 @@ export default class {
// })
// ```
async getHabits () {
return this._filterTasksByType('habit')
return this._filterTasksByType('habits')
}
// # task.getTodos()
@@ -91,7 +92,7 @@ export default class {
// })
// ```
async getTodos () {
return this._filterTasksByType('todo')
return this._filterTasksByType('todos')
}
// # task.score()
@@ -146,8 +147,8 @@ export default class {
async score (id, direction = 'up', body = {}) {
if (!id) throw new IME.MissingArgumentError('Task id is required')
let stats = await this._connection.post(
`user/tasks/${id}/${direction}`,
let {data: stats} = await this._connection.post(
`tasks/${id}/score/${direction}`,
{ send: body }
)
@@ -169,8 +170,8 @@ export default class {
// })
// ```
async post (taskBody) {
let task = await this._connection.post(
'user/tasks',
let {data: task} = await this._connection.post(
'tasks/user',
{ send: taskBody }
)
return task
@@ -193,8 +194,8 @@ export default class {
if (!id) throw new IME.MissingArgumentError('Task id is required')
if (!taskBody) throw new IME.MissingArgumentError('Task body is required')
let task = await this._connection.put(
`user/tasks/${id}`,
let {data: task} = await this._connection.put(
`tasks/${id}`,
{ send: taskBody }
)
@@ -214,17 +215,16 @@ export default class {
async del (id) {
if (!id) throw new IME.MissingArgumentError('Task id is required')
let task = await this._connection.del(`user/tasks/${id}`)
let {data: task} = await this._connection.del(`tasks/${id}`)
return task
}
// NOOP
async _filterTasksByType (type) {
let tasks = await this._connection.get('user/tasks')
let taskByType = filter(tasks, (task) => {
return task.type === type
let {data: tasks} = await this._connection.get('tasks/user', {
query: {type}
})
return taskByType
return tasks
}
}
View
@@ -17,7 +17,7 @@ export default class {
// })
// ```
async get () {
let user = await this._connection.get('user')
let {data: user} = await this._connection.get('user')
return user
}
@@ -40,7 +40,7 @@ export default class {
// })
// ```
async getBuyableGear () {
let gear = await this._connection.get('user/inventory/buy')
let {data: gear} = await this._connection.get('user/inventory/buy')
return gear
}
View
@@ -13,7 +13,7 @@ if (args.length > 2) {
process.env.PORT = process.env.HABITICA_PORT || 3321
process.env.NODE_DB_URI = process.env.HABITICA_DB_URI || 'mongodb://localhost/habitica-node-test'
var server = require(`${PATH_TO_HABITICA}/website/src/server.js`)
var server = require(`${PATH_TO_HABITICA}/website/server/`)
server.listen(process.env.PORT, function () {
if (command) {
@@ -8,7 +8,7 @@ describe('Account', () => {
beforeEach(() => {
api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2`
endpoint: `localhost:${process.env.PORT}/api/v3`
})
username = generateRandomUserName()
password = 'password'
@@ -94,10 +94,10 @@ describe('Account', () => {
beforeEach(async function () {
let registerApi = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2`
endpoint: `localhost:${process.env.PORT}/api/v3`
})
api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2`
endpoint: `localhost:${process.env.PORT}/api/v3`
})
username = generateRandomUserName()
@@ -112,28 +112,28 @@ describe('Account', () => {
let creds = await api.account.login(username, password)
expect(creds.id).to.exist
expect(creds.token).to.exist
expect(creds.apiToken).to.exist
})
it('sets uuid and token after logging in with username', async function () {
let creds = await api.account.login(username, password)
expect(api.getUuid()).to.be.eql(creds.id)
expect(api.getToken()).to.be.eql(creds.token)
expect(api.getToken()).to.be.eql(creds.apiToken)
})
it('logs in with email and password', async function () {
let creds = await api.account.login(email, password)
expect(creds.id).to.exist
expect(creds.token).to.exist
expect(creds.apiToken).to.exist
})
it('sets uuid and token after logging in with email', async function () {
let creds = await api.account.login(email, password)
expect(api.getUuid()).to.be.eql(creds.id)
expect(api.getToken()).to.be.eql(creds.token)
expect(api.getToken()).to.be.eql(creds.apiToken)
})
})
@@ -3,7 +3,7 @@ import {INTERNAL_MODULE_ERRORS as IME} from '../../src/lib/errors'
describe('Content', () => {
let api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2`
endpoint: `localhost:${process.env.PORT}/api/v3`
})
describe('#get', () => {
Oops, something went wrong.

0 comments on commit 44f9cd3

Please sign in to comment.