Skip to content

Commit f5e3d50

Browse files
authored
feat: support for making installation string generation dynamic (#269)
## 🧰 Changes We're working on some enhancements for HTTP snippet generation for external partners and need to make plugin installation strings dynamic -- this reworks how `installation` is set up on a client basis to allow for this.
1 parent f489b1e commit f5e3d50

File tree

11 files changed

+47
-18
lines changed

11 files changed

+47
-18
lines changed

src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ exports[`availableTargets > returns all available targets 1`] = `
4444
{
4545
"description": "Simple REST and HTTP API Client for .NET",
4646
"extname": ".cs",
47-
"installation": "dotnet add package RestSharp",
47+
"installation": [Function],
4848
"key": "restsharp",
4949
"link": "http://restsharp.org/",
5050
"title": "RestSharp",
@@ -130,7 +130,7 @@ exports[`availableTargets > returns all available targets 1`] = `
130130
{
131131
"description": "Promise based HTTP client for the browser and node.js",
132132
"extname": ".js",
133-
"installation": "npm install axios --save",
133+
"installation": [Function],
134134
"key": "axios",
135135
"link": "https://github.com/axios/axios",
136136
"title": "Axios",
@@ -195,7 +195,7 @@ exports[`availableTargets > returns all available targets 1`] = `
195195
{
196196
"description": "Promise based HTTP client for the browser and node.js",
197197
"extname": ".js",
198-
"installation": "npm install axios --save",
198+
"installation": [Function],
199199
"key": "axios",
200200
"link": "https://github.com/axios/axios",
201201
"title": "Axios",
@@ -231,7 +231,7 @@ exports[`availableTargets > returns all available targets 1`] = `
231231
{
232232
"description": "Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml",
233233
"extname": ".ml",
234-
"installation": "opam install cohttp-lwt-unix cohttp-async",
234+
"installation": [Function],
235235
"key": "cohttp",
236236
"link": "https://github.com/mirage/ocaml-cohttp",
237237
"title": "CoHTTP",
@@ -254,7 +254,7 @@ exports[`availableTargets > returns all available targets 1`] = `
254254
{
255255
"description": "PHP with Guzzle",
256256
"extname": ".php",
257-
"installation": "composer require guzzlehttp/guzzle",
257+
"installation": [Function],
258258
"key": "guzzle",
259259
"link": "http://docs.guzzlephp.org/en/stable/",
260260
"title": "Guzzle",
@@ -305,7 +305,7 @@ exports[`availableTargets > returns all available targets 1`] = `
305305
{
306306
"description": "Requests HTTP library",
307307
"extname": ".py",
308-
"installation": "python -m pip install requests",
308+
"installation": [Function],
309309
"key": "requests",
310310
"link": "http://docs.python-requests.org/en/latest/api/#requests.request",
311311
"title": "Requests",
@@ -356,7 +356,7 @@ exports[`availableTargets > returns all available targets 1`] = `
356356
{
357357
"description": "a CLI, cURL-like tool for humans",
358358
"extname": ".sh",
359-
"installation": "brew install httpie",
359+
"installation": [Function],
360360
"key": "httpie",
361361
"link": "http://httpie.org/",
362362
"title": "HTTPie",

src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,23 @@ export class HTTPSnippet {
336336
const results = this.requests.map(request => convert(request, options));
337337
return results;
338338
}
339+
340+
installation(targetId: TargetId, clientId?: ClientId, options?: any) {
341+
if (!this.initCalled) {
342+
this.init();
343+
}
344+
345+
if (!options && clientId) {
346+
options = clientId;
347+
}
348+
349+
const target = targets[targetId];
350+
if (!target) {
351+
return [false];
352+
}
353+
354+
const { info } = target.clientsById[clientId || target.info.default];
355+
const results = this.requests.map(request => (info?.installation ? info.installation(request, options) : false));
356+
return results;
357+
}
339358
}

src/targets/csharp/restsharp/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const restsharp: Client = {
1414
link: 'http://restsharp.org/',
1515
description: 'Simple REST and HTTP API Client for .NET',
1616
extname: '.cs',
17-
installation: 'dotnet add package RestSharp',
17+
installation: () => 'dotnet add package RestSharp',
1818
},
1919
convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => {
2020
const { push, join } = new CodeBuilder();

src/targets/index.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ describe('addTargetClient', () => {
303303
link: 'https://example.com',
304304
description: 'A custom HTTP library',
305305
extname: '.custom',
306+
installation: request => {
307+
return `brew install ${request.fullUrl}`;
308+
},
306309
},
307310
convert: () => {
308311
return 'This was generated from a custom client.';
@@ -314,8 +317,10 @@ describe('addTargetClient', () => {
314317
const snippet = new HTTPSnippet(short.log.entries[0].request as Request, {});
315318

316319
const result = snippet.convert('node', 'custom')[0];
317-
318320
expect(result).toBe('This was generated from a custom client.');
321+
322+
const install = snippet.installation('node', 'custom')[0];
323+
expect(install).toBe('brew install https://httpbin.org/anything');
319324
});
320325
});
321326

src/targets/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ export type TargetId = keyof typeof targets;
2626

2727
export type ClientId = string;
2828

29-
export interface ClientInfo {
29+
export interface ClientInfo<T extends Record<string, any> = Record<string, any>> {
3030
description: string;
3131
extname: Extension;
32-
installation?: string;
32+
/**
33+
* Retrieve or generate a command to install the client.
34+
*
35+
* @example `npm install axios`
36+
*/
37+
installation?: Converter<T>;
3338
key: ClientId;
3439
link: string;
3540
title: string;
@@ -42,7 +47,7 @@ export type Converter<T extends Record<string, any>> = (
4247

4348
export interface Client<T extends Record<string, any> = Record<string, any>> {
4449
convert: Converter<T>;
45-
info: ClientInfo;
50+
info: ClientInfo<T>;
4651
}
4752

4853
export interface ClientPlugin<T extends Record<string, any> = Record<string, any>> {

src/targets/javascript/axios/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const axios: Client = {
2020
link: 'https://github.com/axios/axios',
2121
description: 'Promise based HTTP client for the browser and node.js',
2222
extname: '.js',
23-
installation: 'npm install axios --save',
23+
installation: () => 'npm install axios --save',
2424
},
2525
convert: ({ allHeaders, method, url, queryObj, postData }, options) => {
2626
const opts = {

src/targets/node/axios/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const axios: Client = {
2020
link: 'https://github.com/axios/axios',
2121
description: 'Promise based HTTP client for the browser and node.js',
2222
extname: '.js',
23-
installation: 'npm install axios --save',
23+
installation: () => 'npm install axios --save',
2424
},
2525
convert: ({ method, fullUrl, allHeaders, postData }, options) => {
2626
const opts = {

src/targets/ocaml/cohttp/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const cohttp: Client = {
1919
link: 'https://github.com/mirage/ocaml-cohttp',
2020
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
2121
extname: '.ml',
22-
installation: 'opam install cohttp-lwt-unix cohttp-async',
22+
installation: () => 'opam install cohttp-lwt-unix cohttp-async',
2323
},
2424
convert: ({ fullUrl, allHeaders, postData, method }, options) => {
2525
const opts = {

src/targets/php/guzzle/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const guzzle: Client<GuzzleOptions> = {
2828
link: 'http://docs.guzzlephp.org/en/stable/',
2929
description: 'PHP with Guzzle',
3030
extname: '.php',
31-
installation: 'composer require guzzlehttp/guzzle',
31+
installation: () => 'composer require guzzlehttp/guzzle',
3232
},
3333
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
3434
const opts = {

src/targets/python/requests/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const requests: Client<RequestsOptions> = {
2727
link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
2828
description: 'Requests HTTP library',
2929
extname: '.py',
30-
installation: 'python -m pip install requests',
30+
installation: () => 'python -m pip install requests',
3131
},
3232
convert: ({ fullUrl, postData, allHeaders, method }, options) => {
3333
const opts = {

src/targets/shell/httpie/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const httpie: Client<HttpieOptions> = {
3333
link: 'http://httpie.org/',
3434
description: 'a CLI, cURL-like tool for humans',
3535
extname: '.sh',
36-
installation: 'brew install httpie',
36+
installation: () => 'brew install httpie',
3737
},
3838
convert: ({ allHeaders, postData, queryObj, fullUrl, method, url }, options) => {
3939
const opts = {

0 commit comments

Comments
 (0)