Commit 2449a347 authored by Kristian Freeman's avatar Kristian Freeman

Add axios exercise

parent f5c07630
{
"requires": true,
"name": "async",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/concat-stream": {
"version": "1.6.0",
......
{
"name": "async",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"axios": "^0.18.0",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"sync-request": "^6.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
const posts = require("./lib/posts");
const vorpal = require("vorpal")();
vorpal.delimiter("Welcome to chirp $ ").show();
const logPost = (post, ctx) =>
ctx.log(`${post.text}\nPosted by ${post.user} | Posted at ${post.created}\n`);
const logPosts = (posts, ctx) => posts.forEach(post => logPost(post, ctx));
vorpal
.command("posts", "Get current posts")
.action(async function(args, callback) {
const { data } = await posts.getPosts();
logPosts(data.posts, this);
callback();
});
vorpal
.command("post <user> [text...]", "Post something to chirp")
.action(async function(args, callback) {
const { user, text } = args;
const { data } = await posts.createPost(text.join(" "), user);
logPosts(data.posts, this);
callback();
});
const url = "https://byteconf-training-chirp.herokuapp.com";
/*
* This is the base API layer for interfacing with
* the chirp API. A couple things to be done here:
*
* 1. Import the axios package. If you're unsure how to
* do that, try finding axios on GitHub and looking
* at the first line of the "Example" section.
* 2. Use axios.create to create a custom API function
* that we can re-use in other files. To do this,
* call axios.create and pass in an object as the
* argument to this function. Set "baseURL" to the
* variable url. Set this to the variable api.
* 3. Make api the export for this module, by setting
* module.exports to api.
*/
/*
* In this file, we'll define a few specific methods for requesting
* and updating content from our API.
*
* 1. Import the api file. It's _relative_ to this file, so
* you can require it with a path like ./myFile. What's the filename
* for the API module?
* 2. Define the first API method: getPosts. It should call
* (and return) the value of calling the get function on api, passing in
* the argument "/posts", which specifies the "path" we want to call on the API.
* 3. Define the createPost method, which takes two function arguments: text, and user.
* Call (and return) the post method on API, passing two arguments into it: the string
* "/posts", and an object with the key and value pairs of text and user.
* 4. Finally, export these two functions using module.exports. module.exports should be
* an object: for instance, if I wanted to export the function myFunc, I might do:
*
* module.exports = { myFunc: myFunc }
*
* Note that in ES6, you can simplify this to:
*
* module.exports = { myFunc }
*
* Export both getPosts and createPost.
*/
This diff is collapsed.
{
"name": "axios-exercise",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.18.0",
"vorpal": "^1.12.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment