Skip to content

Commit

Permalink
feat(api-client): add missing endpoints and resolvers (#453)
Browse files Browse the repository at this point in the history
* fix(api): add CMS endpoint

* fix(api): add search category endpoint

* fix(api): add search category endpoint

* fix(api): add search category endpoint
  • Loading branch information
mdanilowicz authored Nov 7, 2023
1 parent 6c2e301 commit f5adaeb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-cheetahs-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

Add category advanced search method
5 changes: 5 additions & 0 deletions .changeset/lemon-doors-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/api-client": minor
---

Add cms endpoint
9 changes: 9 additions & 0 deletions packages/api-client/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,12 @@ export const getOrderDownloadsEndpoint = (
* @public
*/
export const getSitemapEndpoint = () => `/store-api/sitemap`;

/**
* @nolink
* @category Endpoints
* @public
* @param {string} pageId
* @returns {string}
*/
export const getCmsEndpoint = (pageId: string) => `/store-api/cms/${pageId}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getSwCmsPage } from "../pageService";
import { defaultInstance } from "../../apiService";
import { describe, expect, it, beforeEach, vi } from "vitest";

vi.mock("../../../src/apiService");
const mockedApiInstance = defaultInstance;

describe("PageService - getSwCmsPage", () => {
const mockedPost = vi.fn();
beforeEach(() => {
vi.resetAllMocks();
mockedApiInstance.invoke = {
post: mockedPost,
} as any;
});

it("should return getLandingPage for given id", async () => {
mockedPost.mockResolvedValueOnce({});
const result = await getSwCmsPage("idtest8888");
expect(mockedPost).toBeCalledTimes(1);
expect(mockedPost).toBeCalledWith("/store-api/cms/idtest8888");
});
});
16 changes: 16 additions & 0 deletions packages/api-client/src/services/pageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getLandingPageDetailsEndpoint,
getPageResolverEndpoint,
getSeoUrlEndpoint,
getCmsEndpoint,
} from "../endpoints";
import { defaultInstance, ShopwareApiInstance } from "../apiService";
import type {
Expand Down Expand Up @@ -114,3 +115,18 @@ export async function getSeoUrl(

return seoUrlResponse.data;
}

/**
* Returns Shopware CMS page data
*
* @param {string} pageId
* @param {ShopwareApiInstance} contextInstance
* @returns
*/
export async function getSwCmsPage(
pageId: string,
contextInstance: ShopwareApiInstance = defaultInstance,
) {
const resp = await contextInstance.invoke.post(getCmsEndpoint(pageId));
return resp.data;
}
30 changes: 30 additions & 0 deletions packages/composables/src/useCategorySearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
invokePost,
getCategoryEndpoint,
getCategoryDetailsEndpoint,
} from "@shopware-pwa/api-client";
import type { Category, ShopwareSearchParams } from "@shopware-pwa/types";
Expand All @@ -19,6 +20,14 @@ export type UseCategorySearchReturn = {
query?: Partial<ShopwareSearchParams>;
},
): Promise<Category>;

/**
* Search based on the query
*/
advancedSearch(options: {
withCmsAssociations?: boolean;
query: Partial<ShopwareSearchParams>;
}): Promise<Category[]>;
};

/**
Expand Down Expand Up @@ -47,7 +56,28 @@ export function useCategorySearch(): UseCategorySearchReturn {
return result.data;
}

async function advancedSearch(options: {
withCmsAssociations?: boolean;
query: Partial<ShopwareSearchParams>;
}) {
const associations = options?.withCmsAssociations
? cmsAssociations.associations
: {};
const result = await invokePost<Category[]>(
{
address: getCategoryEndpoint(),
payload: {
associations,
...options?.query,
},
},
apiInstance,
);
return result.data;
}

return {
search,
advancedSearch,
};
}

1 comment on commit f5adaeb

@vercel
Copy link

@vercel vercel bot commented on f5adaeb Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.