Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 - Added support for get_auth_backends, dev_get_emails and dev_fetch_api_key #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ zulip({
realm: process.env.ZULIP_REALM,
}).then(z => z.accounts.retrieve())
.then(console.log);

zulip({
username: process.env.ZULIP_USERNAME,
password: process.env.ZULIP_PASSWORD,
realm: process.env.ZULIP_REALM,
}).then(z => z.accounts.dev.retrieve())
.then(console.log);
14 changes: 14 additions & 0 deletions examples/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const zulip = require('../lib/');

const config = {
username: process.env.ZULIP_USERNAME,
password: process.env.ZULIP_PASSWORD,
realm: process.env.ZULIP_REALM,
};

zulip(config)
.then((z) => {
// [ 'password', 'dev' ]
z.auth.backends.retrieve().then(console.log);
})
.catch(err => console.log(err.msg));
11 changes: 11 additions & 0 deletions examples/emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const zulip = require('../lib/');

const config = {
username: process.env.ZULIP_USERNAME,
password: process.env.ZULIP_PASSWORD,
realm: process.env.ZULIP_REALM,
};

zulip(config).then((z) => {
z.emails.dev.retrieve().then(console.log);
}).catch(err => console.log(err.msg));
6 changes: 6 additions & 0 deletions examples/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ zulip(config)
// Prints
// { msg: '', pointer: 3432741029383298, result: 'success' }
z.users.me.pointer.retrieve().then(console.log);

const presenceParams = {
status: 'active',
new_user_input: true,
};
z.users.me.presence.retrieve(presenceParams).then(console.log).catch(console.log);
})
.catch(err => console.log(err.msg));
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const events = require('./resources/events');
const users = require('./resources/users');
const emojis = require('./resources/emojis');
const typing = require('./resources/typing');
const auth = require('./resources/auth');
const emails = require('./resources/emails');

function resources(config) {
return {
Expand All @@ -20,6 +22,8 @@ function resources(config) {
users: users(config),
emojis: emojis(config),
typing: typing(config),
auth: auth(config),
emails: emails(config),
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/resources/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ function accounts(config) {
body: form,
}).then(res => res.json());
},
dev: {
retrieve: () => {
const url = `${config.apiURL}/dev_fetch_api_key`;
const form = new FormData();
form.append('username', config.username);
return fetch(url, {
method: 'POST',
body: form,
}).then(res => res.json());
},
},
};
}

Expand Down
35 changes: 35 additions & 0 deletions src/resources/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const api = require('../api');

function auth(config) {
return {
backends: {
/**
* Retrieves the auth backends available. Returns a promise which in turns will return an
* array of available backend names
* @param params
* @returns {*}
*/
retrieve: (params) => {
const url = `${config.apiURL}/get_auth_backends`;

return api(url, config, 'GET', params).then((res) => {
const backends = [];

if (res.result === 'success') {
if (res.password) backends.push('password');
if (res.google) backends.push('google');
if (res.dev) backends.push('dev');
}

if (!backends) {
throw new Error('No backends available.');
}

return backends;
});
},
},
};
}

module.exports = auth;
15 changes: 15 additions & 0 deletions src/resources/emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const api = require('../api');

function emails(config) {
return {
dev: {
retrieve: (params) => {
const url = `${config.apiURL}/dev_get_emails`;

return api(url, config, 'GET', params);
},
},
};
}

module.exports = emails;
7 changes: 7 additions & 0 deletions src/resources/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ function messages(config) {
const url = `${config.apiURL}/messages`;
return api(url, config, 'POST', params);
},
// TODO : Improve and finalize
flags: {
retrieve: (params) => {
const url = `${config.apiURL}/messages/flags`;
return api(url, config, 'POST', params);
},
},
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/resources/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function users(config) {
return api(url, config, 'GET', params);
},
},
presence: {
retrieve: (params) => {
const url = `${config.apiURL}/users/me/presence`;
return api(url, config, 'POST', params);
},
},
},
};
}
Expand Down