Skip to content

Commit e3f7991

Browse files
committed
feat: support for making installation string generation dynamic
1 parent cf193bf commit e3f7991

File tree

10 files changed

+35
-18
lines changed

10 files changed

+35
-18
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ 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",
4847
"key": "restsharp",
4948
"link": "http://restsharp.org/",
5049
"title": "RestSharp",
@@ -130,7 +129,6 @@ exports[`availableTargets > returns all available targets 1`] = `
130129
{
131130
"description": "Promise based HTTP client for the browser and node.js",
132131
"extname": ".js",
133-
"installation": "npm install axios --save",
134132
"key": "axios",
135133
"link": "https://github.com/axios/axios",
136134
"title": "Axios",
@@ -195,7 +193,6 @@ exports[`availableTargets > returns all available targets 1`] = `
195193
{
196194
"description": "Promise based HTTP client for the browser and node.js",
197195
"extname": ".js",
198-
"installation": "npm install axios --save",
199196
"key": "axios",
200197
"link": "https://github.com/axios/axios",
201198
"title": "Axios",
@@ -231,7 +228,6 @@ exports[`availableTargets > returns all available targets 1`] = `
231228
{
232229
"description": "Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml",
233230
"extname": ".ml",
234-
"installation": "opam install cohttp-lwt-unix cohttp-async",
235231
"key": "cohttp",
236232
"link": "https://github.com/mirage/ocaml-cohttp",
237233
"title": "CoHTTP",
@@ -254,7 +250,6 @@ exports[`availableTargets > returns all available targets 1`] = `
254250
{
255251
"description": "PHP with Guzzle",
256252
"extname": ".php",
257-
"installation": "composer require guzzlehttp/guzzle",
258253
"key": "guzzle",
259254
"link": "http://docs.guzzlephp.org/en/stable/",
260255
"title": "Guzzle",
@@ -305,7 +300,6 @@ exports[`availableTargets > returns all available targets 1`] = `
305300
{
306301
"description": "Requests HTTP library",
307302
"extname": ".py",
308-
"installation": "python -m pip install requests",
309303
"key": "requests",
310304
"link": "http://docs.python-requests.org/en/latest/api/#requests.request",
311305
"title": "Requests",
@@ -356,7 +350,6 @@ exports[`availableTargets > returns all available targets 1`] = `
356350
{
357351
"description": "a CLI, cURL-like tool for humans",
358352
"extname": ".sh",
359-
"installation": "brew install httpie",
360353
"key": "httpie",
361354
"link": "http://httpie.org/",
362355
"title": "HTTPie",

src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ export class HTTPSnippet {
302302
...urlWithParsedQuery,
303303
query: null,
304304
search: null,
305-
}); //?
305+
});
306306

307307
const fullUrl = urlFormat({
308308
...urlWithParsedQuery,
309309
...uriObj,
310-
}); //?
310+
});
311311

312312
return {
313313
...request,
@@ -318,7 +318,7 @@ export class HTTPSnippet {
318318
};
319319
}
320320

321-
convert(targetId: TargetId, clientId?: ClientId, options?: any) {
321+
convert(targetId: TargetId, clientId?: ClientId, options?: any): string[] | false {
322322
if (!this.initCalled) {
323323
this.init();
324324
}
@@ -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): (string | false)[] | false {
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 { installation } = target.clientsById[clientId || target.info.default];
355+
const results = this.requests.map(request => (installation ? 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,8 +14,8 @@ 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',
1817
},
18+
installation: () => 'dotnet add package RestSharp',
1919
convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => {
2020
const { push, join } = new CodeBuilder();
2121
const isSupportedMethod = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes(

src/targets/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export type ClientId = string;
2929
export interface ClientInfo {
3030
description: string;
3131
extname: Extension;
32-
installation?: string;
3332
key: ClientId;
3433
link: string;
3534
title: string;
@@ -43,6 +42,12 @@ export type Converter<T extends Record<string, any>> = (
4342
export interface Client<T extends Record<string, any> = Record<string, any>> {
4443
convert: Converter<T>;
4544
info: ClientInfo;
45+
/**
46+
* Generates a command to install the client.
47+
*
48+
* @example `npm install axios`
49+
*/
50+
installation?: Converter<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,8 +20,8 @@ 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',
2423
},
24+
installation: () => 'npm install axios --save',
2525
convert: ({ allHeaders, method, url, queryObj, postData }, options) => {
2626
const opts = {
2727
indent: ' ',

src/targets/node/axios/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ 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',
2423
},
24+
installation: () => 'npm install axios --save',
2525
convert: ({ method, fullUrl, allHeaders, postData }, options) => {
2626
const opts = {
2727
indent: ' ',

src/targets/ocaml/cohttp/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ 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',
2322
},
23+
installation: () => 'opam install cohttp-lwt-unix cohttp-async',
2424
convert: ({ fullUrl, allHeaders, postData, method }, options) => {
2525
const opts = {
2626
indent: ' ',

src/targets/php/guzzle/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ 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',
3231
},
32+
installation: () => 'composer require guzzlehttp/guzzle',
3333
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
3434
const opts = {
3535
closingTag: false,

src/targets/python/requests/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ 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',
3130
},
31+
installation: () => 'python -m pip install requests',
3232
convert: ({ fullUrl, postData, allHeaders, method }, options) => {
3333
const opts = {
3434
indent: ' ',

src/targets/shell/httpie/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ 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',
3736
},
37+
installation: () => 'brew install httpie',
3838
convert: ({ allHeaders, postData, queryObj, fullUrl, method, url }, options) => {
3939
const opts = {
4040
body: false,

0 commit comments

Comments
 (0)