npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cdc

v1.3.0

Published

Consumer Driven Contracts (CDC)

Downloads

38

Readme

Consumer Driven Contracts (CDC)

wercker status Coverage Status Code Climate Standard - JavaScript Style Guide Gitter

CDC provides a way to define mock provider for consumer and verify contracts against provider. It is written in JavaScript, however can be run in any setup using docker.

Getting started

Using docker-compose

version: '2'
services:
  api:
    build:
      context: .
    depends_on:
      - dependency
    command: npm start
  dependency:
    image: uldissturms/cdc
    volumes:
      - ./contracts/dependency:/usr/app/src/contracts
    command: mock
    ports:
      - "3000:3000"

Where ./contracts/dependency contains index.js that describes contract.

./contracts
└── dependency
    └── index.js

Using docker

docker run -p 3000:3000 -v ${PWD}/contracts:/usr/app/src/contracts uldissturms/cdc mock ./contracts/simple

Using npm

npm i cdc
./node_modules/.bin/cdc mock ./contracts/simple
./node_modules/.bin/cdc verify ./contracts/simple --baseUrl http://localhost:3000

Contracts

Simple contract with schema validation

const joi = require('joi')

module.exports = {
  name: 'simple request/response schema',
  request: {
    path: '/api/simple-schema',
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    body: {
      hello: 'world'
    },
    bodySchema: joi.object().keys({
      hello: joi.string()
    })
  },
  response: {
    body: {
      id: 12345
    },
    bodySchema: joi.object().keys({
      id: joi.number().integer()
    })
  }
}

Response

curl localhost:3000/api/simple-schema -H 'content-type: application/json' -d '{"hello": "world"}'
{"id": 12345}

Usage

Why I wrote my own

| | request | response | | ---------|---------------| --------------| | consumer | verify schema | mock | | provider | request | verify schema |

  • mock - mocks responses for consumer
  • verify - verifies contracts agains provider

Options

  • mock
    • --port, -p - port for running mock server, defaults to 3000 (optional)
    • --no-cors, -C - disable CORS support (optional)
    • --watch, -w - to watch current directory for contract changes (optional)
    • --tls, -t - enable TLS with a self signed certificate ( setting NODE_TLS_REJECT_UNAUTHORIZED=0 when consuming from node might be required )
  • verify
    • --baseUrl, -b - base url to run verifications against (required)

Examples

For more examples take a look at contracts in ./contracts used for tests.

Libraries used

  • Joi (schema valiations)
  • Hapi (mock provider server)
  • Tape (verify consumer contracts against a provider)

Influences

Further reading

Licence

MIT