Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat(client): category service (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored May 4, 2021
1 parent a10806a commit 7eee960
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 41 deletions.
6 changes: 3 additions & 3 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ export function getAvailableShippingMethods(contextInstance?: ShopwareApiInstanc
// @beta
export function getCart(contextInstance?: ShopwareApiInstance): Promise<Cart>;

// @alpha @deprecated (undocumented)
export function getCategories(searchCriteria?: SearchCriteria, contextInstance?: ShopwareApiInstance): Promise<SearchResult<Category[]>>;
// @beta (undocumented)
export function getCategories(searchCriteria?: ShopwareSearchParams, contextInstance?: ShopwareApiInstance): Promise<EntityResult<"category", Category[]>>;

// @alpha (undocumented)
// @beta (undocumented)
export function getCategory(categoryId: string, contextInstance?: ShopwareApiInstance): Promise<Category>;

// @beta (undocumented)
Expand Down
30 changes: 30 additions & 0 deletions docs/landing/resources/api/shopware-6-client.getcategories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [getCategories](./shopware-6-client.getcategories.md)

## getCategories() function

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
export declare function getCategories(searchCriteria?: ShopwareSearchParams, contextInstance?: ShopwareApiInstance): Promise<EntityResult<"category", Category[]>>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| searchCriteria | ShopwareSearchParams | |
| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | |

<b>Returns:</b>

Promise&lt;EntityResult&lt;"category", Category\[\]&gt;&gt;

## Exceptions

ClientApiError

30 changes: 30 additions & 0 deletions docs/landing/resources/api/shopware-6-client.getcategory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [getCategory](./shopware-6-client.getcategory.md)

## getCategory() function

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
export declare function getCategory(categoryId: string, contextInstance?: ShopwareApiInstance): Promise<Category>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| categoryId | string | |
| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | |

<b>Returns:</b>

Promise&lt;Category&gt;

## Exceptions

ClientApiError

2 changes: 2 additions & 0 deletions docs/landing/resources/api/shopware-6-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
| [getAvailableSalutations(contextInstance)](./shopware-6-client.getavailablesalutations.md) | <b><i>(BETA)</i></b> Get all available salutations |
| [getAvailableShippingMethods(contextInstance, params)](./shopware-6-client.getavailableshippingmethods.md) | <b><i>(BETA)</i></b> |
| [getCart(contextInstance)](./shopware-6-client.getcart.md) | <b><i>(BETA)</i></b> Gets the current cart for the sw-context-token. |
| [getCategories(searchCriteria, contextInstance)](./shopware-6-client.getcategories.md) | <b><i>(BETA)</i></b> |
| [getCategory(categoryId, contextInstance)](./shopware-6-client.getcategory.md) | <b><i>(BETA)</i></b> |
| [getCmsPage(path, criteria, contextInstance)](./shopware-6-client.getcmspage.md) | <b><i>(BETA)</i></b> |
| [getCustomer(contextInstance)](./shopware-6-client.getcustomer.md) | <b><i>(BETA)</i></b> Get customer's object |
| [getCustomerAddress(addressId, contextInstance)](./shopware-6-client.getcustomeraddress.md) | <b><i>(BETA)</i></b> Get the customer's address by id |
Expand Down
6 changes: 2 additions & 4 deletions packages/shopware-6-client/__tests__/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ describe("endpoints", () => {
describe("getCategoryEndpoint", () => {
it("should return Shopware category endpoint", async () => {
const result = getCategoryEndpoint();
expect(result).toEqual("/sales-channel-api/v3/category");
expect(result).toEqual("/store-api/category");
});
});

describe("getCategoryDetailsEndpoint", () => {
it("should return Shopware category details endpoint", async () => {
const result = getCategoryDetailsEndpoint(sampleCategoryId);
expect(result).toEqual(
"/sales-channel-api/v3/category/" + sampleCategoryId
);
expect(result).toEqual("/store-api/category/" + sampleCategoryId);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ describe("CategoryService - getCategories", () => {

const result = await getCategories();
expect(mockedPost).toBeCalledTimes(1);
expect(mockedPost).toBeCalledWith("/sales-channel-api/v3/category", {
limit: 10,
});
expect(mockedPost).toBeCalledWith("/store-api/category", undefined);
expect(result.total).toEqual(22);
});

it("should show deprecation info on this method", async () => {
mockedPost.mockResolvedValueOnce({ data: { total: 22 } });
await getCategories();
expect(consoleWarnSpy).toBeCalledWith(
'[DEPRECATED][@shopware-pwa/shopware-6-client][getCategories] This method has been deprecated. Use "getNavigation" instead.'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ describe("CategoryService - getCategory", () => {
it("should return chosen category - without associated products by default", async () => {
mockedGet.mockResolvedValueOnce({
data: {
data: {
id: "3a64e872ca404522a2c5d43ebc751e6b",
products: null,
},
id: "3a64e872ca404522a2c5d43ebc751e6b",
products: null,
},
});
const categoryId = "3a64e872ca404522a2c5d43ebc751e6b";
const result = await getCategory(categoryId);
expect(mockedGet).toBeCalledTimes(1);
expect(mockedGet).toBeCalledWith(
"/sales-channel-api/v3/category/3a64e872ca404522a2c5d43ebc751e6b"
"/store-api/category/3a64e872ca404522a2c5d43ebc751e6b"
);
expect(result).toHaveProperty("id");
expect(result.id).toEqual(categoryId);
Expand Down
4 changes: 2 additions & 2 deletions packages/shopware-6-client/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* @beta
*/
export const getCategoryEndpoint = () => `/sales-channel-api/v3/category`; // will become `/store-api/v4/category`
export const getCategoryEndpoint = () => `/store-api/category`;

/**
* @beta
*/
export const getCategoryDetailsEndpoint = (categoryId: string) =>
`/sales-channel-api/v3/category/${categoryId}`; // replace with `/store-api/v4/category with { ids: [ categoryId ] }`
`/store-api/category/${categoryId}`;

// product-listing

Expand Down
24 changes: 8 additions & 16 deletions packages/shopware-6-client/src/services/categoryService.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import { Category } from "@shopware-pwa/commons/interfaces/models/content/category/Category";
import { getCategoryEndpoint, getCategoryDetailsEndpoint } from "../endpoints";
import { convertSearchCriteria } from "../helpers/searchConverter";
import { SearchResult } from "@shopware-pwa/commons/interfaces/response/SearchResult";
import { EntityResult } from "@shopware-pwa/commons/interfaces/response/EntityResult";
import { ShopwareSearchParams } from "@shopware-pwa/commons/interfaces/search/SearchCriteria";
import { defaultInstance, ShopwareApiInstance } from "../apiService";
import { SearchCriteria } from "@shopware-pwa/commons/interfaces/search/SearchCriteria";
import { deprecationWarning } from "@shopware-pwa/commons";

/**
* @deprecated use {@link getStoreNavigation} method instead
* @throws ClientApiError
* @alpha
* @beta
*/
export async function getCategories(
searchCriteria?: SearchCriteria,
searchCriteria?: ShopwareSearchParams,
contextInstance: ShopwareApiInstance = defaultInstance
): Promise<SearchResult<Category[]>> {
deprecationWarning({
methodName: "getCategories",
newMethodName: "getNavigation",
packageName: "shopware-6-client",
});
): Promise<EntityResult<"category", Category[]>> {
const resp = await contextInstance.invoke.post(
getCategoryEndpoint(),
convertSearchCriteria({ searchCriteria, config: contextInstance.config })
searchCriteria
);

return resp.data;
}

/**
* @throws ClientApiError
* @alpha
* @beta
*/
export async function getCategory(
categoryId: string,
Expand All @@ -40,5 +32,5 @@ export async function getCategory(
getCategoryDetailsEndpoint(categoryId)
);

return resp.data.data;
return resp.data;
}

0 comments on commit 7eee960

Please sign in to comment.