https://github.com/axross/yomogi
https://github.com/axross/yomogi
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/axross/yomogi
- Owner: axross
- License: mit
- Created: 2015-09-17T05:16:55.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-09-25T06:52:55.000Z (about 10 years ago)
- Last Synced: 2025-01-21T14:47:09.492Z (9 months ago)
- Language: JavaScript
- Size: 172 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Yomogi
Yomogi is thin wrapper of `fetch()`.
## Example
```javascript
import Yomogi from 'agent';Yomogi.get('/path/to/api/:id', {
query: { offset: 40, limit: 20 },
param: { id: 3 },
body: {
yeah: 'whoo',
},
header: {},
...
})
.simple()
.fetchJson()
.then(json => {})
.catch(err => {});
```## API
### Yomogi.get(url: string, options: object)
### Yomogi.post(url: string, options: object)
### Yomogi.put(url: string, options: object)
### Yomogi.delete(url: string, options: object)
### Yomogi.head(url: string, options: object)
### Yomogi.options(url: string, options: object)```javascript
Yomogi.get('/path/to/api/page/:page', {
param: {
page: 3,
},
query: {
sortBy: 'id',
order: 'desc',
},
});
```#### options
##### options.method
##### options.url
##### options.params
##### options.query
##### options.body
##### options.header
##### options.xxx### Yomogi#assign()
### Yomogi#simple()
### Yomogi#fetch()
## Tests
- Yomogi#constructor()
- Yomogiインスタンスが返る -> やらんでもええやろ
- methodは存在しないものの場合はエラーが発生する
- urlはStringでない場合はエラーが発生する
- methodは大文字にされる
- this.fullUrlはurl, param, queryをもとに正しいものが生成される
- this.headerは小文字化される
- bodyがObjectまたはArrayの場合、this.realBodyはJSON文字列になり、content-typeに'application/json'が設定される(上書きはされない)
- xxxがthis.xxxとして受け継がれる
- Yomogi#assign()
- Yomogiインスタンスが返る
- thisとoptionsがマージされた新しいインスタンスが返る
- Yomogi#simple()
- Yomogiインスタンスが返る
- methodがGETならGET、そうでなければPOSTに補正される
- CORS Simple Requestとして許可されないheaderは排除される
- CORS Simple Requestとして許可されないContent-Typeは'text/plain'に補正される
- Yomogi#fetch()
- fetch()の結果が返る
- fetchにはfullUrl, thisが渡っていて、bodyにはrealBodyが代わりに使われる
- Yomogi.use()
- Yomogi自身が返る
- functionでない場合はエラーが発生する
- middlewareとして登録され、fetch()時に呼ばれる
- 呼ばれる際は、middlewareとして登録した順番で