@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
63 lines (62 loc) • 1.93 kB
JavaScript
import { ErrorResponse } from "../../../errors/index.mjs";
import { FetchError } from "../../../utils/fetch.mjs";
import { CustomerAPIClient } from "../../../api/customer.mjs";
import {
HttpStatusCode,
HttpStatusMessage
} from "../../../constants/httpStatus.mjs";
import { defineRpcHandler } from "../../../utils/index.mjs";
export const getOrderById = defineRpcHandler(
async ({ orderId }, context) => {
const accessToken = context.accessToken;
if (!orderId || isNaN(orderId)) {
return new ErrorResponse(
HttpStatusCode.NOT_FOUND,
HttpStatusMessage.NOT_FOUND,
"No order ID provided"
);
}
if (!accessToken) {
return new ErrorResponse(
HttpStatusCode.UNAUTHORIZED,
HttpStatusMessage.UNAUTHORIZED,
"No access token present"
);
}
const client = new CustomerAPIClient(context);
try {
return await client.getOrder(context.shopId, orderId);
} catch (error) {
if (error instanceof FetchError) {
if (error.response.status === HttpStatusCode.NOT_FOUND) {
return new ErrorResponse(
HttpStatusCode.NOT_FOUND,
HttpStatusMessage.NOT_FOUND,
"Order not found"
);
}
return new ErrorResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR,
HttpStatusMessage.INTERNAL_SERVER_ERROR,
"Unknown error",
{
detail: `Fetching order "${orderId}" failed with status code ${error.response.status}`
}
);
}
context.log.error("Error while fetching order details", { error });
return new ErrorResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR,
HttpStatusMessage.INTERNAL_SERVER_ERROR,
"Unknown error"
);
}
},
{ method: "POST" }
);
export const getOrderCustomData = defineRpcHandler(
async () => {
return {};
},
{ method: "POST" }
);