Giter Site home page Giter Site logo

res's Introduction

res

Package res provides handy primitives for working with JSON in Go HTTP servers and clients via go-chi/render. It is designed to be lightweight and easy to extend.

GoDoc CI Status Go Report Card Sourcegraph for Repo Reference Count

I originally wrote something similar to this in two UBC Launch Pad projects that I worked on - Inertia and Pinpoint - and felt like it might be useful to have it as a standalone package.

It is currently a work-in-progress - I'm hoping to continue refining the API and add more robust tests.

Usage

go get -u go.bobheadxi.dev/res

Clientside

I implemented something similar to res in Inertia. It has a client that shows how you might leverage this library: inertia/client.Client

import "go.bobheadxi.dev/res"

func main() {
  resp, err := http.Get(os.Getenv("URL"))
  if err != nil {
    log.Fatal(err)
  }
  var info string
  b, err := res.Unmarshal(resp.Body, res.KV{Key: "info", Value: &info})
  if err != nil {
    log.Fatal(err)
  }
  if err := b.Error(); err != nil {
    log.Fatal(err)
  }
  println(info)
}

Serverside

OK

import "go.bobheadxi.dev/res"

func Handler(w http.ResponseWriter, r *http.Request) {
  res.R(w, r, res.MsgOK("hello world!",
    "stuff", "amazing",
    "details", res.M{"world": "hello"}))
}

Will render something like:

{
  "code": 200,
  "message": "hello world",
  "request_id": "12345",
  "body": {
    "stuff": "amazing",
    "details": {
      "world": "hello",
    }
  }
}

Error

import "go.bobheadxi.dev/res"

func Handler(w http.ResponseWriter, r *http.Request) {
  body, err := ioutil.ReadAll(r.Body)
  if err != nil {
    res.R(w, r, res.ErrBadRequest("failed to read request",
      "error", err,
      "details", "something"))
    return
  }
}

Will render something like:

{
  "code": 400,
  "message": "failed to read request",
  "request_id": "12345",
  "error": "could not read body",
  "body": {
    "details": "something",
  }
}

res's People

Stargazers

 avatar

Watchers

 avatar  avatar

res's Issues

response generator

to allow configuration:

  • headers to include?
  • response structure configuration?
  • logging hooks?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.