Skip to content

Commit 33f1c3c

Browse files
committed
Update petstore for 2019
1 parent 7e93057 commit 33f1c3c

19 files changed

+3309
-397
lines changed

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
'extends': 'airbnb-base',
3+
'env': {
4+
'mocha': true,
5+
'node': true,
6+
}
7+
};

component.json

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,34 @@
99
"label": "API key",
1010
"required": true,
1111
"viewClass": "TextFieldWithNoteView",
12-
"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>."
12+
"note": "Please use <b>secret</b> as API key. For more details see <a href='https://petstore.elastic.io/docs/' target='_blank'>Petstore API docs</a>."
1313
}
1414
}
1515
},
1616
"triggers": {
17-
"getPetsByStatusWithGenerators": {
18-
"main": "./lib/triggers/getPetsByStatusWithGenerators.js",
17+
"getPetsByStatus": {
18+
"main": "./lib/triggers/getPetsByStatus.js",
1919
"type": "polling",
20-
"title": "Get Pets By Status With Generators",
20+
"title": "Get Pets By Status (Dynamic Data Example)",
21+
"description": "Get pets by status, with the available statuses pulled dynamically from API",
2122
"fields": {
2223
"status": {
2324
"label": "Pet Status",
2425
"required": true,
2526
"viewClass": "SelectView",
26-
"model": {
27-
"available": "Available",
28-
"pending": "Pending",
29-
"sold": "Sold"
30-
},
27+
"model": "getStatusModel",
3128
"prompt": "Select Pet Status"
3229
}
3330
},
3431
"metadata": {
3532
"out": "./lib/schemas/getPetsByStatus.out.json"
3633
}
3734
},
38-
"getPetsByStatusWithPromises": {
39-
"main": "./lib/triggers/getPetsByStatusWithPromises.js",
35+
"getPetsByStatusWithStaticData": {
36+
"main": "./lib/triggers/getPetsByStatusWithStaticData.js",
4037
"type": "polling",
41-
"title": "Get Pets By Status With Promises",
38+
"title": "Get Pets By Status (Static Data Example)",
39+
"description": "Get pets by status, with the available statuses statically typed to the component.json",
4240
"fields": {
4341
"status": {
4442
"label": "Pet Status",
@@ -55,37 +53,13 @@
5553
"metadata": {
5654
"out": "./lib/schemas/getPetsByStatus.out.json"
5755
}
58-
},
59-
"getPetsByStatusWithDynamicSelectModel": {
60-
"main": "./lib/triggers/getPetsByStatusWithDynamicSelectModel.js",
61-
"type": "polling",
62-
"title": "Get Pets By Status With Dynamic Select Model",
63-
"fields": {
64-
"status": {
65-
"label": "Pet Status",
66-
"required": true,
67-
"viewClass": "SelectView",
68-
"model": "getStatusModel",
69-
"prompt": "Select Pet Status"
70-
}
71-
},
72-
"metadata": {
73-
"out": "./lib/schemas/getPetsByStatus.out.json"
74-
}
7556
}
7657
},
7758
"actions": {
78-
"createPetWithPromise": {
79-
"main": "./lib/actions/createPetWithPromise.js",
80-
"title": "Create a Pet With Promise",
81-
"metadata": {
82-
"in": "./lib/schemas/createPet.in.json",
83-
"out": "./lib/schemas/createPet.out.json"
84-
}
85-
},
86-
"createPetWithGenerators": {
87-
"main": "./lib/actions/createPetWithGenerators.js",
88-
"title": "Create a Pet With Generators",
59+
"createPet": {
60+
"main": "./lib/actions/createPet.js",
61+
"title": "Creates a new pet",
62+
"description": "Creates a pet and adds it to the shop",
8963
"metadata": {
9064
"in": "./lib/schemas/createPet.in.json",
9165
"out": "./lib/schemas/createPet.out.json"

lib/actions/createPet.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { messages } = require('elasticio-node');
2+
const PetstoreClient = require('../petstoreClient');
3+
4+
/**
5+
* Executes the action's logic by sending a request to the Petstore API and emitting response to the platform.
6+
* The function returns a Promise sending a request and resolving the response as platform message.
7+
*
8+
* @param msg incoming messages which is empty for triggers
9+
* @param cfg object to retrieve triggers configuration values, such as apiKey and pet status
10+
* Emits results as a message to the platform
11+
*/
12+
exports.process = async function process(msg, cfg) {
13+
// create a client object that has methods to make a request available to us
14+
const client = new PetstoreClient(this, cfg);
15+
16+
/**
17+
* The format of a message coming into the function is
18+
* msg: {
19+
* body: {
20+
* name:
21+
* status:
22+
* }
23+
* }
24+
* So we deconstruct the object accordingly
25+
*/
26+
const { name } = msg.body;
27+
28+
const statuses = {
29+
Available: 'available',
30+
Pending: 'pending',
31+
Sold: 'sold',
32+
};
33+
34+
// map the status value from the readable value to the internal camel case one
35+
const status = statuses[msg.body.status];
36+
37+
if (!name) {
38+
throw new Error('Name is required');
39+
}
40+
41+
if (!status) {
42+
throw new Error('Status is required');
43+
}
44+
45+
// create pet object to post
46+
const pet = {
47+
name,
48+
status,
49+
};
50+
51+
// make a request using the client and save the result to a parameter =>
52+
// makeRequest takes an object with the necessary fields to complete the request
53+
const result = await client.makeRequest({
54+
url: '/pet',
55+
method: 'POST',
56+
body: pet,
57+
});
58+
59+
// this.emit is a function provided by sailor - we use it to emit messages to
60+
// our platform
61+
await this.emit('data', messages.newMessageWithBody(result));
62+
};

lib/actions/createPetWithGenerators.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/actions/createPetWithPromise.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/petstoreClient.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { ApiKeyRestClient } = require('@elastic.io/component-commons-library');
2+
3+
const API_BASE_URI = 'https://petstore.elastic.io/v2';
4+
5+
// various standard rest clients have been created that handle creating requests and some
6+
// authentication for us. They are found in @elastic.io/component-commons-library
7+
module.exports = class PetstoreClient extends ApiKeyRestClient {
8+
constructor(emitter, cfg) {
9+
// this first line is a hack for a small sailor bug
10+
if (!emitter.logger) emitter.logger = { trace: () => {} };
11+
12+
// begin constructor
13+
super(emitter, cfg);
14+
this.cfg = cfg;
15+
this.cfg.resourceServerUrl = API_BASE_URI;
16+
this.apiKeyHeaderName = 'api-key';
17+
this.apiKeyHeaderValue = cfg.apiKey;
18+
this.authRestClient = new ApiKeyRestClient(emitter, cfg);
19+
}
20+
};

lib/schemas/createPet.in.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"status": {
1010
"type": "string",
1111
"required": true,
12+
"enum": ["Available", "Pending", "Sold"],
1213
"title": "Status"
1314
}
1415
}

lib/triggers/getPetsByStatus.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { messages } = require('elasticio-node');
2+
const PetstoreClient = require('../petstoreClient');
3+
4+
/**
5+
* Executes the trigger's logic by sending a request to the Petstore API and emitting response to the platform.
6+
* The function returns a Promise sending a request and resolving the response as platform message.
7+
*
8+
* @param msg incoming messages which is empty for triggers
9+
* @param cfg object to retrieve triggers configuration values, such as apiKey and pet status
10+
* @returns promise resolving a message to be emitted to the platform
11+
*/
12+
exports.process = async function getPetsByStatus(msg, cfg) {
13+
const client = new PetstoreClient(this, cfg);
14+
15+
// access the value of the status field defined in credentials section of component.json
16+
const { status } = cfg;
17+
18+
if (!status) {
19+
throw new Error('Status field is required');
20+
}
21+
22+
// make a request using the client and save the result to a parameter
23+
const result = await client.makeRequest({
24+
url: `/pet/findByStatus?status=${status}`,
25+
method: 'GET',
26+
});
27+
28+
// emit result using supplied emit function
29+
await this.emit('data', messages.newMessageWithBody(result));
30+
};
31+
32+
33+
/**
34+
* getStatusModel is supplied in component.json as the model for credential statuses
35+
* The results of this function will dynamicaly provide the statuses from the API
36+
* to be displayd on the platform
37+
*/
38+
exports.getStatusModel = async function getStatusModel(cfg) {
39+
const client = new PetstoreClient(this, cfg);
40+
41+
const result = await client.makeRequest({
42+
url: '/pet/statuses',
43+
method: 'GET',
44+
});
45+
46+
const statuses = {};
47+
48+
// map the statuses to a human readable form
49+
result.forEach((status) => {
50+
statuses[status] = status.charAt(0).toUpperCase() + status.substring(1);
51+
});
52+
53+
return statuses;
54+
};

0 commit comments

Comments
 (0)