@woocommerce/data
Version:
WooCommerce Admin data store and utilities
137 lines • 5.23 kB
TypeScript
import { getResourceName } from '../utils';
import type { IdQuery, IdType, Item, ItemQuery } from './types';
/**
* Get a REST path given a template path and URL params.
*
* @param templatePath Path with variable names.
* @param query Item query.
* @param parameters Array of items to replace in the templatePath.
* @return string REST path.
*/
export declare const getRestPath: (templatePath: string, query: Partial<ItemQuery>, parameters: IdType[]) => string;
/**
* Get a key from an item ID and optional parent.
*
* @param query Item Query.
* @param urlParameters Parameters used for URL.
* @return string
*/
export declare const getKey: (query: IdQuery, urlParameters?: IdType[]) => IdType;
type organizeItemsByIdReturn = {
objItems: Record<string, Item>;
ids: IdType[];
};
/**
* This function takes an array of items and reduces it into a single object,
* where each key is a unique identifier generated by
* combining the item ID and optional URL parameters.
* It also returns an array of these keys (`ids`).
*
* @param {Array<Item>} items - The items to process.
* @param {Array<IdType>} urlParameters - The URL parameters used to generate keys.
* @param {Record<string, Item>} currentState - The current state data to merge with.
* @return {organizeItemsByIdReturn} An object with two properties: `objItems` and `ids`.
*/
export declare const organizeItemsById: (items: Item[], urlParameters?: IdType[], currentState?: Record<string, Item>) => organizeItemsByIdReturn;
/**
* Filters the input data object, returning a new object that contains only the keys
* specified in the keys array.
*
* @param {Record<string, unknown>} data - The original data object to filter.
* @param {IdType[]} keys - An array of keys that should be included in the returned object.
* @return {Record<string, unknown>} A new object containing only the specified keys.
*/
export declare function filterDataByKeys(data: Record<string, unknown>, keys: IdType[]): Record<string, unknown>;
/**
* Parse an ID query into a ID string.
*
* @param query Id Query
* @return string ID.
*/
export declare const parseId: (query: IdQuery, urlParameters?: IdType[]) => {
id: string | number;
key: string | number;
};
/**
* Create a new function that adds in the namespace.
*
* @param fn Function to wrap.
* @param namespace Namespace to pass to last argument of function.
* @return Wrapped function
*/
export declare const applyNamespace: <T extends (...args: any[]) => unknown>(fn: T, namespace: string, defaultArgs?: any[]) => (...args: Parameters<T>) => unknown;
/**
* Get the key names from a namespace string.
*
* @param namespace Namespace to get keys from.
* @return Array of keys.
*/
export declare const getNamespaceKeys: (namespace: string) => string[];
/**
* Get URL parameters from namespace and provided query.
*
* @param namespace Namespace string to replace params in.
* @param query Query object with key values.
* @return Array of URL parameter values.
*/
export declare const getUrlParameters: (namespace: string, query: IdQuery | Partial<ItemQuery>) => IdType[];
/**
* Check to see if an argument is a valid type of ID query.
*
* @param arg Unknow argument to check.
* @param namespace The namespace string
* @return boolean
*/
export declare const isValidIdQuery: (arg: unknown, namespace: string) => boolean;
/**
* Replace the initial argument with a key if it's a valid ID query.
*
* @param args Args to check.
* @param namespace Namespace.
* @return Sanitized arguments.
*/
export declare const maybeReplaceIdQuery: (args: unknown[], namespace: string) => unknown[];
/**
* Clean a query of all namespaced params.
*
* @param query Query to clean.
* @param namespace
* @return Cleaned query object.
*/
export declare const cleanQuery: (query: Partial<ItemQuery>, namespace: string) => {
[x: string]: unknown;
context?: string | undefined;
page?: number | undefined;
per_page?: number | undefined;
search?: string | undefined;
after?: string | undefined;
before?: string | undefined;
exclude?: string | undefined;
include?: string | number[] | undefined;
offset?: number | undefined;
order?: "asc" | "desc" | undefined;
orderby?: "date" | "id" | "include" | "title" | "slug" | undefined;
parent?: number[] | undefined;
parent_exclude?: number[] | undefined;
_fields?: string[] | undefined;
parent_id?: IdType | undefined;
order_by?: string | undefined;
};
/**
* Get the identifier for a request provided its arguments.
*
* @param name Name of action or selector.
* @param args Arguments for the request.
* @return Key to identify the request.
*/
export declare const getRequestIdentifier: typeof getResourceName;
/**
* Get a generic action name from a resource action name if one exists.
*
* @param action Action name to check.
* @param resourceName Resurce name.
* @return Generic action name if one exists, otherwise the passed action name.
*/
export declare const getGenericActionName: (action: string, resourceName: string) => string;
export {};
//# sourceMappingURL=utils.d.ts.map