diff --git a/packages/shopper-common/package.json b/packages/shopper-common/package.json new file mode 100644 index 00000000..83198aa5 --- /dev/null +++ b/packages/shopper-common/package.json @@ -0,0 +1,45 @@ +{ + "name": "@elasticpath/shopper-common", + "version": "0.0.0", + "main": "./dist-cjs/index.js", + "types": "./dist-types/index.d.ts", + "module": "./dist-es/index.js", + "sideEffects": false, + "license": "MIT", + "scripts": { + "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:es": "tsc -p tsconfig.es.json", + "build:types": "tsc -p tsconfig.types.json", + "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rimraf ./dist-* && rimraf *.tsbuildinfo", + "test": "vitest run" + }, + "exports": { + ".": { + "types": "./dist-types/index.d.ts", + "node": { + "import": "./dist-es/index.js", + "require": "./dist-cjs/index.js" + }, + "default": "./dist-es/index.js" + }, + "./package.json": "./package.json" + }, + "files": [ + "dist-*" + ], + "devDependencies": { + "@tsconfig/node14": "^1.0.3", + "concurrently": "^7.6.0", + "eslint": "^8.35.0", + "typescript": "^4.9.5", + "vite": "^4.2.1", + "vitest": "^0.29.7" + }, + "dependencies": { + "@moltin/sdk": "^25.0.2" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/shopper-common/src/index.ts b/packages/shopper-common/src/index.ts new file mode 100644 index 00000000..e7c7e32f --- /dev/null +++ b/packages/shopper-common/src/index.ts @@ -0,0 +1,2 @@ +export * from "./products" +export * from "./shared" \ No newline at end of file diff --git a/packages/react-shopper-hooks/lib/product/bundle/bundle.types.ts b/packages/shopper-common/src/products/bundle/bundle.types.ts similarity index 88% rename from packages/react-shopper-hooks/lib/product/bundle/bundle.types.ts rename to packages/shopper-common/src/products/bundle/bundle.types.ts index a813ca3f..1f22ed34 100644 --- a/packages/react-shopper-hooks/lib/product/bundle/bundle.types.ts +++ b/packages/shopper-common/src/products/bundle/bundle.types.ts @@ -1,4 +1,4 @@ -import type { BundleProduct } from "@lib/product/product.types" +import type { BundleProduct } from "../product.types" import type { ProductResponse } from "@moltin/sdk" export type BundleComponents = diff --git a/packages/shopper-common/src/products/bundle/index.ts b/packages/shopper-common/src/products/bundle/index.ts new file mode 100644 index 00000000..d6641a7f --- /dev/null +++ b/packages/shopper-common/src/products/bundle/index.ts @@ -0,0 +1,2 @@ +export * from "./util/create-bundle-configure-validator" +export * from "./bundle.types" diff --git a/packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.test.ts b/packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.test.ts similarity index 95% rename from packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.test.ts rename to packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.test.ts index 0b736c69..d6ca2c7f 100644 --- a/packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.test.ts +++ b/packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.test.ts @@ -2,12 +2,12 @@ import { describe, expect, test } from "vitest" import { BundleComponents, BundleConfigurationSelectedOptions, -} from "@lib/product/bundle/bundle.types" +} from "../bundle.types" import { createBundleConfigureValidator, validatePropertyCount, -} from "@lib/product/bundle/util/create-bundle-configure-validator" -import { DeepPartial } from "@lib/shared/types/deep-partial" +} from "./create-bundle-configure-validator" +import { DeepPartial } from "../../../shared/types/deep-partial" describe("validation-schema", () => { test("createBundleConfigureValidator valid", () => { @@ -67,7 +67,7 @@ describe("validation-schema", () => { } const result = createBundleConfigureValidator( - bundleComponents as BundleComponents + bundleComponents as BundleComponents, )(validData) expect(result).toEqual({ @@ -134,7 +134,7 @@ describe("validation-schema", () => { } const result = createBundleConfigureValidator( - bundleComponents as BundleComponents + bundleComponents as BundleComponents, )(invalidData) expect(result).toEqual({ diff --git a/packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.ts b/packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.ts similarity index 91% rename from packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.ts rename to packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.ts index c1c57415..e5224b4f 100644 --- a/packages/react-shopper-hooks/lib/product/bundle/util/create-bundle-configure-validator.ts +++ b/packages/shopper-common/src/products/bundle/util/create-bundle-configure-validator.ts @@ -1,7 +1,7 @@ import { BundleComponents, BundleConfigurationSelectedOptions, -} from "@lib/product/bundle/bundle.types" +} from "../bundle.types" interface ValidationEntryFailureResult { name: string @@ -28,13 +28,13 @@ type ValidationFailureResult = { type ValidationResult = ValidationSuccessResult | ValidationFailureResult export const createBundleConfigureValidator = ( - bundleComponents: BundleComponents + bundleComponents: BundleComponents, ) => { return function validate( - selectedOptions: BundleConfigurationSelectedOptions + selectedOptions: BundleConfigurationSelectedOptions, ): ValidationResult { const entryResults: ValidationEntryResult[] = Object.keys( - bundleComponents + bundleComponents, ).map((componentKey) => { const componentSelectedOptions = selectedOptions[componentKey] ?? {} @@ -64,7 +64,7 @@ export const createBundleConfigureValidator = ( } function isEntryFailureResult( - entryResult: ValidationEntryResult + entryResult: ValidationEntryResult, ): entryResult is ValidationEntryFailureResult { return !entryResult.success } @@ -72,7 +72,7 @@ function isEntryFailureResult( export function validatePropertyCount( obj: Record, minValue?: number, - maxValue?: number + maxValue?: number, ): ValidationEntryResult { const propertyCount = Object.keys(obj).length diff --git a/packages/shopper-common/src/products/index.ts b/packages/shopper-common/src/products/index.ts new file mode 100644 index 00000000..2c510777 --- /dev/null +++ b/packages/shopper-common/src/products/index.ts @@ -0,0 +1,5 @@ +export * from "./bundle" +export * from "./product.types" +export * from "./util" +export * from "./variation" +export * from "./services/product" diff --git a/packages/react-shopper-hooks/lib/product/product.types.ts b/packages/shopper-common/src/products/product.types.ts similarity index 90% rename from packages/react-shopper-hooks/lib/product/product.types.ts rename to packages/shopper-common/src/products/product.types.ts index 91d661b7..0ef30dfc 100644 --- a/packages/react-shopper-hooks/lib/product/product.types.ts +++ b/packages/shopper-common/src/products/product.types.ts @@ -1,10 +1,10 @@ -import type { DeepOmit, UnDot } from "@lib/shared/types/deep-omit" +import type { DeepOmit, UnDot } from "../shared/types/deep-omit" import type { CatalogsProductVariation, ProductResponse, File, } from "@moltin/sdk" -import type { MatrixObjectEntry } from "@lib/shared/types/matrix-object-entry" +import type { MatrixObjectEntry } from "../shared/types/matrix-object-entry" export interface ProductBase { main_image: File | null diff --git a/packages/react-shopper-hooks/lib/product/services/product.ts b/packages/shopper-common/src/products/services/product.ts similarity index 92% rename from packages/react-shopper-hooks/lib/product/services/product.ts rename to packages/shopper-common/src/products/services/product.ts index 4319111d..3fb804a2 100644 --- a/packages/react-shopper-hooks/lib/product/services/product.ts +++ b/packages/shopper-common/src/products/services/product.ts @@ -5,11 +5,11 @@ import type { File, } from "@moltin/sdk" import { Moltin as EpccClient } from "@moltin/sdk" -import { BundleConfigurationSelectedOptions, BundleProduct } from "@lib/product" +import { BundleConfigurationSelectedOptions, BundleProduct } from "../" export async function getProductById( productId: string, - client: EPCCClient + client: EPCCClient, ): Promise> { return client.ShopperCatalog.Products.With([ "main_image", @@ -22,7 +22,7 @@ export async function getProductById( export async function getFilesByIds( ids: string[], - client: EPCCClient + client: EPCCClient, ): Promise> { return client.Files.Filter({ in: { id: ids } }).All() } @@ -30,7 +30,7 @@ export async function getFilesByIds( export async function configureBundle( productId: string, selectedOptions: BundleConfigurationSelectedOptions, - client: EpccClient + client: EpccClient, ): Promise { const response = await client.ShopperCatalog.Products.Configure({ productId, diff --git a/packages/react-shopper-hooks/lib/product/util/index.ts b/packages/shopper-common/src/products/util/index.ts similarity index 100% rename from packages/react-shopper-hooks/lib/product/util/index.ts rename to packages/shopper-common/src/products/util/index.ts diff --git a/packages/react-shopper-hooks/lib/product/util/product-image-helpers.test.ts b/packages/shopper-common/src/products/util/product-image-helpers.test.ts similarity index 99% rename from packages/react-shopper-hooks/lib/product/util/product-image-helpers.test.ts rename to packages/shopper-common/src/products/util/product-image-helpers.test.ts index 870f0a10..246c69bc 100644 --- a/packages/react-shopper-hooks/lib/product/util/product-image-helpers.test.ts +++ b/packages/shopper-common/src/products/util/product-image-helpers.test.ts @@ -4,7 +4,7 @@ import { getProductMainImage, getProductOtherImageUrls, processImageFiles, -} from "@lib/product/util/product-image-helpers" +} from "./product-image-helpers" describe("product image helper", () => { describe("unit tests", () => { diff --git a/packages/react-shopper-hooks/lib/product/util/product-image-helpers.ts b/packages/shopper-common/src/products/util/product-image-helpers.ts similarity index 100% rename from packages/react-shopper-hooks/lib/product/util/product-image-helpers.ts rename to packages/shopper-common/src/products/util/product-image-helpers.ts diff --git a/packages/react-shopper-hooks/lib/product/util/shopper-product-helpers.ts b/packages/shopper-common/src/products/util/shopper-product-helpers.ts similarity index 97% rename from packages/react-shopper-hooks/lib/product/util/shopper-product-helpers.ts rename to packages/shopper-common/src/products/util/shopper-product-helpers.ts index cb2d29a4..06425f3a 100644 --- a/packages/react-shopper-hooks/lib/product/util/shopper-product-helpers.ts +++ b/packages/shopper-common/src/products/util/shopper-product-helpers.ts @@ -11,12 +11,12 @@ import { SimpleProduct, SimpleProductResponse, BundleProduct, -} from "@lib/product" +} from "../" import { getProductMainImage, getProductOtherImageUrls, -} from "@lib/product/util/product-image-helpers" -import { getFilesByIds, getProductById } from "@lib/product/services/product" +} from "./product-image-helpers" +import { getFilesByIds, getProductById } from "../services/product" export async function createShopperBundleProduct( productResource: ShopperCatalogResource, diff --git a/packages/react-shopper-hooks/lib/product/util/sort-alphabetically.ts b/packages/shopper-common/src/products/util/sort-alphabetically.ts similarity index 100% rename from packages/react-shopper-hooks/lib/product/util/sort-alphabetically.ts rename to packages/shopper-common/src/products/util/sort-alphabetically.ts diff --git a/packages/shopper-common/src/products/variation/index.ts b/packages/shopper-common/src/products/variation/index.ts new file mode 100644 index 00000000..b9c00a5f --- /dev/null +++ b/packages/shopper-common/src/products/variation/index.ts @@ -0,0 +1 @@ +export * from "./variation.types" diff --git a/packages/react-shopper-hooks/lib/product/variation/variation.types.ts b/packages/shopper-common/src/products/variation/variation.types.ts similarity index 100% rename from packages/react-shopper-hooks/lib/product/variation/variation.types.ts rename to packages/shopper-common/src/products/variation/variation.types.ts diff --git a/packages/shopper-common/src/shared/index.ts b/packages/shopper-common/src/shared/index.ts new file mode 100644 index 00000000..f8d0c587 --- /dev/null +++ b/packages/shopper-common/src/shared/index.ts @@ -0,0 +1,5 @@ +export * from "./types/deep-omit" +export * from "./types/deep-partial" +export * from "./types/deep-read-only" +export * from "./types/read-only-non-empty-array" +export * from "./types/matrix-object-entry" diff --git a/packages/react-shopper-hooks/lib/shared/types/deep-omit.ts b/packages/shopper-common/src/shared/types/deep-omit.ts similarity index 100% rename from packages/react-shopper-hooks/lib/shared/types/deep-omit.ts rename to packages/shopper-common/src/shared/types/deep-omit.ts diff --git a/packages/react-shopper-hooks/lib/shared/types/deep-partial.ts b/packages/shopper-common/src/shared/types/deep-partial.ts similarity index 100% rename from packages/react-shopper-hooks/lib/shared/types/deep-partial.ts rename to packages/shopper-common/src/shared/types/deep-partial.ts diff --git a/packages/react-shopper-hooks/lib/shared/types/deep-read-only.ts b/packages/shopper-common/src/shared/types/deep-read-only.ts similarity index 100% rename from packages/react-shopper-hooks/lib/shared/types/deep-read-only.ts rename to packages/shopper-common/src/shared/types/deep-read-only.ts diff --git a/packages/react-shopper-hooks/lib/shared/types/matrix-object-entry.ts b/packages/shopper-common/src/shared/types/matrix-object-entry.ts similarity index 100% rename from packages/react-shopper-hooks/lib/shared/types/matrix-object-entry.ts rename to packages/shopper-common/src/shared/types/matrix-object-entry.ts diff --git a/packages/react-shopper-hooks/lib/shared/types/read-only-non-empty-array.ts b/packages/shopper-common/src/shared/types/read-only-non-empty-array.ts similarity index 100% rename from packages/react-shopper-hooks/lib/shared/types/read-only-non-empty-array.ts rename to packages/shopper-common/src/shared/types/read-only-non-empty-array.ts diff --git a/packages/shopper-common/tsconfig.cjs.json b/packages/shopper-common/tsconfig.cjs.json new file mode 100644 index 00000000..3567d85b --- /dev/null +++ b/packages/shopper-common/tsconfig.cjs.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "outDir": "dist-cjs" + } +} diff --git a/packages/shopper-common/tsconfig.codegen.json b/packages/shopper-common/tsconfig.codegen.json new file mode 100644 index 00000000..70262b0d --- /dev/null +++ b/packages/shopper-common/tsconfig.codegen.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "module": "commonjs" + } +} diff --git a/packages/shopper-common/tsconfig.es.json b/packages/shopper-common/tsconfig.es.json new file mode 100644 index 00000000..809f57bd --- /dev/null +++ b/packages/shopper-common/tsconfig.es.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "lib": ["dom"], + "module": "esnext", + "outDir": "dist-es" + } +} diff --git a/packages/shopper-common/tsconfig.json b/packages/shopper-common/tsconfig.json new file mode 100644 index 00000000..5ff41f74 --- /dev/null +++ b/packages/shopper-common/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@tsconfig/node14/tsconfig.json", + "compilerOptions": { + "downlevelIteration": true, + "importHelpers": true, + "incremental": true, + "removeComments": true, + "resolveJsonModule": true, + "rootDir": "src", + "useUnknownInCatchVariables": false, + "noImplicitAny": false + }, + "exclude": ["./**/*.test.ts", "./codegen.ts", "./vite.config.ts"] +} diff --git a/packages/shopper-common/tsconfig.types.json b/packages/shopper-common/tsconfig.types.json new file mode 100644 index 00000000..bfb5d875 --- /dev/null +++ b/packages/shopper-common/tsconfig.types.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "removeComments": false, + "declaration": true, + "declarationDir": "dist-types", + "emitDeclarationOnly": true + }, + "exclude": ["test/**/*", "dist-types/**/*", "codegen.ts", "./vite.config.ts"], +} diff --git a/packages/shopper-common/vite.config.ts b/packages/shopper-common/vite.config.ts new file mode 100644 index 00000000..9eeef473 --- /dev/null +++ b/packages/shopper-common/vite.config.ts @@ -0,0 +1,11 @@ +/// + +// Configure Vitest (https://vitest.dev/config/) + +import { defineConfig } from "vite" + +export default defineConfig({ + test: { + exclude: ["dist-*", "node_modules"], + }, +})