From 55928eb1e1a9f9d0ac3d1c7efaf573d6a206e297 Mon Sep 17 00:00:00 2001 From: climba03003 Date: Fri, 11 Oct 2019 14:25:36 +0800 Subject: [PATCH 1/4] Add TypeScript Decorator --- index.d.ts | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..db3440a --- /dev/null +++ b/index.d.ts @@ -0,0 +1,206 @@ +import axios from 'axios' +import createHmac from 'create-hmac' +import OAuth from 'oauth-1.0a' +import Url from 'url-parse' + +export type WooCommerceRestApiVersion = + | 'wc/v3' + | 'wc/v2' + | 'wc/v1' + | 'wc-api/v3' + | 'wc-api/v2' + | 'wc-api/v1' +export type WooCommerceRestApiEncoding = 'utf-8' | 'ascii' +export 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 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 + ): any + + /** + * GET requests + * + * @param {String} endpoint + * @param {Object} params + * + * @return {Object} + */ + public get(endpoint: string): any + public get(endpoint: string, params: any): any + + /** + * POST requests + * + * @param {String} endpoint + * @param {Object} data + * @param {Object} params + * + * @return {Object} + */ + public post(endpoint: string, data: any): any + public post(endpoint: string, data: any, params: any): any + + /** + * PUT requests + * + * @param {String} endpoint + * @param {Object} data + * @param {Object} params + * + * @return {Object} + */ + public put(endpoint: string, data: any): any + public put(endpoint: string, data: any, params: any): any + + /** + * DELETE requests + * + * @param {String} endpoint + * @param {Object} params + * @param {Object} params + * + * @return {Object} + */ + public delete(endpoint: string): any + public delete(endpoint: string, params: any): any + + /** + * OPTIONS requests + * + * @param {String} endpoint + * @param {Object} params + * + * @return {Object} + */ + public options(endpoint: string): any + public options(endpoint: string, params: any): any +} + +/** + * Options Exception. + */ +export class OptionsException { + public name: 'Options Error' + public message: string + + /** + * Constructor. + * + * @param {String} message + */ + constructor(message: string) +} + +export default WooCommerceRestApi From d5314a530d14d088e48e0d0d02e3a8b869946dbe Mon Sep 17 00:00:00 2001 From: climba03003 Date: Fri, 11 Oct 2019 14:26:43 +0800 Subject: [PATCH 2/4] Add Decoration Files in package.json --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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", From 15f61f62b4519e0997ce4a47f965acde8a96f460 Mon Sep 17 00:00:00 2001 From: climba03003 Date: Fri, 11 Oct 2019 14:55:59 +0800 Subject: [PATCH 3/4] fix module imports --- index.d.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/index.d.ts b/index.d.ts index db3440a..200b9ca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,17 +1,14 @@ -import axios from 'axios' -import createHmac from 'create-hmac' -import OAuth from 'oauth-1.0a' -import Url from 'url-parse' +import * as OAuth from 'oauth-1.0a' -export type WooCommerceRestApiVersion = +export declare type WooCommerceRestApiVersion = | 'wc/v3' | 'wc/v2' | 'wc/v1' | 'wc-api/v3' | 'wc-api/v2' | 'wc-api/v1' -export type WooCommerceRestApiEncoding = 'utf-8' | 'ascii' -export type WooCommerceRestApiMethod = +export declare type WooCommerceRestApiEncoding = 'utf-8' | 'ascii' +export declare type WooCommerceRestApiMethod = | 'get' | 'post' | 'put' @@ -50,7 +47,7 @@ export interface IWooCommerceRestApiQuery { * * @param {Object} opt */ -export class WooCommerceRestApi { +export default class WooCommerceRestApi { protected classVersion: string protected url: string protected consumerKey: string @@ -202,5 +199,3 @@ export class OptionsException { */ constructor(message: string) } - -export default WooCommerceRestApi From f5880442576bf6fc24adbe13730f0bdc3c77a669 Mon Sep 17 00:00:00 2001 From: climba03003 Date: Fri, 11 Oct 2019 15:20:43 +0800 Subject: [PATCH 4/4] update request return Request should return Promise according to axios --- index.d.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/index.d.ts b/index.d.ts index 200b9ca..f107eb7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -124,7 +124,7 @@ export default class WooCommerceRestApi { endpoint: string, data: any, params: any - ): any + ): Promise /** * GET requests @@ -134,8 +134,8 @@ export default class WooCommerceRestApi { * * @return {Object} */ - public get(endpoint: string): any - public get(endpoint: string, params: any): any + public get(endpoint: string): Promise + public get(endpoint: string, params: any): Promise /** * POST requests @@ -146,8 +146,8 @@ export default class WooCommerceRestApi { * * @return {Object} */ - public post(endpoint: string, data: any): any - public post(endpoint: string, data: any, params: any): any + public post(endpoint: string, data: any): Promise + public post(endpoint: string, data: any, params: any): Promise /** * PUT requests @@ -158,8 +158,8 @@ export default class WooCommerceRestApi { * * @return {Object} */ - public put(endpoint: string, data: any): any - public put(endpoint: string, data: any, params: any): any + public put(endpoint: string, data: any): Promise + public put(endpoint: string, data: any, params: any): Promise /** * DELETE requests @@ -170,8 +170,8 @@ export default class WooCommerceRestApi { * * @return {Object} */ - public delete(endpoint: string): any - public delete(endpoint: string, params: any): any + public delete(endpoint: string): Promise + public delete(endpoint: string, params: any): Promise /** * OPTIONS requests @@ -181,8 +181,8 @@ export default class WooCommerceRestApi { * * @return {Object} */ - public options(endpoint: string): any - public options(endpoint: string, params: any): any + public options(endpoint: string): Promise + public options(endpoint: string, params: any): Promise } /**