@aofl/resource-enumerate
Resource enumerate is a special case API call that returns the interface of the API code in a single endpoint. The response can contain server time, paths to resources, and other information based on application needs. An advantage of this technique is that it eliminates the need to hardcode paths/urls.
@aofl/resource-enumerate
accepts a config object that defines the path to the production api server.
It also supports before()
and after()
hooks. See example below.
Api Documentation
Installation
npm i -S @aofl/resource-enumerate
Usage
const resourceEnumerateConfig = {
apis: {
'awesome-apis': {
url: '/apis/v1.0/resource-enumerate.json',
requestOptions: {
method: 'GET',
cache: 'no-cache'
},
developmentVariables() {
return {
host: location.host
};
},
stageVariables() {
return {
host: location.host
};
},
invalidateCache() {
return false;
}
}
},
developmentConfig: () => import('./dev-api-config'),
stageConfig: () => import('./stage-api-config')
};
export default resourceEnumerateConfig;
[development|stage]Config()
export a function that takes api namespace and the returned object from [development|stage]Variables()
and returns the path to the environment specific resource enumerate api call.
export default (apiNs, {host}) => {
return `//${host}/apis/dev/v1.0/resource-enumerate.json`;
};
export default (apiNs, {host}) => {
return `//${host}/apis/stage/v1.0/resource-enumerate.json`;
};
import {ResourceEnumerate} from '@aofl/resource-enumerate';
import resourceEnumerateConfig from './__config/resource-enumerate-config';
import apiRequestInstance from 'apiRequestInstanceFile :)';
import {getServerEnvironment} from '@aofl/server-environment';
const developmentRegex = /localhost|192\.168\.[\d|\.]+|172\.(1[6-9]|2[0-9]|3[01])\.[\d|\.]+|10\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.[\d|\.]+/;
const stageRegex = /^stage\./;
const environment = getServerEnvironment(developmentRegex, stageRegex);
const resourceEnumerate = new ResourceEnumerate(environment);
const serverTime = {};
resourceEnumerate.before((request, response, next) => {
serverTime[request.namespace] = {
before: Date.now()
};
next(response);
});
resourceEnumerate.after((request, response, next) => {
serverTime[request.namespace].after = Date.now();
serverTime[request.namespace].delta = response.serverTime - Math.round(
(servertime[request.namespace].before + servertime[request.namespace].after) / 2);
next(response);
});
resourceEnumerate.init(resourceEnumerateConfig)
.then(() => {
resourceEnumerate.get('awesome-apis')
.then((resourceEnumeratePayload) => {
apiRequestInstance
.request(resourceEnumeratePayload.api_url + resourceEnumeratePayload.endpoints.getPosts)
.then((response) => {
});
});
});
Caching
Calling get()
will cache the api response by default. There are 2 ways to invalidate cache and fetch a new copy of the resource enumerate data.
- By calling
get()
and passing false
as the second argument (fromCache
). - By implementing
invalidateCache()
in the config object. This function is invoked everytime get()
is invoked and will return the cached data if it evaluates to false
.
...
'awesome-apis': {
url: '/apis/v1.0/resource-enumerate.json'
invalidateCache() {
return Math.random() > 0.8;
}
}
...