Git Product home page Git Product logo

output's Introduction

Introduction:

This package provides functions for returning data to HTTP API calls in a consistent manner so clients can comprehend the data more easily.

Details:

  • Returns data as JSON.
  • Each object returned has a status, a message type, the arbitrary data being returned, and a timestamp.
  • Can be used for successful and error states.
  • Message types allow for clients to understand various data coming from one endpoint.

Data Format:

  • OK: boolean.
  • Type: string.
  • Data: interface, can be anything. Structs are encoded as JS objects.
  • Datetime: YYYY-MM-DD HH:MM:SS.sss string in UTC timezone.
  • ErrorData: object with Error and Message fields.

Message Types:

There are predefined message types included in the package that are used with the defined helper funcs (InsertOK, DataFound, Error, etc.). However, you can define your own message types and use them with Success.

It is strongly advised to limit your custom message types, and keep track of them well, to reduce the inclination to "just use a new message type" with each response. This is more important in larger projects where different authors may create their own, sometimes overlapping, message types.

Use:

This package was designed to send back data from a webapp to client-side JS code that interacts and renders the GUI. Having a consistent format was also nice for logging and diagnostics.

In most cases, you will use the higher level functions (InsertOK, UpdateOK, etc.) instead of Success as these functions will set a message type automatically and reduce the amount of code typed in your projects.

Some demo code:

//success
mt := output.MessageType("myCustomMessagType")
data := map[string]string{
    "key": "value",
    "wyle": "e coyote",
}
output.Success(mt, data, w)

//error
err := funcThatCausesError()
output.Error(err, "Human readable error message or how to fix the error.", w)

output's People

Contributors

c9845 avatar

Watchers

 avatar

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.