Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 84 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,106 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

.idea
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
## 1.0.2 (Jan 4, 2021)
## 1.0.3 (May 28, 2021)

* Make statuses case-insensitive
* Update to sailor version 2.6.24
* Update README documentation
* Changed petstore password in component.json

## 1.0.2 (January 4, 2021)

* Update to sailor version 2.6.21
* Update to version 14 of node

Expand Down
62 changes: 39 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
# petstore-component-nodejs
> Petstore Node.js component template for the [elastic.io platform](http://www.elastic.io "elastic.io platform")
# Petstore Component Node.js

This is a component template which we commonly refer as **the first step of the development** for creating a component to deploy into our platform. This component comes with a basic architecture which can be used on our platform. You can clone it and use it. However, **if you plan to deploy it into [elastic.io platform](https://www.elastic.io "elastic.io platform") you must follow sets of instructions to succeed**.
## Description

## Before you Begin
A fully working and operational component template to use for starting development of your
own component for the [elastic.io platform](http://www.elastic.io "elastic.io platform").
This component comes with a basic architecture which you can use on the platform.

Before you can deploy any code into our system **you must be a registered elastic.io platform user**. Please see our home page at [https://www.elastic.io](https://www.elastic.io) to learn how.
The component interacts with the [petstore sample server](https://petstore.elastic.io/docs/).

> Any attempt to deploy a code into our platform without a registration would be rejected.
Our documentation on [building components in nodejs](https://docs.elastic.io/guides/building-nodejs-component)
has more about each including file and their function.

After the registration and opening of the account you must **[upload your SSH Key](http://go2.elastic.io/manage-ssh-keys)** into our platform.
## Requirements

> If you fail to upload you SSH Key you will get **permission denied** error during the deployment.
To use this component you must be registered platform user. Please see our home
page at [https://www.elastic.io](https://www.elastic.io) to learn how to register.

## Getting Started
### Authentication

After registration and uploading of your SSH Key you can proceed to deploy it into our system. At this stage we suggest you to:
* [Create a team](http://go2.elastic.io/manage-teams) to work on your new component. This is not required but will be automatically created using random naming by our system so we suggest you name your team accordingly.
* [Create a repository](http://go2.elastic.io/manage-repositories) where your new component is going to *reside* inside the team that you have just created.
To authenticate use `secret` as an API key. For more details see the
[Petstore API docs](https://petstore.elastic.io/docs/).

```bash
$ git clone https://github.com/elasticio/petstore-component-nodejs.git your-repository
## Triggers

$ cd your-repository
```
Now you can edit your version of **petstore-component-nodejs** component and build your desired component. Or you can just ``PUSH``it into our system to see the process in action:
### Get Pets By Status

```bash
$ git remote add elasticio your-team@git.elastic.io:your-repository.git
Retrieves pets from the Petstore API by given pet status using Dynamic or Static
Data Sample. The difference is how these functions are made. However, both can have
the following statuses defined by the Petstore API:

$ git push elasticio master
```
Obviously the naming of your team and repository is entirely up-to you and if you do not put any corresponding naming our system will auto generate it for you but the naming might not entirely correspond to your project requirements.
* `Available` - select to get all pets with status `Available`.
* `Pending` - select for `Pending`.
* `Sold` - select for `Sold`.


## Actions

### Creates a new pet

Creates a new Pet by making a `POST` to `/pet` endpoint of the API. The input
fields are:

* `petId` (required) - the Id of the pet.
* `name` - the name of the pet.
* `status` - the status which can have one `Available`, `Pending` and `Sold` values.

## File Structure

The structure of **petstore-component-nodejs** component is quite flexible. [elastic.io platform](https://www.elastic.io) expects only two files to be present in the main directory. These are the ``component.json`` and ``package.json``. Our documentation on [how to build a component in node.js](https://support.elastic.io/support/solutions/articles/14000027123-how-to-build-a-component-in-node-js) has more about each file and their function.
The structure of **petstore-component-nodejs** component is quite flexible.
[elastic.io platform](https://www.elastic.io) expects only two files to be present
in the main directory. These are the ``component.json`` and ``package.json``. Our
documentation on [how to build a component in node.js](https://docs.elastic.io/guides/building-nodejs-component)
has more about each file and their function.
20 changes: 15 additions & 5 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"description": "elastic.io component for the Petstore API",
"docsUrl": "https://github.com/elasticio/petstore-component-nodejs",
"buildType":"docker",
"version": "1.0.3",
"credentials": {
"fields": {
"apiKey": {
"label": "API key",
"required": true,
"viewClass": "TextFieldWithNoteView",
"note": "Please use <b>elasticio</b> as API key. For more details see <a href='https://petstore.elastic.io/docs/' target='_blank'>Petstore API docs</a>."
"viewClass": "PasswordFieldView",
"note": "Please use 'secret' as API key. For more details see Petstore API docs: https://petstore.elastic.io/docs"
}
}
},
Expand All @@ -18,7 +19,10 @@
"main": "./lib/triggers/getPetsByStatus.js",
"type": "polling",
"title": "Get Pets By Status (Dynamic Data Example)",
"description": "Get pets by status, with the available statuses pulled dynamically from API",
"help": {
"description": "Get pets by status, with the available statuses pulled dynamically from API",
"link": "/components/petstore-nodejs"
},
"fields": {
"status": {
"label": "Pet Status",
Expand All @@ -36,7 +40,10 @@
"main": "./lib/triggers/getPetsByStatusWithStaticData.js",
"type": "polling",
"title": "Get Pets By Status (Static Data Example)",
"description": "Get pets by status, with the available statuses statically typed to the component.json",
"help": {
"description": "Get pets by status, with the available statuses statically typed to the component.json",
"link": "/components/petstore-nodejs"
},
"fields": {
"status": {
"label": "Pet Status",
Expand All @@ -59,7 +66,10 @@
"createPet": {
"main": "./lib/actions/createPet.js",
"title": "Creates a new pet",
"description": "Creates a pet and adds it to the shop",
"help": {
"description": "Creates a pet and adds it to the shop",
"link": "/components/petstore-nodejs"
},
"metadata": {
"in": "./lib/schemas/createPet.in.json",
"out": "./lib/schemas/createPet.out.json"
Expand Down
24 changes: 12 additions & 12 deletions lib/actions/createPet.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { messages } = require('elasticio-node');
const PetstoreClient = require('../petstoreClient');

const statuses = ['available', 'pending', 'sold'];

/**
* Executes the action's logic by sending a request to the Petstore API and emitting response to the platform.
* Executes the action's logic by sending a request to the
* Petstore API and emitting response to the platform.
* The function emits the results of the request to the platform as a message
*
* @param msg incoming messages which is empty for triggers
Expand All @@ -25,21 +28,18 @@ exports.process = async function process(msg, cfg) {
*/
const { name } = msg.body;

const statuses = {
Available: 'available',
Pending: 'pending',
Sold: 'sold',
};
if (!msg.body.status) {
throw new Error('Status is required');
}

// map the status value from the readable value to the internal camel case one
const status = statuses[msg.body.status];
const status = msg.body.status.toLowerCase();

if (!name) {
throw new Error('Name is required');
if (!statuses.includes(status)) {
throw new Error('Provided status is invalid.');
}

if (!status) {
throw new Error('Status is required');
if (!name) {
throw new Error('Name is required');
}

// create pet object to post
Expand Down
1 change: 1 addition & 0 deletions lib/petstoreClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const API_BASE_URI = 'https://petstore.elastic.io/v2';
module.exports = class PetstoreClient extends ApiKeyRestClient {
constructor(emitter, cfg) {
// this first line is a hack for a small sailor bug
// eslint-disable-next-line no-param-reassign
if (!emitter.logger) emitter.logger = { trace: () => {} };

// begin constructor
Expand Down
4 changes: 2 additions & 2 deletions lib/triggers/getPetsByStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { messages } = require('elasticio-node');
const PetstoreClient = require('../petstoreClient');

/**
* Executes the trigger's logic by sending a request to the Petstore API and emitting response to the platform.
* Executes the trigger's logic by sending a request to the
* Petstore API and emitting response to the platform.
* The function emits the results of the request to the platform as a message
*
* @param msg incoming messages which is empty for triggers
Expand All @@ -29,7 +30,6 @@ exports.process = async function getPetsByStatus(msg, cfg) {
await this.emit('data', messages.newMessageWithBody(result));
};


/**
* getStatusModel is supplied in component.json as the model for credential statuses
* The results of this function will dynamicaly provide the statuses from the API
Expand Down
3 changes: 2 additions & 1 deletion lib/triggers/getPetsByStatusWithStaticData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { messages } = require('elasticio-node');
const PetstoreClient = require('../petstoreClient');

/**
* Executes the trigger's logic by sending a request to the Petstore API and emitting response to the platform.
* Executes the trigger's logic by sending a request to the
* Petstore API and emitting response to the platform.
* The function emits the results of the request to the platform as a message
*
* @param msg incoming messages which is empty for triggers
Expand Down
Loading