npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gimmehttp

v0.0.1

Published

HTTP request code generator

Downloads

55

Readme

GimmeHttp

GimmeHttp is a library for generating HTTP request code snippets in various languages based on a simple configuration. Whether you need to quickly prototype an API request or generate example code for documentation, GimmeHttp has you covered. Just provide the request details, and let GimmeHttp generate code in your desired language.

Supported Languages and Targets

| Language | Targets | | ---------- | -------------------- | | c | libcurl | | csharp | http, restsharp | | curl | native | | go | http | | javascript | fetch, axios, jQuery | | node | http, nod-fetch | | php | curl, guzzle | | python | requests, http | | ruby | nethttp, faraday | | rust | reqwest | | swift | nsurlsession |

Installation

To install GimmeHttp, simply use npm:

npm install gimmehttp

Simple Example

Here is a quick example of generating a simple GET request in Go:

import { Generate, Register, AllCodes } from 'gimmehttp';

// Register all codes
Register(AllCodes);

// Create request
const request = {
  language: 'go',
  target: 'native',
  http: {
    method: 'GET',
    url: 'https://gofakeit.com'
  }
};

// Generate output with request
const output = Generate(request);
console.log(output);

Output:

package main

import (
  "fmt"
  "net/http"
  "io"
)

func main() {
  url := "https://gofakeit.com"

  req, _ := http.NewRequest("GET", url, nil)

  resp, _ := http.DefaultClient.Do(req)
  defer resp.Body.Close()

  body, _ := io.ReadAll(resp.Body)

  fmt.Println(string(body))
}

Generate Function

The core functionality of GimmeHttp is its Generate function. This function takes in a request object and returns the generated code snippet as a string. The request object should have the following structure:

Generate(request: Request): string

Request Object

interface Request {
  language: string // go, javascript, python, etc.
  target: string // native, axios, requests, etc.

  // HTTP request details
  http: {
    method: string // 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
    url: string // ex: 'https://gofakeit.com'

    // Optional request details
    headers?: { [key: string]: string }
    cookies?: { [key: string]: string }
    body?: any
  }

  // Optional - configuration for the code generation
  config?: {
      // The character(s) to use for indentation
    indent?: string // default: '  '

    // The character(s) to use for joining lines
    join?: string // default: '\n'

    // Whether or not to handle errors in the generated code
    // default: false to help keep the generated code simple by default
    handleErrors?: boolean // default: false
  }
}

Registry Custom Example

If you want to register a custom language and/or target, you can do so using the Register function:

interface Target {
  default?: boolean
  language: string
  target: string
  generate: (config: any, http: any) => string
}
import { Generate, Register, Config, Http } from 'gimmehttp';

const myCustomTarget = {
  language: 'html',
  target: 'href',
  generate(config: Config, http: Http): string {
    // Custom code generation logic
    return `<a href="${http.url}">${http.method}</a>`;
  }
};

Register(myCustomTarget);

const request = {
  language: 'html',
  target: 'href',
  http: {
    method: 'GET',
    url: 'https://gofakeit.com'
  }
};

const output = Generate(request);
console.log(output);

Output:

<a href="https://gofakeit.com">GET</a>

Examples

POST Request Example

const request = {
  language: 'javascript',
  target: 'fetch',
  http: {
    method: 'POST',
    url: 'https://gofakeit.com',
    headers: {
      'Content-Type': 'application/json'
    },
    body: {
      key1: 'value1'
    }
  }
};

const output = Generate(request);
console.log(output);

Output:

fetch("https://gofakeit.com", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({"key1":"value1"}),
})
.then(response => {
  if (!response.ok) {
    throw new Error("Network response was not ok");
  }
  return response.text();
})
.then(data => console.log(data));

Contributing

GimmeHttp is an open-source project that welcomes contributions from the community. If you would like to contribute, please follow these steps:

  1. Fork the repository
  2. npm install
  3. npm run dev
  4. open http://localhost:1111
  5. Make your changes
  6. Write tests
  7. Git commit and push your changes
  8. Submit a pull request

Feel free to contribute to the project, suggest improvements, or report issues on our GitHub page!