Javascript Starter
If you are using Git as command tool :
git clone https://github.com/cloud-apim/serverless-js-module-template.git
You can clone or fork it from the official Cloud-APIM repository from Github.
Here is the default OpenAPI configuration for your Javascript Module :
openapi.json
{
"openapi": "3.1.0",
"info": {
"title": "Javascript Module API",
"version": "1.0.0",
"description": "# Introduction\nWelcome to the reference for the Javascript Module API!",
"contact": {
"name": "Javascript Module API",
"url": "https://www.cloud-apim.com",
"email": "contact@cloud-apim.com"
},
"x-logo-none": {
"url": "https://www.cloud-apim.com/assets/logo/cloud-apim-logo-inverted.png"
}
},
"tags": [],
"paths": {
"/demo": {
"get": {
"tags": [],
"summary": "",
"operationId": "getJsDemo",
"x-cloud-apim-backend": {
"$ref": "#/components/x-cloud-apim-backends/jsdemobackend"
},
"x-cloud-apim-plugins": {
"$ref": "#/components/x-cloud-apim-plugins/jsdemoplugin"
}
}
}
},
"components": {
"x-cloud-apim-plugins": {
"jsdemoplugin": [
{
"enabled": true,
"plugin": "cp:cloud-apim.plugins.JsModulePlugin",
"config": {
"module": "/modules/hello.js"
}
}
]
},
"x-cloud-apim-backends": {
"jsdemobackend": {
"targets": [
{
"hostname": "jsonplaceholder.typicode.com",
"port": 443,
"tls": true
}
],
"root": "/",
"rewrite": false
}
}
}
}
And your default module configuration :
modules/hello.js
exports.on_backend_call = function(ctx) {
const name = (ctx.request.query.name && ctx.request.query.name[0]) ? ctx.request.query.name[0] : 'World';
return {
status: 200,
headers: {
'Content-Type': 'application/json'
},
body_json: { hello: name }
}
};
exports.on_response = function(ctx) {
const name = (ctx.request.query.name && ctx.request.query.name[0]) ? ctx.request.query.name[0] : 'World';
return {
...ctx.otoroshi_response,
headers: {
...ctx.otoroshi_response.headers,
'hello': name,
}
}
}