Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "Simple REST and HTTP API Client for .NET",
"extname": ".cs",
"installation": "dotnet add package RestSharp",
"installation": [Function],
"key": "restsharp",
"link": "http://restsharp.org/",
"title": "RestSharp",
Expand Down Expand Up @@ -130,7 +130,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "Promise based HTTP client for the browser and node.js",
"extname": ".js",
"installation": "npm install axios --save",
"installation": [Function],
"key": "axios",
"link": "https://github.com/axios/axios",
"title": "Axios",
Expand Down Expand Up @@ -195,7 +195,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "Promise based HTTP client for the browser and node.js",
"extname": ".js",
"installation": "npm install axios --save",
"installation": [Function],
"key": "axios",
"link": "https://github.com/axios/axios",
"title": "Axios",
Expand Down Expand Up @@ -231,7 +231,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml",
"extname": ".ml",
"installation": "opam install cohttp-lwt-unix cohttp-async",
"installation": [Function],
"key": "cohttp",
"link": "https://github.com/mirage/ocaml-cohttp",
"title": "CoHTTP",
Expand All @@ -254,7 +254,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "PHP with Guzzle",
"extname": ".php",
"installation": "composer require guzzlehttp/guzzle",
"installation": [Function],
"key": "guzzle",
"link": "http://docs.guzzlephp.org/en/stable/",
"title": "Guzzle",
Expand Down Expand Up @@ -305,7 +305,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "Requests HTTP library",
"extname": ".py",
"installation": "python -m pip install requests",
"installation": [Function],
"key": "requests",
"link": "http://docs.python-requests.org/en/latest/api/#requests.request",
"title": "Requests",
Expand Down Expand Up @@ -356,7 +356,7 @@ exports[`availableTargets > returns all available targets 1`] = `
{
"description": "a CLI, cURL-like tool for humans",
"extname": ".sh",
"installation": "brew install httpie",
"installation": [Function],
"key": "httpie",
"link": "http://httpie.org/",
"title": "HTTPie",
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,23 @@ export class HTTPSnippet {
const results = this.requests.map(request => convert(request, options));
return results;
}

installation(targetId: TargetId, clientId?: ClientId, options?: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry one more thing — wanna make this return type a little stricter?

Suggested change
installation(targetId: TargetId, clientId?: ClientId, options?: any) {
installation(targetId: TargetId, clientId?: ClientId, options?: any): (string | false)[] {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the inferred type is a little messy atm:

HTTPSnippet.installation(targetId: TargetId, clientId?: ClientId, options?: any): (string | false)[] | boolean[]

Copy link
Member Author

@erunion erunion Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going to do isolateddeclarations in a followup

if (!this.initCalled) {
this.init();
}

if (!options && clientId) {
options = clientId;
}

const target = targets[targetId];
if (!target) {
return [false];
}

const { info } = target.clientsById[clientId || target.info.default];
const results = this.requests.map(request => (info?.installation ? info.installation(request, options) : false));
return results;
}
}
2 changes: 1 addition & 1 deletion src/targets/csharp/restsharp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const restsharp: Client = {
link: 'http://restsharp.org/',
description: 'Simple REST and HTTP API Client for .NET',
extname: '.cs',
installation: 'dotnet add package RestSharp',
installation: () => 'dotnet add package RestSharp',
},
convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => {
const { push, join } = new CodeBuilder();
Expand Down
7 changes: 6 additions & 1 deletion src/targets/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ describe('addTargetClient', () => {
link: 'https://example.com',
description: 'A custom HTTP library',
extname: '.custom',
installation: request => {
return `brew install ${request.fullUrl}`;
},
},
convert: () => {
return 'This was generated from a custom client.';
Expand All @@ -314,8 +317,10 @@ describe('addTargetClient', () => {
const snippet = new HTTPSnippet(short.log.entries[0].request as Request, {});

const result = snippet.convert('node', 'custom')[0];

expect(result).toBe('This was generated from a custom client.');

const install = snippet.installation('node', 'custom')[0];
expect(install).toBe('brew install https://httpbin.org/anything');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hell yeah thank you!

});
});

Expand Down
11 changes: 8 additions & 3 deletions src/targets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ export type TargetId = keyof typeof targets;

export type ClientId = string;

export interface ClientInfo {
export interface ClientInfo<T extends Record<string, any> = Record<string, any>> {
description: string;
extname: Extension;
installation?: string;
/**
* Retrieve or generate a command to install the client.
*
* @example `npm install axios`
*/
installation?: Converter<T>;
key: ClientId;
link: string;
title: string;
Expand All @@ -42,7 +47,7 @@ export type Converter<T extends Record<string, any>> = (

export interface Client<T extends Record<string, any> = Record<string, any>> {
convert: Converter<T>;
info: ClientInfo;
info: ClientInfo<T>;
}

export interface ClientPlugin<T extends Record<string, any> = Record<string, any>> {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/javascript/axios/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const axios: Client = {
link: 'https://github.com/axios/axios',
description: 'Promise based HTTP client for the browser and node.js',
extname: '.js',
installation: 'npm install axios --save',
installation: () => 'npm install axios --save',
},
convert: ({ allHeaders, method, url, queryObj, postData }, options) => {
const opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/node/axios/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const axios: Client = {
link: 'https://github.com/axios/axios',
description: 'Promise based HTTP client for the browser and node.js',
extname: '.js',
installation: 'npm install axios --save',
installation: () => 'npm install axios --save',
},
convert: ({ method, fullUrl, allHeaders, postData }, options) => {
const opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/ocaml/cohttp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const cohttp: Client = {
link: 'https://github.com/mirage/ocaml-cohttp',
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
extname: '.ml',
installation: 'opam install cohttp-lwt-unix cohttp-async',
installation: () => 'opam install cohttp-lwt-unix cohttp-async',
},
convert: ({ fullUrl, allHeaders, postData, method }, options) => {
const opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/php/guzzle/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const guzzle: Client<GuzzleOptions> = {
link: 'http://docs.guzzlephp.org/en/stable/',
description: 'PHP with Guzzle',
extname: '.php',
installation: 'composer require guzzlehttp/guzzle',
installation: () => 'composer require guzzlehttp/guzzle',
},
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
const opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/python/requests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const requests: Client<RequestsOptions> = {
link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
description: 'Requests HTTP library',
extname: '.py',
installation: 'python -m pip install requests',
installation: () => 'python -m pip install requests',
},
convert: ({ fullUrl, postData, allHeaders, method }, options) => {
const opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/shell/httpie/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const httpie: Client<HttpieOptions> = {
link: 'http://httpie.org/',
description: 'a CLI, cURL-like tool for humans',
extname: '.sh',
installation: 'brew install httpie',
installation: () => 'brew install httpie',
},
convert: ({ allHeaders, postData, queryObj, fullUrl, method, url }, options) => {
const opts = {
Expand Down
Loading