Git Product home page Git Product logo

content-type's Introduction

@goa/content-type

npm version

@goa/content-type is a fork of Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.

yarn add @goa/content-type

Table Of Contents

API

The package is available by importing its named functions:

import { parse, format } from '@goa/content-type'

parse(
  header: string|http.IncomingMessage|http.ServerResponse,
): ContentType

Parse a Content-Type header either from a string, or a request or response objects.

import('http').IncomingMessage http.IncomingMessage

import('http').ServerResponse http.ServerResponse

_goa.ContentType: The content-type interface.

Name Type Description
type* string The type of the content-type.
parameters* !Object<string, string> An object of the parameters in the media type (name of the parameter will be lower-cased).
import { parse } from '@goa/content-type'

const res = parse('image/svg+xml; charset=utf-8')
console.log(res)

const asRequest = parse({ headers: { // as IncomingMessage
  'content-type': 'image/svg+xml; charset=utf-8',
} })
console.log(asRequest)

const asResponse = parse({ getHeader(header) { // as ServerResponse
  if (header == 'content-type')
    return 'image/svg+xml; charset=utf-8'
} })
console.log(asResponse)
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }

format(
  obj: ContentType,
): string

Format an object into a Content-Type header. This will return a string of the content type for the given object.

SourceOutput
import { format } from '@goa/content-type'

const res = format({
  type: 'image/svg+xml',
  parameters: { charset: 'utf-8' },
})
console.log(res)
image/svg+xml; charset=utf-8

Copyright

Original work by Douglas Christopher Wilson and contributors.


Art Deco © Art Deco for Idio 2019 Idio Tech Nation Visa Tech Nation Visa Sucks

content-type's People

Contributors

zavr-1 avatar

Stargazers

 avatar  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.