Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM #2

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c02bea9
feat: support conditional schemas from the outside, updates dependencies
dgilperez Jan 26, 2022
bfbd4e6
chore: rc files
dgilperez Jan 26, 2022
1b030c7
feat: adds ajw-keywords and drop babel build
dgilperez Jan 26, 2022
99b31b1
release: new version
dgilperez Jan 26, 2022
649f985
feat: adds ajv-keywords
dgilperez Jan 26, 2022
5b600a3
feat: adds ajv-keywords dependency to add schema transforms and more …
dgilperez Feb 16, 2021
6c9e652
chore: package.json
dgilperez Jan 7, 2022
1c7a332
:chore: bumps dependencies, prepares to release fork
dgilperez Jan 25, 2022
843c135
:chore: prepare to release as independent package
dgilperez Jan 24, 2022
d846149
:chore: updates babel parser and environment for lint to run, bumps d…
dgilperez Jan 24, 2022
53ac863
:chore: updates .prettierrc, autofix all lint issues
dgilperez Jan 24, 2022
4f9f4c0
chore: update dependencies, leave ESM for another day, tests green
dgilperez Jan 25, 2022
97f585b
chore: makes linter happy
dgilperez Jan 25, 2022
48ac0c7
chore: package-lock
dgilperez Jan 25, 2022
350f1bc
bug: forgot to setup the build dist and released an empty version
dgilperez Jan 25, 2022
d48f7e3
chore: dist folder added properly to package.json
dgilperez Jan 25, 2022
d834d62
chore: bumps version
dgilperez Jan 25, 2022
7beda93
chore: bumps version
dgilperez Jan 25, 2022
aabba52
wip: transform to esm, removes requires and also migrate from riteawy…
dgilperez Jan 25, 2022
df7a944
wip
dgilperez Jan 25, 2022
215d240
chore: package-lock
dgilperez Jan 26, 2022
d54a472
chore: test conditional schema
dgilperez Jan 26, 2022
7a9032f
Merge branch 'master' into esm
dgilperez Jan 26, 2022
059a0fb
moving all requires to imports, moving runtime requires to async impo…
dgilperez May 7, 2022
64898bd
tests running, even though red
dgilperez May 8, 2022
a06d379
use tape deepEqual instead of equal for arrays and objects
dgilperez May 8, 2022
215340f
chore: updates AJV to latest versions
dgilperez May 8, 2022
c3c2d60
chore: update helmet, busboy and add dotenv dev dependency
dgilperez May 8, 2022
6e60f3a
chore: eslint babel configuration
dgilperez May 8, 2022
20da7a6
feat: migrate to ESM, tests are now green - except for MULTIPART, whi…
dgilperez May 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"presets": [
["@babel/preset-env", {
"modules": false
}]
],

"plugins": [
// Babel uses very small helpers for common functions such as _extend. By
// default this will be added to every file that requires it. This
// duplication is sometimes unnecessary, especially when your application
// is spread out over multiple files.
"@babel/plugin-transform-runtime"
]
}
13 changes: 11 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"parser": "babel-eslint",
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile" : "false",
"babelOptions": {
"configFile": "./.babelrc"
}
},
"root": true,
"extends": [
"@asd14/eslint-config/targets/node"
Expand All @@ -13,8 +19,11 @@
}
},
"rules": {
"import/extensions": "off",
"unicorn/consistent-function-scoping": "warn",
"unicorn/prefer-module": "off",
"unicorn/no-thenable": "off",
"promise/avoid-new": "off",
"valid-jsdoc": "off"
},
}
}
74 changes: 74 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output
.coveralls.yml

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# Sublime text
*.sublime-project
*.sublime-workspace
.tern-port

# Vim ctags
tags

tests
docs
.circleci
10 changes: 7 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"semi": false,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": false,
"quoteProps": "consistent",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"jsxBracketSameLine": true,
"arrowParens": "avoid"
"bracketSameLine": true,
"arrowParens": "avoid",
"proseWrap": "always",
"endOfLine": "lf"
}
29 changes: 29 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"branches": [
{
"name": "master"
}
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
[
"@semantic-release/changelog",
{
"changelogTitle": "# Changelog",
"changelogFile": "CHANGELOG.md"
}
],
[
"@semantic-release/git",
{
"assets": [
"CHANGELOG.md",
"package.json"
]
}
],
"@semantic-release/github"
]
}
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Releases and changelog are automaticly handled by [semantic-release](https://git

All releases are based on Angular's [Git commit message](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines) patterns.

See the [releases section](https://github.com/asd-xiv/blocks/releases) for details.
See the [releases section](https://github.com/heartsradiant/blocks/releases) for details.
108 changes: 54 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- markdownlint-disable first-line-h1 line-length -->

[![CircleCI](https://circleci.com/gh/asd-xiv/blocks.svg?style=svg)](https://circleci.com/gh/asd-xiv/blocks)
[![CircleCI](https://circleci.com/gh/heartsradiant/blocks.svg?style=svg)](https://circleci.com/gh/heartsradiant/blocks)
[![npm package version](https://badge.fury.io/js/%40asd14%2Fblocks.svg)](https://badge.fury.io/js/%40asd14%2Fblocks)
[![Coverage Status](https://coveralls.io/repos/github/asd-xiv/blocks/badge.svg)](https://coveralls.io/github/asd-xiv/blocks)
[![Coverage Status](https://coveralls.io/repos/github/heartsradiant/blocks/badge.svg)](https://coveralls.io/github/heartsradiant/blocks)

# blocks

Expand All @@ -14,33 +14,33 @@

<!-- vim-markdown-toc GFM -->

* [Features](#features)
* [Validate input](#validate-input)
* [Permissions](#permissions)
* [Plugin / Dependency Injection](#plugin--dependency-injection)
* [Promises](#promises)
* [Other](#other)
* [Install](#install)
* [Example](#example)
* [Configuration](#configuration)
* [Routes](#routes)
* [Default "/ping"](#default-ping)
* [Definition](#definition)
* [JSON schemas](#json-schemas)
* [Data formats](#data-formats)
* [Plugins](#plugins)
* [Custom plugin](#custom-plugin)
* [Develop](#develop)
* [Changelog](#changelog)
- [Features](#features)
- [Validate input](#validate-input)
- [Permissions](#permissions)
- [Plugin / Dependency Injection](#plugin--dependency-injection)
- [Promises](#promises)
- [Other](#other)
- [Install](#install)
- [Example](#example)
- [Configuration](#configuration)
- [Routes](#routes)
- [Default "/ping"](#default-ping)
- [Definition](#definition)
- [JSON schemas](#json-schemas)
- [Data formats](#data-formats)
- [Plugins](#plugins)
- [Custom plugin](#custom-plugin)
- [Develop](#develop)
- [Changelog](#changelog)

<!-- vim-markdown-toc -->

## Features

### Validate input

> Pass request data (headers, body, query parameters, URL parameters) through custom JSON Schemas defined for each route. Make sure no unwanted data gets in, de-clutter the route logic and make the API behave more consistent.
If validation fails, an automatic `409 Conflict` response will be sent.
> Pass request data (headers, body, query parameters, URL parameters) through custom JSON Schemas defined for each route. Make sure no unwanted data gets in, de-clutter the route logic and make the API behave more consistent.
> If validation fails, an automatic `409 Conflict` response will be sent.

### Permissions

Expand All @@ -56,13 +56,13 @@ If validation fails, an automatic `409 Conflict` response will be sent.

### Other

* File upload and form parsing for `multipart/form-data` - [`busboy`](https://github.com/mscdex/busboy)
* Middleware support of existing package - [`connect`](https://github.com/senchalabs/connect)
* JSON Web Token - [`jsonwebtoken`](https://github.com/auth0/node-jsonwebtoken)
* Query string parsing - [`qs`](https://github.com/ljharb/qs)
* Route parameter parsing - [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp)
* Cross-origin resource sharing - [`cors`](https://github.com/expressjs/cors)
* Secure your API with various HTTP headers - [`helmet`](https://github.com/helmetjs/helmet)
- File upload and form parsing for `multipart/form-data` - [`busboy`](https://github.com/mscdex/busboy)
- Middleware support of existing package - [`connect`](https://github.com/senchalabs/connect)
- JSON Web Token - [`jsonwebtoken`](https://github.com/auth0/node-jsonwebtoken)
- Query string parsing - [`qs`](https://github.com/ljharb/qs)
- Route parameter parsing - [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp)
- Cross-origin resource sharing - [`cors`](https://github.com/expressjs/cors)
- Secure your API with various HTTP headers - [`helmet`](https://github.com/helmetjs/helmet)

## Install

Expand All @@ -86,11 +86,12 @@ const app = block({
})

// After plugins successfully initialize, start http server
app.then(([middleware, plugins]) => {
app
.then(([middleware, plugins]) => {
const server = http.createServer(middleware)

server.listen({
port: 4269
port: 4269,
})

server.on("error", error => {
Expand Down Expand Up @@ -144,11 +145,10 @@ module.exports = {
*/
schema: require("./something.schema"),


/**
* Check for valid JWT.
*
* @param {object} plugins
* @param {object} plugins
*
* @returns {(object) => Promise<boolean>}
* If false, responds with 401, otherwise continue to ".authorize".
Expand All @@ -158,7 +158,7 @@ module.exports = {
/**
* Check if is allowed to access underlying resource.
*
* @param {object} plugins
* @param {object} plugins
*
* @returns {(object) => Promise<boolean>}
* If false, respond with 403, otherwise continue to ".action".
Expand All @@ -168,13 +168,13 @@ module.exports = {
/**
* Route/Controller logic
*
* @param {object} plugins
* @param {object} plugins
*
* @returns {(object) => Promise<*>} 500 if throws, 201 if POST, 200 otherwise
*/
action: (/* plugins */) => req => {
return {
message: req.ctx.params.id
message: req.ctx.params.id,
}
},
}
Expand All @@ -186,10 +186,10 @@ Input validation is the first step in the processing pipeline. It's meant to val

Schemas can contain only 4 (optional) keys. Each key must be a [ajv](https://github.com/epoberezkin/ajv) compatible object.

* `headers` validates `req.headers`
* `params` validates `req.ctx.params` parsed from URL with [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp)
* `query`: validates `req.ctx.query` parsed from URL with [`qs`](https://github.com/ljharb/qs)
* `body` validates `req.ctx.body` parsed from `req` with `JSON.parse`
- `headers` validates `req.headers`
- `params` validates `req.ctx.params` parsed from URL with [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp)
- `query`: validates `req.ctx.query` parsed from URL with [`qs`](https://github.com/ljharb/qs)
- `body` validates `req.ctx.body` parsed from `req` with `JSON.parse`

See [`src/plugins/route-default.schema.js`](src/plugins/route-default.schema.js) for default values.

Expand Down Expand Up @@ -217,8 +217,8 @@ module.exports = {
pattern: "^[a-z0-9-]+$",
maxLength: 25,
minLength: 25,
}
}
},
},
},

query: {
Expand All @@ -243,16 +243,16 @@ module.exports = {

### Data formats

* _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6)
* _time_: time with optional time-zone
* _date-time_: date-time from the same source (time-zone is mandatory)
* _duration_: duration from [RFC3339](https://tools.ietf.org/html/rfc3339#appendix-A)
* _uri_: full URI
* _email_: email address
* _ipv4_: IP address v4
* _ipv6_: IP address v6
* _regex_: tests whether a string is a valid regular expression by passing it to RegExp constructor
* _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122)
- _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6)
- _time_: time with optional time-zone
- _date-time_: date-time from the same source (time-zone is mandatory)
- _duration_: duration from [RFC3339](https://tools.ietf.org/html/rfc3339#appendix-A)
- _uri_: full URI
- _email_: email address
- _ipv4_: IP address v4
- _ipv6_: IP address v6
- _regex_: tests whether a string is a valid regular expression by passing it to RegExp constructor
- _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122)

```js
{
Expand Down Expand Up @@ -344,7 +344,7 @@ module.exports = {
## Develop

```bash
git clone [email protected]:asd-xiv/blocks.git && \
git clone [email protected]:heartsradiant/blocks.git && \
cd blocks && \
npm run setup
```
Expand All @@ -363,4 +363,4 @@ npm run tdd

## Changelog

See the [releases section](https://github.com/asd-xiv/blocks/releases) for details.
See the [releases section](https://github.com/heartsradiant/blocks/releases) for details.
6 changes: 3 additions & 3 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const http = require("http")
const glob = require("glob")
import http from "http"
import glob from "glob"

const { block } = require("../src")
import { block } from "../src"

// initialize application
const app = block({
Expand Down
Loading