UNPKG

@commercelayer/sdk-utils

Version:
249 lines (237 loc) 11.1 kB
import { CommerceLayerClient, Resource, ListableResourceType, QueryParamsList, ListResponse, ResourceUpdate, UpdatableResourceType, ApiError, DeletableResourceType, HeadersObj, ResourceTypeLock, QueryParams, ResourcesConfig, SdkError, ResourceId, ResourceCreate, CreatableResourceType, RetrievableResourceType, CleanupCreate, Cleanup, ExportCreate, Export, ImportCreate, Import, Address, Authorization, AvalaraAccount, Bundle, BuyXPayYPromotion, Capture, Coupon, CustomerAddress, CustomerPasswordReset, CustomerSubscription, Customer, ExternalPromotion, FixedAmountPromotion, FixedPricePromotion, FreeGiftPromotion, FreeShippingPromotion, GiftCard, InStockSubscription, LineItem, LineItemOption, Order, OrderCopy, OrderSubscription, Parcel, PercentageDiscountPromotion, PriceFrequencyTier, PriceVolumeTier, Promotion, RecurringOrderCopy, Refund, Return, Shipment, ShippingWeightTier, Sku, SkuOption, StockTransfer, Transaction, Void } from '@commercelayer/sdk'; declare class CommerceLayerUtilsConfig { #private; set sdk(cl: CommerceLayerClient); get sdk(): CommerceLayerClient; } declare const CommerceLayerUtils: (cl?: CommerceLayerClient) => CommerceLayerUtilsConfig; type AllParams = Omit<QueryParamsList, 'pageSize' | 'pageNumber' | 'sort'>; declare const retrieveAll: <R extends Resource>(resourceType: ListableResourceType, params?: AllParams & { limit?: number; }) => Promise<ListResponse<R>>; type UpdateResult = { total: number; processed: number; errors: number; resources: Record<string, { success: boolean; error?: ApiError; errorMessage?: string; }>; }; declare const updateAll: <U extends Omit<ResourceUpdate, "id">>(resourceType: UpdatableResourceType, resource: U, params?: AllParams) => Promise<UpdateResult>; type DeleteResult = UpdateResult; declare const deleteAll: (resourceType: DeletableResourceType, params?: AllParams) => Promise<DeleteResult>; declare const all: { retrieveAll: <R extends Resource>(resourceType: ListableResourceType, params?: AllParams & { limit?: number; }) => Promise<ListResponse<R>>; updateAll: <U extends Omit<ResourceUpdate, "id">>(resourceType: UpdatableResourceType, resource: U, params?: AllParams) => Promise<UpdateResult>; }; type PaginationParams<R extends Resource> = Omit<QueryParamsList<R>, 'pageSize'> & { pageSize?: number; }; declare const retrievePage: <R extends Resource>(resourceType: ListableResourceType, params?: PaginationParams<R>) => Promise<ListResponse<R>>; type RateLimitHeader = { count?: number; period?: number; interval: number; limit: number; remaining: number; }; type RateLimitInfo = { limit: number; interval: number; delay: number; requests?: number; }; declare const headerRateLimits: (headers?: HeadersObj) => RateLimitHeader; type TaskOperation = 'retrieve' | 'list' | 'create' | 'update' | 'delete'; type TaskResourceParam = ResourceId | ResourceCreate | ResourceUpdate; type TaskResourceResult = Resource | ListResponse<Resource>; type TaskResult = TaskResourceResult | undefined; declare class InvalidTokenError extends Error { readonly cause: ApiError; constructor(error: ApiError); } type SuccessCallback = (output: TaskResult, task: Task) => Promise<void> | void; type FailureCallback = (error: SdkError, task: Task) => Promise<boolean> | boolean; type TokenCallback = (error: InvalidTokenError, task: Task) => Promise<string> | string; type PrepareResourceResult = TaskResourceParam | undefined; type PrepareResourceCallback = (resource: TaskResourceParam, last: TaskResourceResult) => Promise<PrepareResourceResult> | PrepareResourceResult; type TemplateTask = Partial<Task>; type CreateTask = CRUDTask & { resourceType: CreatableResourceType; operation: 'create'; resource: ResourceCreate; }; type UpdateTask = CRUDTask & { resourceType: UpdatableResourceType; operation: 'update'; resource: ResourceUpdate; }; type DeleteTask = CRUDTask & { resourceType: DeletableResourceType; operation: 'delete'; resource: ResourceId; }; type ListTask = { resourceType: ListableResourceType; operation: 'list'; params?: QueryParamsList; }; type RetrieveTask = CRUDTask & { resourceType: RetrievableResourceType; operation: 'retrieve'; resource: ResourceId; }; type CRUDTask = { resourceType: ResourceTypeLock; operation: 'create' | 'retrieve' | 'update' | 'delete'; resource: Record<string, any>; prepareResource?: PrepareResourceCallback; }; type Task = { label?: string; resourceType: ResourceTypeLock; operation: TaskOperation; params?: QueryParams; options?: ResourcesConfig; executed?: boolean; onSuccess?: { callback?: SuccessCallback; result?: TaskResult; }; onFailure?: { error?: SdkError; haltOnError?: boolean; errorHandler?: FailureCallback; }; } & (CreateTask | UpdateTask | DeleteTask | ListTask | RetrieveTask); type BatchResult = { startedAt: Date; finishedAt?: Date; }; type BatchOptions = { haltOnError?: boolean; refreshToken?: TokenCallback; }; type Batch = { tasks: Task[]; rateLimits?: Partial<Record<ResourceTypeLock, Partial<Record<TaskOperation, RateLimitInfo>>>>; running?: boolean; runningTask?: string; options?: BatchOptions; }; declare const executeBatch: (batch: Batch) => Promise<BatchResult>; declare const batch: { execute: (batch: Batch) => Promise<BatchResult>; }; type JobOptions = { size?: number; delay?: number; queueLength?: number; noGroupId?: boolean; noMetadata?: boolean; }; type CleanupResult = Cleanup; declare const splitCleanup: (clp: CleanupCreate, options?: JobOptions) => Promise<CleanupCreate[]>; declare const cleanupsToBatchTasks: (cleanups: CleanupCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; declare const executeCleanup: (clp: CleanupCreate, options?: JobOptions) => Promise<CleanupResult[]>; declare const cleanups: { split: (clp: CleanupCreate, options?: JobOptions) => Promise<CleanupCreate[]>; execute: (clp: CleanupCreate, options?: JobOptions) => Promise<CleanupResult[]>; toBatchTasks: (cleanups: CleanupCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; }; type ExportResult = Export; declare const splitExport: (exp: ExportCreate, options?: JobOptions) => Promise<ExportCreate[]>; declare const exportsToBatchTasks: (exports: ExportCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; declare const executeExport: (exp: ExportCreate, options?: JobOptions) => Promise<ExportResult[]>; declare const exportz: { split: (exp: ExportCreate, options?: JobOptions) => Promise<ExportCreate[]>; execute: (exp: ExportCreate, options?: JobOptions) => Promise<ExportResult[]>; toBatchTasks: (exports: ExportCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; }; type ImportResult = Import; declare const splitImport: (imp: ImportCreate, options?: JobOptions) => ImportCreate[]; declare const importsToBatchTasks: (imports: ImportCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; declare const executeImport: (imp: ImportCreate, options?: JobOptions) => Promise<ImportResult[]>; declare const imports: { split: (imp: ImportCreate, options?: JobOptions) => ImportCreate[]; execute: (imp: ImportCreate, options?: JobOptions) => Promise<ImportResult[]>; toBatchTasks: (imports: ImportCreate[], baseTask?: TemplateTask) => Array<Task & { operation: "create"; }>; }; declare const denormalizePayload: <R extends Resource>(payload: string) => R | R[]; type CheckStatus = { ok: boolean; topic?: string; message?: string; }; /** * * @param body the webhook body * @param headers all headers map or value of the signature header * @param secret the shared secret string * @returns */ declare const checkSignature: (body: string, headers: Record<string, string> | string, secret: string) => CheckStatus; declare const webhooks: { denormalizePayload: <R extends Resource>(payload: string) => R | R[]; checkSignature: (body: string, headers: Record<string, string> | string, secret: string) => CheckStatus; parse: { addresses(payload: string): Address; authorizations(payload: string): Authorization; avalara_accounts(payload: string): AvalaraAccount; bundles(payload: string): Bundle; buy_x_pay_y_promotions(payload: string): BuyXPayYPromotion; captures(payload: string): Capture; cleanups(payload: string): Cleanup; coupons(payload: string): Coupon; customer_addresses(payload: string): CustomerAddress; customer_password_resets(payload: string): CustomerPasswordReset; customer_subscriptions(payload: string): CustomerSubscription; customers(payload: string): Customer; exports(payload: string): Export; external_promotions(payload: string): ExternalPromotion; fixed_amount_promotions(payload: string): FixedAmountPromotion; fixed_price_promotions(payload: string): FixedPricePromotion; free_gift_promotions(payload: string): FreeGiftPromotion; free_shipping_promotions(payload: string): FreeShippingPromotion; gift_cards(payload: string): GiftCard; imports(payload: string): Import; in_stock_subscriptions(payload: string): InStockSubscription; line_items(payload: string): LineItem; line_items_options(payload: string): LineItemOption; orders(payload: string): Order; order_copies(payload: string): OrderCopy; order_subscriptions(payload: string): OrderSubscription; parcels(payload: string): Parcel | Parcel[]; percentage_discount_promotions(payload: string): PercentageDiscountPromotion; price_frequency_tiers(payload: string): PriceFrequencyTier; price_volume_tiers(payload: string): PriceVolumeTier; promotions(payload: string): Promotion; recurring_order_copies(payload: string): RecurringOrderCopy; refunds(payload: string): Refund; returns(payload: string): Return; shipments(payload: string): Shipment; shipping_weight_tiers(payload: string): ShippingWeightTier; skus(payload: string): Sku; sku_options(payload: string): SkuOption; stock_transfers(payload: string): StockTransfer; transactions(payload: string): Transaction; voids(payload: string): Void; }; }; export { type Batch, type BatchOptions, CommerceLayerUtils, CommerceLayerUtilsConfig, InvalidTokenError, type Task, type TaskResult, all, batch, checkSignature, cleanups, cleanupsToBatchTasks, CommerceLayerUtils as default, deleteAll, denormalizePayload, executeBatch, executeCleanup, executeExport, executeImport, exportsToBatchTasks, exportz, headerRateLimits, imports, importsToBatchTasks, retrieveAll, retrievePage, splitCleanup, splitExport, splitImport, updateAll, webhooks };