Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lvqq/aries

🚀 A openapi tool based on swagger document
https://github.com/lvqq/aries

aries markdown mock openapi request swagger typescript

Last synced: 27 days ago
JSON representation

🚀 A openapi tool based on swagger document

Awesome Lists containing this project

README

        

[![npm version](https://img.shields.io/npm/v/@tooltik/aries.svg)](https://www.npmjs.com/package/@tooltik/aries) [![npm download count](https://img.shields.io/npm/dm/@tooltik/aries.svg)](https://www.npmjs.com/package/@tooltik/aries) [![coverage](https://coveralls.io/repos/github/lvqq/aries/badge.svg?branch=main)](https://coveralls.io/github/lvqq/aries?branch=main) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lvqq/aries/CI) [![license](https://img.shields.io/npm/l/@tooltik/aries.svg)](LICENSE)

## Aries ♈️
Aries is a swagger converter cli, support typescript declaration, axios request function, markdown docs, mock server and so on

## Support
Swagger 2.0, json or yaml format

## Env
NodeJS >= 12

## Install
```bash
# with npm
npm install @tooltik/aries --save-dev

# with yarn
yarn add @tooltik/aries --dev

# with pnpm
pnpm add @tooltik/aries --save-dev
```

## Usage in command line
```
aries [options]
```

### Samples

Generate **typescript declaration**, get `generate.ts`:
```bash
aries to-ts -u ./swagger.json -o ./generate.ts
```

Generate **request function** and **typescript declaration**, get `request.ts` and `request.types.ts`
```bash
aries to-ts -u ./swagger.json -o ./request.ts
```

Generate **markdown** doc, get `doc.md`:
```bash
aries to-ts -u ./swagger.json -o ./doc.md
```

Start a **mock server** :
```bash
aries mock-server -u ./swagger.json
```

### Command options
```bash
Commands:
to-ts [options] Convert swagger to typescript declaration
to-request [options] Convert swagger to request function, generate file and .types file
to-md [options] Convert swagger to markdown docs
to-mock [options] Convert swagger to mock json
mock-server [options] Start a local server to return the mock interface
```

#### to-ts
```
Options:
-u, --url Swagger link to generate, support relative path or remote url
-o --output Specify output file path (default: "./swagger.types.ts")
-p --pattern Pattern to be matched for request path, support glob rule
--no-autoRequired Do not generate the property as required automatically when there is no required array in definitions
```

#### to-request
```
Options:
-u, --url Swagger link to generate, support relative path or remote url
-o --output Specify output file path (default: "./swagger.request.ts")
-p --pattern Pattern to be matched for request path, support glob rule
--no-autoRequired Do not generate the property as required automatically when there is no required array in definitions
```

#### to-md
```
Options:
-u, --url Swagger link to generate, support relative path or remote url
-o --output Specify output file path (default: "./swagger.docs.md")
-p --pattern Pattern to be matched for request path, support glob rule
--no-autoMock Do not generate the mock samples automatically when there is no example in schema
```

#### to-mock
```
Options:
-u, --url Swagger link to generate, support relative path or remote url
-o --output Specify output file path (default: "./swagger.mock.json")
-p --pattern Pattern to be matched for request path, support glob rule
--no-autoMock Do not generate the mock response automatically when there is no example in schema
```

#### mock-server
```
Options:
-u, --url Swagger link, support relative path or remote url
-p, --port Mock server port (default: 3000)
-p --pattern Pattern to be matched for request path, support glob rule
--no-autoMock Do not generate the mock response automatically when there is no example in schema
```

### .ariesrc
Add `.ariesrc.ts` or `.ariesrc.js` in your project root directory. Support all options in command. And the command line has a higher priority than `.ariesrc`

| Property | Type | Required | Default | Description |
| ----- | ----- | ----- | ----- | ----- |
| url | string | Y | - | swagger url, support relative path and remote url |
| output | string | Y | - | output file path |
| pattern | string[] | N | ['/**/*'] | Glob rule to be matched for request path |
| autoMock | boolean | N | true | we use example to generate mock data by default, using `autoMock` to determine whether mock automatically when there is no example in schema when using `to-md/to-mock/mock-server` |
| formatMock | function | N | - | format the mock response or samples when using `to-md/to-mock/mock-server`, the parameter is origin mock data generated by example or autoMock |
| autoRequired | boolean | N | true | determine whether the property is required by default when there is no required property in schema when using `to-ts/to-request` |
| port | number | N | 3000 | change the mock server port when using `mock-server` |

#### .ariesrc samples
```typescript
// .ariesrc.ts
import { defineConfig } from '@tooltik/aries'

export default defineConfig({
url: './swagger.json',
autoMock: true,
autoRequired: true,
})
```

## Api reference
Support usage in esm or cjs, and arguments are same with `.ariesrc` export:

```javascript
import { toTs, toRequest, toMd, mockServer } from '@tooltik/aries'

toTs({
url: './test/swagger.json',
output: './test/output.node.swagger.types.ts',
autoRequired: true,
})

toRequest({
url: './test/swagger.json',
output: './test/output.node.swagger.types.ts',
autoRequired: true,
})

toMd({
url: './test/swagger.json',
output: './test/output.node.swagger.docs.md',
pattern: '/user/**/*',
autoMock: true,
formatMock: (data) => {
return {
code: 0,
msg: 'success',
data,
}
},
})

mockServer({
url: './test/swagger.json',
port: 3000,
autoMock: true,
formatMock: (data) => {
return {
code: 0,
msg: 'success',
data,
}
},
})
```

## Dev
Install
```bash
pnpm install
```

Link package after build
```bash
pnpm build & pnpm link --global
```

Run dev watch
```bash
pnpm dev
```

Make changes and run test
```bash
pnpm test
```

Uninstall
```bash
pnpm rm --global @tooltik/aries
```

## LICENSE
MIT