sutty-base-jekyll-theme/_packs/endpoints/sutty.js

82 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2021-11-22 17:51:50 +00:00
import Axios from "axios";
import * as qs from "qs";
2021-06-01 21:33:49 +00:00
/*
* XXX: We're copying code from @spree/storefront-api-v2-sdk/src/Http.ts
* because we don't know how to mix Typescript :D
*/
export class Sutty {
2021-11-22 17:51:50 +00:00
constructor(host = "http://localhost:3000") {
this.host = host;
2021-06-01 21:33:49 +00:00
this.axios = Axios.create({
baseURL: this.host,
2021-11-22 17:51:50 +00:00
headers: { "Content-Type": "application/json" },
paramsSerializer: (params) =>
qs.stringify(params, { arrayFormat: "brackets" }),
});
2021-06-01 21:33:49 +00:00
}
async getCheckoutURL(tokens = {}) {
2021-11-22 17:51:50 +00:00
const headers = this.spreeOrderHeaders(tokens);
2021-06-01 21:33:49 +00:00
const axiosConfig = {
2021-11-22 17:51:50 +00:00
url: "api/v2/storefront/checkout_redirect.json",
2021-06-01 21:33:49 +00:00
params: {},
2021-11-22 17:51:50 +00:00
method: "get",
headers,
};
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
return await this.axios(axiosConfig);
2021-06-01 21:33:49 +00:00
}
async assignOrderOwnership(tokens = {}, params = {}) {
2021-11-22 17:51:50 +00:00
const headers = this.spreeOrderHeaders(tokens);
2021-06-01 21:33:49 +00:00
const axiosConfig = {
2021-11-22 17:51:50 +00:00
url: "api/v2/storefront/assign_order_ownership.json",
2021-06-01 21:33:49 +00:00
params,
2021-11-22 17:51:50 +00:00
method: "post",
headers,
};
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
return await this.axios(axiosConfig);
2021-06-01 21:33:49 +00:00
}
async assignOrderShippingAddress(tokens = {}, params = {}) {
2021-11-22 17:51:50 +00:00
const headers = this.spreeOrderHeaders(tokens);
2021-06-01 21:33:49 +00:00
const axiosConfig = {
2021-11-22 17:51:50 +00:00
url: "api/v2/storefront/assign_order_shipping_address.json",
2021-06-01 21:33:49 +00:00
params,
2021-11-22 17:51:50 +00:00
method: "post",
headers,
};
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
return await this.axios(axiosConfig);
2021-06-01 21:33:49 +00:00
}
async assignOrderBillingAddress(tokens = {}, params = {}) {
2021-11-22 17:51:50 +00:00
const headers = this.spreeOrderHeaders(tokens);
2021-06-01 21:33:49 +00:00
const axiosConfig = {
2021-11-22 17:51:50 +00:00
url: "api/v2/storefront/assign_order_billing_address.json",
2021-06-01 21:33:49 +00:00
params,
2021-11-22 17:51:50 +00:00
method: "post",
headers,
};
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
return await this.axios(axiosConfig);
2021-06-01 21:33:49 +00:00
}
spreeOrderHeaders(tokens) {
2021-11-22 17:51:50 +00:00
const header = {};
2021-06-01 21:33:49 +00:00
if (tokens.orderToken) {
2021-11-22 17:51:50 +00:00
header["X-Spree-Order-Token"] = tokens.orderToken;
2021-06-01 21:33:49 +00:00
}
if (tokens.bearerToken) {
2021-11-22 17:51:50 +00:00
header["Authorization"] = `Bearer ${tokens.bearerToken}`;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
return header;
2021-06-01 21:33:49 +00:00
}
}