Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Introduction to apiCheck.js


    Because JavaScript is a typeless language, validating your api is a terrific way to help avoid bugs and instruct users of your API without requiring them to check documentation. See how api-check can help you do this. If you're familiar with ReactJS propTypes, this will be familiar to you.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    API-check is a library that allows you to validate your function's APIs. We've this time out log function here, and this accepts a time as a number, a person that has a name, and a call back that is optional, that will be invoked with that person. We want to validate that this API is used correctly, because if it's used incorrectly, then it can yield some unexpected results here where it invokes that callback immediately.

    So, let's go ahead and install API-check first. We NMP-install API-check. This gives us an API-check factory. We require API-check, and then we create our own instance of API-check. We normally do this once, for the entire operation, so we will call this, "MyCheck = API-check factory," and there's some options that you can pass in here, but we'll omit those for now.

    So now, if you want to validate this time you log function, we'll simply say "MyCheck.throw" or warn depending on how you want to validate this API. Throw will throw an error, and warn will simply warn to the console. And then here, you pass your API, and then your arguments. Or, you can pick and choose, and say time and callback.

    Then, your API needs to follow or be at the same index of these. We'll go ahead and just paste arguments here, so now let's define our API. This API is an array where the first item is checking the time. So it'll be MyCheck.number. The next argument is person, this is an object with a name.

    So we'll say "MyCheck.shape name is MyCheck.string." And then finally, we've a callback which is a function. So, we say "MyCheck.func," and it's optional, so we attach optional there. So now, if we look at our output, everything is working just fine, but if we were to pass in something incorrect here, then we are going to get an error.

    Let's take a look at what this looks like. Here, it says "APIcheck failed." Value at argument one must be a number, and then argument two pass, and argument three, which is optional, also passed. And so, it tells us what we pass through the function, what the types were to the function that we passed, and then what the API calls for.

    So first, because in a number, and then the calls for a shape, and that shape has a name that's a string. And then it calls for a function that's optional. And so, if we simply compare the types that we passed, we passed a string, and the API calls for a number. We know exactly where we need to fix this, so if we change this to a number, then everything works, again.

    Let's go ahead and change this person to a boolean. Now, we get a different error where it says argument one passed, argument two must be an object, and then argument three, which is optional, also passed. And so, it'll tell us that we passed through with a type of boolean, and the second argument must be an object.

    It tells us right here that this is our problem. We're missing a name property on that argument that we passed, and so if we simply add name is Joe, then everything works just fine. That's an introduction to API-check. Let's go ahead and review really quickly.

    So first you're going to need to go ahead and install NPM-check, and then create your own instance for your application and API-check. Then you create your API for your function that maps directly to whatever it is that you passed as your second parameter. That's how you validate your APIs with API-check.