This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Introduction to apiCheck.js

3:52 JavaScript lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

Avatar
Martin

Typescript will do quite the same without the overhead of needing library dependencies and extra code in your output :)

In reply to egghead.io
Avatar
Kent C.

Very true, but there are two problems with this:
1) As a library author, I can't force my users to use TypeScript, so type definitions don't help those who don't use TypeScript and I wind up getting a bunch of issues from people who are using the API wrong
2) TypeScript is not capable of type checking the API of an angular directive. api-check makes this possible and is used by angular-formly and angular-scope-types to do this.

The main thing is that TypeScript doesn't do runtime type checking (development only of course, api-check can be disabled for production). This is fine, and I don't think I'd have it any other way. But api-check is really handy for APIs that need runtime checking (like an angular directive or a React component). React doesn't need something like this because it was actually the inspiration for the library in the first place (propTypes).

In reply to Martin

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?