Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
refactor: separate 'parse' and 'compile'
Browse files Browse the repository at this point in the history
Close #27, close #103

BREAKING CHANGE: There won't be any wrapper code called Dredd Transactions anymore. The public interface now splits into two parts, two explicit steps, two distinct responsibilities: parse, and compile
  • Loading branch information
honzajavorek committed Feb 26, 2019
1 parent 2826ffe commit 99f41c2
Show file tree
Hide file tree
Showing 26 changed files with 79 additions and 276 deletions.
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,46 @@ Dredd Transactions library is written in JavaScript (ES2015+).

## Usage

### `compile`
### `parse`

Compiles *HTTP Transactions* from given API description document.
Parses given API description document into API Elements with options specific
to Dredd. Assumes that documents with unrecognizable format are
[API Blueprint][api-blueprint]. Turns any parser failures, including
the unexpected ones, into [API Elements][api-elements] annotations.

```javascript
var dt = require('dredd-transactions');
const parse = require('dredd-transactions/parse');
// const { parse } = require('dredd-transactions');

dt.compile('# My API\n...', 'apiary.apib', function (error, compileResult) {
parse('# My API\n...', (error, parseResult) => {
// ...
});
```

### Arguments
### `compile`

Compiles *HTTP Transactions* from given [API Elements][api-elements]. *HTTP Transactions* are a backbone data structure to Dredd.

- (string) - API description document provided as string.
- (string) - Original file name of the API description document. **To be removed! See [#6][filename-deprecation].**
- (function) - Callback.
```javascript
const compile = require('dredd-transactions/compile');
// const { compile } = require('dredd-transactions');

### Callback Arguments
const compileResult = compile(mediaType, apiElements, filename);
```

- (enum[null, object]) - Standard JavaScript error object.
- ([Compile Result][compile-result-object-spec])
> **Note:** The `filename` argument is optional and about to get deprecated, see [#6][filename-deprecation]

## Data Structures

<a name="parse-result-object"></a>
### Parse Result (object)

Result of parsing.

- `mediaType`: `text/vnd.apiblueprint` (string, default, nullable) - Media type of the input format, can be empty in case of some fatal errors
- `apiElements` ([API Elements][api-elements]) - API Elements parse result

<a name="compile-result-object"></a>
### Compile Result (object)

Expand Down Expand Up @@ -161,9 +175,10 @@ Description of an error or warning which occurred during parsing of the API desc
> **Note:** These properties are to be superseded by whatever comes out of the proposal in [apiaryio/dredd#227](https://github.com/apiaryio/dredd/issues/227).

[dredd]: https://github.com/apiaryio/dredd
[dredd]: https://dredd.org
[mson-spec]: https://github.com/apiaryio/mson
[api-elements]: http://api-elements.readthedocs.org/
[api-blueprint]: https://apiblueprint.org/
[api-blueprint-glossary]: https://github.com/apiaryio/api-blueprint/blob/master/Glossary%20of%20Terms.md
[blueprint-transactions]: https://github.com/apiaryio/blueprint-transactions/

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const parse = require('./parse');
const compile = require('./compile');


module.exports = { parse, compile };
25 changes: 0 additions & 25 deletions lib/index.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dredd-transactions",
"version": "0.0.0-semantically-released",
"description": "Compiles HTTP Transactions (Request-Response pairs) from an API description document",
"main": "lib/index.js",
"main": "index.js",
"engines": {
"node": ">= 6"
},
Expand All @@ -24,7 +24,8 @@
"url": "https://github.com/apiaryio/dredd-transactions"
},
"files": [
"lib",
"parse",
"compile",
"README.md"
],
"dependencies": {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/pretest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');

const fury = require('fury');

const parse = require('../lib/parse');
const parse = require('../parse');


const FIXTURES_DIR = path.join(__dirname, '..', 'test', 'fixtures');
Expand Down
25 changes: 22 additions & 3 deletions scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,29 @@ if [ ! -z "$TRAVIS" ]; then
"The packaging of Dredd Transactions should have prevented this."
echo "======================================================================"
exit 1
else
echo "SUCCESS"
echo "======================================================================"
fi

echo "Importing dredd-transactions"
echo "======================================================================"
echo "const assert = require('assert');" > index.js
echo "const dt = require('dredd-transactions');" >> index.js
echo "assert.ok(typeof dt.parse === 'function');" >> index.js
echo "assert.ok(typeof dt.compile === 'function');" >> index.js
node index.js

echo "======================================================================"
echo "Importing dredd-transactions/parse and dredd-transactions/compile"
echo "======================================================================"
echo "const assert = require('assert');" > index.js
echo "const parse = require('dredd-transactions/parse');" >> index.js
echo "const compile = require('dredd-transactions/compile');" >> index.js
echo "assert.ok(typeof parse === 'function');" >> index.js
echo "assert.ok(typeof compile === 'function');" >> index.js
node index.js

echo "======================================================================"
echo "SUCCESS"
echo "======================================================================"
else
exit 1
fi
10 changes: 5 additions & 5 deletions test/integration/compile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const createAnnotationSchema = require('../schemas/createAnnotationSchema');
const createCompileResultSchema = require('../schemas/createCompileResultSchema');

const { assert, fixtures } = require('../support');
const compile = require('../../lib/compile');
const compile = require('../../compile');


describe('compile() · all API description formats', () => {
Expand Down Expand Up @@ -146,8 +146,8 @@ describe('compile() · all API description formats', () => {
// we need to pretend it's possible in this test.
fixtures('ordinary').forEachDescribe(({ mediaType, apiElements }) => {
const message = '... dummy warning message ...';
const stubbedCompile = proxyquire('../../lib/compile', {
'./compileURI': proxyquire('../../lib/compileURI', {
const stubbedCompile = proxyquire('../../compile', {
'./compileURI': proxyquire('../../compile/compileURI', {
'./expandURItemplate': () => ({ uri: '/honey?beekeeper=Honza', errors: [], warnings: [message] }),
}),
});
Expand Down Expand Up @@ -207,8 +207,8 @@ describe('compile() · all API description formats', () => {
// test.
fixtures('ordinary').forEachDescribe(({ mediaType, apiElements }) => {
const message = '... dummy warning message ...';
const stubbedCompile = proxyquire('../../lib/compile', {
'./compileURI': proxyquire('../../lib/compileURI', {
const stubbedCompile = proxyquire('../../compile', {
'./compileURI': proxyquire('../../compile/compileURI', {
'./validateParams': () => ({ errors: [], warnings: [message] }),
}),
});
Expand Down
8 changes: 4 additions & 4 deletions test/integration/compileAPIB-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const proxyquire = require('proxyquire').noPreserveCache();

const createAnnotationSchema = require('../schemas/createAnnotationSchema');
const createCompileResultSchema = require('../schemas/createCompileResultSchema');
const detectTransactionExampleNumbers = require('../../lib/detectTransactionExampleNumbers');
const detectTransactionExampleNumbers = require('../../compile/detectTransactionExampleNumbers');

const { assert, fixtures } = require('../support');
const compile = require('../../lib/compile');
const compile = require('../../compile');


describe('compile() · API Blueprint', () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('compile() · API Blueprint', () => {

describe('with multiple transaction examples', () => {
const detectTransactionExampleNumbersStub = sinon.spy(detectTransactionExampleNumbers);
const stubbedCompile = proxyquire('../../lib/compile', {
const stubbedCompile = proxyquire('../../compile', {
'./detectTransactionExampleNumbers': detectTransactionExampleNumbersStub,
});
const { mediaType, apiElements } = fixtures('multiple-transaction-examples').apib;
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('compile() · API Blueprint', () => {

describe('without multiple transaction examples', () => {
const detectTransactionExampleNumbersStub = sinon.spy(detectTransactionExampleNumbers);
const stubbedCompile = proxyquire('../../lib/compile', {
const stubbedCompile = proxyquire('../../compile', {
'./detectTransactionExampleNumbers': detectTransactionExampleNumbersStub,
});
const { mediaType, apiElements } = fixtures('one-transaction-example').apib;
Expand Down
6 changes: 3 additions & 3 deletions test/integration/compileOpenAPI2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const proxyquire = require('proxyquire');

const createAnnotationSchema = require('../schemas/createAnnotationSchema');
const createCompileResultSchema = require('../schemas/createCompileResultSchema');
const detectTransactionExampleNumbers = require('../../lib/detectTransactionExampleNumbers');
const detectTransactionExampleNumbers = require('../../compile/detectTransactionExampleNumbers');

const { assert, fixtures } = require('../support');
const compile = require('../../lib/compile');
const compile = require('../../compile');


describe('compile() · OpenAPI 2', () => {
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('compile() · OpenAPI 2', () => {
describe('with multiple responses', () => {
const filename = 'apiDescription.json';
const detectTransactionExampleNumbersStub = sinon.spy(detectTransactionExampleNumbers);
const stubbedCompile = proxyquire('../../lib/compile', {
const stubbedCompile = proxyquire('../../compile', {
'./detectTransactionExampleNumbers': detectTransactionExampleNumbersStub,
});
const { mediaType, apiElements } = fixtures('multiple-responses').openapi2;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/compileOpenAPI3-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const createCompileResultSchema = require('../schemas/createCompileResultSchema');

const { assert, fixtures } = require('../support');
const compile = require('../../lib/compile');
const compile = require('../../compile');


describe('compile() · OpenAPI 3', () => {
Expand Down
Loading

0 comments on commit 99f41c2

Please sign in to comment.