diff --git a/packages/storefront-shop-adapter-oxid/src/paths.ts b/packages/storefront-shop-adapter-oxid/src/paths.ts index 567d838..c2232fc 100644 --- a/packages/storefront-shop-adapter-oxid/src/paths.ts +++ b/packages/storefront-shop-adapter-oxid/src/paths.ts @@ -1,16 +1,14 @@ -export const USER_LOGIN = '/index.php?cl=MakairaUserController&fnc=login' -export const USER_GET_CURRENT = - '/index.php?cl=MakairaUserController&fnc=getCurrentLoggedInUser' -export const USER_LOGOUT = '/index.php?cl=MakairaUserController&fnc=logout' +import { OxidPaths } from './types' -export const CART_GET = '/index.php?cl=MakairaCartController&fnc=getCartItems' -export const CART_ADD = - '/index.php?cl=MakairaCartController&fnc=addProductToCart' -export const CART_REMOVE = - '/index.php?cl=MakairaCartController&fnc=removeCartItem' -export const CART_UPDATE = - '/index.php?cl=MakairaCartController&fnc=updateCartItem' - -export const REVIEW_GET = '/index.php?cl=MakairaReviewController&fnc=getReviews' -export const REVIEW_CREATE = - '/index.php?cl=MakairaReviewController&fnc=createReview' +export const PATHS: OxidPaths = { + USER_LOGIN: '/index.php?cl=MakairaUserController&fnc=login', + USER_GET_CURRENT: + '/index.php?cl=MakairaUserController&fnc=getCurrentLoggedInUser', + USER_LOGOUT: '/index.php?cl=MakairaUserController&fnc=logout', + CART_GET: '/index.php?cl=MakairaCartController&fnc=getCartItems', + CART_ADD: '/index.php?cl=MakairaCartController&fnc=addProductToCart', + CART_REMOVE: '/index.php?cl=MakairaCartController&fnc=removeCartItem', + CART_UPDATE: '/index.php?cl=MakairaCartController&fnc=updateCartItem', + REVIEW_GET: '/index.php?cl=MakairaReviewController&fnc=getReviews', + REVIEW_CREATE: '/index.php?cl=MakairaReviewController&fnc=createReview', +} diff --git a/packages/storefront-shop-adapter-oxid/src/providers/cart.ts b/packages/storefront-shop-adapter-oxid/src/providers/cart.ts index bd7d1e5..6568d8c 100755 --- a/packages/storefront-shop-adapter-oxid/src/providers/cart.ts +++ b/packages/storefront-shop-adapter-oxid/src/providers/cart.ts @@ -10,7 +10,6 @@ import { MakairaUpdateItemFromCart, } from '@makaira/storefront-types' import { StorefrontShopAdapterOxid } from './main' -import { CART_ADD, CART_GET, CART_REMOVE, CART_UPDATE } from '../paths' import { OxidAddItemRaw, OxidAddItemRes, @@ -29,7 +28,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: CART_GET, + path: this.mainAdapter.paths.CART_GET, }) if (status !== 200 || !Array.isArray(response)) { @@ -65,7 +64,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: CART_ADD, + path: this.mainAdapter.paths.CART_ADD, body: { product_id: product.id, amount: quantity, @@ -117,7 +116,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: CART_REMOVE, + path: this.mainAdapter.paths.CART_REMOVE, body: { cart_item_id: product.id, }, @@ -168,7 +167,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: CART_UPDATE, + path: this.mainAdapter.paths.CART_UPDATE, body: { cart_item_id: product.id, amount: quantity, diff --git a/packages/storefront-shop-adapter-oxid/src/providers/main.ts b/packages/storefront-shop-adapter-oxid/src/providers/main.ts index 6235309..f2b3947 100644 --- a/packages/storefront-shop-adapter-oxid/src/providers/main.ts +++ b/packages/storefront-shop-adapter-oxid/src/providers/main.ts @@ -13,8 +13,14 @@ import { StorefrontShopAdapterOxidUser } from './user' import { StorefrontShopAdapterOxidWishlist } from './wishlist' import fetch from 'isomorphic-unfetch' -import { AdditionalOxidOptions, FetchParameters, FetchResponse } from '../types' +import { + AdditionalOxidOptions, + FetchParameters, + FetchResponse, + OxidPaths, +} from '../types' import { StorefrontShopAdapterOxidReview } from './review' +import { PATHS } from '../paths' export class StorefrontShopAdapterOxid< CartProviderType extends MakairaShopProviderCart = StorefrontShopAdapterOxidCart, @@ -45,6 +51,8 @@ export class StorefrontShopAdapterOxid< additionalOptions: AdditionalOxidOptions + paths: OxidPaths + constructor( options: MakairaShopProviderOptions< CartProviderType, @@ -67,6 +75,12 @@ export class StorefrontShopAdapterOxid< this.additionalOptions = { url: options.url, + customPaths: options.customPaths, + } + + this.paths = { + ...PATHS, + ...this.additionalOptions.customPaths, } // @ts-expect-error https://stackoverflow.com/questions/56505560/how-to-fix-ts2322-could-be-instantiated-with-a-different-subtype-of-constraint @@ -89,9 +103,13 @@ export class StorefrontShopAdapterOxid< path, body = {}, }: FetchParameters): Promise> { - let requestUrl = this.additionalOptions.url + let requestUrl = this.additionalOptions.url ?? '' - if (!requestUrl?.endsWith('/') && !path.startsWith('/')) { + if ( + !requestUrl?.endsWith('/') && + !path.startsWith('/') && + !this.additionalOptions.url + ) { requestUrl += '/' } diff --git a/packages/storefront-shop-adapter-oxid/src/providers/review.ts b/packages/storefront-shop-adapter-oxid/src/providers/review.ts index 11d6532..a26f41b 100755 --- a/packages/storefront-shop-adapter-oxid/src/providers/review.ts +++ b/packages/storefront-shop-adapter-oxid/src/providers/review.ts @@ -7,7 +7,6 @@ import { ReviewCreateEvent, } from '@makaira/storefront-types' import { StorefrontShopAdapterOxid } from './main' -import { REVIEW_GET, REVIEW_CREATE } from '../paths' import { OxidCreateReviewRaw, OxidCreateReviewRes, @@ -31,7 +30,7 @@ export class StorefrontShopAdapterOxidReview try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: REVIEW_GET, + path: this.mainAdapter.paths.REVIEW_GET, body: { id: product.id, limit: paginationWithDefaults.limit, @@ -73,7 +72,7 @@ export class StorefrontShopAdapterOxidReview try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: REVIEW_CREATE, + path: this.mainAdapter.paths.REVIEW_CREATE, body: { product_id: review.product.id, rating: review.rating, diff --git a/packages/storefront-shop-adapter-oxid/src/providers/user.test.ts b/packages/storefront-shop-adapter-oxid/src/providers/user.test.ts index 480aad7..2f15fc4 100644 --- a/packages/storefront-shop-adapter-oxid/src/providers/user.test.ts +++ b/packages/storefront-shop-adapter-oxid/src/providers/user.test.ts @@ -1,12 +1,12 @@ import { rest } from 'msw' import { setupServer } from 'msw/node' import { StorefrontShopAdapterOxid } from './' -import { USER_GET_CURRENT, USER_LOGIN, USER_LOGOUT } from '../paths' import { BadHttpStatusError, MakairaResponse, NotImplementedError, } from '@makaira/storefront-types' +import { PATHS } from '../paths' const TARGET_HOST = 'https://example.com' // Will answer all requests with an error and correct error message @@ -24,11 +24,11 @@ const userSuccessServer = setupServer( const pathWithSearch = req.url.pathname + '?' + req.url.searchParams switch (pathWithSearch) { - case USER_GET_CURRENT: + case PATHS.USER_GET_CURRENT: return res( context.json({ ...USER_OBJECT, additionalParameter: 'test123' }) ) - case USER_LOGIN: { + case PATHS.USER_LOGIN: { const successful = typeof req.body === 'string' && JSON.parse(req.body).password === 'password123' @@ -39,7 +39,7 @@ const userSuccessServer = setupServer( return res(context.json(responseData)) } - case USER_LOGOUT: + case PATHS.USER_LOGOUT: return res(context.json({ success: true })) default: return res(context.status(500), context.json({})) @@ -49,11 +49,11 @@ const userSuccessServer = setupServer( const pathWithSearch = req.url.pathname + '?' + req.url.searchParams switch (pathWithSearch) { - case USER_LOGIN: + case PATHS.USER_LOGIN: return res(context.json({ success: true })) - case USER_GET_CURRENT: + case PATHS.USER_GET_CURRENT: return res(context.status(403), context.json({ message: 'Forbidden' })) - case USER_LOGOUT: + case PATHS.USER_LOGOUT: return res(context.status(400), context.json({})) default: return res(context.status(500), context.json({})) diff --git a/packages/storefront-shop-adapter-oxid/src/providers/user.ts b/packages/storefront-shop-adapter-oxid/src/providers/user.ts index 4addd61..5fd1029 100755 --- a/packages/storefront-shop-adapter-oxid/src/providers/user.ts +++ b/packages/storefront-shop-adapter-oxid/src/providers/user.ts @@ -11,7 +11,6 @@ import { UserLogoutEvent, } from '@makaira/storefront-types' import { StorefrontShopAdapterOxid } from './main' -import { USER_GET_CURRENT, USER_LOGIN, USER_LOGOUT } from '../paths' import { AdditionalInputLoginOxid, OxidGetUserRaw, @@ -27,12 +26,20 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser { constructor(private mainAdapter: StorefrontShopAdapterOxid) {} login: MakairaLogin = async ({ - input: { password, username, rememberLogin }, + input: { password, username, rememberLogin = false }, }) => { try { + console.log({ + body: { + password, + username, + rememberLogin, + }, + }) + const { response, status } = await this.mainAdapter.fetchFromShop({ - path: USER_LOGIN, + path: this.mainAdapter.paths.USER_LOGIN, body: { password, username, @@ -88,7 +95,7 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: USER_LOGOUT, + path: this.mainAdapter.paths.USER_LOGOUT, }) if (status !== 200) { @@ -120,7 +127,7 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser { try { const { response, status } = await this.mainAdapter.fetchFromShop({ - path: USER_GET_CURRENT, + path: this.mainAdapter.paths.USER_GET_CURRENT, }) // oxid returns an 403 if no user is logged in. Therefore diff --git a/packages/storefront-shop-adapter-oxid/src/types.ts b/packages/storefront-shop-adapter-oxid/src/types.ts index f321b22..ad80a8d 100755 --- a/packages/storefront-shop-adapter-oxid/src/types.ts +++ b/packages/storefront-shop-adapter-oxid/src/types.ts @@ -143,7 +143,8 @@ export type AdditionalInputLoginOxid = { } export type AdditionalOxidOptions = { - url: string + url?: string + customPaths?: Partial } export type FetchParameters = { @@ -155,3 +156,15 @@ export type FetchResponse = { response: Response status: number } + +export type OxidPaths = { + USER_LOGIN: string + USER_GET_CURRENT: string + USER_LOGOUT: string + CART_GET: string + CART_ADD: string + CART_REMOVE: string + CART_UPDATE: string + REVIEW_GET: string + REVIEW_CREATE: string +}