@stackend/api
Version:
JS bindings to api.stackend.com
737 lines • 21.8 kB
TypeScript
import { Thunk, XcapJsonResult, XcapOptionalParameters } from '../api';
import { GraphQLList, PaginatedGraphQLList, PaginatedGraphQLRequest } from '../util/graphql';
import { ShopConfig, ShopDefaults } from './shopReducer';
import { Community } from '../stackend';
export interface SlimProductImage {
altText: string | null;
/**
* The url to the scaled version of the image
*/
url: string;
}
export interface ProductImage extends SlimProductImage {
/**
* The url to the original version of the image
*/
url__originalSrc: string;
}
export interface MoneyV2 {
amount: string;
currencyCode: string;
}
export declare type WeightUnit = 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES';
/**
* Options for the product: size, color etc
*/
export interface ProductOption {
id: string;
name: string;
values: Array<string>;
}
export interface SelectedProductOption {
name: string;
value: string;
}
export declare type SelectedProductOptions = Array<SelectedProductOption>;
export interface MetaField {
key: string;
value: string;
}
/**
* A variant of a product
*/
export interface ProductVariant {
id: string;
title: string;
availableForSale: boolean;
sku: string;
image: ProductImage | null;
price: MoneyV2;
/**
* Original price, when selling at a reduced price. May be null
*/
compareAtPrice: MoneyV2 | null;
selectedOptions: SelectedProductOptions;
weight: number;
weightUnit: WeightUnit;
}
/**
* Highest and lowest variant prices
*/
export interface PriceRange {
minVariantPrice: MoneyV2;
maxVariantPrice: MoneyV2;
}
export interface SlimProduct {
id: string;
/** permalink */
handle: string;
title: string;
/** Format: "2019-07-11T14:09:26Z" */
updatedAt: string;
/** Format: "2019-07-11T14:09:26Z" */
createdAt: string;
availableForSale: boolean;
/**
* Product type
*/
productType: string;
/** Images. Actual number of images and size depends on context/listing */
images: GraphQLList<SlimProductImage>;
/** Max and min price */
priceRange: PriceRange;
/** List of collection handles */
collections: GraphQLList<{
handle: string;
}>;
/**
* Custom add to cart URL used by some integrators
*/
metafield__stackendAddToCartLink: MetaField | null;
}
/**
* A product, including variants
*/
export interface Product extends SlimProduct {
/**
* Description as html
*/
descriptionHtml: string;
/** Vendor name */
vendor: string;
/**
* Tags
*/
tags: Array<string>;
/**
* Product options
*/
options: Array<ProductOption>;
/**
* Variants of the product
*/
variants: GraphQLList<ProductVariant>;
/** Images. Actual number of images and size depends on context/listing */
images: GraphQLList<ProductImage>;
}
export interface Country {
name: string;
code: string;
continent: string;
phoneNumberPrefix: number;
autocompletionField: string;
provinceKey: 'COUNTY' | 'EMIRATE' | 'GOVERNORATE' | 'PREFECTURE' | 'PROVINCE' | 'REGION' | 'STATE_AND_TERRITORY' | 'STATE';
labels: {
address1: string;
address2: string;
city: string;
company: string;
country: string;
firstName: string;
lastName: string;
phone: string;
postalCode: string;
zone: string;
};
optionalLabels: {
address2: string;
};
formatting: {
edit: string;
show: string;
};
}
export declare enum AddressFieldName {
FirstName = "firstName",
LastName = "lastName",
Country = "country",
City = "city",
PostalCode = "zip",
Zone = "province",
Address1 = "address1",
Address2 = "address2",
Phone = "phone",
Company = "company"
}
export interface MultipleProductListingsResult {
[key: string]: {
request: ListProductsQuery;
listing: PaginatedGraphQLList<SlimProduct>;
};
}
/**
* Result when requesting multiple shop related items using init/data
*/
export interface ShopDataResult {
products: {
[handle: string]: Product;
};
collections: {
[handle: string]: Collection;
};
listings: MultipleProductListingsResult;
shopifyDomainReferenceUrlId: number;
}
export interface GetShopConfigurationResult extends XcapJsonResult {
shop: string | null;
storeFrontAccessToken: string | null;
webhookKey: string | null;
enableCartNotifications: boolean;
}
/**
* Check if the shop is enabled
* @param community
*/
export declare function isShopEnabled(community: Community): boolean;
/**
* Get shopify configuration from store
* @return null if disabled
*/
export declare function getShopifyConfig(): Thunk<ShopConfig | null>;
/**
* Get the shop configuration. Requires admin privs
* @returns {Thunk<XcapJsonResult>}
*/
export declare function getShopConfiguration(): Thunk<Promise<GetShopConfigurationResult>>;
export interface StoreShopConfigurationResult extends XcapJsonResult {
stackendCommunity: Community;
}
/**
* Store the shop configuration. Requires admin privs
* @param shop
* @param storeFrontAccessToken
* @param webhookKey
* @param enableCartNotifications
* @returns {Thunk<XcapJsonResult>}
*/
export declare function storeShopConfiguration({ shop, storeFrontAccessToken, webhookKey, enableCartNotifications }: {
shop: string | null;
storeFrontAccessToken: string | null;
webhookKey: string | null;
enableCartNotifications: boolean;
} & XcapOptionalParameters): Thunk<Promise<StoreShopConfigurationResult>>;
export interface ListProductTypesRequest extends XcapOptionalParameters {
first?: number;
}
export interface ListProductTypesResult extends XcapJsonResult {
productTypes: GraphQLList<string>;
}
/**
* List product types
* @param req
* @returns {Thunk<ListProductTypesResult>}
*/
export declare function listProductTypes(req: ListProductTypesRequest): Thunk<Promise<ListProductTypesResult>>;
export declare enum ProductSortKeys {
VENDOR = "VENDOR",
CREATED_AT = "CREATED_AT",
ID = "ID",
PRICE = "PRICE",
PRODUCT_TYPE = "PRODUCT_TYPE",
RELEVANCE = "RELEVANCE",
TITLE = "TITLE",
UPDATED_AT = "UPDATED_AT",
BEST_SELLING = "BEST_SELLING"
}
/**
* Parse a product sort key
* @param sort
* @param defaultValue
*/
export declare function parseProductSortKey(sort: string | null | undefined, defaultValue?: ProductSortKeys): ProductSortKeys;
export interface ListProductsQuery extends PaginatedGraphQLRequest {
q?: string;
productTypes?: Array<string>;
tags?: Array<string>;
sort?: ProductSortKeys;
imageMaxWidth?: number;
}
export interface ListProductsRequest extends ListProductsQuery, XcapOptionalParameters {
}
export interface ListProductsResult extends XcapJsonResult {
products: PaginatedGraphQLList<SlimProduct>;
}
export declare function applyDefaults(req: ListProductsQuery, defaults: ShopDefaults): void;
/**
* Create a new ListProductsRequest with default options
* @param req
*/
export declare const newListProductsRequest: (req?: Partial<ListProductsRequest>) => Thunk<ListProductsRequest>;
/**
* List products
* @param req
* @returns {Thunk<ListProductsResult>}
*/
export declare function listProducts(req: ListProductsRequest): Thunk<Promise<ListProductsResult>>;
export interface GetProductRequest extends XcapOptionalParameters {
handle: string;
imageMaxWidth?: number;
}
export interface GetProductResult extends XcapJsonResult {
product: Product | null;
}
/**
* Create a new GetProductRequest with default image sizes
* @param req GetProductRequest or handle
*/
export declare const newGetProductRequest: (req: GetProductRequest | string) => Thunk<GetProductRequest>;
/**
* Get a single product
* @param req
* @returns {Thunk<XcapJsonResult>}
*/
export declare function getProduct(req: GetProductRequest): Thunk<Promise<GetProductResult>>;
export interface GetProductsRequest extends XcapOptionalParameters {
handles: Array<string>;
imageMaxWidth?: number;
}
export interface GetProductsResult extends XcapJsonResult {
products: {
[handle: string]: Product;
};
}
/**
* Create a new GetProductsRequest with default image sizes
* @param req GetProductsRequest or handles
*/
export declare const newGetProductsRequest: (req: GetProductsRequest | Array<string>) => Thunk<GetProductsRequest>;
/**
* Get multiple products
* @param req
* @returns {Thunk<XcapJsonResult>}
*/
export declare function getProducts(req: GetProductsRequest): Thunk<Promise<GetProductsResult>>;
export interface ListProductsAndTypesResult extends ListProductsResult {
productTypes: GraphQLList<string>;
}
/**
* List products and types
* @param req
* @returns {Thunk<XcapJsonResult>}
*/
export declare function listProductsAndTypes(req: ListProductsRequest): Thunk<Promise<ListProductsAndTypesResult>>;
/**
* Get the first image of a product
* @param product
*/
export declare function getFirstImage(product: SlimProduct | Product | null): ProductImage | SlimProductImage | null;
/**
* Get a product variant
* @param product
* @param variant
*/
export declare function getProductVariant(product: Product, variant: string): ProductVariant | null;
/**
* Iterate product variants
* @param product
* @param predicate
*/
export declare function forEachProductVariant(product: Product, predicate: (variant: ProductVariant, index: number, product: Product) => void): void;
/**
* Map each product variant
* @param product
* @param apply
*/
export declare function mapProductVariants<T>(product: Product, apply: (variant: ProductVariant, product: Product) => T): Array<T>;
/**
* Get the variant image
*/
export declare function getVariantImage(product: Product, variant: string): ProductImage | null;
/**
* Get the lowest variant price available
* @param product
*/
export declare function getLowestVariantPrice(product: Product): MoneyV2 | null;
/**
* Find a product variant given an image
* @param product
* @param image
*/
export declare function findProductVariantByImage(product: Product, image: ProductImage): ProductVariant | null;
export interface ProductSelection {
[name: string]: string;
}
/**
* Given a selection, find the product variant
* @param product
* @param selection
*/
export declare function findExactProductVariant(product: Product, selection: ProductSelection): ProductVariant | null;
export declare function findAllProductVariants(product: Product, selection: ProductSelection): Array<ProductVariant>;
/**
* Check if a product variant matches the selection
* @param variant
* @param selection
* @param returnPartialMatches
*/
export declare function matchSelection(variant: ProductVariant, selection: ProductSelection, returnPartialMatches: boolean): boolean;
/**
* Given a variant, construct the corresponding selection
* @param product
* @param variant
* @returns {Selection}
*/
export declare function getProductSelection(product: Product | null, variant: ProductVariant | null): ProductSelection;
/**
* Get unique product images, including variant images
* @param product
*/
export declare function getAllUniqueImages(product: Product): Array<ProductImage>;
/**
* A named collection. Slim version used in listings
*/
export interface SlimCollection {
id: string;
description: string;
title: string;
handle: string;
}
/**
* A named collection of products
*/
export interface Collection extends SlimCollection {
descriptionHtml: string;
products: GraphQLList<SlimProduct>;
}
export interface GetCollectionRequest extends XcapOptionalParameters {
handle: string;
imageMaxWidth?: number;
}
export interface GetCollectionResult extends XcapJsonResult {
collection: Collection | null;
}
/**
* Get a collection of products
* @param req
* @returns {Thunk<XcapJsonResult>}
*/
export declare function getCollection(req: GetCollectionRequest): Thunk<Promise<GetCollectionResult>>;
export interface GetCollectionsResult extends XcapJsonResult {
collections: GraphQLList<SlimCollection>;
}
export declare type GetCollectionsRequest = XcapOptionalParameters;
/**
* Get all collections. Requires community admin privileges.
* @param req
* @returns {Thunk<XcapJsonResult>}
*/
export declare function getCollections(req: GetCollectionsRequest): Thunk<Promise<GetCollectionsResult>>;
export interface CreateCartLine {
/** Cart line id */
id?: string;
/**
* Product variant id "gid://shopify/ProductVariant/1"
*/
merchandiseId: string;
/**
* Quantity
*/
quantity?: number;
}
export interface CreateCartRequest {
lines: Array<CreateCartLine>;
buyerIdentity?: CartBuyerIdentity;
imageMaxWidth?: number;
}
export interface CartLine {
id: string;
merchandise: {
/**
* Product variant id
*/
id: string;
product: {
/** Product id */
id: string;
/** Product handle */
handle: string;
};
};
quantity: number;
discountAllocations: Array<any>;
attributes: Array<{
key: string;
value: string;
}>;
estimatedCost: {
subtotalAmount: MoneyV2;
totalAmount: MoneyV2;
};
}
export interface CartRequest {
cartId: string;
imageMaxWidth?: number;
}
export interface Cart {
id: string;
createdAt: string;
updatedAt: string;
lines: GraphQLList<CartLine>;
estimatedCost: {
totalAmount: MoneyV2;
subtotalAmount: MoneyV2;
totalTaxAmount: MoneyV2 | null;
totalDutyAmount: MoneyV2 | null;
};
attributes: Array<any>;
buyerIdentity: {
countryCode: string;
};
}
export interface GetCartResult extends XcapJsonResult {
cart: Cart | null;
}
export interface ModifyCartResult extends GetCartResult {
userErrors?: Array<UserError>;
}
/**
* Create a cart
* @param req
*/
export declare function createCart(req: CreateCartRequest): Thunk<Promise<ModifyCartResult>>;
/**
* Alter the contents of the cart. You can not add new lines using this request
* @param req
*/
export declare function cartLinesUpdate(req: CartLinesUpdateRequest): Thunk<Promise<ModifyCartResult>>;
/**
* Add products of the cart
* @param req
*/
export declare function cartLinesAdd(req: CartLinesUpdateRequest): Thunk<Promise<ModifyCartResult>>;
export declare type GetCartRequest = CartRequest;
/**
* Get a cart
* @param req
*/
export declare function getCart(req: GetCartRequest): Thunk<Promise<GetCartResult>>;
export interface CartLinesUpdateRequest extends CartRequest, CreateCartRequest {
}
export interface CartLinesRemoveRequest extends CartRequest {
lineIds: Array<string>;
}
/**
* Remove a product line from the cart
* @param req
*/
export declare function cartLinesRemove(req: CartLinesRemoveRequest): Thunk<Promise<ModifyCartResult>>;
export interface CartBuyerIdentity {
countryCode?: string;
customerAccessToken?: string;
email?: string;
phone?: string;
}
export interface CartBuyerIdentityUpdateRequest extends CartRequest {
buyerIdentity: CartBuyerIdentity;
}
/**
* Update buyer identity or country
* @param req
*/
export declare function cartBuyerIdentityUpdate(req: CartBuyerIdentityUpdateRequest): Thunk<Promise<ModifyCartResult>>;
export interface LineItem {
quantity: number;
variantId: string;
}
export declare type LineItemArray = Array<LineItem>;
export interface ShippingAddress {
firstName: string;
lastName: string;
address1: string;
address2?: string;
zip: string;
city: string;
province?: string;
country: string;
countryCodeV2: string;
company?: string;
phone?: string;
}
export interface CreateCheckoutInput {
email?: string;
note?: string;
lineItems?: LineItemArray;
shippingAddress?: ShippingAddress;
}
export interface CreateCheckoutRequest extends XcapOptionalParameters {
input: CreateCheckoutInput;
addedProductHandle?: string;
variantId?: string;
quantity?: number;
referenceUrlId?: string;
}
export interface UserError {
field: string;
code: string;
message: string;
}
export interface ShippingRate {
handle: string;
title: string;
price: MoneyV2;
}
export interface CheckoutLineItem {
id: string;
title: string;
quantity: number;
variant: {
id: string;
product: {
id: string;
handle: string;
};
};
}
export interface Checkout {
id: string;
ready: boolean;
webUrl: string;
requiresShipping: boolean;
currencyCode: string;
email: string | null;
note: string | null;
/** Price of the checkout before duties, shipping and taxes. */
subtotalPrice: MoneyV2;
/** The sum of all the prices of all the items in the checkout. Duties, taxes, shipping and discounts excluded */
lineItemsSubtotalPrice: MoneyV2;
/** The sum of all the prices of all the items in the checkout, duties, taxes and discounts included. */
totalPrice: MoneyV2;
/** The amount left to be paid. This is equal to the cost of the line items, duties, taxes and shipping minus discounts and gift cards.*/
paymentDue: MoneyV2;
/** The sum of all the taxes applied to the line items and shipping lines in the checkout. */
totalTax: MoneyV2;
/** Specifies if taxes are included in the line item and shipping line prices. */
taxesIncluded: boolean;
availableShippingRates?: {
ready: boolean;
shippingRates: Array<ShippingRate>;
};
shippingAddress: ShippingAddress | null;
shippingLine: ShippingRate | null;
/** The date and time when the checkout was completed. */
completedAt: string | null;
/** The Order Status Page for this Checkout, null when checkout is not completed. */
orderStatusUrl: string | null;
/** Items in basket */
lineItems: GraphQLList<CheckoutLineItem>;
}
export interface CheckoutResult extends XcapJsonResult {
response: {
checkoutUserErrors: Array<UserError>;
checkout: Checkout | null;
};
}
/**
* Create a checkout
* @param req
*/
export declare function createCheckout(req: CreateCheckoutRequest): Thunk<Promise<CheckoutResult>>;
export interface GetCheckoutRequest extends XcapOptionalParameters {
checkoutId: string;
imageMaxWidth?: number;
}
export interface GetCheckoutResult extends XcapJsonResult {
checkout: Checkout | null;
}
/**
* Get a checkout given an id
* @param req
*/
export declare function getCheckout(req: GetCheckoutRequest): Thunk<Promise<GetCheckoutResult>>;
export interface CheckoutReplaceItemsRequest extends XcapOptionalParameters {
checkoutId: string;
lineItems: LineItemArray;
addedProductHandle?: string;
variantId?: string;
quantity?: number;
referenceUrlId?: string;
}
/**
* Replace the items in the checkout
* @param req
*/
export declare function checkoutReplaceItems(req: CheckoutReplaceItemsRequest): Thunk<Promise<CheckoutResult>>;
export interface SelectShippingRequest extends XcapOptionalParameters {
checkoutId: string;
shippingRateHandle: string;
}
/**
* Select shipping
* @param req
*/
export declare function selectShipping(req: SelectShippingRequest): Thunk<Promise<CheckoutResult>>;
export interface SetCheckoutEmailRequest extends XcapOptionalParameters {
checkoutId: string;
email: string;
}
/**
* Set email
* @param req
*/
export declare function setCheckoutEmail(req: SetCheckoutEmailRequest): Thunk<Promise<CheckoutResult>>;
export interface SetShippingAddressRequest extends XcapOptionalParameters {
checkoutId: string;
address: ShippingAddress;
}
/**
* Set shipping address
* @param req
*/
export declare function setShippingAddress(req: SetShippingAddressRequest): Thunk<Promise<CheckoutResult>>;
/**
* Get the required address fields for the country using the specified locale.
* @param locale (Optional. falls back to community locale)
* @param countryCode
*/
export declare function getAddressFields({ locale, countryCode }: {
locale?: string | null;
countryCode: string;
}): Thunk<Promise<AddressFieldName[][]>>;
/**
* Get the list of countries
* @param locale
*/
export declare function getCountries({ locale }: {
locale?: string;
}): Thunk<Promise<Array<Country>>>;
/**
* Get a country
* @param locale
* @param countryCode
*/
export declare function getCountry({ locale, countryCode }: {
locale?: string;
countryCode: string;
}): Thunk<Promise<Country | null>>;
/**
* Convert the amount to MoneyV2, adjusting to the currency correct number of decimals
* @param amount
* @param currencyCode
*/
export declare function toMoneyV2(amount: number, currencyCode: string): MoneyV2;
/**
* Get the root nodes from a flat array of product types
* @param productTypes
*/
export declare function getProductTypeRoots(productTypes: Array<string> | null | undefined): Array<string>;
/**
* Get the parent product type
* @param productType
*/
export declare function getParentProductType(productType: string | null | undefined): string | null;
export declare function cartFindLine(cart: Cart, productVariantId: string): CartLine | null;
/**
* Turn a cart into checkout line items
* @param cart
*/
export declare function cartToLineItems(cart: Cart): Array<LineItem>;
export interface CartNotifyProductAddedRequest extends XcapOptionalParameters {
handle: string;
variantId: string;
}
/**
* Notify users that someone added a product to their cart
* @param params
*/
export declare function cartNotifyProductAdded(params: CartNotifyProductAddedRequest): Thunk<Promise<XcapJsonResult>>;
//# sourceMappingURL=index.d.ts.map