How to use the openapi-backend function in openapi-backend

To help you get started, we’ve selected a few openapi-backend examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anttiviljami / openapi-backend / examples / express-ts-mock / index.ts View on Github external
import 'source-map-support/register';
import path from 'path';
import OpenAPIBackend from 'openapi-backend';
import express from 'express';
import morgan from 'morgan';

import { Request, Response } from 'express';

const app = express();
app.use(express.json());

// define api
const api = new OpenAPIBackend({
  definition: path.join(__dirname, '..', 'openapi.yml'),
  handlers: {
    validationFail: async (c, req: Request, res: Response) => res.status(400).json({ err: c.validation.errors }),
    notFound: async (c, req: Request, res: Response) => res.status(404).json({ err: 'not found' }),
    notImplemented: async (c, req: Request, res: Response) => {
      const { status, mock } = c.api.mockResponseForOperation(c.operation.operationId);
      return res.status(status).json(mock);
    },
  },
});
api.init();

// logging
app.use(morgan('combined'));

// use as express middleware
github anttiviljami / openapi-backend / examples / serverless-aws / index.ts View on Github external
import 'source-map-support/register';
import * as Lambda from 'aws-lambda';
import OpenAPIBackend from 'openapi-backend';
const headers = {
  'content-type': 'application/json',
  'access-control-allow-origin': '*', // lazy cors config
};

// create api from definition
const api = new OpenAPIBackend({ definition: './openapi.yml' });

// register some handlers
api.register({
  notFound: async (c, event: Lambda.APIGatewayProxyEvent, context: Lambda.Context) => ({
    statusCode: 404,
    body: JSON.stringify({ err: 'not found' }),
    headers,
  }),
  validationFail: async (c, event: Lambda.APIGatewayProxyEvent, context: Lambda.Context) => ({
    statusCode: 400,
    body: JSON.stringify({ err: c.validation.errors }),
    headers,
  }),
  getPets: async (c, event: Lambda.APIGatewayProxyEvent, context: Lambda.Context) => ({
    statusCode: 200,
    body: JSON.stringify({ operationId: c.operation.operationId }),
github anttiviljami / openapi-backend / examples / hapi-typescript / index.ts View on Github external
import 'source-map-support/register';
import OpenAPIBackend from 'openapi-backend';
import Hapi from 'hapi';

const server = new Hapi.Server({ host: '0.0.0.0', port: 9000 });

// define api
const api = new OpenAPIBackend({
  definition: {
    openapi: '3.0.1',
    info: {
      title: 'My API',
      version: '1.0.0',
    },
    paths: {
      '/pets': {
        get: {
          operationId: 'getPets',
          responses: {
            200: { description: 'ok' },
          },
        },
      },
      '/pets/{id}': {
github anttiviljami / openapi-backend / examples / express-typescript / index.ts View on Github external
import 'source-map-support/register';
import OpenAPIBackend from 'openapi-backend';
import express from 'express';
import morgan from 'morgan';

import { Request, Response } from 'express';

const app = express();
app.use(express.json());

// define api
const api = new OpenAPIBackend({
  definition: {
    openapi: '3.0.1',
    info: {
      title: 'My API',
      version: '1.0.0',
    },
    paths: {
      '/pets': {
        get: {
          operationId: 'getPets',
          responses: {
            200: { description: 'ok' },
          },
        },
      },
      '/pets/{id}': {
github anttiviljami / openapicmd / src / commands / mock.ts View on Github external
public async run() {
    const { args, flags } = this.parse(Mock);
    const { port, logger, 'swagger-ui': swaggerui, serveroverride } = flags;

    let portRunning = port;

    const definition = resolveDefinition(args.definition);
    if (!definition) {
      this.error('Please load a definition file', { exit: 1 });
    }

    const api = new OpenAPIBackend({ definition });
    api.register({
      validationFail: (c, ctx) => {
        ctx.status = 400;
        ctx.body = { err: c.validation.errors };
      },
      notFound: (c, ctx) => {
        ctx.status = 404;
        ctx.body = { err: 'not found' };
      },
      notImplemented: (c, ctx) => {
        const { status, mock } = c.api.mockResponseForOperation(c.operation.operationId);
        ctx.status = status;
        ctx.body = mock;
      },
    });
    await api.init();
github anttiviljami / openapi-backend / examples / express-typescript / src / handler.ts View on Github external
import OpenAPIBackend from 'openapi-backend';
import { ErrorObject } from 'ajv';

const dummyHandler = (operationId: string) => async (req: Request, res: Response) => {
  return res.status(200).json({ operationId });
};

const notFoundHandler = async (req: Request, res: Response) => {
  return res.status(404).json({ status: 404, error: 'Not found' });
};

const validationFailHandler = async (errors: ErrorObject[], req: Request, res: Response) => {
  return res.status(400).json({ status: 400, errors });
};

const api = new OpenAPIBackend({
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'api',
      version: '1.0.0',
    },
    paths: {
      '/pets': {
        get: {
          operationId: 'getPets',
          responses: {
            200: { description: 'ok' },
          },
        },
      },
      '/pets/{id}': {

openapi-backend

Build, Validate, Route, Authenticate and Mock using OpenAPI definitions. Framework-agnostic

MIT
Latest version published 3 months ago

Package Health Score

86 / 100
Full package analysis

Popular openapi-backend functions