laravel-request 1.2.26 → 1.2.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +231 -76
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,111 +1,266 @@
|
|
|
1
|
-
|
|
1
|
+
# laravel-request
|
|
2
2
|
|
|
3
|
+
Клиентская библиотека для работы с Laravel API. Поддерживает JSON и MessagePack, fluent-интерфейс в стиле Eloquent Query Builder.
|
|
4
|
+
|
|
5
|
+
## Содержание
|
|
6
|
+
|
|
7
|
+
- [Установка](#установка)
|
|
8
|
+
- [Переменные окружения](#переменные-окружения)
|
|
9
|
+
- [Api](#api)
|
|
10
|
+
- [ApiRequest](#apirequest)
|
|
11
|
+
- [Два типа запросов](#два-типа-запросов)
|
|
12
|
+
- [Выборки: примеры и payload](#выборки-примеры-и-payload)
|
|
13
|
+
- [Действия: call()](#действия-call)
|
|
14
|
+
- [Отключение уведомлений](#отключение-уведомлений)
|
|
15
|
+
- [Прямой вызов по URL](#прямой-вызов-по-url)
|
|
16
|
+
- [Отмена запроса](#отмена-запроса)
|
|
17
|
+
- [Лицензия](#лицензия)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Установка
|
|
22
|
+
|
|
23
|
+
```bash
|
|
3
24
|
npm install laravel-request
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Переменные окружения
|
|
28
|
+
|
|
29
|
+
| Переменная | Описание |
|
|
30
|
+
|------------|----------|
|
|
31
|
+
| `REACT_APP_API_URL` | Базовый URL API |
|
|
32
|
+
| `REACT_APP_DEBUG` | `true`/`false` — логирование запросов и ответов |
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Api
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import { Api } from 'laravel-request';
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
| Метод | Описание |
|
|
43
|
+
|-------|----------|
|
|
44
|
+
| `Api.get(controller, action, data)` | GET |
|
|
45
|
+
| `Api.post(controller, action, data)` | POST |
|
|
46
|
+
| `Api.put(controller, action, data)` | PUT |
|
|
47
|
+
| `Api.patch(controller, action, data)` | PATCH |
|
|
48
|
+
| `Api.delete(controller, action, id, data)` | DELETE |
|
|
49
|
+
| `Api.getArg(controller, action, arg, data)` | GET с аргументом в URL (например, index/123) |
|
|
50
|
+
| `Api.getUrl(url, data)` | Прямой GET по URL |
|
|
4
51
|
|
|
5
|
-
|
|
52
|
+
URL формируется как: `{domain}/api/v1/call/{controller}/{action}`
|
|
6
53
|
|
|
7
|
-
|
|
54
|
+
---
|
|
8
55
|
|
|
9
|
-
|
|
56
|
+
## ApiRequest
|
|
10
57
|
|
|
58
|
+
### Два типа запросов
|
|
59
|
+
|
|
60
|
+
**Выборка (список)** — `Api.get(controller, 'index')` или `'list'`
|
|
61
|
+
Цепочка `where()`, `select()`, `orderBy()` и т.д. + метод выполнения:
|
|
62
|
+
|
|
63
|
+
| Метод | Результат |
|
|
64
|
+
|-------|-----------|
|
|
65
|
+
| `first()` | Первая страница |
|
|
66
|
+
| `all()` | Все записи |
|
|
67
|
+
| `paginate(page, perPage)` | Страница с пагинацией |
|
|
68
|
+
| `pluck(fields)` | Только указанные поля |
|
|
69
|
+
| `call()` | Без указания способа |
|
|
70
|
+
|
|
71
|
+
**Действие** — POST/PUT/PATCH/DELETE или GET одной записи (index)
|
|
72
|
+
Всегда завершается `call()`.
|
|
73
|
+
|
|
74
|
+
### Выборки: примеры и payload
|
|
75
|
+
|
|
76
|
+
Данные уходят в query-параметрах URL (или в теле POST, если длина >5000 символов).
|
|
77
|
+
|
|
78
|
+
При `REACT_APP_API_URL=https://api.example.com` URL формируется как `https://api.example.com/api/v1/call/{controller}/{action}`.
|
|
79
|
+
|
|
80
|
+
**first():**
|
|
81
|
+
|
|
82
|
+
URL: `https://api.example.com/api/v1/call/product/index` (GET)
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
Api.get('product', 'index')
|
|
86
|
+
.select(['id', 'name', 'price'])
|
|
87
|
+
.where('status', '=', 'active')
|
|
88
|
+
.orderBy('created_at', 'desc')
|
|
89
|
+
.first(success, error, final);
|
|
11
90
|
```
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"arguments": [],
|
|
94
|
+
"query": [
|
|
95
|
+
{ "select": [["id", "name", "price"]] },
|
|
96
|
+
{ "where": ["status", "=", "active"] },
|
|
97
|
+
{ "orderBy": ["created_at", "desc"] }
|
|
98
|
+
],
|
|
99
|
+
"data": { "paginateType": "first" },
|
|
100
|
+
"unique_hash": "xK9...",
|
|
101
|
+
"timestamp": 1708123456789
|
|
19
102
|
}
|
|
20
103
|
```
|
|
21
104
|
|
|
22
|
-
|
|
105
|
+
**all():**
|
|
23
106
|
|
|
24
|
-
|
|
107
|
+
URL: `https://api.example.com/api/v1/call/product/index` (GET)
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
Api.get('product', 'index')
|
|
111
|
+
.where('category_id', '=', 1)
|
|
112
|
+
.all(success, error, final);
|
|
25
113
|
```
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"arguments": [],
|
|
117
|
+
"query": [{ "where": ["category_id", "=", 1] }],
|
|
118
|
+
"data": { "paginateType": "all" },
|
|
119
|
+
"unique_hash": "xK9...",
|
|
120
|
+
"timestamp": 1708123456789
|
|
33
121
|
}
|
|
34
122
|
```
|
|
35
123
|
|
|
36
|
-
|
|
124
|
+
**paginate():**
|
|
125
|
+
|
|
126
|
+
URL: `https://api.example.com/api/v1/call/product/index` (GET)
|
|
37
127
|
|
|
128
|
+
```javascript
|
|
129
|
+
Api.get('product', 'index')
|
|
130
|
+
.whereIn('category_id', [1, 2, 3])
|
|
131
|
+
.orderBy('name')
|
|
132
|
+
.paginate(2, 15, success, error, final);
|
|
38
133
|
```
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
134
|
+
```json
|
|
135
|
+
{
|
|
136
|
+
"arguments": [],
|
|
137
|
+
"query": [
|
|
138
|
+
{ "whereIn": ["category_id", [1, 2, 3]] },
|
|
139
|
+
{ "orderBy": ["name"] }
|
|
140
|
+
],
|
|
141
|
+
"data": {
|
|
142
|
+
"paginateType": "paginate",
|
|
143
|
+
"page": 2,
|
|
144
|
+
"perPage": 15
|
|
145
|
+
},
|
|
146
|
+
"unique_hash": "xK9...",
|
|
147
|
+
"timestamp": 1708123456789
|
|
148
|
+
}
|
|
51
149
|
```
|
|
52
|
-
|
|
53
|
-
or
|
|
54
150
|
|
|
151
|
+
**pluck():**
|
|
152
|
+
|
|
153
|
+
URL: `https://api.example.com/api/v1/call/product/index` (GET)
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
Api.get('product', 'index')
|
|
157
|
+
.pluck(['id', 'name'], success, error, final);
|
|
55
158
|
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
})
|
|
68
|
-
}}
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"arguments": [],
|
|
162
|
+
"query": [],
|
|
163
|
+
"data": {
|
|
164
|
+
"paginateType": "pluck",
|
|
165
|
+
"fields": ["id", "name"]
|
|
166
|
+
},
|
|
167
|
+
"unique_hash": "xK9...",
|
|
168
|
+
"timestamp": 1708123456789
|
|
169
|
+
}
|
|
69
170
|
```
|
|
70
171
|
|
|
71
|
-
|
|
72
|
-
You can use
|
|
73
|
-
|
|
74
|
-
all or first or paginate
|
|
172
|
+
**first() — сложный (whereHas, with):**
|
|
75
173
|
|
|
76
|
-
|
|
174
|
+
URL: `https://api.example.com/api/v1/call/product/index` (GET)
|
|
77
175
|
|
|
176
|
+
```javascript
|
|
177
|
+
Api.get('product', 'index')
|
|
178
|
+
.select(['id', 'name', 'price'])
|
|
179
|
+
.where('status', '=', 'active')
|
|
180
|
+
.whereHas('category', (q) => q.where('visible', true))
|
|
181
|
+
.with(['images', 'category'])
|
|
182
|
+
.orderBy('created_at', 'desc')
|
|
183
|
+
.first(success, error, final);
|
|
78
184
|
```
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"arguments": [],
|
|
188
|
+
"query": [
|
|
189
|
+
{ "select": [["id", "name", "price"]] },
|
|
190
|
+
{ "where": ["status", "=", "active"] },
|
|
191
|
+
{ "whereHas": ["category", { "query": [{ "where": ["visible", true] }] }] },
|
|
192
|
+
{ "with": [["images", "category"]] },
|
|
193
|
+
{ "orderBy": ["created_at", "desc"] }
|
|
194
|
+
],
|
|
195
|
+
"data": { "paginateType": "first" },
|
|
196
|
+
"unique_hash": "xK9...",
|
|
197
|
+
"timestamp": 1708123456789
|
|
198
|
+
}
|
|
89
199
|
```
|
|
90
200
|
|
|
91
|
-
|
|
201
|
+
**getArg()** (например, index/123):
|
|
202
|
+
|
|
203
|
+
URL: `https://api.example.com/api/v1/call/user/index` (GET, id=123 в payload)
|
|
92
204
|
|
|
205
|
+
```javascript
|
|
206
|
+
Api.getArg('user', 'index', 123).call(success, error, final);
|
|
207
|
+
```
|
|
208
|
+
```json
|
|
93
209
|
{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
210
|
+
"arguments": [123],
|
|
211
|
+
"query": [],
|
|
212
|
+
"data": {},
|
|
213
|
+
"unique_hash": "xK9...",
|
|
214
|
+
"timestamp": 1708123456789
|
|
97
215
|
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Действия: call()
|
|
98
219
|
|
|
99
|
-
|
|
220
|
+
```javascript
|
|
221
|
+
// URL: https://api.example.com/api/v1/call/user/store (POST)
|
|
222
|
+
Api.post('user', 'store', { name: 'John', email: 'john@example.com' })
|
|
223
|
+
.call(success, error, final);
|
|
100
224
|
|
|
225
|
+
// URL: https://api.example.com/api/v1/call/user/update (PUT)
|
|
226
|
+
Api.put('user', 'update', { id: 1, name: 'John' })
|
|
227
|
+
.call(success, error, final);
|
|
228
|
+
|
|
229
|
+
// URL: https://api.example.com/api/v1/call/user/destroy (DELETE, id=123 в payload)
|
|
230
|
+
Api.delete('user', 'destroy', 123)
|
|
231
|
+
.call(success, error, final);
|
|
232
|
+
|
|
233
|
+
// URL: https://api.example.com/api/v1/call/user/index (GET, id=123 в payload)
|
|
234
|
+
Api.getArg('user', 'index', 123)
|
|
235
|
+
.call(success, error, final);
|
|
101
236
|
```
|
|
102
237
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
238
|
+
### Отключение уведомлений
|
|
239
|
+
|
|
240
|
+
```javascript
|
|
241
|
+
Api.get('product', 'index').withoutNotify().first(success, error, final);
|
|
242
|
+
|
|
243
|
+
Api.post('user', 'store', data)
|
|
244
|
+
.withoutNotify((status) => status !== 422)
|
|
245
|
+
.call(success, error, final);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Прямой вызов по URL
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
Api.getUrl('https://api.example.com/custom/endpoint')
|
|
252
|
+
.callUrl(success, error, final);
|
|
111
253
|
```
|
|
254
|
+
|
|
255
|
+
### Отмена запроса
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
const request = Api.get('product', 'index').first(success, error, final);
|
|
259
|
+
request.getSource().cancel();
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Лицензия
|
|
265
|
+
|
|
266
|
+
MIT © Abramov Oleg
|