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

boclips-api-client

v93.3.1

Published

Boclips API client

Downloads

3,472

Readme

Boclips API client

A JS client to access the API for our frontend applications.

Development

Overview

This client uses pact to create contracts that are then verified against our staging API.

Pact is a code-first tool for testing HTTP and message integrations using contract tests. Contract tests assert that inter-application messages conform to a shared understanding that is documented in a contract. Without contract testing, the only way to ensure that applications will work correctly together is by using expensive and brittle integration tests.

There are two parts to testing our client, tests that test our client can handle http responses (contracts) in a meaningful way and secondly, tests that verify that the contracts we have defined match that of the responses from our backend services.

We define expected interactions which is essentially setting up a mock http layer to test the client against. When the client tests pass, pact writes to a JSON file in ./pacts which is then used as a contract to test against our staging API gateway.

  • npm run test:unit unit tests for services
  • npm run test:client tests that our mocks (interactions) set up are handled correctly by this client. If successful it will create the PACT contracts file under ./pacts
  • npm run test:verify replay all interactions specified under ./pacts against the staging API gateway and verifies that the responses match the expectations

FakeBoclipsClient

A fake implementation of the BoclipsClient is included to help testing in the consumer applications.

Eg. in your integration tests if you include the fake implementation instead of the real, you do not need to mock http requests and can set up some sample data to be used by the client.

const fakeClient = new FakeBoclipsClient();

// sample data set up
fakeClient.collectionsClient.addToFake(testCollection);
fakeClient.collectionsClient.addToFake(otherTestCollection);

// ... your tests here ...

// reset the state of the client in the end
fakeClient.clear();

Sub-clients

The BoclipsClient contains sub-clients to help access the various resources from our API.

Usage in costumers

let axios: AxiosInstance; // the set up axios instance to be used in the API requests (eg. Authentication should be setup by costumer)
let prefix: string; // environment specific URL prefix to be used, eg. https://api.staging-boclips.com

const client = await ApiBoclipsClient.initialize(axios, prefix); 

// Query an existing collection by id
const existingCollection = await client.collectionsClient.get('sample-collection-id');

Adding new sub-clients

  1. Include in BoclipsClient as a new field
  2. Make a proper and a fake implementation and include them in ApiBoclipsClient and FakeBoclipsClient respectively
  3. Follow the sample folder structure:
src
|-- sub-clients
    |-- newSubClient
        |-- client
            |-- NewSubClient.ts -> interface for your new sub client
            |-- ApiNewSubClient.ts -> real implementation
            |-- FakeNewSubClient.ts -> fake implementation for helping testing in consumer apps
            |-- NewSubClient.test.pact.ts -> tests for the new sub client using pact interactions (npm run test:client will run this)
        |-- model -> any data models needed for handling API response, consumer requests, etc.
        |-- pact
            |-- NewSubClientInteractions.ts -> specify the API interactions / rules that you want to cover by the new sub client

Linking Packages

Sometimes it can be annoying to wait for a release before testing changes to this library. To test locally with another app that's using this library simply from the boclips-api-client directory:

npm run link
cd ../your-app
npm link boclips-api-client

This will set up a symlink between the local client library build and the node_modules/ in your-app

(This uses npm link under the hood, more info here)