Skip to content

Commit 20d38eb

Browse files
committed
Update rest-api README example to use superagent over jquery
1 parent c48a604 commit 20d38eb

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

examples/rest-api-example/README.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ See the [Example User Model](./src/modules/user/model.js) in the code or the fol
5959
`./modules/project/model.js`
6060

6161
```js
62-
var $ = require('jquery')
62+
var request = require('superagent-promise')
6363
var sprintf = require('util').format
6464
var BASE_URL = 'https://www.optimizelyapis.com/experiment/v1'
6565
var ENTITY = 'projects'
@@ -71,22 +71,20 @@ exports.entity = ENTITY
7171
* @return {Promise}
7272
*/
7373
exports.fetch = function(id) {
74-
return $.ajax({
75-
url: sprintf('%s/%s/%s', BASE_URL, ENTITY, id),
76-
method: 'GET',
77-
contentType: 'application/json',
78-
})
74+
return request
75+
.get(sprintf('%s/%s/%s', BASE_URL, ENTITY, id))
76+
.accept('json')
77+
.end()
7978
}
8079

8180
/**
8281
* @return {Promise}
8382
*/
8483
exports.fetchAll = function() {
85-
return $.ajax({
86-
url: sprintf('%s/%s', BASE_URL, ENTITY),
87-
method: 'GET',
88-
contentType: 'application/json',
89-
})
84+
return request
85+
.get(sprintf('%s/%s', BASE_URL, ENTITY))
86+
.accept('json')
87+
.end()
9088
}
9189

9290
/**
@@ -95,21 +93,17 @@ exports.fetchAll = function() {
9593
*/
9694
exports.save = function(instance) {
9795
if (instance.id) {
98-
return $.ajax({
99-
url: sprintf('%s/%s/%s', BASE_URL, ENTITY, instance.id),
100-
method: 'PUT',
101-
contentType: 'application/json',
102-
data: JSON.stringify(instance),
103-
dataType: 'json'
104-
})
96+
return request
97+
.put(sprintf('%s/%s/%s', BASE_URL, ENTITY, instance.id))
98+
.type('json')
99+
.send(instance)
100+
.end()
105101
} else {
106-
return $.ajax({
107-
url: sprintf('%s/%s', BASE_URL, ENTITY),
108-
method: 'POST',
109-
contentType: 'application/json',
110-
data: JSON.stringify(instance),
111-
dataType: 'json'
112-
})
102+
return request
103+
.post(sprintf('%s/%s', BASE_URL, ENTITY))
104+
.type('json')
105+
.send(instance)
106+
.end()
113107
}
114108
}
115109

@@ -118,10 +112,10 @@ exports.save = function(instance) {
118112
* @return {Promise}
119113
*/
120114
exports.delete = function(instance) {
121-
return $.ajax({
122-
url: sprintf('%s/%s/%s', BASE_URL, ENTITY, instance.id),
123-
method: 'DELETE',
124-
contentType: 'application/json',
115+
return request
116+
.del(sprintf('%s/%s/%s', BASE_URL, ENTITY, instance.id))
117+
.type('json')
118+
.end()
125119
})
126120
}
127121
```

0 commit comments

Comments
 (0)