# Introduction

![](https://4135423742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwD6MjHg3yMxWgKGDEI%2Fuploads%2FDZ8xDF5p2mTnJJBtRUhY%2Flogo.png?alt=media\&token=32be4b40-6083-4f9d-8b7c-f02f46a38c70)

![](https://img.shields.io/npm/v/packhouse.svg)[![travis-ci](https://travis-ci.org/KHC-ZhiHao/Packhouse.svg?branch=master)](https://travis-ci.org/KHC-ZhiHao/Packhouse)[![Coverage Status](https://coveralls.io/repos/github/KHC-ZhiHao/Packhouse/badge.svg?branch=master)](https://coveralls.io/github/KHC-ZhiHao/Packhouse?branch=master) [![Standard Code Style](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com/)[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/KHC-ZhiHao/Packhouse.svg?logo=lgtm\&logoWidth=18)](https://lgtm.com/projects/g/KHC-ZhiHao/Packhouse/context:javascript)[![](https://img.shields.io/github/stars/KHC-ZhiHao/Packhouse.svg?style=social)](https://github.com/KHC-ZhiHao/Packhouse)

**Packhouse**是一個基於函數式程式設計(Functional Programming)的程式設計模型，其擁有以下特性：

* [**追蹤**](https://packhouse-doc.metalsheep.com/plugins/step)**呼叫上下文**
* **真正的**[**型態檢查**](https://packhouse-doc.metalsheep.com/the-instance/group/mold)
* [**管理與分類**](https://packhouse-doc.metalsheep.com/the-instance/group)**函式**
* **美麗的寫作規範**
* **建構**[**後端服務**](https://packhouse-doc.metalsheep.com/application/api-service)**的能力**

{% hint style="info" %}
開始前可以閱讀[函數式編程指南](https://yucj.gitbooks.io/mostly-adequate-guide-traditional-chinese/content/)了解基本觀念。
{% endhint %}

## 為何採用**Packhouse？**

#### 1. 統一開發格式

使用**Packhouse**開發可以建構統一的Input/Output接口。

#### 2. 建構微服務

**Packhouse**的精神是建構微服務中的微服務，建立細微可控的函式能夠快速反應需求變更，且如果保持函數式編程的核心理念，便可以在專案破碎化的情況下複製模式到各個專案中。

#### 3. 當我們無法使用TypeScript

並不是所有的專案都能運行**TypeScript**，而**Packhouse**是原生的**JavaScript**，不需要經由任何編譯就能執行。

#### 4. Cloud Function

編寫**AWS Lambda**時將所有的邏輯編寫在一個檔案中難以應付頻繁的需求變更，物件導向開發在minify或編譯後難以除錯，雖然我們可以藉由單元測試來避免錯誤，但上線後會發生的事永遠比開發時離奇。

## 無伺服器架構 - Serverless

{% hint style="info" %}
你不需要逐步建立服務，可以直接參考[API Service](https://packhouse-doc.metalsheep.com/application/api-service)章節。
{% endhint %}

無伺服器架構是**Packhouse**絕佳的運作平台，[**Mess**](#shi-yong-an-li)的後端就是使用**Packhouse**與**Serverless Framework**建構整個**API Service**，我們推薦下列兩種**Serverless**框架：

#### [Serverless Framework](https://serverless.com/)

#### [AWS Serverless Application](https://docs.aws.amazon.com/zh_tw/serverlessrepo/latest/devguide/using-aws-sam.html)

## Install

```bash
$ npm i packhouse --save
```

## 運行環境

#### Node 8.x以上。

{% hint style="info" %}
Packhouse並沒有強制必須於哪個環境下運作，它甚至允許於瀏覽器執行，但我們不會在乎瀏覽器兼容性。
{% endhint %}

| <p><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="Edge"></a></p><p>Edge</p> | [![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png)](http://godban.github.io/browsers-support-badges/)Firefox | [![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png)](http://godban.github.io/browsers-support-badges/)Chrome | [![Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png)](http://godban.github.io/browsers-support-badges/)Safari | <p><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari-ios/safari-ios_48x48.png" alt="iOS Safari"></a></p><p>iOS/Safari</p> | [![Samsung](https://raw.githubusercontent.com/alrra/browser-logos/master/src/samsung-internet/samsung-internet_48x48.png)](http://godban.github.io/browsers-support-badges/)Samsung | [![Opera](https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png)](http://godban.github.io/browsers-support-badges/)Opera |
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------: |
|                                                                                            support                                                                                           |                                                                              support                                                                              |                                                                            support                                                                            |                                                                            support                                                                            |                                                                                                        support                                                                                                       |                                                                                       support                                                                                       |                                                                          support                                                                          |

## First Function

以下是最低限度地執行程式：

```javascript
const Packhouse = require('packhouse')
const packhouse = new Packhouse()
const group = {
    tools: {
        sum: {
            handler: (self, v1, v2) => self.success(v1 + v2)
        }
    }
}

packhouse.addGroup('math', () => {
    return {
        data: group
    }
})

packhouse
    .tool('math/sum')
    .promise(10, 20)
    .then(result => console.log(result)) // 30
```

## 使用案例

![Mess的整個後端都由Packhouse建構](https://camo.githubusercontent.com/1ba21fad9b77e10efe2af4686f25cc41621eb02f/68747470733a2f2f6d6573732e6d6574616c73686565702e636f6d2f696d616765732f6c6f676f2e706e67)

[**MESS**](https://mess.metalsheep.com)**是為了通勤族精心設計的閱讀網站，你可以在任何等待時間使用任何裝置隨時閱讀國外媒體或文章，並享受精心設計的使用者介面與翻譯、語音服務，更棒的是：完全免費！**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://packhouse-doc.metalsheep.com/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
