@oas-tools/oas-telemetry 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,23 +13,15 @@ Overall, OAS Telemetry will serve as a valuable tool for developers looking to g
13
13
 
14
14
 
15
15
  ## Usage
16
- OAS Telemetry is compatible with both CommonJS and ECMAScript Modules (ESM)
17
-
18
16
  To use the middelware add this two lines in your index.js (ESM):
19
17
  ```js
20
- // this MUST be the first line in your file (before any imports)
21
- const oasTelemetry = require('oas-telemetry');
18
+ import oasTelemetry from 'oas-telemetry';
19
+ import {readFileSync} from 'fs';
22
20
 
23
- // ...Express app and other imports...
21
+ app.use(oasTelemetry({
22
+ spec : readFileSync('./spec/oas.yaml',{ encoding: 'utf8', flag: 'r' })
23
+ }))
24
24
 
25
- // Now you can use the oasTelemetry middleware, configured with the default options
26
- app.use(oasTelemetry())
27
- ```
28
-
29
- ESM version:
30
- ```js
31
- import oasTelemetry from 'oas-telemetry';
32
- app.use(oasTelemetry())
33
25
  ```
34
26
 
35
27
  ## Custom Configuration
@@ -38,61 +30,66 @@ You can also customize the telemetry configuration by passing options to the mid
38
30
  ```js
39
31
  const customTelemetryConfig = {
40
32
  exporter: myCustomExporter,
41
- baseURL: '/custom-telemetry'
33
+ spec: /* OAS content in json or yaml */
42
34
  };
43
35
 
44
36
  app.use(oasTelemetry(customTelemetryConfig));
45
37
  ```
46
- ## Some Telemetry Endpoints
38
+
39
+ ## Telemetry UI
40
+
41
+ You can access the telemetry UI in the endpoint ``/telemetry``
42
+
43
+
44
+ ## API Telemetry Endpoints
47
45
 
48
46
  OAS Telemetry middleware adds the following endpoints to your Express application:
49
47
 
50
- - /telemetry: Landing page with links to available routes.
48
+
51
49
  - /telemetry/start: Start telemetry data collection.
52
50
  - /telemetry/stop: Stop telemetry data collection.
51
+ - /telemetry/status: Get status of telemetry.
53
52
  - /telemetry/reset: Reset telemetry data.
54
53
  - /telemetry/list: List all telemetry data.
55
54
  - /telemetry/find (POST): Search telemetry data.
56
55
  - /telemetry/heapStats: Shows v8 heapStats.
57
56
 
58
- ## CommonJs Example
59
- ```js index.cjs
60
- // this MUST be the first line in your file (before any imports)
61
- const oasTelemetry = require('@oas-tools/oas-telemetry');
62
57
 
63
- const express = require('express');
64
- const app = express();
65
- const port = 3001;
66
- app.use(express.json());
67
-
68
- // Now you can use the oasTelemetry middleware, configured with the default options
69
- app.use(oasTelemetry())
70
-
71
- app.get('/', (req, res) => {
72
- res.send('Hello World!');
73
- });
74
-
75
- app.listen(port, () => {
76
- console.log(`Example app listening at http://localhost:${port}`);
77
- });
78
- ```
79
-
80
- ## ESM Example
58
+ ## Simple Example
81
59
  ```js index.mjs
82
- // simple express API in ES6 Syntax
83
60
  import oasTelemetry from '@oas-tools/oas-telemetry';
84
61
  import express from 'express';
62
+
85
63
  const app = express();
86
64
  const port = 3000;
65
+
66
+ const spec = { "paths": {
67
+ "/api/v1/pets": {
68
+ "get": {
69
+ "summary": "Get pets",
70
+ "responses":{
71
+ "200": {
72
+ "description": "Success"
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+
80
+ app.use(oasTelemetry({
81
+ spec : JSON.stringify(spec)
82
+ }))
83
+
87
84
  app.use(express.json());
88
85
 
89
- app.use(oasTelemetry())
90
- app.get('/', (req, res) => {
91
- res.send('Hello World!');
86
+ app.get("/api/v1/pets", (req, res) => {
87
+ res.send([{ name: "rocky"},{ name: "pikachu"}]);
92
88
  });
93
89
 
94
90
  app.listen(port, () => {
95
91
  console.log(`Example app listening at http://localhost:${port}`);
92
+ console.log(`Telemetry portal available at http://localhost:${port}/telemetry`);
96
93
  });
97
94
  ```
98
95
 
package/dist/index.cjs CHANGED
@@ -10,12 +10,10 @@ var _v = _interopRequireDefault(require("v8"));
10
10
  var _fs = require("fs");
11
11
  var _path = _interopRequireDefault(require("path"));
12
12
  var _jsYaml = _interopRequireDefault(require("js-yaml"));
13
- var _url = require("url");
13
+ var _ui = _interopRequireDefault(require("./ui.cjs"));
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
  // telemetryMiddleware.js
16
16
 
17
- const _filename = (0, _url.fileURLToPath)(import.meta.url);
18
- const _dirname = _path.default.dirname(_filename);
19
17
  let telemetryStatus = {
20
18
  active: true
21
19
  };
@@ -36,9 +34,6 @@ function oasTelemetry(tlConfig) {
36
34
  }
37
35
  }
38
36
  const router = (0, _express.Router)();
39
-
40
- //const baseURL = telemetryConfig.baseURL;
41
-
42
37
  router.get(baseURL, mainPage);
43
38
  router.get(baseURL + "/detail/*", detailPage);
44
39
  router.get(baseURL + "/spec", specLoader);
@@ -68,18 +63,10 @@ const apiPage = (req, res) => {
68
63
  res.send(text);
69
64
  };
70
65
  const mainPage = (req, res) => {
71
- const data = (0, _fs.readFileSync)(_dirname + '/ui/main.html', {
72
- encoding: 'utf8',
73
- flag: 'r'
74
- });
75
- res.send(data);
66
+ res.send((0, _ui.default)().main);
76
67
  };
77
68
  const detailPage = (req, res) => {
78
- const data = (0, _fs.readFileSync)(_dirname + '/ui/detail.html', {
79
- encoding: 'utf8',
80
- flag: 'r'
81
- });
82
- res.send(data);
69
+ res.send((0, _ui.default)().detail);
83
70
  };
84
71
  const specLoader = (req, res) => {
85
72
  if (telemetryConfig.specFileName) {
@@ -97,7 +84,7 @@ const specLoader = (req, res) => {
97
84
  console.log(`ERROR loading spec file ${telemetryConfig.specFileName}: ${e}`);
98
85
  }
99
86
  } else {
100
- if (telemetryConfig.spec) {
87
+ if (typeof telemetryConfig.spec === 'string' || telemetryConfig.spec instanceof String) {
101
88
  let spec = false;
102
89
  try {
103
90
  spec = JSON.parse(telemetryConfig.spec);
@@ -114,6 +101,11 @@ const specLoader = (req, res) => {
114
101
  res.setHeader('Content-Type', 'application/json');
115
102
  res.send(spec);
116
103
  }
104
+ } else if (typeof telemetryConfig.spec === 'object') {
105
+ res.setHeader('Content-Type', 'application/json');
106
+ res.send(telemetryConfig.spec);
107
+ } else {
108
+ res.status(404);
117
109
  }
118
110
  }
119
111
  };