Skip to content

Commit 7f4dd9e

Browse files
committed
Feature: support put and delete
1 parent d933a49 commit 7f4dd9e

File tree

8 files changed

+100
-6
lines changed

8 files changed

+100
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ Where `url` is a string. Shorthand for `request.method('get').url(url)`.
186186

187187
Where `url` is a string. Shorthand for `request.method('post').url(url)`.
188188

189+
### put
190+
191+
```js
192+
.put(url)
193+
```
194+
195+
Where `url` is a string. Shorthand for `request.method('put').url(url)`.
196+
197+
### delete
198+
199+
```js
200+
.delete(url)
201+
```
202+
203+
Where `url` is a string. Shorthand for `request.method('delete').url(url)`.
204+
189205
### method
190206

191207
Sets the HTTP method.

build/yea.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@
119119

120120
YeaAjaxRequest.prototype.method = function method(method) {
121121
method = method.toUpperCase();
122-
if (['GET', 'POST'].indexOf(method) === -1) {
123-
throw new Error('Invalid method: ' + method);
122+
if (['GET', 'POST', 'PUT', 'DELETE'].indexOf(method) === -1) {
123+
throw new Error('Invalid method: \'' + method + '\'');
124124
}
125125
return new YeaAjaxRequest(mergeConfig(this._config, { method: method }));
126126
};
@@ -133,6 +133,14 @@
133133
return this.method('post').url(url);
134134
};
135135

136+
YeaAjaxRequest.prototype.put = function put(url) {
137+
return this.method('put').url(url);
138+
};
139+
140+
YeaAjaxRequest.prototype['delete'] = function yeaDelete(url) {
141+
return this.method('delete').url(url);
142+
};
143+
136144
YeaAjaxRequest.prototype.url = function url(fullUrl) {
137145
var segments = fullUrl.split('?');
138146
var url = segments[0];

build/yea.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"bundlesize": [
5050
{
5151
"path": "./build/yea.js",
52-
"maxSize": "3.0KB"
52+
"maxSize": "3.1KB"
5353
},
5454
{
5555
"path": "./build/yea.min.js",

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@
119119

120120
YeaAjaxRequest.prototype.method = function method(method) {
121121
method = method.toUpperCase();
122-
if (['GET', 'POST'].indexOf(method) === -1) {
123-
throw new Error('Invalid method: ' + method);
122+
if (['GET', 'POST', 'PUT', 'DELETE'].indexOf(method) === -1) {
123+
throw new Error('Invalid method: \'' + method + '\'');
124124
}
125125
return new YeaAjaxRequest(mergeConfig(this._config, { method: method }));
126126
};
@@ -133,6 +133,14 @@
133133
return this.method('post').url(url);
134134
};
135135

136+
YeaAjaxRequest.prototype.put = function put(url) {
137+
return this.method('put').url(url);
138+
};
139+
140+
YeaAjaxRequest.prototype['delete'] = function yeaDelete(url) {
141+
return this.method('delete').url(url);
142+
};
143+
136144
YeaAjaxRequest.prototype.url = function url(fullUrl) {
137145
var segments = fullUrl.split('?');
138146
var url = segments[0];

test/server/helper-server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ app.use(bodyParser.text({ type: '*/*' }));
1111
app.get('/simple-get', function (req, res) {
1212
return res.send('hello');
1313
});
14+
app.put('/simple-put', function (req, res) {
15+
return res.send('golf');
16+
});
17+
app.delete('/simple-delete', function (req, res) {
18+
return res.send('begone');
19+
});
1420
app.get('/nested/simple-get', function (req, res) {
1521
return res.send('nested hello');
1622
});

test/specs/methods.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ describe('Methods', function () {
5757
});
5858
});
5959

60+
describe('.put', function () {
61+
it('sets method and URL', function () {
62+
expect(yea.put('http://example.com/foo').toObject().method).to.equal('PUT');
63+
expect(yea.put('http://example.com/foo').toObject().url).to.equal('http://example.com/foo');
64+
});
65+
66+
it('is immutable', function () {
67+
var req = yea.put('http://example.com/foo');
68+
expect(req).to.not.equal(yea);
69+
expect(req.constructor).to.equal(yea.constructor);
70+
});
71+
});
72+
73+
describe('.delete', function () {
74+
it('sets method and URL', function () {
75+
expect(yea.delete('http://example.com/foo').toObject().method).to.equal('DELETE');
76+
expect(yea.delete('http://example.com/foo').toObject().url).to.equal('http://example.com/foo');
77+
});
78+
79+
it('is immutable', function () {
80+
var req = yea.delete('http://example.com/foo');
81+
expect(req).to.not.equal(yea);
82+
expect(req.constructor).to.equal(yea.constructor);
83+
});
84+
});
85+
6086
describe('.method', function () {
6187
it('sets method', function () {
6288
expect(yea.method('GET').toObject().method).to.equal('GET');
@@ -67,6 +93,12 @@ describe('Methods', function () {
6793
expect(yea.method('gEt').toObject().method).to.equal('GET');
6894
});
6995

96+
it('throws on unrecognized method', function () {
97+
expect(function () {
98+
yea.method('foobar');
99+
}).to.throw('Invalid method: \'FOOBAR\'');
100+
});
101+
70102
it('is immutable', function () {
71103
var req = yea.method('get');
72104
expect(req).to.not.equal(yea);

test/specs/requests.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ describe('Requests', function () {
2424
});
2525
});
2626

27+
it('makes a PUT request', function () {
28+
return yea
29+
.method('put')
30+
.url('/simple-put')
31+
.send()
32+
.then(function (response) {
33+
expect(response.status).to.equal(200);
34+
expect(response.body).to.be.a('string');
35+
expect(response.body).to.equal('golf');
36+
});
37+
});
38+
39+
it('makes a DELETE request', function () {
40+
return yea
41+
.method('delete')
42+
.url('/simple-delete')
43+
.send()
44+
.then(function (response) {
45+
expect(response.status).to.equal(200);
46+
expect(response.body).to.be.a('string');
47+
expect(response.body).to.equal('begone');
48+
});
49+
});
50+
2751
it('sends request headers', function () {
2852
return yea
2953
.url('/dump-headers')

0 commit comments

Comments
 (0)