|
| 1 | +import * as OAuth from 'oauth-1.0a' |
| 2 | + |
| 3 | +export declare type WooCommerceRestApiVersion = |
| 4 | + | 'wc/v3' |
| 5 | + | 'wc/v2' |
| 6 | + | 'wc/v1' |
| 7 | + | 'wc-api/v3' |
| 8 | + | 'wc-api/v2' |
| 9 | + | 'wc-api/v1' |
| 10 | +export declare type WooCommerceRestApiEncoding = 'utf-8' | 'ascii' |
| 11 | +export declare type WooCommerceRestApiMethod = |
| 12 | + | 'get' |
| 13 | + | 'post' |
| 14 | + | 'put' |
| 15 | + | 'delete' |
| 16 | + | 'options' |
| 17 | + |
| 18 | +export interface IWooCommerceRestApiOptions { |
| 19 | + /* Your Store URL, example: http://woo.dev/ */ |
| 20 | + url: string |
| 21 | + /* Your API consumer key */ |
| 22 | + consumerKey: string |
| 23 | + /* Your API consumer secret */ |
| 24 | + consumerSecret: string |
| 25 | + /* Custom WP REST API URL prefix, used to support custom prefixes created with the `rest_url_prefix filter` */ |
| 26 | + wpAPIPrefix?: string |
| 27 | + /* API version, default is `v3` */ |
| 28 | + version?: WooCommerceRestApiVersion |
| 29 | + /* Encoding, default is 'utf-8' */ |
| 30 | + encoding?: WooCommerceRestApiEncoding |
| 31 | + /* When `true` and using under HTTPS force Basic Authentication as query string, default is `false` */ |
| 32 | + queryStringAuth?: boolean |
| 33 | + /* Provide support for URLs with ports, eg: `8080` */ |
| 34 | + port?: number |
| 35 | + /* Define the request timeout */ |
| 36 | + timeout?: number |
| 37 | + /* Define the custom Axios config, also override this library options */ |
| 38 | + axiosConfig?: any |
| 39 | +} |
| 40 | + |
| 41 | +export interface IWooCommerceRestApiQuery { |
| 42 | + [key: string]: string |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * WooCommerce REST API wrapper |
| 47 | + * |
| 48 | + * @param {Object} opt |
| 49 | + */ |
| 50 | +export default class WooCommerceRestApi { |
| 51 | + protected classVersion: string |
| 52 | + protected url: string |
| 53 | + protected consumerKey: string |
| 54 | + protected consumerSecret: string |
| 55 | + protected wpAPIPrefix: string |
| 56 | + protected version: WooCommerceRestApiVersion |
| 57 | + protected encoding: WooCommerceRestApiEncoding |
| 58 | + protected queryStringAuth: boolean |
| 59 | + protected port: number |
| 60 | + protected timeout: number |
| 61 | + protected axiosConfig: any |
| 62 | + |
| 63 | + /** |
| 64 | + * Class constructor. |
| 65 | + * |
| 66 | + * @param {Object} opt |
| 67 | + */ |
| 68 | + constructor(opt: IWooCommerceRestApiOptions | WooCommerceRestApi) |
| 69 | + |
| 70 | + /** |
| 71 | + * Set default options |
| 72 | + * |
| 73 | + * @param {Object} opt |
| 74 | + */ |
| 75 | + private _setDefaultsOptions(opt: IWooCommerceRestApiOptions): void |
| 76 | + |
| 77 | + /** |
| 78 | + * Parse params object. |
| 79 | + * |
| 80 | + * @param {Object} params |
| 81 | + * @param {Object} query |
| 82 | + */ |
| 83 | + private _parseParamsObject(params: any, query: any): IWooCommerceRestApiQuery |
| 84 | + |
| 85 | + /** |
| 86 | + * Normalize query string for oAuth |
| 87 | + * |
| 88 | + * @param {String} url |
| 89 | + * @param {Object} params |
| 90 | + * |
| 91 | + * @return {String} |
| 92 | + */ |
| 93 | + private _normalizeQueryString(url: string, params: any): string |
| 94 | + |
| 95 | + /** |
| 96 | + * Get URL |
| 97 | + * |
| 98 | + * @param {String} endpoint |
| 99 | + * @param {Object} params |
| 100 | + * |
| 101 | + * @return {String} |
| 102 | + */ |
| 103 | + private _getUrl(endpoint: string, params: any): string |
| 104 | + |
| 105 | + /** |
| 106 | + * Get OAuth |
| 107 | + * |
| 108 | + * @return {Object} |
| 109 | + */ |
| 110 | + private _getOAuth(): OAuth |
| 111 | + |
| 112 | + /** |
| 113 | + * Do requests |
| 114 | + * |
| 115 | + * @param {String} method |
| 116 | + * @param {String} endpoint |
| 117 | + * @param {Object} data |
| 118 | + * @param {Object} params |
| 119 | + * |
| 120 | + * @return {Object} |
| 121 | + */ |
| 122 | + private _request( |
| 123 | + method: WooCommerceRestApiMethod, |
| 124 | + endpoint: string, |
| 125 | + data: any, |
| 126 | + params: any |
| 127 | + ): Promise<any> |
| 128 | + |
| 129 | + /** |
| 130 | + * GET requests |
| 131 | + * |
| 132 | + * @param {String} endpoint |
| 133 | + * @param {Object} params |
| 134 | + * |
| 135 | + * @return {Object} |
| 136 | + */ |
| 137 | + public get(endpoint: string): Promise<any> |
| 138 | + public get(endpoint: string, params: any): Promise<any> |
| 139 | + |
| 140 | + /** |
| 141 | + * POST requests |
| 142 | + * |
| 143 | + * @param {String} endpoint |
| 144 | + * @param {Object} data |
| 145 | + * @param {Object} params |
| 146 | + * |
| 147 | + * @return {Object} |
| 148 | + */ |
| 149 | + public post(endpoint: string, data: any): Promise<any> |
| 150 | + public post(endpoint: string, data: any, params: any): Promise<any> |
| 151 | + |
| 152 | + /** |
| 153 | + * PUT requests |
| 154 | + * |
| 155 | + * @param {String} endpoint |
| 156 | + * @param {Object} data |
| 157 | + * @param {Object} params |
| 158 | + * |
| 159 | + * @return {Object} |
| 160 | + */ |
| 161 | + public put(endpoint: string, data: any): Promise<any> |
| 162 | + public put(endpoint: string, data: any, params: any): Promise<any> |
| 163 | + |
| 164 | + /** |
| 165 | + * DELETE requests |
| 166 | + * |
| 167 | + * @param {String} endpoint |
| 168 | + * @param {Object} params |
| 169 | + * @param {Object} params |
| 170 | + * |
| 171 | + * @return {Object} |
| 172 | + */ |
| 173 | + public delete(endpoint: string): Promise<any> |
| 174 | + public delete(endpoint: string, params: any): Promise<any> |
| 175 | + |
| 176 | + /** |
| 177 | + * OPTIONS requests |
| 178 | + * |
| 179 | + * @param {String} endpoint |
| 180 | + * @param {Object} params |
| 181 | + * |
| 182 | + * @return {Object} |
| 183 | + */ |
| 184 | + public options(endpoint: string): Promise<any> |
| 185 | + public options(endpoint: string, params: any): Promise<any> |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Options Exception. |
| 190 | + */ |
| 191 | +export class OptionsException { |
| 192 | + public name: 'Options Error' |
| 193 | + public message: string |
| 194 | + |
| 195 | + /** |
| 196 | + * Constructor. |
| 197 | + * |
| 198 | + * @param {String} message |
| 199 | + */ |
| 200 | + constructor(message: string) |
| 201 | +} |
0 commit comments