Skip to content

Commit

Permalink
refactor: move contracts to ambient module
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 24, 2019
1 parent f4e66d0 commit 20c001f
Show file tree
Hide file tree
Showing 53 changed files with 1,174 additions and 6,363 deletions.
128 changes: 19 additions & 109 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,40 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of contents

- [Adonis BodyParser 🎯](#adonis-bodyparser-)
- [AdonisJs BodyParser](#adonisjs-bodyparser)
- [Features](#features)
- [Usage](#usage)
- [Installation](#installation)
- [Setup](#setup)
- [Node/OS Target](#nodeos-target)
- [Development](#development)
- [Tests & Linting](#tests--linting)
- [General Practices](#general-practices)
- [Issues & PR](#issues--pr)
- [Regression Tests](#regression-tests)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Adonis BodyParser 🎯
<div align="center">
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px">
</div>

This is the official body parser middleware for the Adonis framework. Apart from just reading the request body and files, it has really cool features.
# AdonisJs BodyParser
> Bodyparser middleware to AdonisJs
> **NOTE**: Only support Adonis4.0 or greater
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![](https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript)


[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Appveyor][appveyor-image]][appveyor-url]
[![Coveralls][coveralls-image]][coveralls-url]

<img src="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1497112678/adonis-purple_pzkmzt.svg" width="200px" align="right" hspace="30px" vspace="80px">
The BodyParser middleware parses the incoming HTTP requests body and files, so that you can access them using the `request` instance. The module is written specifically to work with AdonisJs only.

## Features

1. Support for url-encoded and multipart form bodies.
2. Accepts [JSON Patch [RFC6902]](https://tools.ietf.org/html/rfc6902)
3. Accepts [JSON API v1](http://jsonapi.org/)
4. Accepts [JSON csp-report](https://mathiasbynens.be/notes/csp-reports)
5. Accepts raw text and buffers.
6. Seamless support for nested arrays within the form body.
7. Stream files directly to **s3** using [node-flydrive](https://github.com/Slynova-Org/node-flydrive)
8. Conditionally consume uploaded files as streams.
9. Set hard limits on server to deny request payload greater than `x` bytes.

1. Support for `multipart/form-data`, `application/x-www-form-urlencoded` as well raw HTTP requests.
2. Extensive support for JSON request body including `application/vnd.api+json`, `application/json-patch+json`, `application/csp-report` and `application/json` content type.
3. Exposes API to stream file uploads without writing to the `tmp` directory.

## Usage
The body parser is pre-configured with every new AdonisJs application. But in case you want to setup it manually.

### Installation
```bash
npm i --save adonis-bodyparser
```

### Setup
Make sure to register the provider inside `start/app.js` file.

```js
const providers = [
...
'adonis-bodyparsers/providers/BodyParser'
]
```

Once the provider has been setup, it will automatically register a global middleware for you.

## Node/OS Target

This repo/branch is supposed to run fine on all major OS platforms and targets `Node.js >=7.0`

## Development

Great! If you are planning to contribute to the framework, make sure to adhere to following conventions, since a consistent code-base is always joy to work with.

Run the following command to see list of available npm scripts.

```
npm run
```

### Tests & Linting

1. Lint your code using standardJs. Run `npm run lint` command to check if there are any linting errors.
2. Make sure you write tests for all the changes/bug fixes.
3. Also you can write **regression tests**, which shows that something is failing but doesn't breaks the build. Which is actually a nice way to show that something fails. Regression tests are written using `test.failing()` method.
4. Make sure all the tests are passing on `travis` and `appveyor`.

### General Practices

Since Es6 is in, you should strive to use latest features. For example:

1. Use `Spread` over `arguments` keyword.
2. Never use `bind` or `call`. After calling these methods, we cannot guarantee the scope of any methods and in AdonisJs codebase we do not override the methods scope.
3. Make sure to write proper docblock.

## Issues & PR

It is always helpful if we try to follow certain practices when creating issues or PR's, since it will save everyone's time.

1. Always try creating regression tests when you find a bug (if possible).
2. Share some context on what you are trying to do, with enough code to reproduce the issue.
3. For general questions, please create a forum thread.
4. When creating a PR for a feature, make sure to create a parallel PR for docs too.


## Regression Tests

Regression tests are tests, which shows how a piece of code fails under certain circumstance, but the beauty is even after the failure, the test suite will never fail. Actually is a nice way to notify about bugs, but making sure everything is green.

The regression tests are created using
Install the addon using `adonis cli` as follows:

```sh
adonis install @adonisjs/bodyparser
```
test.failing('2 + 2 is always 4, but add method returns 6', (assert) => {
assert.true(add(2, 2), 4)
})
```

Now since the `add` method has a bug, it will return `6` instead of `4`. But the build will pass.

[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/adonis-bodyparser/master.svg?style=flat-square

[appveyor-url]: https://ci.appveyor.com/project/thetutlage/adonis-bodyparser

[npm-image]: https://img.shields.io/npm/v/@adonisjs/bodyparser.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@adonisjs/bodyparser
The command will create the `config/bodyparser.ts` config file. Also, you can manually copy the [config from here](config/index.ts).

[travis-image]: https://img.shields.io/travis/adonisjs/adonis-bodyparser/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/adonisjs/adonis-bodyparser

[coveralls-image]: https://img.shields.io/coveralls/adonisjs/adonis-bodyparser/develop.svg?style=flat-square
[circleci-image]: https://img.shields.io/circleci/project/github/adonisjs/adonis-bodyparser/master.svg?style=for-the-badge&logo=appveyor
[circleci-url]: https://circleci.com/gh/adonisjs/adonis-bodyparser "circleci"

[coveralls-url]: https://coveralls.io/github/adonisjs/adonis-bodyparser
[npm-image]: https://img.shields.io/npm/v/@adonisjs/bodyparser.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@adonisjs/bodyparser "npm"
3 changes: 2 additions & 1 deletion adonis-typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* file that was distributed with this source code.
*/

import { FileValidationOptions, MultipartFileContract, MultipartContract } from '../src/Contracts'
/// <reference path="../src/Contracts/index.ts" />
import { FileValidationOptions, MultipartFileContract, MultipartContract } from '@ioc:Adonis/Src/BodyParser'

declare module '@ioc:Adonis/Src/Request' {
interface RequestContract {
Expand Down
2 changes: 1 addition & 1 deletion config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import * as uuid from 'uuid/v1'
import { BodyParserConfig } from '../src/Contracts'
import { BodyParserConfig } from '@ioc:Adonis/Src/BodyParser'

/**
* Default config to be used. It will be deep merged
Expand Down
48 changes: 48 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

![](https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg)

AdonisJs BodyParser
===================

> Bodyparser middleware to AdonisJs
[![circleci-image](https://img.shields.io/circleci/project/github/adonisjs/adonis-bodyparser/master.svg?style=for-the-badge&logo=appveyor)](https://circleci.com/gh/adonisjs/adonis-bodyparser "circleci") [![npm-image](https://img.shields.io/npm/v/@adonisjs/bodyparser.svg?style=for-the-badge&logo=npm)](https://npmjs.org/package/@adonisjs/bodyparser "npm") ![](https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript)

The BodyParser middleware parses the incoming HTTP requests body and files, so that you can access them using the `request` instance. The module is written specifically to work with AdonisJs only.

Features
--------

1. Support for `multipart/form-data`, `application/x-www-form-urlencoded` as well raw HTTP requests.
2. Extensive support for JSON request body including `application/vnd.api+json`, `application/json-patch+json`, `application/csp-report` and `application/json` content type.
3. Exposes API to stream file uploads without writing to the `tmp` directory.

Usage
-----

Install the addon using `adonis cli` as follows:

```sh
adonis install @adonisjs/bodyparser
```

The command will create the `config/bodyparser.ts` config file. Also, you can manually copy the [config from here](config/index.ts).

## Index

### External modules

* ["adonis-typings/index"](modules/_adonis_typings_index_.md)
* ["config/index"](modules/_config_index_.md)
* ["lib/index"](modules/_lib_index_.md)
* ["providers/BodyParserProvider"](modules/_providers_bodyparserprovider_.md)
* ["src/BodyParser/index"](modules/_src_bodyparser_index_.md)
* ["src/Contracts/index"](modules/_src_contracts_index_.md)
* ["src/FormFields/index"](modules/_src_formfields_index_.md)
* ["src/Multipart/File"](modules/_src_multipart_file_.md)
* ["src/Multipart/index"](modules/_src_multipart_index_.md)
* ["src/Multipart/processMultipart"](modules/_src_multipart_processmultipart_.md)
* ["src/Multipart/streamFile"](modules/_src_multipart_streamfile_.md)

---

Loading

0 comments on commit 20c001f

Please sign in to comment.