5.10.0 • Published 2 months ago

@stoplight/prism-http v5.10.0

Weekly downloads
16,397
License
Apache-2.0
Repository
github
Last release
2 months ago

HTTP Client

This package provides a HTTP client featuring the ability to:

  • mock responses based of OpenAPI spec
  • make requests to a HTTP server
  • validate the request and/or the response according to the provided OpenAPI Specification file

In essence it's an HTTP client with the ability to mock responses and validate requests.

The goal of this document is to provide you with some basic code examples to get you started and to cover some of the advanced scenarios.

Important

If you're a regular user and not a PRO, you might to want to use the User facing client which provides a better an higher lever API

Table of Contents

Installation

yarn add @stoplight/prism-http

Basic Usages

Note: the examples in this document use the following spec

openapi: 3.0.2
paths:
  /todos:
    get:
      parameters:
        - name: title
          in: query
          style: form
          schema:
            enum:
              - eat
              - drink
      responses:
        200:
          description: Get Todo Items
          content:
            'text/plain':
              example: Write great OpenAPI specs!

Mock All Responses

const Prism = require('@stoplight/prism-http');

// Create Prism instance and configure it as a mocker generating static examples
const prism = Prism.createInstance({
  mock: { dynamic: false },
  validate: { request: { query: true } },
});

// Make a "GET /todos" request
return prism.request(
  {
    method: 'get',
    url: {
      path: '/todos',
    },
    headers: {
      Accept: 'text/plain',
    },
  },
  operations
);

Output

{
  statusCode: 200,
  headers: { 'Content-type': 'text/plain' },
  body: 'Write great OpenAPI specs!'
}

Note: the request method returns a TaskEither monad. So in case you want to extract the result, you're going to need to use the pipe function.

return pipe(
  prism.request(
    {
      method: 'get',
      url: {
        path: '/todos',
      },
      headers: {
        Accept: 'text/plain',
      },
    },
    operations
  ),
  TE.fold(console.error, console.log)
)(); // returns a Promise

Mock Single Response

In the following example we will first instantiate Prism to make requests to an actual server.

Later we alter than behaviour by passing a config object to the request function.

const Prism = require('@stoplight/prism-http');

// Note that by default we don't want to mock responses
const prism = Prism.createInstance({ mock: { dynamic: false } });

// Make a "GET /todos" request
return prism.request(
  {
    method: 'get',
    url: {
      path: '/facts',
      baseUrl: 'https://cat-fact.herokuapp.com',
    },
  },
  {
    // We can override the default behaviour per request.
    mock: {
      dynamic: true,
    },
  },
  operations
);

Make Request To An Upstream Server

We don't want to mock a reqeust, we simply want to make the request, hit the actual server and get the response back.

const Prism = require('@stoplight/prism-http');

// Create Prism instance and configure it to make HTTP requests (mock: {dynamic: false})
const config = { mock: { dynamic: false } };
const prism = Prism.createInstance(config);

return prism.request({
  method: 'get',
  url: {
    path: '/facts',
    baseUrl: 'https://cat-fact.herokuapp.com',
  },
});

In response you'll get some... facts about cats. For example:

Cats aren’t the only animals that purr — squirrels, lemurs, elephants, and even gorillas purr too.

:)

Advanced Topics

Creating Prism Instance

In order to create and instance of HTTP Client (later referred to as prism for simplicity) use the createInstance function, like so:

const Prism = require('@stoplight/prism-http');
const config = { mock: { dynamic: false } };
const prism = Prism.createInstance(config /*, components */);

There are two (both optional) arguments you can supply createInstance with:

  • config (of IHttpConfig type)
  • components (of PrismHttpComponents type)

We will cover the config argument in next section and we'll leave components for some other time (overriding default components is the ultimate advanced stuff).

Config Object

Prism's config object (IHttpConfig) allows you to manipulate Prism's behaviour in many ways. For instance:

  • turn validation on and off
  • influence Prism's mocked response generation strategy

The actual interface looks like this (but rather than explain each property we're going to look at some examples)

export interface IHttpConfig extends IPrismConfig {
  mock: { dynamic: false } | IHttpOperationConfig;
  validateRequest: boolean;
  validateResponse: boolean;
  checkSecurity: boolean;
  errors: boolean;
}

Config Examples

When mocking a response generate static response

const config = {
  mock: {
    dynamic: false,
  },
};

Generating static responses means that if an OpenAPI operation's response has any examples defined the payload we construct will use those examples.

This contrasts "dynamic responses" which means generating responses from a json schema.

Return named example of a specified key

const config = {
  mock: {
    exampleKey: 'key',
  },
};

In OpenAPI Specification 3 one can construct such response that will have multiple examples mapped by keys. This configuration allows you to be very specific which example you want to choose.

Return 403 response of specified mime type

const config = {
  mock: {
    code: 403,
    mediaTypes: ['application/xml'],
  },
};

This will enforce a 403 response (given that such response is defined in your OpenAPI file).

Loading specs

The Http package does not have any pre-canned loader for the specification files. You need to feed it with an array of operations that can be used for the request/response cycle.

You can find an example by looking at the operations.ts file in the CLI package.

Making requests

To make a basic request you need to do the following

const request = {
  method: 'get',
  url: {
    path: '/path', // must be prefixed with slash
  },
};
const promise = prism.request(request, operations);

The request object has the following interface

export interface IHttpRequest {
  method: HttpMethod;
  url: IHttpUrl;
  headers?: IHttpNameValue;
  body?: unknown;
}

All of this is pretty standard except the url.baseUrl which we will describe in more details.

Server Validation

Consider a request

prism.request(
  {
    method: 'get',
    url: {
      path: '/facts', // must be prefixed with slash
      baseUrl: 'https://cat-fact.herokuapp.com',
    },
  },
  operations
);

Notice that baseUrl is defined.

This will instruct Prism to do one of the two:

  • if mocking is disabled ({ mock: {dynamic: false} })
    • it will use that baseUrl to make a request to the server (GET https://cat-fact.herokuapp.com/facts)
    • it will verify whether the provided baseUrl matches any of the servers defined in your OpenAPI and add an input warning to the .request return value if it is not valid
  • if mocking is enabled
    • it will verify whether the provided baseUrl matches any of the servers defined in your OpenAPI and return an error if it is not valid

Understanding response

The prism.request resolved (it's a Promise!) value consists of:

  • input - copy of the request object you provided
  • output - the HTTP response
  • validations
    • input - list of warnings/errors related to you http request object
    • output - list of warnings/errors related to you http response object

Validations are still subject of tests and experimentation therefore we will not cover them fully for the time being.

One example however is that for a given example

const Prism = require('@stoplight/prism-http');

// Create Prism instance and configure it to make http requests
const config = { mock: { dynamic: false } };
const prism = Prism.createInstance(config);
prism.request(
  {
    method: 'get',
    url: {
      path: '/facts',
      baseUrl: 'https://cat-fact.herokuapp.com',
    },
  },
  operations
);

You would get this in response

[
  {
    message: 'Route not resolved, no resource provided.',
    source: 'https://stoplight.io/prism/errors#NO_RESOURCE_PROVIDED_ERROR',
    code: 404,
    severity: 1
  }
]

Prism Decision Flow Diagram

The below diagram represents all logical decision we make to figure out the best HTTP response to the specific request.

Decision Flow Diagram

5.8.3

4 months ago

5.8.2

5 months ago

5.9.0

4 months ago

5.10.0

2 months ago

5.8.1

7 months ago

5.7.0

8 months ago

5.6.0

8 months ago

5.5.4

10 months ago

5.5.3

11 months ago

5.1.0

1 year ago

5.2.0

1 year ago

5.3.2

1 year ago

5.3.1

1 year ago

5.3.0

1 year ago

5.4.0

1 year ago

5.5.2

12 months ago

5.5.1

1 year ago

5.5.0

1 year ago

4.14.1

2 years ago

4.14.0

2 years ago

5.0.1

1 year ago

5.0.0

2 years ago

4.13.0

2 years ago

4.12.0

2 years ago

4.11.1

2 years ago

4.10.6

2 years ago

4.10.5

2 years ago

4.10.4

2 years ago

4.10.1

2 years ago

4.10.2

2 years ago

4.10.3

2 years ago

4.9.3

3 years ago

4.10.0

2 years ago

4.11.0

2 years ago

4.9.0

3 years ago

4.9.2

3 years ago

4.9.1

3 years ago

4.8.0

3 years ago

4.7.0

3 years ago

4.6.1

3 years ago

4.6.0

3 years ago

4.4.3

3 years ago

4.4.2

3 years ago

4.5.0

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.3.4

3 years ago

4.3.3

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.6

3 years ago

4.2.5

3 years ago

4.2.4

4 years ago

4.2.3

4 years ago

4.2.2

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-beta.71

4 years ago

4.0.0-beta.70

4 years ago

4.0.0-beta.69

4 years ago

4.0.0-beta.68

4 years ago

4.0.0-beta.67

4 years ago

4.0.0-beta.66

4 years ago

4.0.0-beta.65

4 years ago

4.0.0-beta.64

4 years ago

3.3.7

4 years ago

4.0.0-beta.62

4 years ago

4.0.0-beta.63

4 years ago

4.0.0-beta.61

4 years ago

4.0.0-beta.60

4 years ago

4.0.0-beta.59

4 years ago

4.0.0-beta.58

4 years ago

4.0.0-beta.57

4 years ago

4.0.0-beta.56

4 years ago

4.0.0-beta.55

4 years ago

4.0.0-beta.54

4 years ago

4.0.0-beta.53

4 years ago

4.0.0-beta.52

4 years ago

4.0.0-beta.51

4 years ago

4.0.0-beta.50

4 years ago

4.0.0-beta.48

4 years ago

4.0.0-beta.49

4 years ago

4.0.0-beta.47

4 years ago

4.0.0-beta.44

4 years ago

4.0.0-beta.43

4 years ago

4.0.0-beta.46

4 years ago

4.0.0-beta.45

4 years ago

4.0.0-beta.42

4 years ago

4.0.0-beta.40

4 years ago

4.0.0-beta.41

4 years ago

4.0.0-beta.39

4 years ago

4.0.0-beta.38

4 years ago

4.0.0-beta.37

4 years ago

4.0.0-beta.36

4 years ago

4.0.0-beta.33

4 years ago

4.0.0-beta.32

4 years ago

4.0.0-beta.35

4 years ago

4.0.0-beta.34

4 years ago

4.0.0-beta.31

4 years ago

4.0.0-beta.30

4 years ago

4.0.0-beta.28

4 years ago

4.0.0-beta.27

4 years ago

4.0.0-beta.29

4 years ago

4.0.0-beta.26

4 years ago

4.0.0-beta.25

4 years ago

4.0.0-beta.24

4 years ago

4.0.0-beta.22

4 years ago

4.0.0-beta.23

4 years ago

4.0.0-beta.21

4 years ago

4.0.0-beta.20

4 years ago

3.3.6

4 years ago

4.0.0-beta.19

4 years ago

4.0.0-beta.15

4 years ago

4.0.0-beta.14

4 years ago

4.0.0-beta.17

4 years ago

4.0.0-beta.16

4 years ago

4.0.0-beta.13

4 years ago

4.0.0-beta.18

4 years ago

4.0.0-beta.11

4 years ago

4.0.0-beta.12

4 years ago

4.0.0-beta.10

4 years ago

4.0.0-beta.9

4 years ago

4.0.0-beta.8

4 years ago

3.3.5

4 years ago

4.0.0-beta.7

4 years ago

4.0.0-beta.6

5 years ago

4.0.0-beta.5

5 years ago

4.0.0-beta.3

5 years ago

4.0.0-beta.2

5 years ago

4.0.0-beta.1

5 years ago

4.0.0-alpha.0

5 years ago

3.3.4

5 years ago

3.3.3

5 years ago

3.3.2

5 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.3.0-beta.0

5 years ago

3.2.9

5 years ago

3.2.8

5 years ago

3.2.7

5 years ago

3.2.6

5 years ago

3.3.0-alpha.0

5 years ago

3.2.5

5 years ago

3.2.4

5 years ago

3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.2.0-beta.12

5 years ago

3.2.0-beta.11

5 years ago

3.2.0-beta.10

5 years ago

3.2.0-beta.9

5 years ago

3.2.0-beta.8

5 years ago

3.2.0-beta.7

5 years ago

3.2.0-beta.6

5 years ago

3.2.0-beta.5

5 years ago

3.2.0-beta.4

5 years ago

3.2.0-beta.3

5 years ago

3.2.0-beta.2

5 years ago

3.2.0-beta.1

5 years ago

3.2.0-alpha.0

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

3.0.0-beta.6

5 years ago

3.0.0-beta.5

5 years ago

3.0.0-beta.4

5 years ago

3.0.0-beta.3

5 years ago

3.0.0-beta.2

5 years ago

3.0.0-beta.1

5 years ago

3.0.0-beta.0

5 years ago

3.0.0-alpha.16

5 years ago

3.0.0-alpha.15

5 years ago

3.0.0-alpha.14

5 years ago

3.0.0-alpha.13

5 years ago

3.0.0-alpha.12

5 years ago

3.0.0-alpha.11

6 years ago

3.0.0-alpha.10

6 years ago

3.0.0-alpha.9

6 years ago

3.0.0-alpha.8

6 years ago

3.0.0-alpha.7

6 years ago

3.0.0-alpha.6

6 years ago

3.0.0-alpha.5

6 years ago

3.0.0-alpha.4

6 years ago

3.0.0-alpha.3

6 years ago

3.0.0-alpha.2

6 years ago

3.0.0-alpha.1

6 years ago

3.0.0-alpha.0

6 years ago

3.0.0-pre.2

6 years ago

3.0.0-pre.1

6 years ago

3.0.0-pre.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago