| May | JUN | Jul |
| 25 | ||
| 2019 | 2020 | 2021 |
COLLECTED BY
Collection: Arquivo.pt: the Portuguese web-archive
You can export generated code to a file: or. Also, generated code will be previewed in the Code Inspector (⇧⌘C to show/hide).
com.mycompany.MyImporter).
Create files and folders
(一)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. MyCodeGenerator.js).
(三)Additionally you can have a README file, or anything else.
Your file tree should look like:
Extensions/
com.mycompany.MyCodeGenerator/
●MyCodeGenerator.js
●README.md
Structure the JavaScript file
generate(context, requests, options) Required: generates the code
context: a Context instance
requests: an array of Request items
options:
inputs: the dictionary with all user inputs
file:
●name: file name, if exported to file
●path: file path, if exported to file
hideCredentials whether user opted to obfuscate credentials
identifier Required: the extension identifier
●title Required: the extension display name
●fileExtension Optional: a file extension (excluding the dot .) for the output file
●languageHighlighter Optional: a keyword identifying the language highlighter for the output (bash, coffeescript, css, go, http, java, javascript, json, markdown, objectivec, perl, php, python, ruby, scala, swift, xml, yaml)
●inputs Optional: the inputs the extension will have (if any)
●help Optional: a URI pointing to the dynamic value documentation (if any)
// Extensions are implemented as JavaScript classes var MyCodeGenerator = function() { // implement the generate() method to generate code this.generate = function(context, requests, options) { var client_code = ""; // generate the code return client_code; } } // set the extension identifier (must be same as the directory name) MyCodeGenerator.identifier = "com.mycompany.MyCodeGenerator"; // give a display name to your Code Generator MyCodeGenerator.title = "My Code Generator"; // call to register function is required registerCodeGenerator(MyCodeGenerator)Define user inputs
generate() call.
Learn how to add input fields to your extension.
Implement generate()
generate(context, requests, options) method and return the code you want to generate. It gets:
context: a Context instance
requests: an array of Request items
options:
inputs: the dictionary with all user inputs
file:
●name: file name, if exported to file
●path: file path, if exported to file
hideCredentials whether user opted to obfuscate credentials
Also, all available classes and functions are in the Reference.
Here’s an example:
this.generate = function(context, requests, options) { var generated = ""; // iterate requests (`Request` objects) for (var i in requests) { var request = requests[i]; // iterate on request headers var headers = request.headers; for (var header_name in headers) { var header_value = headers[header_name]; // do something } // get the latest response status code var status_code = request.getLastExchange().responseStatusCode; // get the latest response body var body = request.getLastExchange().responseBody; generated += status_code + "\n" + body + "\n\n"; } // return the generated string return generated; }Use a template library (example with mustache.js)
mustache.js script into your extension directory. It’s available on GitHub.
(二)Use require()orloadScript() functions to import ./mustache.
(三)Create a mustache template file.
(四)Import the template file in your JavaScript code, using the readFile() function.
(五)Render template.
Here’s an example JavaScript file:
var mustache = require('./mustache') // legacy, import the mustache.js script (to Paw 2.2.2) // require("mustache.js"); var MyCodeGenerator = function() { this.generate = function(context, requests, options) { var generated = ""; // import the mustache template var template = readFile("my-template.mustache"); // iterate requests (`Request` objects) for (var i in requests) { var request = requests[i]; // define your view var view = { "request": request, "content_type": request.getHeaderByName('Content-Type'), }; // render the template generated += Mustache.render(template, view) + "\n\n"; } return generated; } } MyCodeGenerator.identifier = "com.mycompany.MyCodeGenerator"; MyCodeGenerator.title = "My Code Generator"; registerCodeGenerator(MyCodeGenerator)And an example of mustache template file:
var send_request = function() { var url = "{{{request.url}}}"; var content_type = "{{{content_type}}}"; }
.coffee file (e.g. MyCodeGenerator.coffee)
(二)Add a Cakefile that compiles your code into JavaScript (here’s an example: Cakefile)
(三)Share/commit the compiled JavaScript file along with the other files. (We’re in the process of using GitHub Releases to live compile files).
●Blog
●Updates
●Twitter
●Press
●Purchase
●Account
●Terms
●Privacy Policy
●© 2020 Paw Cloud