Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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 }),
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}': {
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}': {
const OpenAPIBackend = require('openapi-backend').default;
const express = require('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}': {
const OpenAPIBackend = require('openapi-backend').default;
const Koa = require('koa');
const bodyparser = require('koa-bodyparser');
const app = new Koa();
app.use(bodyparser());
// 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' },
},
},
post: {
operationId: 'createPet',
const OpenAPIBackend = require('openapi-backend').default;
// 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}': {
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();
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}': {