From 6a48709c61789d952150e2386507b480aa728c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Meyer?= Date: Thu, 7 Sep 2023 13:37:40 +0200 Subject: [PATCH] move criteria for seo url --- lib/shopware/api-extended.ts | 51 ++++++++++++++++++++++++++++-------- lib/shopware/api.ts | 31 +++------------------- lib/shopware/criteria.ts | 25 ++++++++++++++++++ lib/shopware/index.ts | 4 ++- lib/shopware/types.ts | 6 ++--- 5 files changed, 74 insertions(+), 43 deletions(-) diff --git a/lib/shopware/api-extended.ts b/lib/shopware/api-extended.ts index 97a2294d6..66913eea0 100644 --- a/lib/shopware/api-extended.ts +++ b/lib/shopware/api-extended.ts @@ -4,42 +4,45 @@ type schemas = components['schemas']; type operationsWithoutOriginal = Omit< operations, + | 'addLineItem' + | 'deleteLineItem' + | 'readCart' | 'readCategory' | 'readCategoryList' | 'readNavigation' | 'readProduct' | 'readProductCrossSellings' | 'readProductListing' + | 'readSeoUrl' | 'searchPage' - | 'addLineItem' - | 'readCart' - | 'deleteLineItem' >; export type extendedPaths = + | 'addLineItem post /checkout/cart/line-item' + | 'deleteLineItem delete /checkout/cart/line-item?id[]={ids}' + | 'readCart get /checkout/cart?name' | 'readCategory post /category/{navigationId}?slots' | 'readCategoryList post /category' | 'readNavigation post /navigation/{activeId}/{rootId} sw-include-seo-urls' | 'readProduct post /product' | 'readProductCrossSellings post /product/{productId}/cross-selling' | 'readProductListing post /product-listing/{categoryId}' + | 'readSeoUrl post /seo-url' | 'searchPage post /search' - | 'addLineItem post /checkout/cart/line-item' - | 'readCart get /checkout/cart?name' - | 'deleteLineItem delete /checkout/cart/line-item?id[]={ids}' | operationPaths; export type extendedOperations = operationsWithoutOriginal & { + addLineItem: extendedAddLineItem; + deleteLineItem: extendedDeleteLineItem; + readCart: extendedReadCart; readCategory: extendedReadCategory; readCategoryList: extendedReadCategoryList; readNavigation: extendedReadNavigation; readProduct: extendedReadProduct; readProductCrossSellings: extendedReadProductCrossSellings; readProductListing: extendedReadProductListing; + readSeoUrl: extendedReadSeoUrl; searchPage: extendedSearchPage; - addLineItem: extendedAddLineItem; - readCart: extendedReadCart; - deleteLineItem: extendedDeleteLineItem; }; export type messageKeys = @@ -133,11 +136,18 @@ export type ExtendedCategory = Omit & { filter?: { - field: string; + field?: string; type: string; - value: string | boolean | null; + value?: string | boolean | null; + queries?: ExtendedCriteriaFilter[]; }[]; }; @@ -359,6 +369,25 @@ type extendedReadProductListing = { }; }; +type extendedReadSeoUrl = { + requestBody?: { + content: { + 'application/json': ExtendedCriteria; + }; + }; + responses: { + /** Entity search result containing seo urls. */ + 200: { + content: { + 'application/json': { + elements?: components['schemas']['SeoUrl'][]; + } & components['schemas']['EntitySearchResult']; + }; + }; + 404: components['responses']['404']; + }; +}; + type extendedCartItems = components['schemas']['ArrayStruct'] & { items?: Partial[]; }; diff --git a/lib/shopware/api.ts b/lib/shopware/api.ts index 5c14e091d..64f57d759 100644 --- a/lib/shopware/api.ts +++ b/lib/shopware/api.ts @@ -64,7 +64,7 @@ export async function requestCategory( try { return await getApiClient().invoke('readCategory post /category/{navigationId}?slots', { navigationId: categoryId, - criteria + ...criteria }); } catch (error) { if (error instanceof ApiClientError) { @@ -167,35 +167,10 @@ export async function requestSeoUrls(routeName: RouteNames, page: number = 1, li } export async function requestSeoUrl( - handle: string, - page: number = 1, - limit: number = 1 + criteria: Partial ): Promise { try { - const criteriaSeoUrls = { - page: page, - limit: limit, - filter: [ - { - type: 'multi', - operator: 'or', - queries: [ - { - type: 'equals', - field: 'seoPathInfo', - value: handle + '/' - }, - { - type: 'equals', - field: 'seoPathInfo', - value: handle - } - ] - } - ] - }; - // @ts-ignore - return await getApiClient().invoke('readSeoUrl post /seo-url', criteriaSeoUrls); + return await getApiClient().invoke('readSeoUrl post /seo-url', criteria); } catch (error) { if (error instanceof ApiClientError) { console.error(error); diff --git a/lib/shopware/criteria.ts b/lib/shopware/criteria.ts index 0de5b1007..82dab1d6f 100644 --- a/lib/shopware/criteria.ts +++ b/lib/shopware/criteria.ts @@ -214,6 +214,31 @@ export function getDefaultCrossSellingCriteria(page: number = 1, limit: number = }; } +export function getSeoUrlCriteria(handle: string, page: number = 1, limit: number = 1) { + return { + page: page, + limit: limit, + filter: [ + { + type: 'multi', + operator: 'or', + queries: [ + { + type: 'equals', + field: 'seoPathInfo', + value: handle + '/' + }, + { + type: 'equals', + field: 'seoPathInfo', + value: handle + } + ] + } + ] + }; +} + export function getSortingCriteria(sortKey?: string, reverse?: boolean) { switch (true) { case sortKey === 'CREATED_AT' && reverse === true: diff --git a/lib/shopware/index.ts b/lib/shopware/index.ts index 21a4b0384..3b1b67a3f 100644 --- a/lib/shopware/index.ts +++ b/lib/shopware/index.ts @@ -22,6 +22,7 @@ import { getDefaultProductsCriteria, getDefaultSearchProductsCriteria, getDefaultSubCategoriesCriteria, + getSeoUrlCriteria, getSortingCriteria } from './criteria'; import { @@ -81,7 +82,8 @@ export async function getPage(handle: string | []): Promise { export async function getFirstSeoUrlElement( handle: string ): Promise { - const seoURL = await requestSeoUrl(handle); + const seoURLCriteria = getSeoUrlCriteria(handle); + const seoURL = await requestSeoUrl(seoURLCriteria); if (seoURL && seoURL.elements && seoURL.elements.length > 0 && seoURL.elements[0]) { return seoURL.elements[0]; } diff --git a/lib/shopware/types.ts b/lib/shopware/types.ts index d91f58b77..ead9ed77d 100644 --- a/lib/shopware/types.ts +++ b/lib/shopware/types.ts @@ -11,10 +11,10 @@ export type ProductListingCriteria = { } & Omit & ExtendedCriteria; export type RouteNames = - | 'frontend.navigation.page' - | 'frontend.detail.page' | 'frontend.account.customer-group-registration.page' - | 'frontend.landing.page'; + | 'frontend.detail.page' + | 'frontend.landing.page' + | 'frontend.navigation.page'; /** Return Types */ export type CategoryListingResultSW = {