Skip to content

Commit ec4a8d1

Browse files
authored
feat: shortcuts for HTTP methods (#15)
* feat: shortcuts for HTTP methods GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
1 parent 57c0dcc commit ec4a8d1

File tree

5 files changed

+140
-24
lines changed

5 files changed

+140
-24
lines changed

README.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
> REST-API helper, wrapped around `fetch`.
1212
1313
# Table of Contents
14-
* [Installation](#installation)
15-
* [API](#api)
16-
* [Usage](#usage)
17-
* [Example](#example)
18-
* [Contributing](#contributing)
19-
* [License](#license)
14+
15+
* [Installation](#installation)
16+
* [API](#api)
17+
* [Usage](#usage)
18+
* [Example](#example)
19+
* [Contributing](#contributing)
20+
* [License](#license)
2021

2122
# Installation
2223

@@ -66,32 +67,38 @@ Query-like object.
6667

6768
Method to be called on `fetch`'s response.
6869

70+
Alternatively you can use provided shortcuts for [every HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
71+
6972
**Example:**
7073
```js
74+
import { GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH } from '../src/shortcuts';
75+
7176
const userResource = {
7277
namespace: 'user',
7378

7479
methods: {
75-
7680
// id = 1, extanted = true
77-
// GET: /api/user/get/1?extended=true
78-
get: ({ id, extended }) => ({ path: ['get', id], query: { extended: !!extended } }),
79-
80-
// POST: /api/user/edit
81+
// GET: /api/user/1?extended=true
82+
get: ({ id, extended }) => ({ path: [id], query: { extended: !!extended } }),
83+
84+
// POST: /api/user/1/edit
8185
save: ({ id, firstname, lastname }) => {
82-
const formData = new FormData();
83-
84-
formData.append('firstname', firstname);
85-
formData.append('lastname', lastname);
86-
87-
return {
88-
path: 'edit',
89-
options: {
90-
method: 'POST',
91-
body: formData
92-
}
93-
};
94-
}
86+
const formData = new FormData();
87+
88+
formData.append('firstname', firstname);
89+
formData.append('lastname', lastname);
90+
91+
return {
92+
path: [id, 'edit'],
93+
options: {
94+
method: 'POST',
95+
body: formData
96+
}
97+
};
98+
},
99+
100+
// DELETE: /api/user/1
101+
delete: ({ id }) => DELETE({ path: [id] })
95102
}
96103
}
97104
```

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import createAPI from './createAPI';
2+
export * from './shortcuts';
23

34
export default createAPI;

src/shortcuts.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const createShortcut = method => ({ options = {}, ...rest} = {}) => ({
2+
...rest,
3+
options: {
4+
method,
5+
...options
6+
}
7+
});
8+
9+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
10+
export const GET = createShortcut('GET');
11+
export const HEAD = createShortcut('HEAD');
12+
export const POST = createShortcut('POST');
13+
export const PUT = createShortcut('PUT');
14+
export const DELETE = createShortcut('DELETE');
15+
export const CONNECT = createShortcut('CONNECT');
16+
export const OPTIONS = createShortcut('OPTIONS');
17+
export const TRACE = createShortcut('TRACE');
18+
export const PATCH = createShortcut('PATCH');

test/coverage.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import test from 'ava';
2+
import module from '../src';
3+
4+
test('coverage', t => t.pass());

test/shortcuts.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import test from 'ava';
2+
import {
3+
GET,
4+
HEAD,
5+
POST,
6+
PUT,
7+
DELETE,
8+
CONNECT,
9+
OPTIONS,
10+
TRACE,
11+
PATCH
12+
} from '../src/shortcuts';
13+
14+
const dummy = {
15+
path: ['path', 'to', 'resource'],
16+
query: { param: 'value' },
17+
options: { credentials: 'include' },
18+
method: 'text'
19+
};
20+
21+
test('should set correct HTTP method', t => {
22+
t.deepEqual(GET(dummy), {
23+
path: ['path', 'to', 'resource'],
24+
query: { param: 'value' },
25+
options: { credentials: 'include', method: 'GET' },
26+
method: 'text'
27+
}, 'GET');
28+
t.deepEqual(HEAD(dummy), {
29+
path: ['path', 'to', 'resource'],
30+
query: { param: 'value' },
31+
options: { credentials: 'include', method: 'HEAD' },
32+
method: 'text'
33+
}, 'HEAD');
34+
t.deepEqual(POST(dummy), {
35+
path: ['path', 'to', 'resource'],
36+
query: { param: 'value' },
37+
options: { credentials: 'include', method: 'POST' },
38+
method: 'text'
39+
}, 'POST');
40+
t.deepEqual(PUT(dummy), {
41+
path: ['path', 'to', 'resource'],
42+
query: { param: 'value' },
43+
options: { credentials: 'include', method: 'PUT' },
44+
method: 'text'
45+
}, 'PUT');
46+
t.deepEqual(DELETE(dummy), {
47+
path: ['path', 'to', 'resource'],
48+
query: { param: 'value' },
49+
options: { credentials: 'include', method: 'DELETE' },
50+
method: 'text'
51+
}, 'DELETE');
52+
t.deepEqual(CONNECT(dummy), {
53+
path: ['path', 'to', 'resource'],
54+
query: { param: 'value' },
55+
options: { credentials: 'include', method: 'CONNECT' },
56+
method: 'text'
57+
}, 'CONNECT');
58+
t.deepEqual(OPTIONS(dummy), {
59+
path: ['path', 'to', 'resource'],
60+
query: { param: 'value' },
61+
options: { credentials: 'include', method: 'OPTIONS' },
62+
method: 'text'
63+
}, 'OPTIONS');
64+
t.deepEqual(TRACE(dummy), {
65+
path: ['path', 'to', 'resource'],
66+
query: { param: 'value' },
67+
options: { credentials: 'include', method: 'TRACE' },
68+
method: 'text'
69+
}, 'TRACE');
70+
t.deepEqual(PATCH(dummy), {
71+
path: ['path', 'to', 'resource'],
72+
query: { param: 'value' },
73+
options: { credentials: 'include', method: 'PATCH' },
74+
method: 'text'
75+
}, 'PATCH');
76+
});
77+
78+
test('should add method even when nothing is passed', t => {
79+
const result = GET();
80+
t.deepEqual(result, { options: { method: 'GET'} });
81+
});
82+
83+
test('should not take precedence over passed params', t => {
84+
const result = PUT({ options: { method: 'POST' } });
85+
t.deepEqual(result, { options: { method: 'POST'} });
86+
});

0 commit comments

Comments
 (0)