Skip to content

Commit

Permalink
Reformat with Prettier.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Dec 10, 2020
1 parent 3daad9c commit 53d7807
Show file tree
Hide file tree
Showing 40 changed files with 698 additions and 476 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"plugins": [
"@babel/plugin-transform-destructuring",
"@babel/plugin-transform-instanceof",
["@babel/plugin-transform-runtime",
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"proseWrap": "never",
"singleQuote": true
}
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,70 @@
# zulip-js [![Build Status](https://travis-ci.com/zulip/zulip-js.svg?branch=master)](https://travis-ci.com/github/zulip/zulip-js)

Javascript library to access the Zulip API

# Usage

## Initialization

### With API Key

```js
const zulip = require('zulip-js');
const config = {
username: process.env.ZULIP_USERNAME,
apiKey: process.env.ZULIP_API_KEY,
realm: process.env.ZULIP_REALM
realm: process.env.ZULIP_REALM,
};

zulip(config).then(zulip => {
zulip(config).then((zulip) => {
// The zulip object now initialized with config
zulip.streams.subscriptions.retrieve().then(res => {
zulip.streams.subscriptions.retrieve().then((res) => {
console.log(res);
});
});
```

### With Username & Password

You will need to first retrieve the API key by calling `zulip(config)` and then use the zulip object that it passes to `.then()`

```js
const zulip = require('zulip-js');
const config = {
username: process.env.ZULIP_USERNAME,
password: process.env.ZULIP_PASSWORD,
realm: process.env.ZULIP_REALM
realm: process.env.ZULIP_REALM,
};

//Fetch API Key
zulip(config).then(zulip => {
zulip(config).then((zulip) => {
// The zulip object now contains the API Key
zulip.streams.subscriptions.retrieve().then(res => {
zulip.streams.subscriptions.retrieve().then((res) => {
console.log(res);
});
});
```

### With zuliprc

Create a file called `zuliprc` (in the same directory as your code) which looks like:

```
[api]
[email protected]
key=wlueAg7cQXqKpUgIaPP3dmF4vibZXal7
site=http://localhost:9991
```

Please remember to add this file to your `.gitignore`!
Calling `zulip({ zuliprc: 'zuliprc' } )` will read this file and then pass a configured zulip object to `.then()`.
Please remember to add this file to your `.gitignore`! Calling `zulip({ zuliprc: 'zuliprc' } )` will read this file and then pass a configured zulip object to `.then()`.

```js
const zulip = require('zulip-js');
const path = require('path');
const zuliprc = path.resolve(__dirname, 'zuliprc');
zulip({ zuliprc }).then(zulip => {
zulip({ zuliprc }).then((zulip) => {
// The zulip object now contains the config from the zuliprc file
zulip.streams.subscriptions.retrieve().then(res => {
zulip.streams.subscriptions.retrieve().then((res) => {
console.log(res);
});
});
Expand Down Expand Up @@ -98,7 +104,7 @@ zulip.callEndpoint('/messages', 'POST', params);
| `zulip.accounts.retrieve()` | POST `/fetch_api_key` | returns a promise that you can use to retrieve your `API key`. |
| `zulip.emojis.retrieve()` | GET `/realm/emoji` | retrieves the list of realm specific emojis. |
| `zulip.events.retrieve()` | GET `/events` | retrieves events from a queue. You can pass it a params object with the id of the queue you are interested in, the last event id that you have received and wish to acknowledge. You can also specify whether the server should not block on this request until there is a new event (the default is to block). |
| `zulip.messages.send()` | POST `/messages` | returns a promise that can be used to send a message.|
| `zulip.messages.send()` | POST `/messages` | returns a promise that can be used to send a message. |
| `zulip.messages.retrieve()` | GET `/messages` | returns a promise that can be used to retrieve messages from a stream. You need to specify the id of the message to be used as an anchor. Use `1000000000` to retrieve the most recent message, or [`zulip.users.me.pointer.retrieve()`](#fetching-a-pointer-for-a-user) to get the id of the last message the user read. |
| `zulip.messages.render()` | POST `/messages/render` | returns a promise that can be used to get rendered HTML for a message text. |
| `zulip.messages.update()` | PATCH `/messages/<msg_id>` | updates the content or topic of the message with the given `msg_id`. |
Expand Down Expand Up @@ -135,9 +141,7 @@ Use `npm test` to run the tests.

## Writing Tests

Currently, we have a simple testing framework which stubs our network
requests and also allows us to test the input passed to it. This is what
a sample test for an API endpoint looks like:
Currently, we have a simple testing framework which stubs our network requests and also allows us to test the input passed to it. This is what a sample test for an API endpoint looks like:

```js
const chai = require('chai');
Expand All @@ -153,18 +157,22 @@ describe('Users', () => {
subject: 'test',
content: 'sample test',
};
const validator = (url, options) => { // Function to test the network request parameters.
const validator = (url, options) => {
// Function to test the network request parameters.
url.should.equal(`${common.config.apiURL}/users`);
Object.keys(options.body.data).length.should.equal(4);
options.body.data.subject.should.equal(params.subject);
options.body.data.content.should.equal(params.content);
};
const output = { // The data returned by the API in JSON format.
const output = {
// The data returned by the API in JSON format.
already_subscribed: {},
result: 'success',
};
const stubs = common.getStubs(validator, output); // Stub the network modules.
users(common.config).retrieve(params).should.eventually.have.property('result', 'success'); // Function call.
users(common.config)
.retrieve(params)
.should.eventually.have.property('result', 'success'); // Function call.
common.restoreStubs(stubs); // Restore the stubs.
});
});
Expand Down
6 changes: 3 additions & 3 deletions examples/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"no-console": 0
}
"rules": {
"no-console": 0
}
}
3 changes: 2 additions & 1 deletion examples/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ zulip({
username: process.env.ZULIP_USERNAME,
apiKey: process.env.ZULIP_API_KEY,
realm: process.env.ZULIP_REALM,
}).then((z) => z.accounts.retrieve())
})
.then((z) => z.accounts.retrieve())
.then(console.log);
3 changes: 2 additions & 1 deletion examples/emojis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config = {
// },
// ...

zulip(config).then((z) => z.emojis.retrieve())
zulip(config)
.then((z) => z.emojis.retrieve())
.then(console.log)
.catch((err) => console.log(err.msg));
40 changes: 21 additions & 19 deletions examples/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ const config = {
realm: process.env.ZULIP_REALM,
};

zulip(config).then((z) => {
// Retrieve events from a queue, blocking until there is an event (or the request timesout)
const params = {
queue_id: process.env.ZULIP_QUEUE_ID,
last_event_id: -1,
dont_block: false,
};
return z.events.retrieve(params).then(console.log);
// Prints
// { msg: '',
// result: 'success',
// handler_id: 2005928,
// events:
// [ { flags: [Object], message: [Object], type: 'message', id: 0 },
// { type: 'heartbeat', id: 1 },
// { flags: [], message: [Object], type: 'message', id: 2 },
// { flags: [], message: [Object], type: 'message', id: 3 },
// { flags: [], message: [Object], type: 'message', id: 4 } ] }
}).catch((err) => console.log(err.message));
zulip(config)
.then((z) => {
// Retrieve events from a queue, blocking until there is an event (or the request timesout)
const params = {
queue_id: process.env.ZULIP_QUEUE_ID,
last_event_id: -1,
dont_block: false,
};
return z.events.retrieve(params).then(console.log);
// Prints
// { msg: '',
// result: 'success',
// handler_id: 2005928,
// events:
// [ { flags: [Object], message: [Object], type: 'message', id: 0 },
// { type: 'heartbeat', id: 1 },
// { flags: [], message: [Object], type: 'message', id: 2 },
// { flags: [], message: [Object], type: 'message', id: 3 },
// { flags: [], message: [Object], type: 'message', id: 4 } ] }
})
.catch((err) => console.log(err.message));
8 changes: 5 additions & 3 deletions examples/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const config = {
realm: process.env.ZULIP_REALM,
};

zulip(config).then((z) => {
z.filters.retrieve().then(console.log);
}).catch((err) => console.log(err.message));
zulip(config)
.then((z) => {
z.filters.retrieve().then(console.log);
})
.catch((err) => console.log(err.message));
33 changes: 19 additions & 14 deletions examples/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ const config = {

// Initialization with zuliprc
const zuliprc = path.resolve(__dirname, 'zuliprc');
zulip({ zuliprc }).then((z) => {
// The zulip object now contains the API key
console.log(z.config);
return z.streams.subscriptions();
}).then(console.log)
zulip({ zuliprc })
.then((z) => {
// The zulip object now contains the API key
console.log(z.config);
return z.streams.subscriptions();
})
.then(console.log)
.catch((err) => console.log(err.message));

// Initialization with username & API key
zulip(config).then((z) => {
// The zulip object now contains the API key
console.log(z.config);
return z.config.apiKey;
}).then((key) => {
// Initialization with API key
config.apiKey = key;
return zulip(config).streams.subscriptions();
}).then(console.log)
zulip(config)
.then((z) => {
// The zulip object now contains the API key
console.log(z.config);
return z.config.apiKey;
})
.then((key) => {
// Initialization with API key
config.apiKey = key;
return zulip(config).streams.subscriptions();
})
.then(console.log)
.catch((err) => console.log(err.message));
12 changes: 9 additions & 3 deletions examples/interactive_call_endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ const zulip = require('../lib');

if (process.argv[2] === 'help') {
console.log('This is a helper script to test Zulip APIs.');
console.log('Call with: npm run call <method> <endpoint> <json_params> <zuliprc path>.');
console.log('Put your zuliprc file in ~/.zuliprc or specify the 4th argument above.');
console.log(
'Call with: npm run call <method> <endpoint> <json_params> <zuliprc path>.'
);
console.log(
'Put your zuliprc file in ~/.zuliprc or specify the 4th argument above.'
);
process.exit(0);
}
const method = process.argv[2] || 'GET';
const endpoint = process.argv[3] || '/users/me';
const params = process.argv[4] || '';
const zuliprc = process.argv[5] ? path.resolve(__dirname, process.argv[5]) : path.resolve(homedir, '.zuliprc');
const zuliprc = process.argv[5]
? path.resolve(__dirname, process.argv[5])
: path.resolve(homedir, '.zuliprc');

zulip({ zuliprc })
.then((z) => z.callEndpoint(endpoint, method, params))
Expand Down
Loading

0 comments on commit 53d7807

Please sign in to comment.