Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

feat: promises rewrite and drop support for node 10 #131

Merged
merged 2 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
reviewers:
- erunion
labels:
- dependencies
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: monthly
reviewers:
- erunion
labels:
- dependencies
commit-message:
prefix: chore(deps)
prefix-development: chore(deps-dev)

- package-ecosystem: npm
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
reviewers:
- erunion
labels:
- dependencies
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install npm@7
run: npm install -g npm@7

- name: Install deps
run: npm ci

- name: Run tests
run: npm test
25 changes: 0 additions & 25 deletions .github/workflows/nodejs.yml

This file was deleted.

3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
__tests__/
.github/
examples/
coverage/
test/
.eslint*
.prettier*
47 changes: 29 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Swagger 2 or OAS 3? YAML or JSON? URL, path, string or object? Who cares! It jus

This module uses a bunch of other great modules to do the heavy lifting, and normalizes everything!

[![Build](https://github.com/readmeio/oas-normalize/workflows/CI/badge.svg)](https://github.com/readmeio/oas-normalize/) [![](https://img.shields.io/npm/v/oas-normalize)](https://npm.im/oas-normalize)

[![](https://cl.ly/1h271F1M1e2T/Untitled-2.png)](http://readme.io)

# Install
Expand All @@ -15,23 +17,22 @@ npm install oas-normalize --save
It's pretty simple:

```javascript
const OAS = require('oas-normalize');

const oas = new OAS('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml'); // Or a string, pathname, JSON blob, whatever
oas.validate((err, spec) => {
if (err) {
console.log(err.errors);
return;
}
console.log(spec); // spec will always be JSON, and valid
const OASNormalize = require('oas-normalize');

const oas = new OASNormalize('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml'); // Or a string, pathname, JSON blob, whatever

oas.validate().then(definition => {
console.log(definition); // definition will always be JSON, and valid
}).catch(err => {
console.log(err.errors);
});
```

# Errors

For validation errors, when available, you'll get back an object:

```
```json
{
"errors": [
{
Expand All @@ -47,22 +48,32 @@ For validation errors, when available, you'll get back an object:

# Helper functions

> **Note:** All of these functions are promise-driven.

If you want some more functionality, you can do anything here:

| Function | What it does |
|----------------|----------------------------------------------------------------|
| oas.load(cb) | Just load the file, valid or not, as JSON |
| oas.bundle(cb) | Bring together all files into one JSON blob (but keep `$refs`) |
| oas.deref(cb) | Resolve `$refs` |
| oas.validate(cb, [convertToLatest?])) | Validate the whole thing! |
| Function | What it does |
| :--- | :--- |
| `.load()` | Just load the file, valid or not, as JSON |
| `.bundle()` | Bring together all files into one JSON blob (but retain `$ref` pointers) |
| `.deref()` | Resolve `$ref` pointers |
| `.validate([convertToLatest?]))` | Validate the whole thing! |

# Other little features

### Always return OAS 3

If you want `.validate` to always return a OAS 3 document, include a `true` as the second param: `oas.validate(action, true);`
If you want `.validate` to always return an OpenAPI 3.0 definition, supply `true` as its argument:

```js
OASNormalize.validate(true).then(...);
```

### Enable local paths

For security reasons, you need to enable it. Use `new OAS('./whatever.json', { enablePaths: true })` to load local files.
For security reasons, you need to enable it; supply `enablePaths` to the class instance:

```js
const oas = new OASNormalize('./petstore.json', { enablePaths: true })
```

File renamed without changes.
46 changes: 46 additions & 0 deletions __tests__/__fixtures__/bundle/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"openapi": "3.0.0",
"servers": [
{
"url": "http://petstore.swagger.io/v2"
}
],
"info": {
"version": "1.0.0",
"title": "Swagger Petstore"
},
"paths": {
"/pet": {
"post": {
"responses": {
"405": {
"description": "Invalid input"
}
},
"requestBody": {
"$ref": "#/components/requestBodies/Pet"
}
}
}
},
"components": {
"requestBodies": {
"Pet": {
"content": {
"application/json": {
"schema": {
"$ref": "./__tests__/__fixtures__/bundle/pet.json"
}
},
"application/xml": {
"schema": {
"$ref": "./__tests__/__fixtures__/bundle/pet.json"
}
}
},
"description": "Pet object that needs to be added to the store",
"required": true
}
}
}
}
26 changes: 26 additions & 0 deletions __tests__/__fixtures__/bundle/pet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "object",
"required": ["name", "photoUrls"],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"readOnly": true
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": ["available", "pending", "sold"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,114 +75,6 @@
}
}
}
},
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "addPet",
"requestBody": {
"description": "Pet to add to the store",
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewPet"
}
}
}
},
"responses": {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/pets/{id}": {
"get": {
"description": "Returns a user based on a single ID, if the user does not have access to the pet",
"operationId": "find pet by id",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to fetch",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"responses": {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"delete": {
"description": "deletes a single pet based on the ID supplied",
"operationId": "deletePet",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to delete",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"responses": {
"204": {
"description": "pet deleted"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
Expand Down
Loading