Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
update guides and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Jun 3, 2019
1 parent c216ac3 commit 73dfa4a
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/boom/create-error.md
@@ -0,0 +1,6 @@
---
title: createError
subtitle: micro-boom's create-error function
---

## Coming soon
14 changes: 13 additions & 1 deletion docs/docs.json
@@ -1,5 +1,17 @@
{
"server": "server",
"query": "query",
"params": "params"
"params": "params",
"route/light": "light",
"micro": {
"buffer": "micro/buffer",
"text": "micro/text",
"json": "micro/json",
"run": "micro/run",
"send": "micro/send",
"sendError": "micro/send-error"
},
"boom": {
"createError": "boom/create-error"
}
}
6 changes: 6 additions & 0 deletions docs/light.md
@@ -0,0 +1,6 @@
---
title: light
subtitle: the main function for routes
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/buffer.md
@@ -0,0 +1,6 @@
---
title: buffer
subtitle: micro's buffer function
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/json.md
@@ -0,0 +1,6 @@
---
title: json
subtitle: micro's json function
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/run.md
@@ -0,0 +1,6 @@
---
title: run
subtitle: micro's run function
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/send-error.md
@@ -0,0 +1,6 @@
---
title: sendError
subtitle: micro's send-error function
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/send.md
@@ -0,0 +1,6 @@
---
title: send
subtitle: micro's send function
---

## Coming soon
6 changes: 6 additions & 0 deletions docs/micro/text.md
@@ -0,0 +1,6 @@
---
title: text
subtitle: micro's text function
---

## Coming soon
42 changes: 41 additions & 1 deletion guides/error-handling.md
Expand Up @@ -3,4 +3,44 @@ title: error handling
subtitle: easily return errors from inside your endpoint
---

## Coming Soon
## Introduction

Error handling is done mostly by throwing a new Error. Each route is wrapped with a try/catch block so that you can safely throw at any point. The reason throwing for errors is nice is because it allows you to escape from any point in your code!

## Usage

Simply import [`createError`](/docs/boom/create-error) and throw it at any point. createError will create a boom error resulting in a pretty JSON output.

```js
const { light, createError } = require('light');

module.exports = light({
path: '/',
async handler(req, res) {
throw createError(401, 'sorry, you cannot access this route');
return {
hello: 'world',
};
},
});
```

This will result in JSON which looks like this:

```json
{
"statusCode": 401,
"error": "Unauthorized",
"message": "sorry, you cannot access this route"
}
```

## Error Object

You can also throw a standard JavaScript `Error` object with a `message` and `statusCode` (defaults to 500).

```js
const err = new Error('Rate limit exceeded')
err.statusCode = 429
throw err
```
2 changes: 1 addition & 1 deletion guides/getting-started.md
Expand Up @@ -8,7 +8,7 @@ subtitle: a quick guide on how to start with light
### Prerequisites

- Node >= `8.0.0`
- NPM >= `8.0.0`
- NPM >= `5.0.0`

### Install

Expand Down
43 changes: 41 additions & 2 deletions guides/methods.md
Expand Up @@ -3,6 +3,45 @@ title: methods
subtitle: respond to custom methods when in server mode
---

**Please note that methods are environment specific. The details documented below only apply to [server mode](/guides/server-vs-serverless#server) and serverless environment will have their own method handling**
**Please note that methods are environment specific. The details documented below only apply to [server mode](/guides/server-vs-serverless#server) since serverless environment will have their own method handling**

## Coming Soon
## Introduction

Under the hood, light uses [find-my-way](https://github.com/delvedor/find-my-way) to route in server mode. find-my-way supports the following HTTP methods:

- `get`
- `delete`
- `head`
- `patch`
- `post`
- `put`
- `options`
- `all`

**NOTE: Each serverless provider will have a different way to handle route method, but most just default to `all` for all routes.**

## Usage

When in server mode, you can define a custom method.

```js
const light = require('light');

module.exports = light({
path: '/',
method: 'get', // your method here
async handler() {
// ...
},
});
```

Additionally, you can provide an array if you want to support multiple methods.

```js
{
// ...
method: ['get', 'post', 'options'], // your methods here
// ...
};
```
17 changes: 11 additions & 6 deletions website/src/pages/docs.jsx
Expand Up @@ -8,10 +8,15 @@ import Hero from '../components/Hero';
import Sidebar from '../components/Sidebar';

export default class Posts extends React.Component {
static async getInitialProps({ query }) {
static async getInitialProps({ query, req, res }) {
let { title } = query;
if (!title) {
title = 'server';
if (req) {
res.writeHead(302, { Location: '/docs/server' });
return res.end();
}

return Router.push('/docs/server');
}

const fetchPost = await fetch(join(process.env.BASE_URL, `docs/${title}.md`));
Expand All @@ -21,12 +26,12 @@ export default class Posts extends React.Component {
const metadata = split.shift().trim();
const content = split.join('---').trim();

const res = {};
res.content = content;
const response = {};
response.content = content;

metadata.split('\n').forEach((line) => {
const [attr, val] = line.split(':');
res[attr.trim()] = val.trim();
response[attr.trim()] = val.trim();
});

const fetchSidebar = await fetch(join(process.env.BASE_URL, `docs/docs.json`));
Expand All @@ -36,7 +41,7 @@ export default class Posts extends React.Component {
query,
menu,
path: title,
...res,
...response,
};
}

Expand Down
17 changes: 11 additions & 6 deletions website/src/pages/guides.jsx
Expand Up @@ -8,10 +8,15 @@ import Hero from '../components/Hero';
import Sidebar from '../components/Sidebar';

export default class Posts extends React.Component {
static async getInitialProps({ query }) {
static async getInitialProps({ query, req, res }) {
let { title } = query;
if (!title) {
title = 'getting-started';
if (req) {
res.writeHead(302, { Location: '/guides/getting-started' });
return res.end();
}

return Router.push('/guides/getting-started');
}

const fetchPost = await fetch(join(process.env.BASE_URL, `guides/${title}.md`));
Expand All @@ -21,12 +26,12 @@ export default class Posts extends React.Component {
const metadata = split.shift().trim();
const content = split.join('---').trim();

const res = {};
res.content = content;
const response = {};
response.content = content;

metadata.split('\n').forEach((line) => {
const [attr, val] = line.split(':');
res[attr.trim()] = val.trim();
response[attr.trim()] = val.trim();
});

const fetchSidebar = await fetch(join(process.env.BASE_URL, `guides/guides.json`));
Expand All @@ -36,7 +41,7 @@ export default class Posts extends React.Component {
query,
menu,
path: title,
...res,
...response,
};
}

Expand Down

0 comments on commit 73dfa4a

Please sign in to comment.