To implement the Products controller, you need to implement 2 API endpoints, we will use these endpoints to fetch products from your system.
Prerequisites
SDK
If you are using the SDK, you can implement the Products controller by following the code example below. You don't need to get into the details of the API endpoints, the SDK will take care of that for you.
import { Integrator } from '@churnkey/sdk'
import { Context } from '../Context'
import { Product } from '../models/Product'
// import { Families } from './Families'
export const Products = Integrator.Products.config({
ctx: Context,
type: Integrator.Product.Type.Standalone,
// type: Integrator.Product.Type.Family,
// Families: Families,
async retrieve(ctx, options) {
const yourProduct = await ctx.db.findProduct(options.id)
return new Product(yourProduct)
},
async list(ctx, options) {
const yourProducts = await ctx.db.listProducts({
limit: options.limit,
offset: options.cursor // the value you pass as `next` below
})
return {
data: yourProducts.map(product => new Product(product)),
// pass the next cursor if there are more items
next: yourProducts.length === options.limit ? offset + limit : undefined
}
}
})
Endpoints
Retrieve Required
GET /churnkey/products/:id
This endpoint fetches Product by its id. Usually, implementation will include finding a product in your database and mapping it to the Product model.
Must return Product model. See Product model documentation.
See Error Responses.
import { Product } from '../models/Product'
app.get('/churnkey/products/:id', async (req, res) => {
const product = await db.findProductById(req.params.id)
if (!product) {
return res.status(404).send({ code: 404, message: 'Product not found' })
}
res.send(new Product(product))
})
List Required
GET /churnkey/products
This endpoint fetches a list of products from your database. You should find products in your database (with pagination), map them to the Product model and return a paginated list.
Maximum number of items to return
Cursor for pagination. The actual value is whatever you decided to use as next in the response.
Array of items. The type of item is defined in the endpoint documentation, e.g. Customer for /customers endpoint
Either a next id or an offset for the next page. You decide what to use, we will send next value as a cursor query parameter back to you. If next is empty, there are no more pages
import { Product } from '../models/Product'
app.get('/churnkey/products', async (req, res) => {
const limit = Number.parseInt(req.query.limit)
const offset = Number.parseInt(req.query.cursor)
const products = await db.findProducts({ limit, offset })
res.send({
data: products.map(c => new Product(c)),
next: products.length === limit ? offset + limit : undefined
})
})
Webhooks
Coming soon.