Skip to content

Commit

Permalink
fix(component): params should be optional
Browse files Browse the repository at this point in the history
Get params should be optional

BREAKING CHANGE: Params must be optional since we could use the endpoit with/without it

#25
  • Loading branch information
Yuri-Lima committed Mar 18, 2023
1 parent b4b5fa0 commit 9284915
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
41 changes: 23 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ProductsMainParams,
SystemStatusParams,
CouponsParams,
CustomersParams,
CustomersParams
} from "./typesANDinterfaces.js"; // Typescript types for the library

/**
Expand Down Expand Up @@ -128,20 +128,24 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
*
* @return {String}
*/
_normalizeQueryString(url: string, params: Record<string, any>): string {
_normalizeQueryString(url: string, params: Partial<Record<string, any>>): string {
/**
* Exit if url and params are not defined
*/
if (url.indexOf("?") === -1 && Object.keys(params).length === 0) {
return url;
}
const query = new Url(url, true).query; // Parse the query string returned by the url

// console.log("params:", params);
const values = [];

let queryString = "";

// Include params object into URL.searchParams.
this._parseParamsObject(params, query);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const a = this._parseParamsObject(params, query);
// console.log("A:", a);

/**
* Loop through the params object and push the key and value into the values array
Expand All @@ -152,6 +156,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
}

values.sort(); // Sort the values array


for (const i in values) {
/*
Expand All @@ -175,7 +180,9 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
/**
* Return the url with the queryString
*/
return url.split("?")[0] + "?" + queryString;
const urlObject = url.split("?")[0] + "?" + queryString

return urlObject;
}

/**
Expand All @@ -186,7 +193,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
*
* @return {String}
*/
_getUrl(endpoint: string, params: Record<string, unknown>): string {
_getUrl(endpoint: string, params: Partial<Record<string, unknown>>): string {
const api = this._opt.wpAPIPrefix + "/"; // Add prefix to endpoint

let url =
Expand All @@ -204,9 +211,9 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
}

/**
* If isHttps is false, normalize the query string
* If isHttps is true, normalize the query string
*/
if (!this._opt.isHttps) {
if (this._opt.isHttps) {
return this._normalizeQueryString(url, params);
}

Expand Down Expand Up @@ -249,10 +256,11 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
_request(
method: WooRestApiMethod,
endpoint: string,
data: Record<string, unknown>,
params: Partial<Record<string, unknown>> = {}
data?: Record<string, unknown>,
params: Record<string, unknown> = {}
): Promise<any> {
const url = this._getUrl(endpoint, params);


const header: RawAxiosRequestHeaders = {
Accept: "application/json",
Expand Down Expand Up @@ -323,11 +331,8 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
*
* @return {Object}
*/
get<T extends WooRestApiEndpoint>(
endpoint: T,
params: Partial<WooRestApiParams> = {}
): Promise<any> {
return this._request("GET", endpoint, {}, params);
get(endpoint: WooRestApiEndpoint, params?: Partial<WooRestApiParams>): Promise<any> {
return this._request("GET", endpoint, undefined, params);
}

/**
Expand All @@ -342,7 +347,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
post<T extends WooRestApiEndpoint>(
endpoint: T,
data: Record<string, unknown>,
params: Partial<WooRestApiParams> = {}
params?: Partial<WooRestApiParams>
): Promise<any> {
return this._request("POST", endpoint, data, params);
}
Expand All @@ -359,7 +364,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
put<T extends WooRestApiEndpoint>(
endpoint: T,
data: Record<string, unknown>,
params: Partial<WooRestApiParams> = {}
params?: Partial<WooRestApiParams>
): Promise<any> {
return this._request("PUT", endpoint, data, params);
}
Expand All @@ -375,7 +380,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
*/
delete<T extends WooRestApiEndpoint>(
endpoint: T,
params: Partial<WooRestApiParams> = {}
params?: Partial<WooRestApiParams>
): Promise<any> {
return this._request("DELETE", endpoint, {}, params);
}
Expand All @@ -390,7 +395,7 @@ export default class WooCommerceRestApi<T extends WooRestApiOptions> {
*/
options<T extends WooRestApiEndpoint>(
endpoint: T,
params: Partial<WooRestApiParams> = {}
params?: Partial<WooRestApiParams>
): Promise<any> {
return this._request("OPTIONS", endpoint, {}, params);
}
Expand Down
7 changes: 3 additions & 4 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe("#options", () => {
const endpoint = "products";
const expected = "https://test.dev/wp-rest/wc/v3/" + endpoint;
const url = api._getUrl(endpoint, {});

expect(url).toBe(expected);
});
});
Expand All @@ -34,7 +33,7 @@ describe("#methods", () => {
queryStringAuth: false, // Force Basic Authentication as query string true and using under HTTPS
});

test("_getUrl should return full endpoint URL", () => {
test("_getUrl should return full endpoint URL", () => { // Fix #1 This is the same test as the one above
const endpoint = "products";
const expected = "https://test.dev/wp-json/wc/v3/" + endpoint;
const url = api._getUrl(endpoint, {});
Expand Down Expand Up @@ -73,7 +72,7 @@ describe("#requests", () => {
ok: true,
});

return api.post("orders", {}).then((response: any) => {
return api.post("coupons", {}, {}).then((response: any) => {
expect(response.status).toBe(201);
});
});
Expand All @@ -84,7 +83,7 @@ describe("#requests", () => {
ok: true,
});

return api.get("orders", {}).then((response: any) => {
return api.get("orders").then((response: any) => {
expect(response.status).toBe(200);
});
});
Expand Down
17 changes: 8 additions & 9 deletions src/typesANDinterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export declare type WooRestApiEndpoint =
| "orders"
| "products"
| "system_status"
| ""
// | "reports" // TODO: add support for reports
// | "settings" // TODO: add support for settings
// | "webhooks" // TODO: add support for webhooks
// | "shipping" // TODO: add support for shipping
// | "shipping_methods" // TODO: add support for shipping_methods
// | "taxes" // TODO: add support for taxes
// | "payment_gateways" // TODO: add support for payment_gateways
| "reports" // TODO: add support for reports
| "settings" // TODO: add support for settings
| "webhooks" // TODO: add support for webhooks
| "shipping" // TODO: add support for shipping
| "shipping_methods" // TODO: add support for shipping_methods
| "taxes" // TODO: add support for taxes
| "payment_gateways" // TODO: add support for payment_gateways

export declare type IWooRestApiQuery = Record<string, unknown>;

Expand Down Expand Up @@ -317,7 +316,7 @@ type SystemStatusSecurity = {
/* End of Types */

/* Start of Interfaces */
interface Coupons {
export interface Coupons {
id: number;
code: string;
amount: string;
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
"module": "NodeNext", /* Specify what module code is generated. */
"rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
"crypto": [
"node_modules/crypto-js"
]
}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": [
"./node_modules/@types",
Expand Down

0 comments on commit 9284915

Please sign in to comment.