Giter Club home page Giter Club logo

koa-client's Introduction

Koa-Client

A client-side implementation of koa that automatically intercepts anchor clicks and form submissions. The goal of this project is to mimic the koa server implementation as closely as possible so that middleware can work on the client (such as koa-mount-route) while keeping file size to a minimum.

Install

npm install koa-client

Public API

###app.use(function) mount middleware function

###app.listen([selector|element]) intercept clicks and submissions from all children of the element. Default is document.body.

###app.request(req, [opts]) sends a request through app's middleware functions. It also updates the browser url using History API. This is the same function that is used internally to route requests that are intercepted from the element specified to app.listen. opts is an optional options object, the only option is replace_state, that when true uses history.replaceState instead of history.pushState to update the browser url.

###app.redirect(req) shorthand for app.request(req, { replace_state:true })

###app.refresh() shorthand for app.request(window.location.href, { replace_state:true })

Differences from Server

Context

Added Properties

ctx.browser = true

a variable that always returns true. helpful for middleware that you intend to run on both the client and server versions of koa.

Similar Properties

ctx.assert(test, status, message)

simple assertion, if test fails throws a plain Error with status attached. this is not an instance of a ClientError or ServerError.

ctx.throw(err|msg|status[, err|msg|status])

similar to above, throws a plain Error with status attached. this is not an instance of a ClientError or ServerError.

ctx.cookies.get(name)/ctx.cookies.set(name, val[, opts])

get and set cookies using document.cookie

ctx.app/ctx.request/ctx.response

references the same propeties as the server:

Removed Properties

  • ctx.req removed
  • ctx.res removed
  • ctx.respond() removed

Accessors/Methods

Aside from those that have been removed, koa-client provides the same accessors that are on the server, although the implementation on the request/response may differ.

Request
  • ctx.host
  • ctx.hostname
  • ctx.protocol
  • ctx.header
  • ctx.headers
  • ctx.method
  • ctx.method=
  • ctx.url
  • ctx.url=
  • ctx.originalUrl
  • ctx.path
  • ctx.path=
  • ctx.query
  • ctx.query=
  • ctx.querystring
  • ctx.querystring=
  • ctx.get()
  • ctx.fresh removed
  • ctx.stale removed
  • ctx.socket removed
  • ctx.secure removed
  • ctx.ip removed
  • ctx.ips removed
  • ctx.subdomains removed
  • ctx.is() removed
  • ctx.accepts() removed
  • ctx.acceptsEncodings() removed
  • ctx.acceptsCharsets() removed
  • ctx.acceptsLanguages() removed
Response
  • ctx.body
  • ctx.body=
  • ctx.status
  • ctx.status=
  • ctx.message
  • ctx.message=
  • ctx.length=
  • ctx.length
  • ctx.type=
  • ctx.type
  • ctx.headerSent
  • ctx.redirect()
  • ctx.attachment()
  • ctx.set()
  • ctx.remove()
  • ctx.lastModified=
  • ctx.etag=

Request

Unmodified

The following are actually slightly modified, since there is no node req underlying the getters/setters, but the functionality should be exactly the same.

  • request.host
  • request.hostname
  • request.protocol
  • request.method
  • request.method=
  • request.url
  • request.url=
  • request.originalUrl
  • request.path
  • request.path=
  • request.query
  • request.query=
  • request.querystring
  • request.querystring=
  • request.search
  • request.search=

Similar Properties

request.header[s]/request.get()

does not get headers from an underlying node res object. expects lowercase header names.

Removed Properties

  • request.fresh removed
  • request.stale removed
  • request.socket removed
  • request.secure removed
  • request.ip removed
  • request.ips removed
  • request.subdomains removed
  • request.is() removed
  • request.accepts() removed
  • request.acceptsEncodings() removed
  • request.acceptsCharsets() removed
  • request.acceptsLanguages() removed

Response

Similar Properties

response.redirect(url[, alt])

does not set response.body or response.type, otherwise the same

response.header[s]/response.get()/response.set()/response.remove()

does not set headers on an underlying node res object. forces toLowerCase() on all header names.

Unspecified Properties

The following are unspecified properties of the response object. They have no getter/setter but you can still use them, they just won't do their magic behind the scence. (ex. request.body = {} will only set request.body to {}, it will not update content type/length headers.

  • response.body
  • response.status
  • response.type
  • response.headerSent
  • response.message
  • response.lastModified
  • response.etag
  • response.length

Removed Properties

  • response.attachment() removed
  • response.vary() removed
  • response.socket removed
  • response.is() removed
  • response.writeable removed

koa-client's People

Contributors

mlrawlings avatar

Watchers

James Cloos avatar Dylan Piercey avatar  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.