1.0.2 • Published 2 years ago

@x82-softworks/aws-api v1.0.2

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
2 years ago

@x82-softworks/aws-api

An OpenAPI compatible API builder for AWS API Gateway and Lambda

This project is intended to create a proxy lambda setup with API Gateway. It smooths over many problems with standard express hosting as it is tailored to the AWS lambda environment.

Auto generated docs

https://x82-open-source.gitlab.io/npm/aws-api

Example usage

// src/index.js
import spec from "./spec"

export const handler = spec.lambda()
// src/spec.js
import { STRING, create } from "@x82-softworks/aws-api"
import routes from "./routes"
import selfPkg from "../package.json"

const CORS_WHITELIST = (process.env.CORS_WHITELIST || "").split(",")
const SUPPORT_EMAIL = process.env.SUPPORT_EMAIL
const HOST = process.env.HOST
const VERSION = process.env.VERSION

const spec = create({
  info: {
    version: VERSION,
    description: selfPkg.description,
    title: HOST,
    contact: {
      name: "API Support",
      email: SUPPORT_EMAIL,
    },
  },
  host: HOST,
  basePath: VERSION ? "/" + VERSION + "/" : "/",
  security: [
    {
      apiKey: [],
    },
  ],
})
//Set the default cors
spec.cors((req, res) => {
  try {
    let origin = new URL(req.headers.origin).hostname
    if (CORS_WHITELIST.indexOf(origin) !== -1) {
      res.headers["Access-Control-Allow-Origin"] = req.headers.origin
    }
  } catch (err) {
    //ignore
  }

  res.headers["Access-Control-Allow-Methods"] =
    "GET,OPTIONS,HEAD,PUT,PATCH,DELETE,POST"
  res.headers["Access-Control-Allow-headers"] =
    "Content-Type,authorization,x-date"
})
//Now load the schemas
routes(spec)
export default spec
// src/routes
import example from "./example"
import openapi from "./openapi"
export default (spec, services) => {
  ;[example, openapi].forEach(routeController =>
    routeController(spec, services)
  )
}
// src/openapi
export default spec => {
  spec.get(
    "/openapi.json",
    {
      description: "Retrieves the OpenApi spec doc",
      responses: {
        200: {
          description,
          content: {
            "application/json": {
              schema: {},
            },
          },
        },
      },
      //No security needed
      security: [],
    },
    async (req, res) => {
      res.body = spec.getRoot()
    }
  )
}
import { ARRAY, BOOLEAN, INTEGER, NUMBER, OBJECT, STRING } from '@x82-softworks/aws-api';
// src/example
export default spec => {
  spec.post(
    '/example/endpoint',
    {
      description: 'Retrieves the OpenApi spec doc',
      requestBody: {
            description:'The json input',
            content: {
            'application/json': {
                schema: {
                    properties:{
                            fizz: {
                                    type: STRING,
                                    description: 'Your fizz value'
                                },
                                bang: {
                                    type: NUMBER,
                                    description: 'Your bang amount'
                                }
                    },
                    additionalProperties: false,
                    required: required: ['fizz','bang']
                    }
                }
            }
        },
        responses:{
            200: {
            description,
            content: {
            'application/json': {
                    schema:{
                    description: 'The result',
                    type: OBJECT,
                    properties: {
                        fizz: {
                                type: STRING,
                                description: 'Your fizz value'
                            },
                            bang: {
                                type: NUMBER,
                                description: 'Your bang amount'
                            }
                    }
                }
            }
        }
      },
      //No security needed
      security: []
    },
    async (req, res) => {
      res.body = {
          fizz :req.body.fizz,
          bang: req.body.bang
      };
    }
  );
};
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.2.3

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.2

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.2

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago