-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34fa70f
commit d648acc
Showing
10 changed files
with
191 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/infrastructure/src/lib/serializers/cms-menu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { | ||
ButterCMSMenuItem, | ||
Category, | ||
Menu, | ||
MenuItem, | ||
Page, | ||
} from "@nimara/domain/objects/Menu"; | ||
|
||
import type { MenuGet_menu_Menu_items_MenuItem } from "#root/public/saleor/cms-menu/graphql/queries/generated"; | ||
|
||
const serializeSaleorMenuItem = ({ | ||
id, | ||
name, | ||
translation, | ||
level, | ||
url, | ||
category, | ||
collection, | ||
page, | ||
children, | ||
}: MenuGet_menu_Menu_items_MenuItem): MenuItem => { | ||
return { | ||
id, | ||
name: translation?.name || name, | ||
translation, | ||
level, | ||
url, | ||
category: category | ||
? { | ||
...category, | ||
name: category.translation?.name || category.name, | ||
} | ||
: null, | ||
collection: collection | ||
? { | ||
...collection, | ||
name: collection.translation?.name || collection.name, | ||
} | ||
: null, | ||
page: page | ||
? { | ||
...page, | ||
title: page.translation?.title || page.title, | ||
} | ||
: null, | ||
children, | ||
}; | ||
}; | ||
|
||
const serializeButterCMSMenuItem = (items: ButterCMSMenuItem[]): MenuItem[] => { | ||
return items.map((item) => { | ||
const categoryId = item.category.length | ||
? item.category[0].split("category[_id=")[1]?.split("]")[0] || "" | ||
: ""; | ||
|
||
const category: Category | null = categoryId | ||
? { | ||
id: categoryId, | ||
name: item.name, | ||
slug: item.name.toLowerCase(), | ||
translation: null, | ||
} | ||
: null; | ||
|
||
const pageSlug = | ||
typeof item.page === "string" | ||
? item.page.split("/").filter(Boolean).pop() || "" | ||
: ""; | ||
const page: Page | null = item.page | ||
? { | ||
id: item.meta.id.toString(), | ||
slug: pageSlug, | ||
title: item.name, | ||
translation: null, | ||
} | ||
: null; | ||
|
||
return { | ||
id: item.meta.id.toString(), | ||
name: item.name, | ||
translation: null, | ||
level: 0, | ||
url: item.url, | ||
category, | ||
collection: null, | ||
page, | ||
children: null, | ||
}; | ||
}); | ||
}; | ||
|
||
export const serializeMenu = ( | ||
items: MenuGet_menu_Menu_items_MenuItem[] | ButterCMSMenuItem[], | ||
source: "saleor" | "butterCms", | ||
): Menu => { | ||
if (source === "saleor") { | ||
return { | ||
items: (items as MenuGet_menu_Menu_items_MenuItem[]).map( | ||
serializeSaleorMenuItem, | ||
), | ||
}; | ||
} else if (source === "butterCms") { | ||
return { | ||
items: serializeButterCMSMenuItem(items as ButterCMSMenuItem[]), | ||
}; | ||
} | ||
|
||
return { items: [] }; | ||
}; |
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
packages/infrastructure/src/public/butter-cms/cms-menu/infrastructure/cms-menu-get-infra.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Butter from "buttercms/lib/butter"; | ||
|
||
import type { ButterCMSMenuItem } from "@nimara/domain/objects/Menu"; | ||
|
||
import { serializeMenu } from "#root/lib/serializers/cms-menu"; | ||
import { convertLanguageCode } from "#root/lib/serializers/cms-page"; | ||
import type { CMSMenuGetInfra } from "#root/use-cases/cms-menu/types"; | ||
|
||
import type { ButterCMSMenuServiceConfig } from "../types"; | ||
|
||
export const butterCMSMenuGetInfra = | ||
({ token }: ButterCMSMenuServiceConfig): CMSMenuGetInfra => | ||
async ({ languageCode, slug }) => { | ||
const locale = languageCode ? convertLanguageCode(languageCode) : undefined; | ||
|
||
// Using `as any` to bypass TypeScript's deep type inference issues with ButterCMS types. | ||
const menu = await (Butter(token).content as any).retrieve( | ||
["navigation_menu"], | ||
locale ? { locale } : undefined, | ||
); | ||
|
||
if (!menu?.data?.data?.navigation_menu) { | ||
return null; | ||
} | ||
|
||
const selectedMenu = menu.data.data.navigation_menu.find( | ||
(menu: ButterCMSMenuItem) => | ||
menu.name.toLowerCase() === slug?.toLowerCase(), | ||
); | ||
|
||
return { | ||
menu: serializeMenu( | ||
selectedMenu.menu_items as ButterCMSMenuItem[], | ||
"butterCms", | ||
), | ||
}; | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/infrastructure/src/public/butter-cms/cms-menu/providers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { cmsMenuGetUseCase } from "#root/use-cases/cms-menu/cms-menu-get-use-case"; | ||
import type { CMSMenuService } from "#root/use-cases/cms-menu/types"; | ||
|
||
import { butterCMSMenuGetInfra } from "./infrastructure/cms-menu-get-infra"; | ||
import type { ButterCMSMenuServiceConfig } from "./types"; | ||
|
||
export const butterCMSMenuService = (config: ButterCMSMenuServiceConfig) => | ||
({ | ||
menuGet: cmsMenuGetUseCase({ | ||
cmsMenuGetInfra: butterCMSMenuGetInfra(config), | ||
}), | ||
}) satisfies CMSMenuService; |
3 changes: 3 additions & 0 deletions
3
packages/infrastructure/src/public/butter-cms/cms-menu/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ButterCMSMenuServiceConfig = { | ||
token: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/infrastructure/src/public/saleor/cms-page/infrastructure/cms-page-get-infra.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters