diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f107eb7 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,201 @@ +import * as OAuth from 'oauth-1.0a' + +export declare type WooCommerceRestApiVersion = + | 'wc/v3' + | 'wc/v2' + | 'wc/v1' + | 'wc-api/v3' + | 'wc-api/v2' + | 'wc-api/v1' +export declare type WooCommerceRestApiEncoding = 'utf-8' | 'ascii' +export declare type WooCommerceRestApiMethod = + | 'get' + | 'post' + | 'put' + | 'delete' + | 'options' + +export interface IWooCommerceRestApiOptions { + /* Your Store URL, example: http://woo.dev/ */ + url: string + /* Your API consumer key */ + consumerKey: string + /* Your API consumer secret */ + consumerSecret: string + /* Custom WP REST API URL prefix, used to support custom prefixes created with the `rest_url_prefix filter` */ + wpAPIPrefix?: string + /* API version, default is `v3` */ + version?: WooCommerceRestApiVersion + /* Encoding, default is 'utf-8' */ + encoding?: WooCommerceRestApiEncoding + /* When `true` and using under HTTPS force Basic Authentication as query string, default is `false` */ + queryStringAuth?: boolean + /* Provide support for URLs with ports, eg: `8080` */ + port?: number + /* Define the request timeout */ + timeout?: number + /* Define the custom Axios config, also override this library options */ + axiosConfig?: any +} + +export interface IWooCommerceRestApiQuery { + [key: string]: string +} + +/** + * WooCommerce REST API wrapper + * + * @param {Object} opt + */ +export default class WooCommerceRestApi { + protected classVersion: string + protected url: string + protected consumerKey: string + protected consumerSecret: string + protected wpAPIPrefix: string + protected version: WooCommerceRestApiVersion + protected encoding: WooCommerceRestApiEncoding + protected queryStringAuth: boolean + protected port: number + protected timeout: number + protected axiosConfig: any + + /** + * Class constructor. + * + * @param {Object} opt + */ + constructor(opt: IWooCommerceRestApiOptions | WooCommerceRestApi) + + /** + * Set default options + * + * @param {Object} opt + */ + private _setDefaultsOptions(opt: IWooCommerceRestApiOptions): void + + /** + * Parse params object. + * + * @param {Object} params + * @param {Object} query + */ + private _parseParamsObject(params: any, query: any): IWooCommerceRestApiQuery + + /** + * Normalize query string for oAuth + * + * @param {String} url + * @param {Object} params + * + * @return {String} + */ + private _normalizeQueryString(url: string, params: any): string + + /** + * Get URL + * + * @param {String} endpoint + * @param {Object} params + * + * @return {String} + */ + private _getUrl(endpoint: string, params: any): string + + /** + * Get OAuth + * + * @return {Object} + */ + private _getOAuth(): OAuth + + /** + * Do requests + * + * @param {String} method + * @param {String} endpoint + * @param {Object} data + * @param {Object} params + * + * @return {Object} + */ + private _request( + method: WooCommerceRestApiMethod, + endpoint: string, + data: any, + params: any + ): Promise + + /** + * GET requests + * + * @param {String} endpoint + * @param {Object} params + * + * @return {Object} + */ + public get(endpoint: string): Promise + public get(endpoint: string, params: any): Promise + + /** + * POST requests + * + * @param {String} endpoint + * @param {Object} data + * @param {Object} params + * + * @return {Object} + */ + public post(endpoint: string, data: any): Promise + public post(endpoint: string, data: any, params: any): Promise + + /** + * PUT requests + * + * @param {String} endpoint + * @param {Object} data + * @param {Object} params + * + * @return {Object} + */ + public put(endpoint: string, data: any): Promise + public put(endpoint: string, data: any, params: any): Promise + + /** + * DELETE requests + * + * @param {String} endpoint + * @param {Object} params + * @param {Object} params + * + * @return {Object} + */ + public delete(endpoint: string): Promise + public delete(endpoint: string, params: any): Promise + + /** + * OPTIONS requests + * + * @param {String} endpoint + * @param {Object} params + * + * @return {Object} + */ + public options(endpoint: string): Promise + public options(endpoint: string, params: any): Promise +} + +/** + * Options Exception. + */ +export class OptionsException { + public name: 'Options Error' + public message: string + + /** + * Constructor. + * + * @param {String} message + */ + constructor(message: string) +} diff --git a/package.json b/package.json index 8b8eb67..e8a8df4 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,11 @@ "url": "https://github.com/woocommerce/woocommerce-rest-api-js/issues" }, "main": "index", + "types": "index.d.ts", "files": [ "index.js", - "index.mjs" + "index.mjs", + "index.d.ts" ], "dependencies": { "axios": "^0.19.0",