Skip to content

Commit

Permalink
feat: created shopper common
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Oct 19, 2023
1 parent 2fdb8aa commit 8a2bf56
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 22 deletions.
45 changes: 45 additions & 0 deletions packages/shopper-common/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 2 additions & 0 deletions packages/shopper-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./products"
export * from "./shared"
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
2 changes: 2 additions & 0 deletions packages/shopper-common/src/products/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./util/create-bundle-configure-validator"
export * from "./bundle.types"
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe("validation-schema", () => {
}

const result = createBundleConfigureValidator(
bundleComponents as BundleComponents
bundleComponents as BundleComponents,
)(validData)

expect(result).toEqual({
Expand Down Expand Up @@ -134,7 +134,7 @@ describe("validation-schema", () => {
}

const result = createBundleConfigureValidator(
bundleComponents as BundleComponents
bundleComponents as BundleComponents,
)(invalidData)

expect(result).toEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BundleComponents,
BundleConfigurationSelectedOptions,
} from "@lib/product/bundle/bundle.types"
} from "../bundle.types"

interface ValidationEntryFailureResult {
name: string
Expand All @@ -28,13 +28,13 @@ type ValidationFailureResult = {
type ValidationResult<T> = ValidationSuccessResult<T> | ValidationFailureResult

export const createBundleConfigureValidator = (
bundleComponents: BundleComponents
bundleComponents: BundleComponents,
) => {
return function validate(
selectedOptions: BundleConfigurationSelectedOptions
selectedOptions: BundleConfigurationSelectedOptions,
): ValidationResult<BundleConfigurationSelectedOptions> {
const entryResults: ValidationEntryResult[] = Object.keys(
bundleComponents
bundleComponents,
).map((componentKey) => {
const componentSelectedOptions = selectedOptions[componentKey] ?? {}

Expand Down Expand Up @@ -64,15 +64,15 @@ export const createBundleConfigureValidator = (
}

function isEntryFailureResult(
entryResult: ValidationEntryResult
entryResult: ValidationEntryResult,
): entryResult is ValidationEntryFailureResult {
return !entryResult.success
}

export function validatePropertyCount(
obj: Record<string, any>,
minValue?: number,
maxValue?: number
maxValue?: number,
): ValidationEntryResult {
const propertyCount = Object.keys(obj).length

Expand Down
5 changes: 5 additions & 0 deletions packages/shopper-common/src/products/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./bundle"
export * from "./product.types"
export * from "./util"
export * from "./variation"
export * from "./services/product"
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShopperCatalogResource<ProductResponse>> {
return client.ShopperCatalog.Products.With([
"main_image",
Expand All @@ -22,15 +22,15 @@ export async function getProductById(

export async function getFilesByIds(
ids: string[],
client: EPCCClient
client: EPCCClient,
): Promise<ShopperCatalogResource<File[]>> {
return client.Files.Filter({ in: { id: ids } }).All()
}

export async function configureBundle(
productId: string,
selectedOptions: BundleConfigurationSelectedOptions,
client: EpccClient
client: EpccClient,
): Promise<BundleProduct["response"]> {
const response = await client.ShopperCatalog.Products.Configure({
productId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BundleProductResponse>,
Expand Down
1 change: 1 addition & 0 deletions packages/shopper-common/src/products/variation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./variation.types"
5 changes: 5 additions & 0 deletions packages/shopper-common/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions packages/shopper-common/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"outDir": "dist-cjs"
}
}
6 changes: 6 additions & 0 deletions packages/shopper-common/tsconfig.codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"module": "commonjs"
}
}
8 changes: 8 additions & 0 deletions packages/shopper-common/tsconfig.es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"lib": ["dom"],
"module": "esnext",
"outDir": "dist-es"
}
}
14 changes: 14 additions & 0 deletions packages/shopper-common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
10 changes: 10 additions & 0 deletions packages/shopper-common/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -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"],
}
11 changes: 11 additions & 0 deletions packages/shopper-common/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="vitest" />

// Configure Vitest (https://vitest.dev/config/)

import { defineConfig } from "vite"

export default defineConfig({
test: {
exclude: ["dist-*", "node_modules"],
},
})

0 comments on commit 8a2bf56

Please sign in to comment.