Skip to content

Commit

Permalink
Merge pull request #91 from MakairaIO/oxid-custom-paths-and-relativ-url
Browse files Browse the repository at this point in the history
Add option for custom paths and relative URLs in Oxid-Shop-Adapter
  • Loading branch information
Jan authored Jan 25, 2023
2 parents 14f6829 + bb75600 commit 8c8f639
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 39 deletions.
28 changes: 13 additions & 15 deletions packages/storefront-shop-adapter-oxid/src/paths.ts
Original file line number Diff line number Diff line change
@@ -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',
}
9 changes: 4 additions & 5 deletions packages/storefront-shop-adapter-oxid/src/providers/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,7 +28,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidGetCartRes>({
path: CART_GET,
path: this.mainAdapter.paths.CART_GET,
})

if (status !== 200 || !Array.isArray(response)) {
Expand Down Expand Up @@ -65,7 +64,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidAddItemRes>({
path: CART_ADD,
path: this.mainAdapter.paths.CART_ADD,
body: {
product_id: product.id,
amount: quantity,
Expand Down Expand Up @@ -117,7 +116,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidRemoveItemRes>({
path: CART_REMOVE,
path: this.mainAdapter.paths.CART_REMOVE,
body: {
cart_item_id: product.id,
},
Expand Down Expand Up @@ -168,7 +167,7 @@ export class StorefrontShopAdapterOxidCart implements MakairaShopProviderCart {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidUpdateItemRes>({
path: CART_UPDATE,
path: this.mainAdapter.paths.CART_UPDATE,
body: {
cart_item_id: product.id,
amount: quantity,
Expand Down
24 changes: 21 additions & 3 deletions packages/storefront-shop-adapter-oxid/src/providers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,6 +51,8 @@ export class StorefrontShopAdapterOxid<

additionalOptions: AdditionalOxidOptions

paths: OxidPaths

constructor(
options: MakairaShopProviderOptions<
CartProviderType,
Expand All @@ -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
Expand All @@ -89,9 +103,13 @@ export class StorefrontShopAdapterOxid<
path,
body = {},
}: FetchParameters): Promise<FetchResponse<Response>> {
let requestUrl = this.additionalOptions.url
let requestUrl = this.additionalOptions.url ?? ''

if (!requestUrl?.endsWith('/') && !path.startsWith('/')) {
if (
!requestUrl?.endsWith('/') &&
!path.startsWith('/') &&
!this.additionalOptions.url
) {
requestUrl += '/'
}

Expand Down
5 changes: 2 additions & 3 deletions packages/storefront-shop-adapter-oxid/src/providers/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ReviewCreateEvent,
} from '@makaira/storefront-types'
import { StorefrontShopAdapterOxid } from './main'
import { REVIEW_GET, REVIEW_CREATE } from '../paths'
import {
OxidCreateReviewRaw,
OxidCreateReviewRes,
Expand All @@ -31,7 +30,7 @@ export class StorefrontShopAdapterOxidReview
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidGetReviewsRes>({
path: REVIEW_GET,
path: this.mainAdapter.paths.REVIEW_GET,
body: {
id: product.id,
limit: paginationWithDefaults.limit,
Expand Down Expand Up @@ -73,7 +72,7 @@ export class StorefrontShopAdapterOxidReview
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidCreateReviewRes>({
path: REVIEW_CREATE,
path: this.mainAdapter.paths.REVIEW_CREATE,
body: {
product_id: review.product.id,
rating: review.rating,
Expand Down
14 changes: 7 additions & 7 deletions packages/storefront-shop-adapter-oxid/src/providers/user.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand All @@ -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({}))
Expand All @@ -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({}))
Expand Down
17 changes: 12 additions & 5 deletions packages/storefront-shop-adapter-oxid/src/providers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,12 +26,20 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser {
constructor(private mainAdapter: StorefrontShopAdapterOxid) {}

login: MakairaLogin<AdditionalInputLoginOxid, OxidLoginRaw, Error> = async ({
input: { password, username, rememberLogin },
input: { password, username, rememberLogin = false },
}) => {
try {
console.log({
body: {
password,
username,
rememberLogin,
},
})

const { response, status } =
await this.mainAdapter.fetchFromShop<OxidLoginRes>({
path: USER_LOGIN,
path: this.mainAdapter.paths.USER_LOGIN,
body: {
password,
username,
Expand Down Expand Up @@ -88,7 +95,7 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidLogoutRes>({
path: USER_LOGOUT,
path: this.mainAdapter.paths.USER_LOGOUT,
})

if (status !== 200) {
Expand Down Expand Up @@ -120,7 +127,7 @@ export class StorefrontShopAdapterOxidUser implements MakairaShopProviderUser {
try {
const { response, status } =
await this.mainAdapter.fetchFromShop<OxidGetUserRes>({
path: USER_GET_CURRENT,
path: this.mainAdapter.paths.USER_GET_CURRENT,
})

// oxid returns an 403 if no user is logged in. Therefore
Expand Down
15 changes: 14 additions & 1 deletion packages/storefront-shop-adapter-oxid/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export type AdditionalInputLoginOxid = {
}

export type AdditionalOxidOptions = {
url: string
url?: string
customPaths?: Partial<OxidPaths>
}

export type FetchParameters = {
Expand All @@ -155,3 +156,15 @@ export type FetchResponse<Response = any> = {
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
}

0 comments on commit 8c8f639

Please sign in to comment.