Skip to content

Commit

Permalink
Adds a draft of "fromTraffic"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 10, 2021
1 parent 2449c35 commit f26a0a2
Show file tree
Hide file tree
Showing 16 changed files with 39,517 additions and 313 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"json.schemas": [
{
"fileMatch": ["test/oas/fixtures/*.json"],
"url": "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/f1adc846131b33be72df6a0c87e5e5da59dde0ff/schemas/v3.0/schema.json"
}
]
}
127 changes: 0 additions & 127 deletions CODE_OF_CONDUCT.md

This file was deleted.

59 changes: 0 additions & 59 deletions CONTRIBUTING.md

This file was deleted.

1 change: 1 addition & 0 deletions PREREQUISITES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [ ] Export `cleanUrl` function from MSW.
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
# Library name
# `@mswjs/source`

> A short description about what your library is.
## Install

## Motivation
```sh
$ npm install @mswjs/source -D
# or
$ yarn add @mswjs/source -D
```

## Sources

### Browser traffic (HAR)

> Elaborate on the reason behind this library: why may people need it? What issues does it solve? How is it different from the similar libraries?
```js
import { fromTraffic } from '@mswjs/source'
import traffic from './github.com.har'

## Getting started
export const handlers = fromTraffic(traffic)
```

### Test runtime

> Go through the steps necessary to install, configure, and use your library.
```js
// jest.setup.js
import { setupServer } from 'msw/node'
import { fromRuntime } from '@mswjs/source'

### Install
const server = setupServer()

```bash
$ npm install <LIBRARY_NAME>
beforeAll(() => {
server.listen()
fromRuntime(server)
})
```

## Documentation
### OpenAPI (Swagger)

> Reference the documentation website, or write the documentation straight in this README file.
- Explain what spec properties are used as mocked responses.

## Contributing
```js
import { fromOpenApi } from '@mswjs/source'
import apiDocument from 'api.spec.json'

Please read the [Contribution guidelines](CONTRIBUTING.md) to start with your awesome contributions!
const apiDocument = fs.readFileSync('spec.json')
export const handlers = fromOpenApi(apiDocument)
```
50 changes: 0 additions & 50 deletions STANDARDS.ts

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"clean": "rimraf ./lib",
"build": "yarn lint && yarn clean && rollup -c rollup.config.ts",
"test": "jest test",
"test:lib": "jest STANDARDS.ts --testRegex=\"(STANDARDS\\.ts)\"",
"prepublishOnly": "yarn build && yarn test:lib && yarn test"
},
"files": [
Expand Down Expand Up @@ -53,7 +52,7 @@
"husky": "^4.3.8",
"jest": "^26.6.3",
"lint-staged": "^10.5.3",
"msw": "^0.26.2",
"msw": "^0.34.0",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"rollup": "^2.38.4",
Expand All @@ -64,6 +63,8 @@
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"openapi-types": "^7.2.3"
"@types/har-format": "^1.2.7",
"openapi-types": "^7.2.3",
"outvariant": "^1.2.0"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { fromOpenAPI } from './oas/fromOpenAPI'
export { fromOpenApi } from './oas/fromOpenApi'
23 changes: 14 additions & 9 deletions src/oas/fromOpenAPI.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { rest } from 'msw'
import { RequestHandler, rest } from 'msw'
import { OpenAPIV3, OpenAPIV2 } from 'openapi-types'
import { parse } from '@apidevtools/swagger-parser'
import * as SwaggerParser from '@apidevtools/swagger-parser'

export async function fromOpenAPI(
const parser = new SwaggerParser()

/**
* Generates request handlers fro the given OpenAPI document.
*/
export async function fromOpenApi(
document: string | OpenAPIV3.Document | OpenAPIV2.Document,
) {
const api = await parse(document)
): Promise<RequestHandler[]> {
const api = await parser.dereference(document)

const handlers = Object.entries(api.paths).reduce<any>(
const handlers = Object.entries(api.paths).reduce<RequestHandler[]>(
(handlers, [url, pathDef]) => {
const nextHandlers = Object.entries(pathDef).map(
([method, def]: [string, any]) => {
if (!(method in rest)) {
return
}

// @ts-expect-error tbd
// @ts-expect-error TBD
const resolvedUrl = api.basePath
? // @ts-expect-error tbd
? // @ts-expect-error TBD
new URL(url, api.basePath).toString()
: url

// @ts-expect-error tbd
// @ts-expect-error TBD
return rest[method](resolvedUrl, (req, res, ctx) => {
const status = req.url.searchParams.get('response') || '200'
const response = def.responses[status]
Expand Down
Loading

0 comments on commit f26a0a2

Please sign in to comment.