Paw can import third party files (e.g. from other HTTP clients, API description formats, HTTP archives, source files…) via importers. You can import via:
Create your extension
Choose an Extension Identifier
Choose an Extension Identifier
The Extension Identifier should be a reverse domain name and have a unique and meaningful name (e.g. com.mycompany.MyImporter
).
Create files and folders
Create files and folders
Open the Extensions Directory: you can find it in the .
- Create a new folder and name it after your Extension Identifier.
- Create a
.js
JavaScript file named after the last component of the identifier (e.g. MyImporter.js
).
- Additionally you can have a
README
file, or anything else.
Your file tree should look like:
Structure the JavaScript file
Structure the JavaScript file
An Extension is represented as a JavaScript class.
Methods
Importers must have two methods:
import(context, items, options)
Required: generates the output code
canImport(context, items)
Optional: evaluates items
and returns a bool
indicating if the extension can import the items; alternatively, can return the level of confidence as a float
between 0.0
(can’t import) and 1.0
(can import for sure)
context
: a Context instance
items
: an array of string items
Static properties
Also, the class object have these properties:
identifier
Required: the extension identifier
title
Required: the extension display name
fileExtensions
Optional: an array of file extensions (excluding the dot .
) that the importer can handle
inputs
Optional: the inputs the extension will have (if any)
help
Optional: a URI pointing to the dynamic value documentation (if any)
Example
// Extensions are implemented as JavaScript classes
var MyImporter = function() {
// implement the import() method to generate code
this.import = function(context, items, options) {
// Parse each string of `items`
var desc += "\n";
for (var i in items) {
var item = items[i];
desc += " * " + item.content + "\n";
if (/* parsing failed */) {
// Failure
throw new Error "Invalid input file";
}
}
// Create requests (and groups)
var request = context.createRequest('My Imported Request', 'POST', 'https://api.domain.com/path', 'This is a description of the imported request: ' + desc);
// If file was dragged and dropped over the request list, insert request at proper location (group and order)
if (options.parent !== undefined) {
options.parent.insertChild(request, options.order);
}
// Success
return true;
}
// check if we can import these items
this.canImport = function(context, items) {
var knownItems = 0;
for (var i in items) {
var item = items[i];
var itemString = item.content;
// analyze each string to determine if can import
knownItems++;
}
var confidence = knownItems/(items.length);
console.log("Confidence: " + confidence);
return confidence;// or, true
}
}
// set the Extension Identifier (must be same as the directory name)
MyImporter.identifier = "com.mycompany.MyImporter";
// give a display name to your Importer
MyImporter.title = "My Importer";
// supported file extensions
MyImporter.fileExtensions = ["index", "hya"];
// call to register function is required
registerImporter(MyImporter)
Implement import()
Implement import()
Implement the import(context, items, options)
method, parse items
to add requests, groups or environments.
It gets:
It returns:
boolean
: if false
it’s a failure
Promise
: we wait on the Promise, passing two arguments to .then(resolve, reject)
Error
: if an Error object is returned, it’s considered a failure, the Error.message
is shown to the user
undefined
(nothing returned): it’s considered a success
If an Error is thrown during the execution: it’s considered a JavaScript exception, the user is warned of the problem and the Error.message
is displayed.
Also, all available classes and functions are in the Reference.
Here’s an example:
this.import = function(context, items, options) {
// Parse strings like `POST http://api.domain.com/path`
var i = 1;
for (var i in items) {
var item = items[i];
var match = item.content.match(/^([a-z]+)\s+(.*)$/i);
if (match == null) {
// Failure
throw new Error "Invalid input file";
}
// Create request
var request = context.createRequest('Request ' + i, match[1], match[2]);
i++;
}
// Success
return true;
}
Share your importer
Your importer is probably helpful for others too, so feel free to push it to GitHub, and let us know so we will add it to the Extension list.
Here’s an example of Extension on GitHub: Swagger Importer for Paw