Back-end API made easily accessible via auto-generated methods
by Vytenis Urbonavičius
This API is an abstraction layer on top of browser fetch API. It allows accessing back-end services by simply calling auto-generated methods. Library can be configured and extended for use in particular use cases.
Let's say there is a REST service which is capable of controlling devices. It is capable of retrieving a list of devices by making a request:
GET /devicesTo access this service from front-end in the simplest way possible we can:
import {Api} from 'api-link'
...
const api = new Api()
const devices = await api.getDevices()
...
Note that getDevices method is auto-generated and it does not need to be explicitly defined. Request method is resolved by convention. By default prefix of the function defines request method to be used. However, should one want to disable this automatic behavior or change the convention to something else - API Link is highly configurable and allows to do so.
API Link is exported as a UMD module. When it is not imported but accessed via window instead, it is available under the name of ApiLink.
You can find a working example available under example directory in the api-link package.
To launch this example:
Here is a peek at how multiple different API calls are made in the example:
const baseUrl = 'http://localhost:3000'
const api = new window.ApiLink.Api({baseUrl})
const makeCalls = async () => {
responses = {
getConstant: await api.getConstant(),
getMirror: await api.getMirror({hello: 'world'}),
postMirror: await api.postMirror({
hello: 'world',
world: 'hello',
}),
}
}
Important: developer has option to disable / change any conventions described below.
By default API method names determine what request method will be used. Following methods are supported:
In the above cases prefix is removed from the method name before using it.
Methods are then converted to param-case.
If method does not start with one of these prefixes, it is assumed to be "post". It is convenient when one wants to name methods using another verb. For example syncDevices would actually call "/sync-devices".
GET requests put arguments to URL query. All other types of requests put arguments to body as a serialized JSON string.
API Link expects response to be sent as JSON.
When initializing _Api_, a configuration object can be provided as constructor argument. Here are supported object keys:
Additional documentation can be found under /docs directory inside API Link package.
Before choosing to use API Link one should consider how much configuration and corner-cases would need to be sorted-out in order to make it work for a particular case.
If server does not provide a unified way to reach end-points, using API Link could increase complexity without providing sufficient benefit to justify its usage.
API Link is best when it is used with a tidy and consistent back-end solution.
In many cases it might be a good idea to extend Api class provided by API Link. There are multiple benefits to this:
In order for extension to work properly, it needs to be done in a certain way:
class CustomApi extends window.ApiLink.Api {
constructor() {
super({baseUrl})
}
getDevices() {
console.log('Injected log before request')
return this._proxy.getDevices()
}
}
Note that calling super.getConstant() would not work due to the nature of how auto-generated methods are implemented.
When dealing with REST services, these services might have a path which accept certain argument as part of the path:
/devices/1To support these paths, it is necessary to alter API Link behavior slightly:
const api = new Api({
requestQueryMapper: (args, getQuery) => {
let query = args.id ? `/${args.id}` : ''
delete args.id
query += getQuery(args)
return query
},
})
This logic would make API Link use id attribute as part of path. So now when API is called, this is going to be the result:
api.getDevices({id: 1})
// This calls "/devices/1" - not "/devices?id=1 anymore
Another common case is when services have URLs like this:
/devices
/devices/1/info
/v2/devicesThere are multiple ways how to handle such paths:
const api = new Api({
requestMethodMapper: (methodName, args) => {
const {requestMethod} = window.ApiLink.resolveMethodByConvention(methodName)
methodName =
methodName === 'getDevices' && args.id
? `devices/${args.id}/info`
: methodName
return {requestMethod, methodName}
},
requestQueryMapper: (query, getQuery) => {
delete query.id
return getQuery(query)
},
})
Above code would convert path from "/devices?id=123" to "/devices/123/info" when id is provided. Hooks are rather low level to allow maximum flexibility.
TypeScript developers will notice that auto-generated methods are not typed. This is because at the moment library has no way of knowing what data types are expected to be used.
Types can be added manually by extending Api class using method described earlier. Ideally, server could be generating definitions which could then be converted to TypeScript interface. However, at this point it is out of scope for API Link.
Generated using TypeDoc