apiconnect-example-generator
v3.0.90
Published
API Connect Example Generator
Downloads
1,598
Keywords
Readme
API Explorer Example Generator
Description
This module is used by apiconnect.
IBM API Connect is a complete solution that addresses all aspects of the API lifecycle, for both on-premises and cloud environments. It offers comprehensive capabilities to create, run, manage, secure and monetize APIs and microservices. Delivering an unparalleled, integrated user experience, it enables rapid deployment and simplified administration of APIs.
This module is used to generate examples for a given JSON schema, e.g. for use in a REST API response.
Note: The v3.x version of this module has removed the code snippet generation capabilities. That functionality is now part of apiconnect-explorer. If you still require that functionality then you will need to add it to your application directly or stick with the 2.x version of this module.
Usage
ES6 import:
import {
exampleGenerator,
generatorInner,
resolveReferences,
vkbeautify
} from 'apiconnect-example-generator';
In the browser
Load this file in the browser to gain access to an exampleGen
global.
<script src='./node_modules/apiconnect-example-generator/dist/umd/example-generator.min.js' crossorigin></script>
<script>
console.log(exampleGen);
</script>
Worker script
apiconnect-example-generator
also provides a Web Worker UMD. You can use this to generate examples off the main UI thread.
const worker = new Worker('./node_modules/apiconnect/example-generator/dist/umd/worker-script.min.js');
worker.onmessage = function (event) {
console.log(event.data);
};
worker.onerror = function (error) {
console.error(error);
};
worker.postMessage({
type: 'highlight',
content: 'console.log("hello world");',
language: 'JavaScript'
});
API
exampleGenerator
exampleGenerator.generateExampleResponse
Generates an example response object for the given operation by looking for schema definitions first for a 200 response, then a 201 response, and finally a default response.
exampleGenerator.generateExampleparameter
Generates an example parameter for the given operation parameter, optionally supplying a content type (XML or JSON), and also optionally beautifying the response for readability.
exampleGenerator.createDummyValue
Generates a dummy value given a schema and key
generatorInner
This is the function which the Web Worker script uses internally. You can use this as an alternative. It accepts the same input as the Web Worker's postMessage
, and it returns a Promise
which yields with the result.
import { generatorInner } from 'apiconnect-example-generator';
generatorInner({
type: 'highlight',
content: 'console.log("hello world");',
language: 'JavaScript'
}).then(function (data) {
console.log(data);
});