@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
72 lines (71 loc) • 3.19 kB
TypeScript
import * as rpcMethods from './rpc/methods';
import type { RpcContext } from './types';
export { rpcMethods };
export * from './errors';
export * from './utils';
export * from './constants';
export * from './cache';
export * from './helpers';
export * from './types';
export type { BasketItemUpdateData } from './rpc/methods';
/**
* Core RPC methods available within `@scayle/storefront-core`.
*/
type RpcMethodsCore = typeof rpcMethods;
/**
* Interface for extending RPC methods within `@scayle/storefront-core`. Allows custom RPC methods
* to be added to the SCAYLE Storefront application by extending this interface.
*
* @example
* ```typescript
* // nuxt.config.ts
* import * as customRpcMethods from './rpc'
* declare module '@scayle/storefront-core' {
* export interface RpcMethodsStorefrontInternal extends typeof customRpcMethods {}
* }
* ```
*/
export interface RpcMethodsStorefrontInternal {
}
/**
* Combined RPC methods, merging core methods with SCAYLE Storefront application extensions.
* Extensions from `@scayle/storefront-core` take precedence in case of naming conflicts.
*/
export type RpcMethods = {
[Key in keyof RpcMethodsCore | keyof RpcMethodsStorefrontInternal]: Key extends keyof RpcMethodsStorefrontInternal ? RpcMethodsStorefrontInternal[Key] : Key extends keyof RpcMethodsCore ? RpcMethodsCore[Key] : never;
};
/**
* Type alias representing the name of an RPC method. This uses a
* resolution technique to avoid inlining method names in compiled
* type declarations, which is essential for supporting dynamically
* added RPC methods from the SCAYLE Storefront application.
*
* @see https://github.com/microsoft/TypeScript/pull/42149#issuecomment-865385560
*
* Without this workaround, the signature to `rpcCall` looks something like
* `rpcCall(m: 'addItemToBasket' | 'addItemToWishlist'...)`.
* But we need it to look like `rpcCall(m: RpcMethodName)` in order to include
* RPC methods added by the SCAYLE Storefront application.
*/
type Resolve<T> = T extends T ? T : never;
export type RpcMethodName = Resolve<keyof RpcMethods>;
/**
* Describes the type of a specific RPC method by its name.
*
* @template N The name of the RPC method.
*/
export type RpcMethod<N extends RpcMethodName> = RpcMethods[N];
/**
* Describes the type of the parameters for a specific RPC method.
*
* @template N The name of the RPC method.
*/
export type RpcMethodParameters<N extends RpcMethodName> = Parameters<RpcMethod<N>>[0];
/**
* Describes the return type of a specific RPC method.
*
* @template N The name of the RPC method.
*/
export type RpcMethodReturnType<N extends RpcMethodName> = ReturnType<RpcMethod<N>>;
export type RpcMethodCall = <N extends RpcMethodName, P extends RpcMethodParameters<N>, TResult extends Exclude<Awaited<RpcMethodReturnType<N>>, Response>>(...args: P extends RpcContext ? [N] : [N, P]) => Promise<TResult>;
export { ExistingItemHandling, AddToBasketFailureKind, UpdateBasketItemFailureKind, AddToWishlistFailureKind, PromotionEffectType, FilterTypes, APISortOption, APISortOrder, SmartSortingKey, type Pagination, type ProductSortConfig, type ShopCountryCustomData, } from '@scayle/storefront-api';