3.3.105 • Published 12 months ago

@patrtorg/hic-facere-ratione v3.3.105

Weekly downloads
-
License
MIT
Repository
github
Last release
12 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

12 months ago

3.3.104

12 months ago

3.3.105

12 months ago

3.3.102

12 months ago

3.3.101

12 months ago

3.3.100

12 months ago

3.3.99

1 year ago

2.3.99

1 year ago

2.3.98

1 year ago

2.3.97

1 year ago

2.3.96

1 year ago

2.3.95

1 year ago

2.3.94

1 year ago

2.3.93

1 year ago

2.3.92

1 year ago

2.3.91

1 year ago

2.3.90

1 year ago

2.3.89

1 year ago

2.3.88

1 year ago

2.3.87

1 year ago

2.3.86

1 year ago

2.3.85

1 year ago

1.3.85

1 year ago

1.3.84

1 year ago

1.3.83

1 year ago

1.3.82

1 year ago

1.3.81

1 year ago

1.3.80

1 year ago

1.3.79

1 year ago

1.3.78

1 year ago

1.3.77

1 year ago

1.3.76

1 year ago

1.3.75

1 year ago

1.3.74

1 year ago

1.3.73

1 year ago

1.3.72

1 year ago

1.3.71

1 year ago

1.3.70

1 year ago

1.3.69

1 year ago

1.3.68

1 year ago

1.3.67

1 year ago

1.3.66

1 year ago

1.3.65

1 year ago

1.3.64

1 year ago

1.3.63

1 year ago

1.3.62

1 year ago

1.3.61

1 year ago

1.3.60

1 year ago

1.3.59

1 year ago

1.3.58

1 year ago

1.3.57

1 year ago

1.3.56

1 year ago

1.3.55

1 year ago

1.3.54

1 year ago

1.3.53

1 year ago

1.3.52

1 year ago

1.3.51

1 year ago

1.3.50

1 year ago

1.2.50

1 year ago

1.2.49

1 year ago

1.2.48

1 year ago

1.2.47

1 year ago

1.2.46

1 year ago

1.2.45

1 year ago

1.2.44

1 year ago

1.2.43

1 year ago

1.2.42

1 year ago

1.2.41

1 year ago

1.2.40

1 year ago

1.2.39

1 year ago

1.2.38

1 year ago

1.1.38

1 year ago

1.1.37

1 year ago

1.1.36

1 year 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