@tailor-platform/shopify
Version:
Shopify Admin API client with Tailor Platform integration
337 lines (335 loc) • 11.8 kB
JavaScript
// src/errors.ts
var ShopifyApiError = class extends Error {
errors;
extensions;
constructor(message, errors, extensions) {
super(message);
this.name = "ShopifyApiError";
this.errors = errors;
this.extensions = extensions;
}
};
var NetworkError = class extends Error {
statusCode;
response;
constructor(message, statusCode, response) {
super(message);
this.name = "NetworkError";
this.statusCode = statusCode;
this.response = response;
}
};
var ConfigurationError = class extends Error {
constructor(message) {
super(message);
this.name = "ConfigurationError";
}
};
// src/client.ts
var TAILOR_API_ENDPOINT = "https://shopify-g4l5uulnln.erp.dev/query";
function validateConfig(config) {
if (!config.storeDomain) {
throw new ConfigurationError("storeDomain is required");
}
if (!config.apiVersion) {
throw new ConfigurationError("apiVersion is required");
}
if (!config.accessToken) {
throw new ConfigurationError("accessToken (integration token) is required");
}
if (!config.accessToken.startsWith("tst_")) {
throw new ConfigurationError(
'Invalid integration token format. Expected token starting with "tst_"'
);
}
}
function gql(strings, ...values) {
const query = strings.reduce(
(acc, str, i) => acc + str + (values[i] ?? ""),
""
);
return {
query
};
}
function createAdminApiClient(config) {
validateConfig(config);
return {
request: async (operation, options) => {
const query = typeof operation === "string" ? operation : operation.query;
const input = {
integrationToken: config.accessToken,
shopDomain: config.storeDomain,
apiVersion: config.apiVersion,
query,
variables: options?.variables ? JSON.stringify(options.variables) : void 0,
headers: options?.headers ? JSON.stringify(options.headers) : void 0
};
const graphqlQuery = `
mutation GraphQLProxy($input: GraphQLProxyInput!) {
graphqlProxy(input: $input) {
success
data
errors
extensions
userErrors
}
}
`;
try {
const response = await fetch(TAILOR_API_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify({
query: graphqlQuery,
variables: { input }
})
});
if (!response.ok) {
throw new NetworkError(
`Network request failed with status ${response.status}`,
response.status,
response
);
}
const result = await response.json();
if (result.errors && result.errors.length > 0) {
throw new NetworkError(
"GraphQL proxy request failed",
response.status,
response
);
}
const proxyResponse = result.data?.graphqlProxy;
if (!proxyResponse) {
throw new NetworkError(
"Invalid response from proxy",
response.status,
response
);
}
if (!proxyResponse.success) {
let errors = [];
let extensions;
if (proxyResponse.errors) {
try {
errors = JSON.parse(proxyResponse.errors);
} catch {
errors = [{ message: proxyResponse.errors }];
}
}
if (proxyResponse.extensions) {
try {
extensions = JSON.parse(proxyResponse.extensions);
} catch {
}
}
if (errors.length > 0) {
const firstError = errors[0];
throw new ShopifyApiError(
firstError.message ?? "Request failed",
errors,
extensions
);
}
throw new ShopifyApiError("Request failed without specific error");
}
let shopifyData;
if (proxyResponse.data) {
shopifyData = JSON.parse(proxyResponse.data);
}
const shopifyResponse = shopifyData !== void 0 ? { data: shopifyData } : {};
if (proxyResponse.errors) {
try {
shopifyResponse.errors = JSON.parse(proxyResponse.errors);
} catch {
shopifyResponse.errors = [{ message: proxyResponse.errors }];
}
}
if (proxyResponse.extensions) {
try {
shopifyResponse.extensions = JSON.parse(proxyResponse.extensions);
} catch {
}
}
return shopifyResponse;
} catch (error) {
if (error instanceof ShopifyApiError || error instanceof NetworkError) {
throw error;
}
if (error instanceof Error) {
throw new NetworkError(error.message);
}
throw new NetworkError("An unknown error occurred");
}
}
};
}
// src/constants.ts
var ApiVersion = {
// Stable versions (as of 2025)
October25: "2025-10",
July25: "2025-07",
April25: "2025-04",
January25: "2025-01",
October24: "2024-10",
July24: "2024-07",
April24: "2024-04",
January24: "2024-01",
October23: "2023-10",
// Special versions
Unstable: "unstable"
};
var LATEST_API_VERSION = ApiVersion.October25;
var API_VERSION_ALIASES = {
latest: LATEST_API_VERSION,
...ApiVersion
};
var API_VERSIONS = ApiVersion;
var WebhookTopic = {
// App
APP_UNINSTALLED: "app/uninstalled",
APP_SUBSCRIPTIONS_UPDATE: "app_subscriptions/update",
APP_SUBSCRIPTIONS_APPROACHING_CAPPED_AMOUNT: "app_subscriptions/approaching_capped_amount",
// Carts
CARTS_CREATE: "carts/create",
CARTS_UPDATE: "carts/update",
// Checkouts
CHECKOUTS_CREATE: "checkouts/create",
CHECKOUTS_UPDATE: "checkouts/update",
CHECKOUTS_DELETE: "checkouts/delete",
// Collections
COLLECTIONS_CREATE: "collections/create",
COLLECTIONS_UPDATE: "collections/update",
COLLECTIONS_DELETE: "collections/delete",
// Customers
CUSTOMERS_CREATE: "customers/create",
CUSTOMERS_UPDATE: "customers/update",
CUSTOMERS_DELETE: "customers/delete",
CUSTOMERS_DISABLE: "customers/disable",
CUSTOMERS_ENABLE: "customers/enable",
CUSTOMERS_DATA_REQUEST: "customers/data_request",
CUSTOMERS_REDACT: "customers/redact",
CUSTOMERS_EMAIL_MARKETING_CONSENT_UPDATE: "customers/email_marketing_consent/update",
CUSTOMER_GROUPS_CREATE: "customer_groups/create",
CUSTOMER_GROUPS_UPDATE: "customer_groups/update",
CUSTOMER_GROUPS_DELETE: "customer_groups/delete",
CUSTOMER_PAYMENT_METHODS_CREATE: "customer_payment_methods/create",
CUSTOMER_PAYMENT_METHODS_UPDATE: "customer_payment_methods/update",
CUSTOMER_PAYMENT_METHODS_REVOKE: "customer_payment_methods/revoke",
// Disputes
DISPUTES_CREATE: "disputes/create",
DISPUTES_UPDATE: "disputes/update",
// Draft Orders
DRAFT_ORDERS_CREATE: "draft_orders/create",
DRAFT_ORDERS_UPDATE: "draft_orders/update",
DRAFT_ORDERS_DELETE: "draft_orders/delete",
// Fulfillments
FULFILLMENTS_CREATE: "fulfillments/create",
FULFILLMENTS_UPDATE: "fulfillments/update",
FULFILLMENT_EVENTS_CREATE: "fulfillment_events/create",
FULFILLMENT_EVENTS_DELETE: "fulfillment_events/delete",
FULFILLMENT_ORDERS_CANCELLATION_REQUEST_ACCEPTED: "fulfillment_orders/cancellation_request_accepted",
FULFILLMENT_ORDERS_CANCELLATION_REQUEST_REJECTED: "fulfillment_orders/cancellation_request_rejected",
FULFILLMENT_ORDERS_CANCELLATION_REQUEST_SUBMITTED: "fulfillment_orders/cancellation_request_submitted",
FULFILLMENT_ORDERS_CANCELLED: "fulfillment_orders/cancelled",
FULFILLMENT_ORDERS_FULFILLMENT_REQUEST_ACCEPTED: "fulfillment_orders/fulfillment_request_accepted",
FULFILLMENT_ORDERS_FULFILLMENT_REQUEST_REJECTED: "fulfillment_orders/fulfillment_request_rejected",
FULFILLMENT_ORDERS_FULFILLMENT_REQUEST_SUBMITTED: "fulfillment_orders/fulfillment_request_submitted",
FULFILLMENT_ORDERS_FULFILLMENT_SERVICE_FAILED_TO_COMPLETE: "fulfillment_orders/fulfillment_service_failed_to_complete",
FULFILLMENT_ORDERS_IN_PROGRESS: "fulfillment_orders/in_progress",
FULFILLMENT_ORDERS_LINE_ITEMS_PREPARED_FOR_LOCAL_DELIVERY: "fulfillment_orders/line_items_prepared_for_local_delivery",
FULFILLMENT_ORDERS_LINE_ITEMS_PREPARED_FOR_PICKUP: "fulfillment_orders/line_items_prepared_for_pickup",
FULFILLMENT_ORDERS_MOVED: "fulfillment_orders/moved",
FULFILLMENT_ORDERS_OPEN: "fulfillment_orders/open",
FULFILLMENT_ORDERS_ORDER_ROUTING_COMPLETE: "fulfillment_orders/order_routing_complete",
FULFILLMENT_ORDERS_PLACED_ON_HOLD: "fulfillment_orders/placed_on_hold",
FULFILLMENT_ORDERS_READY_FOR_PICKUP: "fulfillment_orders/ready_for_pickup",
FULFILLMENT_ORDERS_RELEASED_FROM_HOLD: "fulfillment_orders/released_from_hold",
FULFILLMENT_ORDERS_RESCHEDULED: "fulfillment_orders/rescheduled",
FULFILLMENT_ORDERS_SCHEDULED_FULFILLMENT_ORDER_READY: "fulfillment_orders/scheduled_fulfillment_order_ready",
// Inventory
INVENTORY_ITEMS_CREATE: "inventory_items/create",
INVENTORY_ITEMS_UPDATE: "inventory_items/update",
INVENTORY_ITEMS_DELETE: "inventory_items/delete",
INVENTORY_LEVELS_CONNECT: "inventory_levels/connect",
INVENTORY_LEVELS_UPDATE: "inventory_levels/update",
INVENTORY_LEVELS_DISCONNECT: "inventory_levels/disconnect",
// Locations
LOCATIONS_CREATE: "locations/create",
LOCATIONS_UPDATE: "locations/update",
LOCATIONS_DELETE: "locations/delete",
LOCATIONS_ACTIVATE: "locations/activate",
LOCATIONS_DEACTIVATE: "locations/deactivate",
// Orders
ORDERS_CREATE: "orders/create",
ORDERS_UPDATE: "orders/update",
ORDERS_DELETE: "orders/delete",
ORDERS_CANCELLED: "orders/cancelled",
ORDERS_EDITED: "orders/edited",
ORDERS_FULFILLED: "orders/fulfilled",
ORDERS_PAID: "orders/paid",
ORDERS_PARTIALLY_FULFILLED: "orders/partially_fulfilled",
ORDER_TRANSACTIONS_CREATE: "order_transactions/create",
// Products
PRODUCTS_CREATE: "products/create",
PRODUCTS_UPDATE: "products/update",
PRODUCTS_DELETE: "products/delete",
PRODUCT_LISTINGS_ADD: "product_listings/add",
PRODUCT_LISTINGS_UPDATE: "product_listings/update",
PRODUCT_LISTINGS_REMOVE: "product_listings/remove",
PRODUCT_FEEDS_CREATE: "product_feeds/create",
PRODUCT_FEEDS_UPDATE: "product_feeds/update",
// Refunds
REFUNDS_CREATE: "refunds/create",
// Shop
SHOP_UPDATE: "shop/update",
SHOP_REDACT: "shop/redact",
// Themes
THEMES_CREATE: "themes/create",
THEMES_UPDATE: "themes/update",
THEMES_DELETE: "themes/delete",
THEMES_PUBLISH: "themes/publish",
// Other
SUBSCRIPTION_BILLING_ATTEMPTS_SUCCESS: "subscription_billing_attempts/success",
SUBSCRIPTION_BILLING_ATTEMPTS_FAILURE: "subscription_billing_attempts/failure",
SUBSCRIPTION_BILLING_ATTEMPTS_CHALLENGED: "subscription_billing_attempts/challenged",
SUBSCRIPTION_CONTRACTS_CREATE: "subscription_contracts/create",
SUBSCRIPTION_CONTRACTS_UPDATE: "subscription_contracts/update",
TENDER_TRANSACTIONS_CREATE: "tender_transactions/create",
COMPANY_LOCATIONS_CREATE: "company_locations/create",
COMPANY_LOCATIONS_UPDATE: "company_locations/update",
COMPANY_LOCATIONS_DELETE: "company_locations/delete"
};
var WebhookTopicReverse = Object.entries(WebhookTopic).reduce(
(acc, [key, value]) => {
acc[value] = key;
return acc;
},
{}
);
function getWebhookEventName(topic) {
return WebhookTopic[topic];
}
function getWebhookTopicEnum(eventName) {
return WebhookTopicReverse[eventName];
}
export {
API_VERSIONS,
API_VERSION_ALIASES,
ApiVersion,
ConfigurationError,
LATEST_API_VERSION,
NetworkError,
ShopifyApiError,
WebhookTopic,
WebhookTopicReverse,
createAdminApiClient,
getWebhookEventName,
getWebhookTopicEnum,
gql
};
//# sourceMappingURL=index.js.map