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

decoder-api-tester

v4.0.0

Published

CLI to test api result decoders

Downloads

33

Readme

ApiTester

The goal of this package is to test your project's decoders with their corresponding api.

We recommend using the typescript-json-decoder package which provides detailed error messages.

Features and specifications

  • The tested decoders must throw an error if it cannot decode a json.
  • A CLI is provided to use run the tests.
  • A report can be generated as an html file using the -r option.
  • A complete log of the tests is generated after each execution.
  • The process will always return 1 if one or more test failed.
  • This package uses Axios to run the Http requests.

Setup

Instalation : npm i decoder-api-tester


Create the apitester-config.ts file in the root of your project.

A configuration contains a list of API configuration which contains a list of test configuration.

import { ApiTesterConfig } from "api-tester";
import { firstDecoder, secondDecoder } from "src/decoders";
import { getAccessToken, isWantedFormat } from "src/utils";

const config: ApiTesterConfig = {
    apisConfig: [
        {
            baseUrl: "https://my-api.com/",
            defaultMethod: "post",
            defaultinterceptor: (axiosConfig: AxiosRequestConfig, context: any) => {
                axiosConfig.headers["AccessToken"] = getAccessToken();
                axiosConfig.params = { id: context.myId }
                return axiosConfig;
            },
            defaultBeforeDecode: (json: unknown) => {
                if (json.code == 404) {
                    throw 'Error 404';
                }
            },
            defaultOnDecoded: (data: Data, json: any, setContext: (context: any) => void) => {
                if (!isWantedFormat(data.formatedString)) {
                    throw 'formatedString is not on the right format';
                }
                setContext({ myId: data.array[0].id })
            },
            defaultQueryParameters: {
                apiKey: "my-api-key"
            },
            defaultHeaders: {
                "Connection": "Keep-Alive"
            },
            defaultData: {
                foo: "bar"
            },
            tests: [
                // This test will use the default options 
                {
                    description: "Post data",
                    endpointPath: "data/post",
                    decoder: firstDecoder
                },
                {
                    description: "Get random data",
                    endpointPath: "data/random",
                    method: "get",
                    decoder: secondDecoder,
                    interceptor: (axiosConfig: AxiosRequestConfig) => {
                        return axiosConfig;
                    },
                    beforeDecode: (json: unknown) => {
                        if (json.code == 404) {
                            throw 'Error 404';
                        }
                    },
                    onDecoded: (data: Data) => {
                        if (!isWantedFormat(data.formatedString)) {
                            throw 'formatedString is not on the right format';
                        }
                    },
                    queryParameters: {
                        limit: false
                    },
                    headers: {},
                    data: {},
                },
            ],
        }
    ]
};

export default config;

Run the command api-tester

Type api-tester -h to show command manual.

Config parameters

Common parameters

The default options set in the api will be used if none are set in a test. Both the default functions and the functions will run if they are set.

| Attribute | Type | Description | |-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------| | method | 'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' | 'purge' | 'link' | 'unlink' | http rest method | | decoder | (input: unknown) => T | decoder function | | interceptor | (axiosConfig: AxiosRequestConfig, contest: any) => AxiosRequestConfig | function called after the axios config object is generated, it must return axiosConfig | | beforeDecode | (json: any) => void | function called after the axios config object is generated, it must return axiosConfig | | onDecoded | (data: T, json: any, setContext: (context: any) => void) => void | function called after decoder, it must throw an error if the decoded data or the received raw json is not valid | | queryParameters | Dict<string[] | string | number | boolean> | query parameters of the request | | headers | Dict<string | number | boolean> | headers of the request | | data | any | data stored in the body of the request |

Api

| Attribute | Type | Description | |-----------|---------------------------------|---------------------| | baseUrl | string | base url of the api | | tests | Test[] | tests configuration |

Test

| Attribute | Type | Description | |----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------| | description | string | name or description of the test | | endpointPath | string | route to the endpoint |