Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/empty-frogs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noaignite/utils': minor
---

Add initial version of `cacheAside`
5 changes: 5 additions & 0 deletions .changeset/shiny-kangaroos-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noaignite/next-centra-checkout': patch
---

Add initial version of `createGetCentraWebhookEvents`
3 changes: 3 additions & 0 deletions packages/next-centra-checkout/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import nextConfig from '@noaignite/style-guide/eslint/next'

export default nextConfig
63 changes: 63 additions & 0 deletions packages/next-centra-checkout/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@noaignite/next-centra-checkout",
"version": "0.0.0",
"private": false,
"description": "Next.js helpers for Centra checkout api",
"keywords": [
"backend",
"centra",
"nextjs",
"react",
"typescript",
"vercel"
],
"bugs": {
"url": "https://github.com/noaignite/accelerator/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/noaignite/accelerator.git",
"directory": "packages/next-centra-checkout"
},
"license": "MIT",
"author": "NoA Ignite",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**",
"README.md"
],
"scripts": {
"build": "tsup",
"lint": "eslint .",
"test:coverage": "vitest run --coverage",
"test:unit": "vitest run",
"test:unit:watch": "vitest"
},
"dependencies": {
"@noaignite/utils": "workspace:*"
},
"devDependencies": {
"@noaignite/centra-types": "workspace:*",
"@noaignite/style-guide": "workspace:*",
"@types/react": "^19.1.2",
"next": "^15.0.0",
"nock": "14.0.1",
"react": "^19.1.0",
"tsup": "^8.3.5",
"typescript": "5.4.5"
},
"peerDependencies": {
"next": "^14.0.0 || ^15.0.0",
"react": "^18.0.0 || ^19.0.0",
"react-dom": "^18.0.0 || ^19.0.0"
},
"engines": {
"node": ">=20.0.0"
},
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import nock from 'nock'
import { describe, expect, it } from 'vitest'
import { createCentraProductsCacheClientHelpers } from './createCentraProductsCacheClientHelpers'

describe('createCentraProductsCacheClientHelpers', () => {
const hostname = 'http://localhost:3000'
const path = 'api/centra'
const endpointBasePath = `${hostname}/${path}`
const { fetchProducts } = createCentraProductsCacheClientHelpers({
endpointBasePath,
})

it('returns a fetchProducts callback', () => {
expect(fetchProducts).toBeDefined()
})

describe('fetchProducts', () => {
it('performs a GET request by the passed endpoint base path', async () => {
const market = '1'
const pricelist = '2'
const language = 'en'
const id = '123'

const mockedProduct = { product: id }

const scope = nock(hostname)
.get(`/${path}/${market}/${pricelist}/${language}?ids=${id}`)
.reply(200, [mockedProduct])

const results = await fetchProducts(
{
market,
pricelist,
language,
},
[id],
)

expect(scope.isDone()).toBe(true)
expect(results[0]).toMatchObject(mockedProduct)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { SessionContext } from './createCentraProductsCacheServerHelpers'

type ClientHelperOptions = {
/**
* Endpoint base path.
* The returned API endpoint should be absolute, meaning either a full URL or a path beginning
* with a `/`. The returned API endpoint should not contain any search parameters.
*
* @example `/api/centra`
*/
endpointBasePath: string
}

/**
* Create Centra product cache client helpers.
* Helpers to be called client-side to retrieve cached data from Centra.
* To be used in conjunction with `createCentraProductsCacheServerHelpers`
*/
export function createCentraProductsCacheClientHelpers<TProduct>(options: ClientHelperOptions) {
const { endpointBasePath } = options

return {
/**
* Fetch products.
*
* @param sessionContext - The current users session context.
* @param ids - The product IDs to be fetched.
*/
fetchProducts: async (sessionContext: SessionContext, ids: string[], init?: RequestInit) => {
const endpoint = `${endpointBasePath}/${sessionContext.market}/${sessionContext.pricelist}/${sessionContext.language}`

return fetch(`${endpoint}?ids=${ids.join(',')}`, init).then(
(response) => response.json() as Promise<TProduct[]>,
)
},
}
}
Loading