# varkes-openapi-mock

> Open API Mock

Latest version **0.3.19** (published 2019-03-04) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install varkes-openapi-mock
pnpm add varkes-openapi-mock
yarn add varkes-openapi-mock
bun add varkes-openapi-mock
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.19 |
| Published | 2019-03-04 |
| First published | 2018-10-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 123.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Maintainers | badrawi |

## Links

- npm: https://www.npmjs.com/package/varkes-openapi-mock
- Repository: https://github.com/kyma-incubator/varkes
- Homepage: https://github.com/kyma-incubator/varkes#readme
- Issues: https://github.com/kyma-incubator/varkes/issues
- npm.io page: https://npm.io/package/varkes-openapi-mock

## Dependencies (9)

- [path](https://npm.io/package/path.md) ^0.12.7
- [util](https://npm.io/package/util.md) ^0.11.1
- [morgan](https://npm.io/package/morgan.md) ^1.9.1
- [express](https://npm.io/package/express.md) ^4.16.4
- [winston](https://npm.io/package/winston.md) ^3.2.1
- [body-parser](https://npm.io/package/body-parser.md) ^1.18.3
- [api-spec-converter](https://npm.io/package/api-spec-converter.md) ^2.7.31
- [json-to-pretty-yaml](https://npm.io/package/json-to-pretty-yaml.md) ^1.2.2
- [swagger-express-middleware](https://npm.io/package/swagger-express-middleware.md) ^2.0.1

## Recent versions

- 0.3.19 (latest) — 2019-03-04
- 0.3.18 — 2019-02-10
- 0.3.17 — 2019-02-07
- 0.3.16 — 2019-02-01
- 0.3.15 — 2019-01-31
- 0.3.14 — 2019-01-31
- 0.3.13 — 2019-01-31
- 0.3.12 — 2019-01-22
- 0.3.11 — 2019-01-21
- 0.3.10 — 2019-01-21
- 0.3.9 — 2019-01-21
- 0.3.8 — 2019-01-17
- 0.3.7 — 2019-01-16
- 0.3.6 — 2019-01-14
- 0.3.5 — 2019-01-14
- … 60 more at https://npm.io/package/varkes-openapi-mock/versions

## README

# OpenAPI Mock
[![npm version](https://badge.fury.io/js/varkes-openapi-mock.svg)](https://badge.fury.io/js/varkes-openapi-mock)

Mocks res calls given an OpenAPI specification 

After startup, you have access to:
- rest API - http://localhost:10000/myApiBasePath/metadata
- console - http://localhost:10000/myApiBasePath/console

## Starting the application

### Run local

To run it local, specify a config file (as located in the /test folder) like that:
```
npm install
node server/server <varkes_config.js>
```

### Run local using docker

To run it using docker, call:
```
docker run -p 10000:10000 eu.gcr.io/kyma-project/incubator/develop/varkes-openapi-mock:latest
```

### Run in Kyma

To run the mock using Kyma as runtime envrironment, run the following kubectl command to set up a namespace:

```bash
kubectl create namespace mocks
kubectl label namespace mocks env=true
```

and to deploy the mock
```bash
kubectl apply -n mocks -f https://raw.githubusercontent.com/kyma-incubator/varkes/master/openapi-mock/deployment/deployment.yaml
```

## Features

- **Supports OpenAPI 3.0 specs in JSON or YAML** <br>
OpenAPI Mock uses [Swagger-Express-Middleware](https://github.com/BigstickCarpet/swagger-express-middleware) to parse, validate, and dereference OpenAPI files.  You can also create your custom implementation for a response and for errors.

- **Records Every Request made to the node** <br>
Creates a requests.log file that contains the urls being called, the header of the request and the body of the request if exists using the [morgan](https://www.npmjs.com/package/morgan) logging framework.

- **Returns the OpenAPI specification as metadata** <br>
By calling '/metadata' user can see the OpenAPI specification being use in text/x-yaml format

- **Returns a dummy OAuth2 token** <br>
By calling the endpoint '/authorizationserver/oauth/token' and adding the OAuth2 requirements as query params user can get a dummy OAuth2 token

## Installation and Use

Install using [NPM](https://docs.npmjs.com/getting-started/what-is-npm).

````bash
npm install
````
Then you need to copy your OpenAPI yaml into the api/swagger directory as [swagger.yml](https://github.com/kyma-incubator/varkes/blob/master/examples/openapi-app/swagger.yaml)<br>
OR you could simply change the path in the [config.js](https://github.com/kyma-incubator/varkes/blob/master/examples/openapi-app/config.js) file specified by the "specification" element

You need to remove the host in order for swagger-express-middleware to do it's magic
<br>
You don't need to write a custom response in your javascript code for every endpoint in the file,
for the endpoints that your are satisfied with a default response you add the default key with the response object to your response of the endpoint in the file as shown

````yaml
responses:
        200:
          description: "OK"
          schema:
            course:
              code:
                type: "string"
              name:
                type: "string"
            default:
              course:
                code: "C1"
                name: "course1"
````

Node js code
--------------------------
The entry point for the application is the app.js file which reads the swagger file [swagger.yml](https://github.com/kyma-incubator/varkes/blob/master/examples/openapi-app/swagger.yaml) and creates an instance of the [mock_controller](https://github.com/kyma-incubator/varkes/blob/master/examples/openapi-app/api/mocks/mock_controller.js) where the user  

- **Return custom Error messages as response to certain error codes or messages in the customErrorResponses function** <br>
        the following example checks if the error status is not known or if it's one of the status that are defined in the [config.js](https://github.com/kyma-incubator/varkes/blob/master/openapi-mock/test/config.js) file and in response sends the corresponding error message

````javascript
 app = app_modified;
app.use(function (err, req, res, next) {
    if (!err.status) {
        err.status = 500;
    }
    try {
        res.status(err.status);
        res.type('json');
        res.send(util.format(config.error_messages[err.status]));
    }
    catch (err) {
        console.error(err)
    }
});
````
[Config.js](https://github.com/kyma-incubator/varkes/blob/master/openapi-mock/test/config.js)
--------------------------
In this file you define the paths of all the important files like the [swagger.yml](https://github.com/kyma-incubator/varkes/blob/master/openapi-mock/swagger.yaml) file and the requests.log file.You can also add any kind of global element needed for the application. You also define all the custom error messages corresponding to their status code as following

````javascript
module.exports = {
    specification: 'swagger.yaml',
    request_log_path: 'requests.log',
    added_endpoints: [
        {
            filePath: "OAuth_template.yaml",
            url: '/authorizationserver/oauth/token'
        }
    ],
    customResponsePath: '../../custom_responses',
    error_messages: {
        500: '{"error":\"Something went Wrong\"}',
        400: '{"error":\"Errorrrr\"}',
        404: '{"error":\"End Point not found\"}'
    },
    port: 10000
}
````

---
_Source: https://npm.io/package/varkes-openapi-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
