Skip to main content
Version: 🚧 Canary

🛍️ Product

The Product service exposes public product listing plus protected product, image, variant, organization, and liker operations backed by the SDK's product collections.

Start here

This is an integration fragment: the host must provide MongoDB collections, body parsing, and (for cookie transport) cookie parsing before mounting the returned service.

import express from 'express';
import cookieParser from 'cookie-parser';
import { services } from '@nodeblocks/backend-sdk';

app.use(express.json());
app.use(cookieParser()); // Required only when authMode is 'cookie'.
app.use(
'/api',
services.productService(
{
identities,
products,
profiles,
productVariants,
organizations,
},
{
authSecrets: {
authEncSecret: 'development-encryption-secret',
authSignSecret: 'development-signing-secret',
},
},
{ fileStorageDriver }
)
);
ConfigurationDefault / source behaviorEffect
authSecrets.authEncSecretRequired; no runtime defaultToken encryption configuration.
authSecrets.authSignSecretRequired; no runtime defaultToken signing configuration.
authModeOmitted or 'bearer' uses bearer tokens'cookie' uses cookie tokens and requires cookie middleware.
identity.typeIdsPassed to configured validators when suppliedSupports identity-type checks.
organization.rolesPassed to configured validators when suppliedSupports organization-role checks.

Common tasks

TaskStart withContract
Create, update, copy, or remove a productcreateProductFeatureFeatures, routes, and schemas
Upload or attach product imagescreateProductImageFeatureImage feature and image route
Manage product variantscreateProductVariantFeatureVariant features and variant schemas
Trace reusable behavior or errorsBlocksBlocks and handlers
Review access checksValidatorsValidators

Bearer HTTP workflow

List products with the public findProductsRoute, or create one with the protected createProductRoute. The creation body is defined by createProductSchema; the request requires a valid administrative bearer identity and returns the pipeline result.

API_BASE_URL='http://localhost:8080/api'
ACCESS_TOKEN='replace-with-a-valid-access-token'

curl "$API_BASE_URL/products?page=1&limit=20"

curl -X POST "$API_BASE_URL/products" \
-H "authorization: Bearer $ACCESS_TOKEN" \
-H 'content-type: application/json' \
-d '{"name":"Example product","description":"Short description"}'

The list returns 200 with paginated product data; creation returns the pipeline result for an authorized caller.

Set authMode: 'cookie' and register cookie parsing in the host before mounting. Protected routes use the same schemas and validators, but isAuthenticated() reads the access token from cookies rather than a bearer header.

API_BASE_URL='http://localhost:8080/api'
ACCESS_COOKIE='accessToken=replace-with-a-valid-access-token'

curl -X POST "$API_BASE_URL/products" \
-H "cookie: $ACCESS_COOKIE" \
-H 'content-type: application/json' \
-d '{"name":"Example product","description":"Short description"}'

Custom feature composition

Compose a public SDK feature, such as createProductFeature, inside a custom defService setup when only a selected endpoint set should be mounted. The custom host must provide the same injected dataStores, configuration, and optional fileStorageDriver required by its chosen routes.

import {partial} from 'ramda';
import {features, primitives, utils} from '@nodeblocks/backend-sdk';

const productRouter = primitives.defService(
partial(primitives.compose(features.createProductFeature, features.findProductsFeature), [{
authenticate: utils.getBearerTokenInfo,
configuration: {authSecrets},
dataStores: {identities, products, profiles, productVariants, organizations},
fileStorageDriver,
}]),
);

app.use('/api', productRouter);

Reference map

PagePurpose
BlocksReusable product operations and errors.
FeaturesSchema-to-route composers.
HandlersRoute pipeline operations.
RoutesCanonical HTTP endpoint matrix and pipelines.
SchemasField-level request and reusable data contracts.
ValidatorsShared access checks composed by Product routes.

Organization schemas provide the shared organization-ID request schema used by the organization listing route. File-storage schemas provide the signed-upload request schema used by image-upload URLs. Authentication documents the shared token transport and authentication behavior.