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

api-examples

v0.0.0

Published

Common utils for api examples

Downloads

3

Readme

Api Examples

Common utils for api examples

Add to my code

Add to your package.json

npm install api-examples

How it works

We lean on convention to minimise code.

As a producer

New up ApiExamples with your service name.

Then when you want to produce an api example from an e2e test:

  • write your e2e test you normally would
  • call save with: a test name, the request and the response [1]

This will write a request and response json output to:

__apiExamples/${serviceName}/${example-name}

For example the below:

it('saves hotel config', async () => {
    const postResponse = await request.post(`${baseUrl}/hotels`, {
      json: { apiKey: 'hotel api key', client: 'hotel client' },
      resolveWithFullResponse: true,
    });
    expect(postResponse.statusCode).toBe(201);

    apiExamples.save('hotel-config', postResponse.request, postResponse.request.response); // [1]
});

will publish to a file called __apiExamples/figgy/hotel-config.json:

{
  "request": {
    "method": "POST",
    "path": "/hotels"
  },
  "response": {
    "statusCode": 201,
    "statusMessage": "Created",
    "headers": {
      "content-type": "application/json; charset=utf-8",
      "content-length": "50",
      "date": "Thu, 07 Jun 2018 16:08:11 GMT",
      "connection": "close"
    },
    "body": {
      "apiKey": "hotel api key",
      "client": "hotel client"
    }
  }
}

As part of your build you rsync the folder to gcp bucket api-examples:

gsutil -m rsync -cr __apiExamples gs://api-examples/

As a consumer

New up ApiExamples with the service name you're testing against.

You can then pull api examples from a gcloud bucket called api-examples. [1]

Then match against the examples to get a response [2]

You can match on:

  • method
  • path
  • body

Then we can assert on the response [3]

const ApiExamples = require('api-examples');
const apiExamples = new ApiExamples('figgy');
const expect = require('expect.js');

describe('figgy', ()=>{

  before(async function() {
    this.timeout(10000);
    await apiExamples.pullApiExamples(); // [1] <--
  });

  it('gives us a hotel from client and property id', async () => {
    const response = await apiExamples.findMatchingExample({  // [2] <--
      method: 'POST'
    });

    expect(response.statusCode).to.be(201); // [3] <--
  })
});