Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding the ability to import custom targets and target clients #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 2, 2020

Conversation

erunion
Copy link
Contributor

@erunion erunion commented May 26, 2020

This adds the ability to import custom targets, and custom target clients by way of two new APIs:

  • addTarget
  • addTargetClient

Why? A couple reasons!

  1. Lately it's seemed that pulling in support for new targets, and clients, has become a bit of a blocker. It's no fun to have a repo with more than a handful of useful clients as open pull requests waiting to be merged. With the ability to have a sort of plugin infrastructure, users can just load in whatever custom target they'd like that isn't officially supported here.
  2. I work at @readmeio and we're working on generating some slimmed down code samples on our customer sites, which is requiring us to create custom target clients and without the ability to inject these into the library (short of maintaining an ongoing fork), there's no way we can currently convert HAR representations for them.

Adding custom targets/languages

const customTargetModule = require('./customTargetModule');
HTTPSnippet.addTarget(customTargetModule);

The syntax for adding a new custom targets is identical to how they're currently stored within src/targets:

module.exports = {
  info: {
    key: 'js-variant',
    title: 'JavaScript Variant',
    extname: '.js',
    default: 'xhr'
  },
  xhr: require('./xhr')
};

With this, you'd supply this full export into addTarget and it'll handle validating the construction of the object and appending it to the instance targets variable so it can later be used with .convert(...).

Adding a custom target client

const customClient = require('./customClient');
HTTPSnippet.addTargetClient('node', customClient);

Since clients currently have no awareness to what target/language they're for, the API for adding a custom client is a little different however the data structure behind the plugin is still identical to how they're currently structured.

var helpers = require('./helpers')
var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
  // code here to handle translating a HAR to Guzzle
}

module.exports.info = {
  key: 'guzzle',
  title: 'Guzzle',
  link: 'http://docs.guzzlephp.org/en/stable/',
  description: 'Guzzle is a PHP HTTP client library'
}

As with addTarget, you'd just load this file and pass it directly into addTargetClient and then you can immediately generate code snippets for it:

const snippet = new HTTPSnippet(har);
console.log(snippet.convert('php', 'guzzle'));

@reynolek reynolek merged commit 7755339 into Kong:master Jun 2, 2020
@erunion
Copy link
Contributor Author

erunion commented Jun 3, 2020

Thanks @reynolek! Do you mind tagging a new release?

@reynolek
Copy link
Contributor

reynolek commented Jun 4, 2020

Tagged and pushed v1.20.0

@erunion erunion deleted the feat/plugin-architecture branch August 7, 2020 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants