Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"editor.formatOnSave": true,
"javascript.format.enable": true,
"eslint.workingDirectories": [
"packages/react-components",
"packages/docs",
],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"css.validate": false,
"less.validate": false,
"scss.validate": false,
"editor.defaultFormatter": "biomejs.biome",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
"quickfix.biome": "explicit",
"source.organizeImports.biome": "explicit"
}
}
}
58 changes: 29 additions & 29 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons":"asNeeded"
}
}
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons": "asNeeded"
}
}
}
95 changes: 50 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
{
"name": "root",
"private": true,
"author": {
"name": "Alessandro Casazza",
"email": "[email protected]"
},
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "pnpm -r build",
"prepare": "husky",
"test": "pnpm -r test",
"docs:dev": "pnpm --filter docs storybook",
"docs:build": "pnpm --filter docs build-storybook",
"components:build:dev": "pnpm --filter react-components build:dev",
"components:build": "pnpm --filter react-components build",
"make:version": "lerna version --no-private",
"make:version:force": "lerna version --no-private --force-publish",
"dep:major": "pnpm dlx npm-check-updates --packageFile '**/package.json' -u -i",
"dep:minor": "pnpm dep:major -t minor -i"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"husky": "^9.1.7",
"lerna": "^8.2.1",
"typescript": "^5.8.2"
},
"pnpm": {
"overrides": {
"postcss@<8.4.31": ">=8.4.31",
"graphql@>=16.3.0 <16.8.1": ">=16.8.1",
"vite@>=4.4.0 <4.4.12": ">=4.4.12",
"@babel/traverse@<7.23.2": ">=7.23.2",
"vite@>=4.0.0 <=4.5.1": ">=4.5.2",
"follow-redirects@<1.15.4": ">=1.15.4",
"express@<4.19.2": ">=4.19.2",
"vite@>=5.1.0 <=5.1.6": ">=5.1.7",
"tar@<6.2.1": ">=6.2.1",
"ejs@<3.1.10": ">=3.1.10",
"braces@<3.0.3": ">=3.0.3",
"ws@>=8.0.0 <8.17.1": ">=8.17.1",
"micromatch@<4.0.8": ">=4.0.8",
"rollup@>=4.0.0 <4.22.4": ">=4.22.4",
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5"
}
}
"name": "root",
"private": true,
"author": {
"name": "Alessandro Casazza",
"email": "[email protected]"
},
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "pnpm -r build",
"prepare": "husky",
"test": "pnpm -r test",
"docs:dev": "pnpm --filter docs storybook",
"docs:build": "pnpm --filter docs build-storybook",
"components:build:dev": "pnpm --filter react-components build:dev",
"components:build": "pnpm --filter react-components build",
"make:version": "lerna version --no-private",
"make:version:force": "lerna version --no-private --force-publish",
"dep:major": "pnpm dlx npm-check-updates --packageFile '**/package.json' -u -i",
"dep:minor": "pnpm dep:major -t minor -i"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"husky": "^9.1.7",
"lerna": "^8.2.2",
"typescript": "^5.8.3",
"vitest": "^3.1.1"
},
"pnpm": {
"overrides": {
"postcss@<8.4.31": ">=8.4.31",
"graphql@>=16.3.0 <16.8.1": ">=16.8.1",
"vite@>=4.4.0 <4.4.12": ">=4.4.12",
"@babel/traverse@<7.23.2": ">=7.23.2",
"vite@>=4.0.0 <=4.5.1": ">=4.5.2",
"follow-redirects@<1.15.4": ">=1.15.4",
"express@<4.19.2": ">=4.19.2",
"vite@>=5.1.0 <=5.1.6": ">=5.1.7",
"tar@<6.2.1": ">=6.2.1",
"ejs@<3.1.10": ">=3.1.10",
"braces@<3.0.3": ">=3.0.3",
"ws@>=8.0.0 <8.17.1": ">=8.17.1",
"micromatch@<4.0.8": ">=4.0.8",
"rollup@>=4.0.0 <4.22.4": ">=4.22.4",
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5"
},
"onlyBuiltDependencies": [
"msw",
"nx"
]
}
}
70 changes: 70 additions & 0 deletions packages/core/extender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { test } from "vitest"
import { getAccessToken } from "./src/auth/getAccessToken.js"

const clientId = import.meta.env.VITE_SALES_CHANNEL_CLIENT_ID
const integrationClientId = import.meta.env.VITE_INTEGRATION_CLIENT_ID
const integrationClientSecret = import.meta.env.VITE_INTEGRATION_CLIENT_SECRET
const scope = import.meta.env.VITE_SALES_CHANNEL_SCOPE
const domain = import.meta.env.VITE_DOMAIN
let accessToken: Awaited<ReturnType<typeof getAccessToken>> | undefined =
undefined

export interface CoreTestInterface {
accessToken: Awaited<ReturnType<typeof getAccessToken>>
config: {
clientId: string
scope?: string
domain: string
}
}

/**
* This test is used to run integration tests with the sales channel client.
*/
export const coreTest = test.extend<CoreTestInterface>({
// biome-ignore lint/correctness/noEmptyPattern: need to object destructure as the first argument
accessToken: async ({}, use) => {
if (accessToken == null) {
accessToken = await getAccessToken({
grantType: "client_credentials",
config: {
clientId,
scope,
domain,
},
})
}
use(accessToken)
accessToken = undefined
},
config: {
clientId,
scope,
domain,
},
})

/**
* This test is used to run integration tests with the integration client.
*/
export const coreIntegrationTest = test.extend<CoreTestInterface>({
// biome-ignore lint/correctness/noEmptyPattern: need to object destructure as the first argument
accessToken: async ({}, use) => {
if (accessToken == null) {
accessToken = await getAccessToken({
grantType: "client_credentials",
config: {
clientId: integrationClientId,
clientSecret: integrationClientSecret,
domain,
},
})
}
use(accessToken)
accessToken = undefined
},
config: {
clientId: integrationClientId,
domain,
},
})
45 changes: 45 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@commercelayer/core",
"version": "1.0.0",
"description": "Commerce Layer Components Core SDK",
"type": "module",
"main": "./dist/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/index.js",
"default": "./dist/index.cjs"
}
},
"keywords": ["jamstack", "headless", "ecommerce", "api", "components"],
"scripts": {
"check-exports": "attw --pack .",
"lint": "biome lint --error-on-warnings ./src && tsc",
"lint:fix": "pnpm biome lint --write ./src",
"test": "pnpm run lint && vitest --silent",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"build": "tsup",
"ci": "pnpm build && pnpm check-exports && pnpm lint"
},
"publishConfig": {
"access": "public"
},
"author": {
"name": "Alessandro Casazza",
"email": "[email protected]"
},
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.4",
"@vitest/coverage-v8": "^3.1.1",
"tsup": "^8.4.0",
"typescript": "^5.8.3",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.1.1"
},
"dependencies": {
"@commercelayer/js-auth": "^6.7.2",
"@commercelayer/sdk": "6.39.0"
}
}
40 changes: 40 additions & 0 deletions packages/core/src/auth/getAccessToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { authenticate } from "@commercelayer/js-auth"
import { describe, expect, vi } from "vitest"
import { coreTest } from "#extender"
import { getAccessToken } from "./getAccessToken"

vi.mock("@commercelayer/js-auth", () => ({
authenticate: vi.fn(),
}))

describe("getAccessToken", () => {
coreTest(
"should call authenticate with the correct parameters",
async ({ accessToken, config }) => {
const token = accessToken?.accessToken
const grantType = "client_credentials"
const mockToken = { accessToken: token }
// @ts-expect-error No types for this function
authenticate.mockResolvedValue(mockToken)
const result = await getAccessToken({ grantType, config })
await expect(authenticate).toHaveBeenCalledWith(grantType, config)
expect(result).toEqual(mockToken)
expect(result).toHaveProperty("accessToken")
expect(result.accessToken).toBe(mockToken.accessToken)
},
)

coreTest("should throw an error if authenticate fails", async () => {
const grantType = "client_credentials"
const config = {
clientId: "test-client-id",
clientSecret: "test-client-secret",
}
const mockError = new Error("Authentication failed")
// @ts-expect-error No types for this function
authenticate.mockRejectedValue(mockError)
await expect(getAccessToken({ grantType, config })).rejects.toThrow(
"Authentication failed",
)
})
})
20 changes: 20 additions & 0 deletions packages/core/src/auth/getAccessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { authenticate } from "@commercelayer/js-auth"

interface AuthenticateProps {
grantType: Parameters<typeof authenticate>[0]
config: Parameters<typeof authenticate>[1]
}

/**
* Retrieves an access token using the provided grant type and configuration.
*
* @param {AuthenticateProps['grantType']} grantType - The type of grant to use for authentication.
* @param {AuthenticateProps['config']} config - The configuration object for authentication.
* @returns {Promise<ReturnType<typeof authenticate>>} A promise that resolves to the access token.
*/
export async function getAccessToken({
grantType,
config,
}: AuthenticateProps): ReturnType<typeof authenticate> {
return await authenticate(grantType, config)
}
Empty file added packages/core/src/index.ts
Empty file.
29 changes: 29 additions & 0 deletions packages/core/src/prices/getPrices.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Price, QueryParamsList } from "@commercelayer/sdk"
import { describe, expect } from "vitest"
import { coreTest } from "#extender"
import { getPrices } from "./getPrices.js"

describe("getPrices", () => {
coreTest("should return a list of prices", async ({ accessToken }) => {
const token = accessToken?.accessToken
const result = await getPrices({ accessToken: token })
expect(result).toBeDefined()
})

coreTest("should return a single price", async ({ accessToken }) => {
const token = accessToken?.accessToken
const params = {
filters: {
sku_code_eq: "DIGITALPRODUCT",
},
} satisfies QueryParamsList<Price>
// Call the getPrices function
const result = await getPrices({ accessToken: token, params })
// Assert the expected result
expect(result).toBeDefined()
expect(result.getRecordCount()).toBe(1)
// Add more assertions based on the expected behavior of the getPrices function
})

// Add more test cases for different scenarios
})
Loading
Loading