Tiny, fully-featured REST client for the unofficial devRant API.
- Promise driven via Async / Await
- Typescript support
- Works both in Node and in browsers
- Under 1kb
- Minimal dependencies
- Test suite
I have built this library because existing ones have not been recently maintained, or don't support all existing endpoints.
yarn add devrant-liteor
npm install devrant-liteThen you can include the following at the top of your code:
importDevRantfrom'devrant-lite';constclient=DevRant.withCredentials('username','password');client.get(...)client.post(...)You will need to create an account on https://devrant.com/, if you haven't already.
Otherwise, you will only have access to some public endpoints, like devrant/rants.
There are three ways of authentication:
No Authentication:
constclient=newDevRant();const{ rants }=awaitclient.get('devrant/rants');
With User Credentials:
I recommend grabbing the auth token with the
.getAuthToken()method afterwards and using the.withAuthToken()method next time.constclient=DevRant.withCredentials('username','password');const{ data }=awaitclient.get('users/me/notif-feed',{last_time: 0});
With Auth Token:
constclient=DevRant.withAuthToken({key: '123abcdefghijklmnopqrstuvxyz',id: 12345678,user_id: 12345});const{ data }=awaitclient.get('users/me/notif-feed',{last_time: 0});
The constructor accepts an options object with the following options:
Sets the base url for the api.
Useful for proxying or testing.
Default: 'https://devrant.com/api'
Sets the app ID to use.
The app id will be sent with every request.
Default: 3
Similar as the app ID, this sets the platform ID,
which will also be sent with every request.
Default: 3
A helper function which creates a new instance of the DevRant class and then sends a request to the users/auth-token endpoint to get an auth token.
When the request was successful it sets the auth token with the setAuthToken(auth_token) method.
A helper function which creates a new instance of the DevRant class and then sets the auth token with the setAuthToken(auth_token) method.
Use the post method for actions that get state.
Returns a Promise resolving to the API response object, or rejecting on error.
The response and error objects will also have a hiddenheaders property with the HTTP response headers returned by
the devRant API.
constclient=DevRant.withCredentials('username','password');// Get todays top rantsconst{ rants }=awaitclient.get("devrant/rants",{sort: 'top',range: 'day',limit: 20,skip: 0,});Same return as get().
Use the post method for actions that change or create state.
For example, to post a new rant:
constclient=DevRant.withCredentials('username','password');// Post a rant with text "Hello World!"awaitclient.post('devrant/rants',{rant: 'Hello World!',tags: 'hello, world',type: 6});Same return as get() and post().
Use the delete() method for actions that delete state. For example, to delete a rant:
constclient=DevRant.withCredentials('username','password');// Delete rant with ID 12345awaitclient.delete('devrant/rants/12345');Sets the auth token to be used for authentication. An auth token object consists of the id, key and user_id properties.
Returns the auth token used for authentication.
You can find many more examples for various resources/endpoints in the tests.
constresponse=awaitclient.get('devrant/rants');console.log(`Content Length: ${response.headers.get('Content-Type')}`);console.log(response.rants[0]);.get(), .post() and .delete() reject on error, so you can use try/catch to handle errors. The error object contains an error property with the error message and the default success property,
which is false, in this case. The error object will also have a hiddenheaders property with the HTTP response headers returned by the devRant API.
try{constresponse=awaitclient.get("some/endpoint");// ... use response here ...}catch(e){if('success'ine){// devRant API errorconsole.error(e.error);}else{// non-API error, e.g. network problem or invalid JSON in response}}- Fork/clone the repo
- Run
yarn/npm install - Place your credentials in a .env file in the project's root directory, under the following variables:
DEVRANT_USERNAME=...DEVRANT_PASSWORD=...
- Run
yarn/npm testand make sure all tests pass - Make sure all tests pass. NOTE: tests will take over 10 minutes to finish.
- Commit using a descriptive message (please squash commits into one per fix/improvement!)
- Run
git pushand submit your PR!
Authors: