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

proco

v0.1.7

Published

Suite of utilities for working with the API Contracts testing model.

Downloads

2

Readme

proco

This package ships with 2 primary functions, categorized as such:

  1. Helpers
  2. CLI

Helpers

There are 2 types of helpers in this package.

Functions

.generateResponseFromSchema(rootSchemaPath, schemaPath) [async]

Generates a fake object, based on the schema (contract), that can be use to nock server responses.

  • rootSchemaPath: The path to where your contracts are stored.
  • schemaPath: The path, from the rootSchemaPath to where the schema file is located.

Example:

const nock = require('nock');
const { generateResponseFromSchema } = require('api-contracts');
const producerContractPath = '../__contracts__';

describe('Example Test', () => {
  it('loads the contract', async () => {
    const response = await generateResponseFromSchema(producerContractPath, 'example.contract.json');
    const expectedResponse = { isAwesome: true };

    nock('http://example.com')
    .get('/example')
    .reply(200, response);

    const request = await makeARequest();
    expect(request.body).toEqual(expectedResponse);
  });
});

.loadSchema(rootSchemaPath, schemaPath)

Loads a schema file to use for validating a payload.

  • rootSchemaPath: The path to where your contracts are stored.
  • schemaPath: The path, from the rootSchemaPath to where the schema file is located.

Example:

const nock = require('nock');
const { matchers } = require('jest-json-schema');
const {
  generateResponseFromSchema,
  loadSchema,
} = require('api-contracts');
const producerContractPath = '../__contracts__';
const consumerContractPath = '@org/consumer-contracts/contracts';

expect.extend(matchers);

describe('Example Test', () => {
    it('Returns the correct payload', async () => {
      const routeRequest = {
        params: { accountNumber: 1 },
      };
      const response = await generateResponseFromSchema(producerContractPath, '/Example/get.contract.json');

      nock('http://example.com')
      .get('/example/1')
      .reply(200, response);

      const schema = loadSchema(consumerContractPath, '/Example/get.contract.json');

      const request = await getExample(routeRequest);
      expect(request.body).toMatchSchema(schema);
    });
  });

json-schema types, and properties

Json-schema types, and properties exist to make defining schemas for your application much easier, and less redundant. To do this, we take commonly used elements in a schema, define them here, then in your application schemas you can simply do the following:

const { types, properties } = require('api-contracts');


module.exports = {
  title: 'Example',
  type: 'object',
  required: ['email', 'links'],

  properties: {
    email: types.email,
    links: properties.links,
  }
};

To use json-schema types, or properties in your . You will need to require them

types

Types live in the ./types folder. These are simple definition of a single property type. For example, here is the code used for the uri type:

{
  "id": "types.uri",
  "type": "string",
  "pattern": "[0-9a-fA-F]{8,20}"
}

properties

Properties live in the ./properties folder. These are more complex definitions that typically will include children, and might even import various types. Here is what the links property looks like:

{
  "id": "properties.links",
  "type": "object",
  "required": [ "self" ],
  "properties": {
    "self": {
      "type": "string",
      "pattern": "^https?:\\/\\/[a-f]{3,10}\\.[a-f]{3}"
    }
  }
}

CLI

The CLI is used to version bump and publish contracts inside an application. Once this package is installed in your application you can run the commands shown below from the repo's root directoy. If your contracts are stored in the __contracts__ folder, the CLI will take care of navigating to that folder to publish, and version bump.

Usage:

# To see all available options
proco --help