@pcreative/commerce-contract 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +66 -0
- package/package.json +37 -0
- package/src/adapters/api.d.ts +36 -0
- package/src/adapters/api.js +825 -0
- package/src/index.d.ts +397 -0
- package/src/index.js +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Copyright (c) 2026-present Pcreative
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
The rights granted above may only be exercised to develop and distribute
|
|
14
|
+
applications that integrate or interoperate with pcreative Commerce software or
|
|
15
|
+
services.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# @pcreative/commerce-contract
|
|
2
|
+
|
|
3
|
+
Contrato de datos de commerce (v1.0): las entidades que consume un tema y los
|
|
4
|
+
adaptadores que las resuelven. **Sin dependencias y sin el SDK del backend.**
|
|
5
|
+
|
|
6
|
+
Un tema no importa ningún SDK de nadie: consume este contrato.
|
|
7
|
+
De ahí salen las dos propiedades que sostienen el producto — el mismo tema sirve
|
|
8
|
+
para otro backend mañana, y el backend no queda casado con temas de un solo
|
|
9
|
+
framework.
|
|
10
|
+
|
|
11
|
+
## Uso
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { createCommerce } from "@pcreative/commerce-contract/api"
|
|
15
|
+
|
|
16
|
+
const commerce = createCommerce({
|
|
17
|
+
baseUrl: process.env.NEXT_PUBLIC_COMMERCE_URL,
|
|
18
|
+
publishableKey: process.env.NEXT_PUBLIC_COMMERCE_KEY,
|
|
19
|
+
countryCode: "es",
|
|
20
|
+
// El framework aporta su caché sin que el adaptador sepa de frameworks:
|
|
21
|
+
requestInit: { next: { revalidate: 300 } },
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const { items, count } = await commerce.listProducts({ category: "iluminacion", limit: 24 })
|
|
25
|
+
const carrito = await commerce.cart.addItem(cartId, variantId, 1)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Reglas del contrato
|
|
29
|
+
|
|
30
|
+
**Los importes son decimales en unidades mayores** (`19.99`, no `1999`) y llevan
|
|
31
|
+
su moneda al lado. Ningún tema tiene que adivinar la escala, que es de donde sale
|
|
32
|
+
la mitad de los errores de precio en una tienda.
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { formatMoney, sumMoney } from "@pcreative/commerce-contract"
|
|
36
|
+
formatMoney({ amount: 19.99, currency: "eur" }) // "19,99 €"
|
|
37
|
+
sumMoney(a, b) // lanza si mezclas monedas
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Los errores llevan el código HTTP dentro** (`err.status`), porque un 404 de
|
|
41
|
+
carrito caducado y un 401 por clave publicable mal puesta piden cosas distintas
|
|
42
|
+
al tema. Un carrito que ya no existe devuelve `null`, no una excepción.
|
|
43
|
+
|
|
44
|
+
**`checkout.complete()` puede devolver `{ order }` o `{ cart, error }`** — las
|
|
45
|
+
dos con HTTP 200. Si el cobro falla, el carrito vuelve intacto y con el motivo.
|
|
46
|
+
|
|
47
|
+
## Escribir otro adaptador
|
|
48
|
+
|
|
49
|
+
Implementa la interfaz `CommerceClient` de [`src/index.d.ts`](src/index.d.ts) y
|
|
50
|
+
compruébalo:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { assertCommerceClient } from "@pcreative/commerce-contract"
|
|
54
|
+
const faltan = assertCommerceClient(miCliente) // [] si cumple
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`account` y `content` son opcionales: un backend puede no tener cuentas de
|
|
58
|
+
cliente ni blog.
|
|
59
|
+
|
|
60
|
+
## Tests
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
node --test "test/*.test.js"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Los tests inyectan un `fetch` de mentira, así que corren sin backend.
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pcreative/commerce-contract",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Contrato de datos de commerce: los tipos que consume un tema y los adaptadores que los resuelven. Sin dependencias ni SDK del backend.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"default": "./src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./api": {
|
|
15
|
+
"types": "./src/adapters/api.d.ts",
|
|
16
|
+
"default": "./src/adapters/api.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node --test \"test/*.test.js\""
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://pcreative.dev",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"email": "soporte@pcreative.dev"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CommerceClient } from "../index.js"
|
|
2
|
+
|
|
3
|
+
export interface OpcionesCommerce {
|
|
4
|
+
/** URL del backend, sin barra final. */
|
|
5
|
+
baseUrl: string
|
|
6
|
+
/** Clave publicable del canal de venta. */
|
|
7
|
+
publishableKey?: string
|
|
8
|
+
/**
|
|
9
|
+
* Leer el catálogo del comercio propio (`/tienda/*`) en vez del motor
|
|
10
|
+
* anterior (`/store/*`). **Por defecto `true`**, que es lo que sirve
|
|
11
|
+
* `npm start`.
|
|
12
|
+
*
|
|
13
|
+
* Ponerlo a `false` es la vuelta atrás, y va emparejado con
|
|
14
|
+
* `npm run start:motor` en el backend. Uno sin el otro deja la tienda vacía
|
|
15
|
+
* sin dar ningún error.
|
|
16
|
+
*
|
|
17
|
+
* 🔴 Esto no estaba declarado aquí, y por eso nadie lo encendía: quien
|
|
18
|
+
* escribía un tema no podía descubrir que existía. Una opción que solo
|
|
19
|
+
* conoce quien ha leído el código fuente no es una opción.
|
|
20
|
+
*/
|
|
21
|
+
catalogoPropio?: boolean
|
|
22
|
+
/** País de la región por defecto, p.ej. "es". Sin él se usa la primera región. */
|
|
23
|
+
countryCode?: string
|
|
24
|
+
/**
|
|
25
|
+
* Idioma del contenido, p.ej. "en-US". Viaja como cabecera en todas las
|
|
26
|
+
* peticiones: el idioma es del visitante, no de la consulta. Lo que no esté
|
|
27
|
+
* traducido vuelve en el idioma original.
|
|
28
|
+
*/
|
|
29
|
+
locale?: string
|
|
30
|
+
/** `fetch` alternativo: para tests, o el de un framework con su caché. */
|
|
31
|
+
fetch?: typeof globalThis.fetch
|
|
32
|
+
/** Opciones extra por petición (`next: { revalidate }`, `cache`…). */
|
|
33
|
+
requestInit?: RequestInit
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export declare function createCommerce(opciones: OpcionesCommerce): CommerceClient
|