1 | [![npm][npm]][npm-url]
|
2 | [![deps][deps]][deps-url]
|
3 | [![size][size]][size-url]
|
4 |
|
5 | # @subiz/ajax@1.0.29
|
6 | * *exception-free*
|
7 | * simple
|
8 | * tiny (2K Gzipped)
|
9 | * zero dependencies
|
10 | * *immutable*
|
11 |
|
12 | fetch api wrapper
|
13 |
|
14 | # Work in progress
|
15 | 0. docs
|
16 | 1. add timeout options
|
17 |
|
18 | ## Usage
|
19 |
|
20 | ### Simple
|
21 | ```
|
22 | const ajax = require('@subiz/ajax')
|
23 |
|
24 | // try to making GET https://app.subiz.net/v4/ping
|
25 |
|
26 | let req = new ajax.newRequest()
|
27 | .setBase("https://app.subiz.net/v4/")
|
28 | .setPath("ping")
|
29 | .setMethod("GET")
|
30 |
|
31 | // or shorter
|
32 |
|
33 | let req = new ajax.get("https://app.subiz.net/v4/", "ping")
|
34 |
|
35 | ```
|
36 |
|
37 | ### Derived from old object
|
38 | ```
|
39 | req = req.post(undefined, "ping")
|
40 | // POST https://app.subiz.net/v4/ping
|
41 |
|
42 | // send get request to https://app.subiz.net/v4/ping
|
43 | let [code,body, err] = await req.setPath("ping").send()
|
44 | // [200, '{"message":"pong from dashboard backend"}', undefined]
|
45 |
|
46 | // tell Ajax to parse json for us
|
47 | [code, body, err] = await req.setPath("ping").setParser("json").send()
|
48 | // [200, {message: "ping from dashboard backend"}, undefined
|
49 |
|
50 | ```
|
51 | ## References
|
52 | ### Request object
|
53 | Request objects represences HTTP request.
|
54 |
|
55 | Request object are immutable, which mean, you cannot change its current state or data. Immutiblity makes object behavious more predictable, reducing bug.
|
56 |
|
57 | The object's state is initialize once when creating the object. You can create request object by either using Ajax object or making a derived object from old one.
|
58 |
|
59 | Available methods:
|
60 | #### `addQuery(key, value)`
|
61 | create a new derived object by appending new (key, value) pair into old request query
|
62 |
|
63 | #### `removeQuery(key)`
|
64 | create a new derived object by removing a (key, value) pair from old request query
|
65 |
|
66 | #### `setQuery(query)`
|
67 | create a new derived object by replacing old query with new one.
|
68 |
|
69 | examples:
|
70 | ```js
|
71 | req = req.setQuery({a:"xin chao", b: 6})
|
72 | ```
|
73 |
|
74 | #### `beforeHook(promise)`
|
75 | create a new derived object by registering (appending) a before hook
|
76 |
|
77 | #### `clearHooks`
|
78 | create a new derived object by removing all before hooks and after hooks
|
79 |
|
80 | #### `afterHook(promise)`
|
81 | create a new derived object by registering (appending) a hook
|
82 |
|
83 | #### `setPath(path)`
|
84 | create a new derived object by changing old request path. New request url will be compose from `base` and `path`.
|
85 |
|
86 | #### `setHeader(header)`
|
87 | create a new derived object by merging old header with new header
|
88 |
|
89 | #### `setMeta(key, value)`
|
90 | attach hidden metadata to request, those key-value will not be sent to the server, designed to keep state in hooks
|
91 |
|
92 | examples:
|
93 | ```js
|
94 | req = req.setHeader({"x-real-ip": "193.155.45.3"})
|
95 | ```
|
96 | #### `get(base, path)`
|
97 | #### `post(base, path)`
|
98 | #### `put(base, path)`
|
99 | #### `del(base, path)`
|
100 | #### `head(base, path)`
|
101 | #### `patch(base, path)`
|
102 | #### `setBase`
|
103 | #### `setMethod`
|
104 | #### `contentTypeJson`
|
105 | #### `contentTypeForm`
|
106 | #### `setContentType`
|
107 | #### `setParser`
|
108 | #### `send`
|
109 | #### `setMeta`
|
110 |
|
111 | ### Ajax object (singleton)
|
112 | Ajax object is lets you create request object. Available requests:
|
113 | + `newRequest()`: // create new empty request
|
114 | + `get([base, path])`: // create new GET request to address (base+path)
|
115 | + `post([base, path])`: // create new POST request to address (base+path)
|
116 | + `put([base, path])`: // create new PUT request to address (base+path)
|
117 | + `del([base, path])`: // create new DELETE request to address (base+path)
|
118 | + `head([base, path])`: // create new HEAD request to address (base+path)
|
119 | + `patch([base, path])`: // create new PATCH request to address (base+path)
|
120 |
|
121 | ### Before hook
|
122 | *before hooks* let you modify the request right before sending it.
|
123 | You register a *before hook* by calling `beforeHook`. beforeHook function return a promise and take in a reference to the request object as parameter.
|
124 |
|
125 | for examples: to add query `?a=5` and `?b=6` right before sending the request
|
126 | ```js
|
127 | let req = ajax.get("https://google.com")
|
128 |
|
129 | req = req.beforeHook(async param => {
|
130 | param.request = param.request.addQuery('a', 5)
|
131 | }).beforeHook(async param => {
|
132 | param.request = param.request.addQuery('b', 6)
|
133 | })
|
134 |
|
135 | await req.send() // https://google.com?a=5&b=6
|
136 | ```
|
137 |
|
138 | before hook is called one by one in registered order, which hook registered first will be called
|
139 | first.
|
140 |
|
141 | ### With after hook
|
142 | ```
|
143 | const apireq = new ajax.Request()
|
144 | .setBase("https://appv4.subiz.com/4.0/")
|
145 | .setMethod("GET")
|
146 | .afterHook(param => {
|
147 | if (param.code !== 500) return
|
148 | var retry = param.request.meta.retry || 0
|
149 | if (retry === 3) return
|
150 | var req = param.request.setMeta('retry', retry + 1) // increase number of attempt
|
151 | // continue retry
|
152 | return req.send().then(out => {
|
153 | var [code, body, err] = out
|
154 | param.code = code
|
155 | param.body = body
|
156 | param.err = err
|
157 | })
|
158 | })
|
159 | })
|
160 |
|
161 | let [code, body, err] = await apireq.setPath("me").send()
|
162 |
|
163 | ```
|
164 |
|
165 | [npm]: https://img.shields.io/npm/v/@subiz/ajax.svg
|
166 | [npm-url]: https://npmjs.com/package/@subiz/ajax
|
167 | [deps]: https://david-dm.org/@subiz/ajax.svg
|
168 | [deps-url]: https://david-dm.org/@subiz/ajax
|
169 | [size]: https://packagephobia.now.sh/badge?p=@subiz/ajax
|
170 | [size-url]: https://packagephobia.now.sh/result?p=@subiz/ajax
|