Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion website/i18n/th-TH.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"localized-strings": {
"next": "ต่อไป",
"previous": "ก่อนหน้า",
"tagline": "Express middleware สำหรับสร้าง format JSON response",
"tagline": "Format express json response with \"express-response-formatter\"",
"apis": "APIs",
"installation": "การติดตั้ง",
"quick-start": "เริ่มแบบรวดเร็ว",
Expand Down
30 changes: 15 additions & 15 deletions website/translated_docs/th/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
id: apis
title: APIs
---
| METHOD | CODE | PARAMS |
| ------------------------- | ---- | ------------------------------ |
| res.ok() | 200 | res.ok(data) |
| res.created() | 201 | res.created(data) |
| res.noContent() | 204 | - |
| res.badRequest() | 400 | res.badRequest(error) |
| res.unauthorized() | 401 | res.unauthorized(error) |
| res.forbidden() | 403 | res.forbidden(error) |
| res.notFound() | 404 | res.notFound(error) |
| res.methodNotAllowed() | 405 | res.methodNotAllowed(error) |
| res.unprocessableEntity() | 422 | res.unprocessableEntity(error) |
| res.internalServerError() | 500 | res.internalServerError(error) |
| res.badGateway() | 502 | res.badGateway(error) |
| res.serviceUnavailable() | 503 | res.serviceUnavailable(error) |
| res.gatewayTimeout() | 504 | res.gatewayTimeout(error) |
| METHOD | STATUS CODE |
| ---------------------------------------- | ----------- |
| res.formatter.ok(data, meta?) | 200 |
| res.formatter.created(data, meta?) | 201 |
| res.formatter.noContent(data, meta?) | 204 |
| res.formatter.badRequest(errors) | 400 |
| res.formatter.unauthorized(errors) | 401 |
| res.formatter.forbidden(errors) | 403 |
| res.formatter.notFound(errors) | 404 |
| res.formatter.methodNotAllowed(errors) | 405 |
| res.formatter.unprocess(errors) | 422 |
| res.formatter.serverError(errors) | 500 |
| res.formatter.badGateway(errors) | 502 |
| res.formatter.serviceUnavailable(errors) | 503 |
| res.formatter.gatewayTimeout(errors) | 504 |
4 changes: 2 additions & 2 deletions website/translated_docs/th/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: การติดตั้ง
---
## ใช้ npm

npm install expressjs-response --save
npm install express-response-formatter --save


## ใช้ yarn

yarn add expressjs-response
yarn add express-response-formatter
121 changes: 67 additions & 54 deletions website/translated_docs/th/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,107 @@ title: เริ่มแบบรวดเร็ว
---
## โค้ดเริ่มต้น

ใช้เป็น express middleware
Example usage

```js
import express from 'express'
import responseEnhancer from 'expressjs-response'
const app = require('express')()
const responseEnhancer = require('express-response-formatter')

const app = express()
// Add formatter functions to "res" object via "responseEnhancer()"
app.use(responseEnhancer())

// use in express middleware
app.use(responseEnhancer({
withStatusCode: true, // Include status code in response body.
withStatusMessage: true, // Include status message in response body.
}))
app.get('/success', (req, res) => {
const users = [
{ name: 'Dana Kennedy' },
{ name: 'Warren Young' },
]

// example usage
app.get('/success', (req, res) => res.ok({ name: 'John Doe' }))
app.get('/badrequest', (req, res) => res.badRequest('Invalid parameter.'))
app.get('/badgateway', (req, res) => res.badGateway())
// It's enhance "res" with "formatter" which contain formatter functions
res.formatter.ok(users)
})

app.listen(3000, () => console.log('Start at http://localhost:3000'))
```

## ตัวอย่างการใช้งานและผลลัพธ์

### 200 OK

```js
res.ok({ name: 'John Doe' })
```
Result

```json
HTTP/1.1 200 Ok
{
"status": "success",
"data": {
"name": "John Doe"
"data": [
{
"name": "Dana Kennedy"
},
{
"name": "Warren Young"
}
]
}
```

### 400 Bad Request
## More usages

```js
res.badRequest()
```

```json
HTTP/1.1 400 Bad Request
{
"status": "fail",
"error": {
"code": "400",
"message": "Bad Request"
}
}
```

### 400 Bad Request แบบส่ง Parameter
### 200 OK with "meta field"

```js
res.badRequest('Invalid parameter.')
app.get('/success-with-meta', (req, res) => {
const users = [
{ name: 'Dana Kennedy' },
{ name: 'Warren Young' },
]

const meta = {
total: 2,
limit: 10,
offset: 0,
}

res.formatter.ok(users, meta)
})
```

```json
HTTP/1.1 400 Bad Request
HTTP/1.1 200 Ok
{
"status": "fail",
"error": {
"code": "400",
"message": "Bad Request",
"detail": "Invalid parameter."
"meta": {
"total": 2,
"limit": 10,
"offset": 0,
},
"data": [
{
"name": "Dana Kennedy"
},
{
"name": "Warren Young"
}
]
}
```

### 502 Bad Gateway
### 400 Bad Request with "multiple errors"

```js
res.badGateway()
app.get('/bad-request', (req, res) => {
const errors = [
{ detail: 'Field id is required.' },
{ detail: 'Field foo is required.' },
]

res.formatter.badRequest(errors)
})
```

```json
HTTP/1.1 502 Bad Gateway
HTTP/1.1 400 Bad Request
{
"status": "error",
"error": {
"code": "502",
"message": "Bad Gateway"
"errors": [
{
"detail": "Field id is required."
},
{
"detail": "Field foo is required."
}
]
}
```