Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
feat: Switch methods to use v3
Browse files Browse the repository at this point in the history
  • Loading branch information
crookedneighbor committed May 23, 2016
1 parent d174535 commit 44f9cd3
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@ let Habitica = require('habitica');
let api = new Habitica({ let api = new Habitica({
uuid: 'your-habitica.com-user-id', uuid: 'your-habitica.com-user-id',
token: 'your-habitica.com-api-token', 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
}); });
``` ```


Expand Down
14 changes: 7 additions & 7 deletions src/account.js
Expand Up @@ -47,10 +47,10 @@ export default class {
confirmPassword: password confirmPassword: password
} }


let user = await this._connection.post( let {data: user} = await this._connection.post(
'register', 'user/auth/local/register',
{send: creds} {send: creds}
) )


this._connection.setCredentials({ this._connection.setCredentials({
uuid: user._id, uuid: user._id,
Expand Down Expand Up @@ -79,14 +79,14 @@ export default class {
username: usernameEmail, username: usernameEmail,
password password
} }
let creds = await this._connection.post( let {data: creds} = await this._connection.post(
'user/auth/local', 'user/auth/local/login',
{send: loginCreds} {send: loginCreds}
) )


this._connection.setCredentials({ this._connection.setCredentials({
uuid: creds.id, uuid: creds.id,
token: creds.token token: creds.apiToken
}) })


return creds return creds
Expand Down
6 changes: 3 additions & 3 deletions src/content.js
Expand Up @@ -36,7 +36,7 @@ export default class {
// }) // })
// ``` // ```
async get (path) { async get (path) {
let content = await this._connection.get('content') let {data: content} = await this._connection.get('content')


if (path) { if (path) {
let nestedContent = get(content, path) let nestedContent = get(content, path)
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class {
// }) // })
// ``` // ```
async getKeys (path) { async getKeys (path) {
let content = await this._connection.get('content') let {data: content} = await this._connection.get('content')
if (path) { if (path) {
let nestedContent = get(content, path) let nestedContent = get(content, path)


Expand All @@ -98,7 +98,7 @@ export default class {
// }) // })
// ``` // ```
async getUserPaths () { async getUserPaths () {
let paths = await this._connection.get('content/paths') let {data: paths} = await this._connection.get('models/user/paths')


return paths return paths
} }
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -16,7 +16,7 @@ module.exports = class {
// //
// Set up your instance of Habitica. // 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 // ```js
// let Habitica = require('habitica') // let Habitica = require('habitica')
Expand Down Expand Up @@ -98,7 +98,7 @@ module.exports = class {
// api.setCredentials({ // api.setCredentials({
// uuid: 'new-user-id', // uuid: 'new-user-id',
// token: 'new-api-token', // token: 'new-api-token',
// endpoint: 'http://localhost:3000/api/v2' // endpoint: 'http://localhost:3000/api/v3'
// }) // })
// ``` // ```
setCredentials (creds) { setCredentials (creds) {
Expand Down
14 changes: 10 additions & 4 deletions src/lib/connection.js
Expand Up @@ -5,7 +5,7 @@ export default class {
constructor (options) { constructor (options) {
this._uuid = options.uuid this._uuid = options.uuid
this._token = options.token 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 this.delete = this.del
} }
Expand All @@ -23,9 +23,15 @@ export default class {
} }


setCredentials (creds = {}) { setCredentials (creds = {}) {
this._uuid = creds.uuid || this._uuid if (creds.uuid !== undefined) {
this._token = creds.token || this._token this._uuid = creds.uuid
this._endpoint = creds.endpoint || this._endpoint }
if (creds.token !== undefined) {
this._token = creds.token
}
if (creds.endpoint !== undefined) {
this._endpoint = creds.endpoint
}
} }


get (route, options = {}) { get (route, options = {}) {
Expand Down
14 changes: 7 additions & 7 deletions src/tag.js
Expand Up @@ -31,13 +31,13 @@ export default class {
// }) // })
// ``` // ```
async get (id) { async get (id) {
let url = 'user/tags' let url = 'tags'


if (id) { if (id) {
url += `/${id}` url += `/${id}`
} }


let tags = await this._connection.get(url) let {data: tags} = await this._connection.get(url)


return tags return tags
} }
Expand All @@ -55,8 +55,8 @@ export default class {
// }) // })
// ``` // ```
async post (tagBody) { async post (tagBody) {
let tags = await this._connection.post( let {data: tags} = await this._connection.post(
'user/tags', 'tags',
{send: tagBody} {send: tagBody}
) )


Expand All @@ -80,8 +80,8 @@ export default class {
if (!id) throw new IME.MissingArgumentError('Tag id is required') if (!id) throw new IME.MissingArgumentError('Tag id is required')
if (!tagBody) throw new IME.MissingArgumentError('Tag body is required') if (!tagBody) throw new IME.MissingArgumentError('Tag body is required')


let tag = await this._connection.put( let {data: tag} = await this._connection.put(
`user/tags/${id}`, `tags/${id}`,
{ send: tagBody } { send: tagBody }
) )


Expand All @@ -101,7 +101,7 @@ export default class {
async del (id) { async del (id) {
if (!id) throw new IME.MissingArgumentError('Tag id is required') 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 return remainingTags
} }
Expand Down
36 changes: 18 additions & 18 deletions src/task.js
@@ -1,7 +1,6 @@
// Task // Task
// checkmark box // checkmark box
// Get em, check em, level up! // Get em, check em, level up!
import filter from 'lodash/filter'
import {INTERNAL_MODULE_ERRORS as IME} from './lib/errors' import {INTERNAL_MODULE_ERRORS as IME} from './lib/errors'


export default class { export default class {
Expand Down Expand Up @@ -31,13 +30,15 @@ export default class {
// }) // })
// ``` // ```
async get (id) { async get (id) {
let url = 'user/tasks' let url = 'tasks'


if (id) { if (id) {
url += `/${id}` url += `/${id}`
} else {
url += '/user'
} }


let tasks = await this._connection.get(url) let {data: tasks} = await this._connection.get(url)


return tasks return tasks
} }
Expand All @@ -52,7 +53,7 @@ export default class {
// }) // })
// ``` // ```
async getDailys () { async getDailys () {
return this._filterTasksByType('daily') return this._filterTasksByType('dailys')
} }


// # task.getRewards() // # task.getRewards()
Expand All @@ -65,7 +66,7 @@ export default class {
// }) // })
// ``` // ```
async getRewards () { async getRewards () {
return this._filterTasksByType('reward') return this._filterTasksByType('rewards')
} }


// # task.getHabits() // # task.getHabits()
Expand All @@ -78,7 +79,7 @@ export default class {
// }) // })
// ``` // ```
async getHabits () { async getHabits () {
return this._filterTasksByType('habit') return this._filterTasksByType('habits')
} }


// # task.getTodos() // # task.getTodos()
Expand All @@ -91,7 +92,7 @@ export default class {
// }) // })
// ``` // ```
async getTodos () { async getTodos () {
return this._filterTasksByType('todo') return this._filterTasksByType('todos')
} }


// # task.score() // # task.score()
Expand Down Expand Up @@ -146,8 +147,8 @@ export default class {
async score (id, direction = 'up', body = {}) { async score (id, direction = 'up', body = {}) {
if (!id) throw new IME.MissingArgumentError('Task id is required') if (!id) throw new IME.MissingArgumentError('Task id is required')


let stats = await this._connection.post( let {data: stats} = await this._connection.post(
`user/tasks/${id}/${direction}`, `tasks/${id}/score/${direction}`,
{ send: body } { send: body }
) )


Expand All @@ -169,8 +170,8 @@ export default class {
// }) // })
// ``` // ```
async post (taskBody) { async post (taskBody) {
let task = await this._connection.post( let {data: task} = await this._connection.post(
'user/tasks', 'tasks/user',
{ send: taskBody } { send: taskBody }
) )
return task return task
Expand All @@ -193,8 +194,8 @@ export default class {
if (!id) throw new IME.MissingArgumentError('Task id is required') if (!id) throw new IME.MissingArgumentError('Task id is required')
if (!taskBody) throw new IME.MissingArgumentError('Task body is required') if (!taskBody) throw new IME.MissingArgumentError('Task body is required')


let task = await this._connection.put( let {data: task} = await this._connection.put(
`user/tasks/${id}`, `tasks/${id}`,
{ send: taskBody } { send: taskBody }
) )


Expand All @@ -214,17 +215,16 @@ export default class {
async del (id) { async del (id) {
if (!id) throw new IME.MissingArgumentError('Task id is required') 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 return task
} }


// NOOP // NOOP
async _filterTasksByType (type) { async _filterTasksByType (type) {
let tasks = await this._connection.get('user/tasks') let {data: tasks} = await this._connection.get('tasks/user', {
let taskByType = filter(tasks, (task) => { query: {type}
return task.type === type
}) })
return taskByType return tasks
} }
} }
4 changes: 2 additions & 2 deletions src/user.js
Expand Up @@ -17,7 +17,7 @@ export default class {
// }) // })
// ``` // ```
async get () { async get () {
let user = await this._connection.get('user') let {data: user} = await this._connection.get('user')


return user return user
} }
Expand All @@ -40,7 +40,7 @@ export default class {
// }) // })
// ``` // ```
async getBuyableGear () { async getBuyableGear () {
let gear = await this._connection.get('user/inventory/buy') let {data: gear} = await this._connection.get('user/inventory/buy')


return gear return gear
} }
Expand Down
2 changes: 1 addition & 1 deletion tasks/start-server.js
Expand Up @@ -13,7 +13,7 @@ if (args.length > 2) {
process.env.PORT = process.env.HABITICA_PORT || 3321 process.env.PORT = process.env.HABITICA_PORT || 3321
process.env.NODE_DB_URI = process.env.HABITICA_DB_URI || 'mongodb://localhost/habitica-node-test' 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 () { server.listen(process.env.PORT, function () {
if (command) { if (command) {
Expand Down
14 changes: 7 additions & 7 deletions test/integration/account.test.js
Expand Up @@ -8,7 +8,7 @@ describe('Account', () => {


beforeEach(() => { beforeEach(() => {
api = new Habitica({ api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2` endpoint: `localhost:${process.env.PORT}/api/v3`
}) })
username = generateRandomUserName() username = generateRandomUserName()
password = 'password' password = 'password'
Expand Down Expand Up @@ -94,10 +94,10 @@ describe('Account', () => {


beforeEach(async function () { beforeEach(async function () {
let registerApi = new Habitica({ let registerApi = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2` endpoint: `localhost:${process.env.PORT}/api/v3`
}) })
api = new Habitica({ api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2` endpoint: `localhost:${process.env.PORT}/api/v3`
}) })


username = generateRandomUserName() username = generateRandomUserName()
Expand All @@ -112,28 +112,28 @@ describe('Account', () => {
let creds = await api.account.login(username, password) let creds = await api.account.login(username, password)


expect(creds.id).to.exist 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 () { it('sets uuid and token after logging in with username', async function () {
let creds = await api.account.login(username, password) let creds = await api.account.login(username, password)


expect(api.getUuid()).to.be.eql(creds.id) 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 () { it('logs in with email and password', async function () {
let creds = await api.account.login(email, password) let creds = await api.account.login(email, password)


expect(creds.id).to.exist 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 () { it('sets uuid and token after logging in with email', async function () {
let creds = await api.account.login(email, password) let creds = await api.account.login(email, password)


expect(api.getUuid()).to.be.eql(creds.id) expect(api.getUuid()).to.be.eql(creds.id)
expect(api.getToken()).to.be.eql(creds.token) expect(api.getToken()).to.be.eql(creds.apiToken)
}) })
}) })


Expand Down
2 changes: 1 addition & 1 deletion test/integration/content.test.js
Expand Up @@ -3,7 +3,7 @@ import {INTERNAL_MODULE_ERRORS as IME} from '../../src/lib/errors'


describe('Content', () => { describe('Content', () => {
let api = new Habitica({ let api = new Habitica({
endpoint: `localhost:${process.env.PORT}/api/v2` endpoint: `localhost:${process.env.PORT}/api/v3`
}) })


describe('#get', () => { describe('#get', () => {
Expand Down

0 comments on commit 44f9cd3

Please sign in to comment.