Skip to content

Commit 57b8a8b

Browse files
author
pooya parsa
committed
feat: allow passing body as argument
1 parent 62d4b3f commit 57b8a8b

4 files changed

Lines changed: 71 additions & 82 deletions

File tree

docs/migration.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,3 @@ Supported response types:
3131
- `formData`
3232
- `arrayBuffer`
3333
- `blob`
34-
35-
## Sending requests with body
36-
37-
Despire axios, fetch and ky always accept **two** arguments for making requests (input and options). You have to pass request body in options:
38-
39-
For plain data or `Body`:
40-
41-
```diff
42-
-- this.$axios.post('/url', 'some data')
43-
++ this.$http.post('/url', { body: 'some data' })
44-
```
45-
46-
For JSON:
47-
48-
```diff
49-
-- this.$axios.post('/url', { name: 'foo' })
50-
++ this.$http.post('/url', { json: { name: 'foo' } })
51-
```
52-
53-
* `json` is a shortcut to `body` that sets `content-type` header and serializes JSON object.

docs/usage.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
Available HTTP methods:
66

7-
- `get`
8-
- `post`
9-
- `put`
10-
- `patch`
11-
- `head`
12-
- `delete`
7+
- `get(url, options?)`
8+
- `head(url, options?)`
9+
- `delete(url, options?)`
10+
- `post(url, body?, options?)`
11+
- `put(url, body?, options?)`
12+
- `patch(url, body?, options?)`
1313

14-
For making a request use `$http.<method>(<url>, <options>)`. Returns a Promise that either rejects in case of network errors or resolves to a [Reponse](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. You can use methods to convert response stream into usable data:
14+
Calling these methods, returns a Promise that resolves to a [Reponse](https://developer.mozilla.org/en-US/docs/Web/API/Response) object or rejects in case of network errors.
15+
16+
You can use methods to convert response stream into usable data:
1517

1618
- `json`
1719
- `text`
1820
- `formData`
1921
- `arrayBuffer`
2022
- `blob`
2123

22-
**Example: Fetch a json file**
24+
**Example: Get a json file**
2325

2426
```js
2527
await $http.get('https://unpkg.com/nuxt/package.json').json()
@@ -31,7 +33,7 @@ Alternatively for json only you can use `$` prefixed shortcut:
3133
await $http.$get('https://unpkg.com/nuxt/package.json')
3234
```
3335

34-
See [ky](https://github.com/sindresorhus/ky) docs for all available options.
36+
See [ky](https://github.com/sindresorhus/ky#options) docs for all available options.
3537

3638
### Sending Body
3739

lib/plugin.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,33 @@ class HTTP {
4444
}
4545
}
4646

47-
for (let method of ['get', 'post', 'put', 'patch', 'head', 'delete']) {
48-
HTTP.prototype[method] = async function (input, options) {
47+
for (let method of ['get', 'head', 'delete', 'post', 'put', 'patch']) {
48+
const hasBody = ['post', 'put', 'patch'].includes(method)
49+
50+
HTTP.prototype[method] = async function (url, arg1, arg2) {
51+
let options
52+
53+
if (!hasBody) {
54+
options = arg1
55+
} else {
56+
options = arg2 || {}
57+
if (arg1 !== undefined) {
58+
if (arg1.constructor === Object) {
59+
options.json = arg1
60+
} else {
61+
options.body = arg1
62+
}
63+
}
64+
}
65+
4966
const _options = { ...this._defaults, ...options }
5067

51-
if (/^https?/.test(input)) {
68+
if (/^https?/.test(url)) {
5269
delete _options.prefixUrl
5370
}
5471

5572
try {
56-
const response = await this._ky[method](input, _options)
73+
const response = await this._ky[method](url, _options)
5774
return response
5875
} catch (error) {
5976
// Call onError hook
@@ -65,8 +82,8 @@ for (let method of ['get', 'post', 'put', 'patch', 'head', 'delete']) {
6582
}
6683
}
6784

68-
HTTP.prototype['$' + method] = function (input, options) {
69-
return this[method](input, options).then(res => res.json())
85+
HTTP.prototype['$' + method] = function (url, arg1, arg2) {
86+
return this[method](url, arg1, arg2).then(res => res.json())
7087
}
7188
}
7289

types/index.d.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,114 +4,104 @@ import './vuex'
44

55
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
66

7-
type JSONObject = { [key: string]: JSONValue };
8-
interface JSONArray extends Array<JSONValue> { }
9-
type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
10-
11-
interface OptionsWithoutBody extends Omit<Options, 'body'> {
12-
method?: 'get' | 'head'
13-
}
14-
15-
interface OptionsWithBody extends Options {
16-
method?: 'post' | 'put' | 'delete'
17-
}
7+
type RequestBody = string | number | boolean | null | object | BodyInit
188

199
interface NuxtHTTPInstance {
2010
/**
21-
* Fetches the `input` URL with the option `{method: 'get'}`.
11+
* Fetches the `url` with the option `{method: 'get'}`.
2212
*
23-
* @param input - `Request` object, `URL` object, or URL string.
13+
* @param url - `Request` object, `URL` object, or URL string.
2414
* @returns Promise with `Body` method added.
2515
*/
26-
get(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
16+
get(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
2717

2818
/**
29-
* Fetches the `input` URL with the option `{method: 'post'}`.
19+
* Fetches the `url` with the option `{method: 'post'}`.
3020
*
31-
* @param input - `Request` object, `URL` object, or URL string.
21+
* @param url - `Request` object, `URL` object, or URL string.
3222
* @returns Promise with `Body` method added.
3323
*/
34-
post(input: Request | URL | string, options?: Options): ResponsePromise;
24+
post(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
3525

3626
/**
37-
* Fetches the `input` URL with the option `{method: 'put'}`.
27+
* Fetches the `url` with the option `{method: 'put'}`.
3828
*
39-
* @param input - `Request` object, `URL` object, or URL string.
29+
* @param url - `Request` object, `URL` object, or URL string.
4030
* @returns Promise with `Body` method added.
4131
*/
42-
put(input: Request | URL | string, options?: Options): ResponsePromise;
32+
put(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
4333

4434
/**
45-
* Fetches the `input` URL with the option `{method: 'patch'}`.
35+
* Fetches the `url` with the option `{method: 'patch'}`.
4636
*
47-
* @param input - `Request` object, `URL` object, or URL string.
37+
* @param url - `Request` object, `URL` object, or URL string.
4838
* @returns Promise with `Body` method added.
4939
*/
50-
patch(input: Request | URL | string, options?: Options): ResponsePromise;
40+
patch(url: Request | URL | string, body?: RequestBody, options?: Options): ResponsePromise;
5141

5242
/**
53-
* Fetches the `input` URL with the option `{method: 'head'}`.
43+
* Fetches the `url` with the option `{method: 'head'}`.
5444
*
55-
* @param input - `Request` object, `URL` object, or URL string.
45+
* @param url - `Request` object, `URL` object, or URL string.
5646
* @returns Promise with `Body` method added.
5747
*/
58-
head(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
48+
head(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;
5949

6050
/**
61-
* Fetches the `input` URL with the option `{method: 'delete'}`.
51+
* Fetches the `url` with the option `{method: 'delete'}`.
6252
*
63-
* @param input - `Request` object, `URL` object, or URL string.
53+
* @param url - `Request` object, `URL` object, or URL string.
6454
* @returns Promise with `Body` method added.
6555
*/
66-
delete(input: Request | URL | string, options?: Options): ResponsePromise;
56+
delete(url: Request | URL | string, options?: Options): ResponsePromise;
6757

6858
/**
69-
* Fetches the `input` URL with the option `{method: 'get'}`.
59+
* Fetches the `url` with the option `{method: 'get'}`.
7060
*
71-
* @param input - `Request` object, `URL` object, or URL string.
61+
* @param url - `Request` object, `URL` object, or URL string.
7262
* @returns Promise that resolves to JSON parsed value.
7363
*/
74-
$get<T= JSONValue>(input: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
64+
$get<T = JSONValue>(url: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
7565

7666
/**
77-
* Fetches the `input` URL with the option `{method: 'post'}`.
67+
* Fetches the `url` with the option `{method: 'post'}`.
7868
*
79-
* @param input - `Request` object, `URL` object, or URL string.
69+
* @param url - `Request` object, `URL` object, or URL string.
8070
* @returns Promise that resolves to JSON parsed value.
8171
*/
82-
$post<T = JSONValue>(input: Request | URL | string, options?: Options): Promise<T>;
72+
$post<T = JSONValue>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
8373

8474
/**
85-
* Fetches the `input` URL with the option `{method: 'put'}`.
75+
* Fetches the `url` with the option `{method: 'put'}`.
8676
*
87-
* @param input - `Request` object, `URL` object, or URL string.
77+
* @param url - `Request` object, `URL` object, or URL string.
8878
* @returns Promise that resolves to JSON parsed value.
8979
*/
90-
$put<T = JSONValue>(input: Request | URL | string, options?: Options): Promise<T>;
80+
$put<T = JSONValue>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
9181

9282
/**
93-
* Fetches the `input` URL with the option `{method: 'patch'}`.
83+
* Fetches the `url` with the option `{method: 'patch'}`.
9484
*
95-
* @param input - `Request` object, `URL` object, or URL string.
85+
* @param url - `Request` object, `URL` object, or URL string.
9686
* @returns Promise that resolves to JSON parsed value.
9787
*/
98-
$patch<T = JSONValue>(input: Request | URL | string, options?: Options): Promise<T>;
88+
$patch<T = JSONValue>(url: Request | URL | string, body?: RequestBody, options?: Options): Promise<T>;
9989

10090
/**
101-
* Fetches the `input` URL with the option `{method: 'head'}`.
91+
* Fetches the `url` with the option `{method: 'head'}`.
10292
*
103-
* @param input - `Request` object, `URL` object, or URL string.
93+
* @param url - `Request` object, `URL` object, or URL string.
10494
* @returns Promise that resolves to JSON parsed value.
10595
*/
106-
$head<T = JSONValue>(input: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
96+
$head<T = JSONValue>(url: Request | URL | string, options?: Omit<Options, 'body'>): Promise<T>;
10797

10898
/**
109-
* Fetches the `input` URL with the option `{method: 'delete'}`.
99+
* Fetches the `url` with the option `{method: 'delete'}`.
110100
*
111-
* @param input - `Request` object, `URL` object, or URL string.
101+
* @param url - `Request` object, `URL` object, or URL string.
112102
* @returns Promise that resolves to JSON parsed value.
113103
*/
114-
$delete<T = JSONValue>(input: Request | URL | string, options?: Options): Promise<T>;
104+
$delete<T = JSONValue>(url: Request | URL | string, options?: Options): Promise<T>;
115105

116106

117107
/**

0 commit comments

Comments
 (0)