Skip to content

Commit fb922c7

Browse files
committed
chore: first commit
0 parents  commit fb922c7

14 files changed

Lines changed: 575 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
3+
config
4+
bin

.npmignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.idea/
2+
.circleci
3+
.github
4+
.gitignore
5+
.prettierrc
6+
.travis.yml
7+
Makefile
8+
node_modules/
9+
.DS_Store
10+
CONTRIBUTE.md
11+
12+
# source
13+
**/*.ts
14+
*.ts
15+
/test
16+
17+
# definitions
18+
!**/*.d.ts
19+
!*.d.ts
20+
21+
# configuration
22+
renovate.json
23+
gulpfile.js
24+
tsconfig.json
25+
tools
26+
/test/jest-config.json
27+
/.vscode
28+
.commitlintrc.json
29+
.eslintrc.js
30+
.eslintignore
31+
.release-it.json
32+
33+
# sample
34+
/sample

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org/

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Router Comparsion - Compare your server response data
2+
3+
Comparing response data from servers when is migrating services.
4+
5+
## Installation:
6+
7+
```
8+
$ npm install -g @somosprte/route-comparison
9+
```
10+
11+
## Flags
12+
13+
`--config` used to specify location of request file(s) to be used in execution of requests
14+
15+
## Configuration
16+
17+
Example `config.yaml`:
18+
19+
```yaml
20+
servers:
21+
original: https://localhost:3333/v1
22+
compare: https://localhost:3333/v2
23+
24+
requests:
25+
/healthcheck:
26+
url: /healthcheck
27+
method: GET
28+
29+
/users:
30+
urls:
31+
- /users/new
32+
- /users/create
33+
method: POST
34+
headers:
35+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxfQ.0Y4AEK4_wc8-qtz7ik1VMWc9bOStRWOCH_MNEm38dq0
36+
data: |
37+
{
38+
"email: "viniciusgutierrez@prte.com.br",
39+
"password": "12345678"
40+
}
41+
ignore: |
42+
{
43+
"data.attributes.avatar"
44+
}
45+
```
46+
47+
## Usage
48+
49+
For multiple files:
50+
```
51+
$ route-comparison --config=requests
52+
```
53+
54+
For specific file:
55+
```
56+
$ route-comparison --config=requests/request.yaml
57+
```

catalog-info.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: backstage.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: lm-router-compairson
5+
annotations:
6+
backstage.io/techdocs-ref: dir:.
7+
github.com/project-slug: somosprte/lm-router-compairson
8+
spec:
9+
type: other
10+
lifecycle: unknown
11+
owner: prte

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Hello World!

mkdocs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: 'Route Comparison'
2+
3+
nav:
4+
- Home: index.md
5+
6+
plugins:
7+
- techdocs-core

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@somosprte/route-comparison",
3+
"version": "0.0.3-ALPHA",
4+
"license": "MIT",
5+
"publisher": "somosprte",
6+
"publishConfig": {
7+
"access": "public"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/somosprte/prte-route-comparison.git"
12+
},
13+
"author": {
14+
"name": "Vinícius Gutierrez da Silva Rocha",
15+
"email": "srgutyerrez@gmail.com"
16+
},
17+
"engines": {
18+
"node": "18.x.x"
19+
},
20+
"bin": {
21+
"route-comparison": "bin/index.js"
22+
},
23+
"scripts": {
24+
"build": "rm -rf bin;tsc"
25+
},
26+
"dependencies": {
27+
"axios": "^1.2.0",
28+
"json-diff": "^1.0.0",
29+
"yaml": "^2.1.3"
30+
},
31+
"devDependencies": {
32+
"@types/json-diff": "^0.9.1",
33+
"@types/node": "^18.11.7",
34+
"@types/yaml": "^1.9.7",
35+
"typescript": "^4.8.4"
36+
}
37+
}

src/index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#! /usr/bin/env node
2+
3+
import { existsSync, readFileSync } from 'fs'
4+
5+
import { readdirSync, statSync } from 'fs'
6+
import { parse } from 'yaml'
7+
8+
import { Validator } from './utils/validator'
9+
import { Request } from './schema/request'
10+
11+
const startComparison = async () => {
12+
let config = process.argv.map((flag) => {
13+
return flag.replace(/(--?|-?)+[A-Za-z]+=/, '')
14+
})[2]
15+
16+
if (!config) {
17+
throw new Error('please provide a config file or directory')
18+
}
19+
20+
if (!existsSync(config)) {
21+
throw new Error(`cannot find config file or directory`)
22+
}
23+
24+
const configDatas: { fileName: string; request: Request }[] = []
25+
26+
if (statSync(config).isDirectory()) {
27+
const files = readdirSync(config)
28+
29+
for await (const file of files) {
30+
configDatas.push({
31+
fileName: file.split('.')[0],
32+
request: parse(String(readFileSync(config.startsWith('/') ? `${config}/${file}` : `./${config}/${file}`))) as Request
33+
})
34+
}
35+
} else {
36+
configDatas.push({
37+
fileName: config.split('/')[config.split('/').length - 1].split('.')[0],
38+
request: parse(String(readFileSync(config.startsWith('/') ? config : `./${config}`))) as Request
39+
})
40+
}
41+
42+
for await (const { fileName, request } of configDatas) {
43+
const original = request['servers'].original
44+
const compare = request['servers'].compare
45+
46+
Object.values(request.requests).forEach(async (request) => {
47+
const validator = new Validator(
48+
[
49+
original + (request.url ?? request.urls![0]),
50+
compare + (request.url ?? request.urls![1])
51+
],
52+
request.method,
53+
request.headers,
54+
request.data,
55+
fileName,
56+
JSON.parse(request.ignore ?? '[]')
57+
)
58+
59+
const responses = await validator.execute()
60+
61+
validator.compare(responses)
62+
})
63+
}
64+
}
65+
66+
startComparison()

src/schema/request.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type Request = {
2+
servers: {
3+
original: string;
4+
compare: string;
5+
}
6+
requests: {
7+
[ key: string ]: {
8+
url?: string
9+
urls?: string[]
10+
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
11+
headers?: object
12+
data?: object
13+
ignore?: string
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)