repl-talk.js
Version:
An easy to use API to access data relating to users and posts on https://repl.it.
329 lines (211 loc) • 7.87 kB
Markdown
## Description
`repl-talk.js` allows you to interface with user and post related data from https://repl.it quick and easy. Please note that this is **NOT** and official API for https://repl.it. I will, however, try my best to keep this API running smooth, both for https://repl.it's servers and you.
## Installing
Installing `repl-talk.js` is super easy! In your command line/terminal, write the following:
```bash
npm install repl-talk.js
```
After pressing the `enter` key and waiting for it to install, you are ready to use the package!
## Getting Started
After we install `repl-talk.js`, we can start to use it. First, let's require it and create the `client` that we will use to access the API:
```js
const replit = require("repl-talk.js");
const client = new replit.Client();
```
Now we have our client, we can use `client.login(username, password, callback)` to "login" to the API. While we aren't logging into the API services, we are making sure that you have a valid https://repl.it account. Without an account, you will not be able to use the API. So let's login:
```js
client.login("ExampleAccountUsername", "ExampleAccountPassword", data => {
// Next lot of code will go here
});
```
I feel that it is important to say a few things. The account `username` and `password`, along with any other data you may use is not recorded in anyway by the API. However, your account's `id` is sent with the request header, so that https://repl.it knows who has sent the request. We do this to ensure that any API abuse can be caught and handled by the professionals at https://repl.it. Apart from this, your account is only used to verify that you are registered with https://repl.it.
If you wish, you may get the data which the query to log you in returns. This can give you more information than if you were to simply query you username or id.
## Calls
Let's look at an example call:
`client.user(username, callback [, items])`
*As of the latest update, there are no longer a `client.users` and `client.posts`. It has been simplified down to the new names found below.*
**Callbacks**
The `callback` for every function will return `data`, which will contain the data for the query. These callbacks look like this:
```js
client.user("ExampleUsername", data => {
// Callback code here
});
```
**Items**
Items allow you to select what you want to be returned by the API. You may only need some data, not all of it. Not only does this make the JSON easier to use, but it also makes the requests faster to complete. Here is how you would do this:
```js
client.user("ExampleUsername", data => {
// Callback code here
}, ["id", "username", "karma"]);
```
The different things you can put in this array are the same as the ones listed below each call. The default items for each query will now only get *useful* data, you can still get any data you want as normal, or use a constant below to get it all.
## Queries
**client.user(id, callback [, items])**
This is the most basic way to get the details of a user. By using their `id`, we ensure that we will be getting the right user very time.
~~`client.username`~~: No longer exists as `client.user` can take both usernames (as strings) or ids (as integers).
- `username`
- `firstName`
- `lastName`
- `bio`
- `isVerified`
- `displayName`
- `fullName`
- `url`
- `isLoggedIn`
- `isSubscribed`
- `karma`
**client.repl(id, callback [, items])**
This will get a repl based on its `id`. This will look like a UUID (if your `id` is not working, try removing the two letters at the front).
- `id`
- `language`
- `isPrivate`
- `isStarred`.
- `title`
- `description`
- `folderId`
- `isRenamed`
- `languageDisplayName`
- `timeCreated`
- `timeUpdated`
- `pinnedToProfile`
- `files`
- `hostedUrl`
- `terminalUrl`
**client.enterprise(id, callback [, items])**
This will get the data for the enterprise with the given `id`.
- `id`
- `startDate`
- `endDate`
- `teacherSeats`
- `studentSeats`
**client.organization(id, callback [, items])**
This will get the data for the organisation with the given `id`.
- `id`
- `name`
- `country`
- `postalCide`
- `state`
- `city`
- `googlePlaceId`
- `timeCreated`
- `timeUpdated`
- `timeDeleted`
**client.post(id, callback [, items])**
This will get the data for the post with the given `id`.
- `id`
- `title`
- `body`
- `voteCount`
- `commentCount`
- `isPinned`
- `isLocked`
- `timeCreated`
- `timeUpdated`
- `url`
- `isAnnouncement`
- `isAnswered`
- `isAnswerable`
- `preview`
**client.board(id, callback [, items])**
This will get the data for the board with the given `id`.
- `id`.
- `name`
- `description`
- `titleCta`
- `bodyCta`
- `replRequired`
- `timeCreated`
- `timeUpdated`
- `url`
**client.comment(id, callback [, items])**
This will get the data for the comment with the given `id`.
- `id`
- `body`
- `voteCount`
- `timeCreated`
- `timeUpdated`
- `url`
- `isAnswer`
- `preview`
**client.query(use, callback)**
This one is for advanced users only, who how the Schema for the GraphQL API which https://repl.it uses. To use this, you will need to parse in an Object with the following details:
```js
client.query({
"from": "user",
"using": {"id": 1},
"get": ["id", "username", "karma"]
}, data => {});
```
## Items
**client.roles([items])**
This will return a string to be put in the `items` of `client.user`:
- `id`
- `key`
- `name`
- `tagline`
**client.languages([items])**
This will return a string to be put in the `items` of `client.user`:
- `id`
- `displayName`
- `key`
- `category`
- `tagline`
- `icon`
- `isNew`
**client.posts([items])**
This will return a string to be put in the `items` of `client.user`, :
- `id`
- `title`
- `body`
- `voteCount`
- `commentCount`
- `isPinned`
- `isLocked`
- `timeCreated`
- `timeUpdated`
- `url`
- `isAnnouncement`
- `isAnswered`
- `isAnswerable`
- `preview`
**client.comments([items])**
This will return a string to be put in the `items` of `client.user`:
- `id`
- `body`
- `voteCount`
- `timeCreated`
- `timeUpdated`
- `url`
- `isAnswer`
- `preview`
**client.from(use)**
This will return a string to be used in the valid queries `items`. This is for advanced users, and will most likely be used in conjunction with manual queries. Once again, you will need prior knowledge of the GraphQL API to use this one effectively. You will have to parse in an Object with the following details:
```js
client.from({
"from": "roles",
"get": ["id", "name", "tagline"]
});
```
## Constants
**client.board(id, callback [, items])**
Along with `client.board` comes constants which defines the different board ids. These include:
- client.ASK
- ~~client.SHARE~~ (I'm still not aware of the id for this board)
- client.ANNOUNCEMENTS
- client.CHALLENGE
- client.LEARN
**All**
Each query comes with a constant that can be used in its `items` argument. This will allow you to get all the data from a query. Here are these constants:
- `client.ALL_FROM_USER`
- `client.ALL_FROM_REPL`
- `client.ALL_FROM_ENTERPRISE`
- `client.ALL_FROM_ORGANIZATION`
- `client.ALL_FROM_POST`
- `client.ALL_FROM_BOARD`
- `client.ALL_FROM_COMMENT`
- `client.ALL_FROM_ROLE`
- `client.ALL_FROM_LANGUAGE`
## Errors
The error system is still not amazing, however, it does now log the errors to the console for you. You'll know now when there is an error with your query as there will be a message starting with:
`> repl-talk.js error: ...`
This will give you some insight into what went wrong so it is easier to fix.