Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Commit 20e1eee

Browse files
committed
Add methods for updating orders and getting customers
1 parent 1d25288 commit 20e1eee

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-commerce-js",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Netlify Commerce API client for JavaScript",
55
"main": "lib/index.js",
66
"files": ["lib/", "README.md"],

src/index.js

+49-17
Original file line numberDiff line numberDiff line change
@@ -253,35 +253,67 @@ export default class NetlifyCommerce {
253253
return Promise.resolve(null);
254254
}
255255

256-
orderHistory() {
257-
if (this.user) {
258-
return this.authHeaders().then((headers) => this.api.request("/orders", {
259-
headers
260-
}));
261-
} else {
262-
return Promise.reject(
263-
"You must be authenticated to fetch order history"
264-
);
256+
updateOrder(orderId, attributes) {
257+
return this.authHeaders(true).then((headers) => this.api.request(`/orders/${orderId}`, {
258+
headers,
259+
method: "PUT",
260+
body: JSON.stringify(attributes)
261+
}));
262+
}
263+
264+
orderHistory(params) {
265+
let path = "/orders";
266+
if (params && params.user_id) {
267+
path = `/users/${params.user_id}/orders`;
268+
delete params.user_id;
269+
}
270+
if (params) {
271+
const query = [];
272+
for (const key in params) {
273+
query.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
274+
}
275+
path += `?${query.join("&")}`
265276
}
277+
return this.authHeaders(true).then((headers) => this.api.request(path, {
278+
headers
279+
}));
266280
}
267281

268282
orderDetails(orderID) {
269-
if (this.user) {
270-
return this.authHeaders().then((headers) => this.api.request(`/orders/${orderID}`, {
283+
return this.authHeaders(true).then((headers) => this.api.request(`/orders/${orderID}`, {
271284
headers
272285
}));
273-
} else {
274-
return Promise.reject(
275-
"You must be authenticated to fetch order history"
276-
);
286+
}
287+
288+
userDetails(userId) {
289+
userId = userId || (this.user && this.user.id);
290+
291+
return this.authHeaders(true).then((headers) => this.api.request(`/users/${userId}`, {
292+
headers
293+
}));
294+
}
295+
296+
users(params) {
297+
let path = "/users/";
298+
if (params) {
299+
const query = [];
300+
for (const key in params) {
301+
query.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
302+
}
303+
path += `?${query.join("&")}`
277304
}
305+
return this.authHeaders(true).then((headers) => this.api.request(path, {
306+
headers
307+
}));
278308
}
279309

280-
authHeaders() {
310+
authHeaders(required) {
281311
if (this.user) {
282312
return this.user.jwt().then((token) => ({Authorization: `Bearer ${token}`}));
283313
}
284-
return Promise.resolve({});
314+
return required ? Promise.reject(
315+
"The API action requires authentication"
316+
) : Promise.resolve({});
285317
}
286318

287319
loadCart() {

0 commit comments

Comments
 (0)