Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gimmehttp

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gimmehttp

HTTP request code generator

  • 0.0.5
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
83
decreased by-63.6%
Maintainers
1
Weekly downloads
 
Created
Source

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

LanguageTargets
clibcurl
csharphttp, restsharp
curlnative
gohttp
javascriptfetch, axios, jQuery
nodehttp, nod-fetch
phpcurl, guzzle
pythonrequests, http
rubynethttp, faraday
rustreqwest
swiftnsurlsession

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 } from 'gimmehttp'

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

// Generate code
const { code, error } = Generate(settings)
if (error) {
  console.error(error)
}

// Output generated code
console.log(code)

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(settings: Settings): Outcome

Settings Object

interface Settigns {
  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
  }
}

Outcome Object

The Generate function returns an Outcome object. If the object contains an error property, an error occurred during code generation.

import { Generate } from 'gimmehttp'

const { code, error, language, client } = Generate(request)
if (error) {
  console.error(error)
}

// Output generated code
console.log(code)
console.log(language)
console.log(client)
interface Outcome {
  error?: string // An error message if an error occurred
  
  // or //

  language?: string // Language used
  client?: string // Client used, set to default if not specified
  code?: string // Generated code
}

Registry Custom Example

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

interface Target {
  default?: boolean
  language: string
  target: string
  generate: (config: Config, http: Http) => string
}
import { Register, Generate } from 'gimmehttp'
import type { 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 settings = {
  language: 'html',
  target: 'href',
  http: {
    method: 'GET',
    url: 'https://gofakeit.com'
  }
}

const { code, error } = Generate(settings)
if (error) {
  console.error(error)
}
console.log(code)

Output:

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

Examples

POST Request Example

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

const { code, error } = Generate(settings)
if (error) {
  console.error(error)
}
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!

Keywords

FAQs

Package last updated on 20 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc