3.3.105 • Published 10 months ago

@patrtorg/hic-facere-ratione v3.3.105

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

OpenAPI-Enforcer

Tools for using the Open API Specification (OAS)

Supports OAS 2.0 (formerly Swagger) and OAS 3.x.x

Features

  • Create an API.
  • Validate your OAS documents.
  • Serialize, deserialize, and validate values against OAS schemas.
  • Identify the operation associated with a request.
  • Parse, deserialize, and validate request parameters.
  • Facilitated response building.
  • Generate random valid values for a schema.
  • Plugin environment for custom document validation and extended functionality including custom data type formats.

Website - @patrtorg/hic-facere-ratione.com

Installation

npm install @patrtorg/hic-facere-ratione

Examples

Loading and Validating a Document

Use the Enforcer to load and resolve all $ref values and then to validate the complete document.

const Enforcer = require('@patrtorg/hic-facere-ratione')

async function run () {
  const [openapi, error, warning] = await Enforcer('./path/to/openapi.yml', {
    fullResult: true
  })
  if (error !== undefined) console.error(error)
  if (warning !== undefined) console.warn(warning)
  if (openapi !== undefined) console.log('Document is valid')
}

run.catch(console.error)

Processing an Incoming Request

const Enforcer = require('@patrtorg/hic-facere-ratione')

async function run () {
  // Because we don't specify `fullResult: true`, any errors will throw an exception and
  // warnings will be logged to the console.
  const openapi = await Enforcer('./path/to/openapi.yml')

  // If the request is valid then the req object will contain the parsed and validated request.
  // If it is invalid then the error will contain details about what was wrong with the
  // request and these details are safe to return to the client that made the request.
  const [ req, error ] = openapi.request({
    method: 'POST',
    path: '/tasks',
    // the body should be parsed by a JSON.parse() prior to passing in (if applicable).
    body: { task: 'Buy Milk', quantity: 2 }
  })

  // You can use the req.operation property to look at the properties from your OpenAPI document.
  // A good use of this is to look at the operationId you defined there to determine which path
  // is being used to handle the request.
  if (req.operaton.operationId === 'my-operation-id') {
    // ... additional request processing
  }
}

run.catch(console.error)

Producing a Valid Result

const Enforcer = require('@patrtorg/hic-facere-ratione')

async function run () {
  const openapi = await Enforcer('./path/to/openapi.yml')

  const [ req ] = openapi.request({
    method: 'POST',
    path: '/tasks',
    // the body should be parsed by a JSON.parse() prior to passing in (if applicable).
    body: { task: 'Buy Milk', quantity: 2 }
  })

  const body = { id: 1, task: 'Buy Milk', quantity: 2, dateCompleted: null }
  const headers = {}

  // This will validate the response code, body, and headers. It will also correctly serialize
  // the body and headers for sending to the client that made the request. Using this method
  // you'll never send back a response that does not match what your OpenAPI document defines.
  const [ res, error ] = req.response(200, body, headers)
  console.log(res.body, res.headers)
}

run.catch(console.error)
3.3.103

10 months ago

3.3.104

10 months ago

3.3.105

10 months ago

3.3.102

10 months ago

3.3.101

10 months ago

3.3.100

10 months ago

3.3.99

10 months ago

2.3.99

10 months ago

2.3.98

10 months ago

2.3.97

10 months ago

2.3.96

10 months ago

2.3.95

10 months ago

2.3.94

10 months ago

2.3.93

10 months ago

2.3.92

10 months ago

2.3.91

10 months ago

2.3.90

10 months ago

2.3.89

10 months ago

2.3.88

10 months ago

2.3.87

10 months ago

2.3.86

10 months ago

2.3.85

10 months ago

1.3.85

10 months ago

1.3.84

10 months ago

1.3.83

11 months ago

1.3.82

11 months ago

1.3.81

11 months ago

1.3.80

11 months ago

1.3.79

11 months ago

1.3.78

11 months ago

1.3.77

11 months ago

1.3.76

11 months ago

1.3.75

11 months ago

1.3.74

11 months ago

1.3.73

11 months ago

1.3.72

11 months ago

1.3.71

11 months ago

1.3.70

11 months ago

1.3.69

11 months ago

1.3.68

11 months ago

1.3.67

11 months ago

1.3.66

11 months ago

1.3.65

11 months ago

1.3.64

11 months ago

1.3.63

11 months ago

1.3.62

11 months ago

1.3.61

11 months ago

1.3.60

11 months ago

1.3.59

11 months ago

1.3.58

11 months ago

1.3.57

11 months ago

1.3.56

11 months ago

1.3.55

11 months ago

1.3.54

11 months ago

1.3.53

12 months ago

1.3.52

12 months ago

1.3.51

12 months ago

1.3.50

12 months ago

1.2.50

12 months ago

1.2.49

12 months ago

1.2.48

12 months ago

1.2.47

12 months ago

1.2.46

12 months ago

1.2.45

12 months ago

1.2.44

12 months ago

1.2.43

12 months ago

1.2.42

12 months ago

1.2.41

12 months ago

1.2.40

12 months ago

1.2.39

12 months ago

1.2.38

12 months ago

1.1.38

12 months ago

1.1.37

12 months ago

1.1.36

12 months ago

1.1.35

1 year ago

1.1.34

1 year ago

1.1.33

1 year ago

1.1.32

1 year ago

1.1.31

1 year ago

1.1.30

1 year ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago