Introduction to apiCheck.js

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

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.

[00:00] 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.

[00:26] 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.

[00:56] 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.

[01:20] 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.

[01:45] 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.

[02:09] 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.

[02:29] 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.

[02:51] 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.

[03:12] 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.

[03:30] 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.

egghead
egghead
~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today