Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit 8bbd970

Browse files
Adding JS programming guide and fixes for the Java one
1 parent e3b63e5 commit 8bbd970

File tree

4 files changed

+164
-11
lines changed

4 files changed

+164
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ The output NPM package will be generated at `output/node`.
131131
The programming guide the Java Bindings can be found [here](doc/java.md).
132132
Javadoc for the bindings can be consulted [here](https://raml-org.github.io/api-modeling-framework/doc/java/apidocs/index.html).
133133

134+
### JS Bindings
135+
136+
The programming guide the JS Bindings can be found [here](doc/js.md).
137+
134138
### API Modeling Framework Clojurescript/Web library
135139

136140
``` shell

bindings/js/src/examples/BasicFunctionality.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {Type} from "../core/domain/Type";
1111
import {AMF} from "../../index";
1212

1313
// Parsing
14-
let apiFile = "file:///Users/antoniogarrote/Development/api-modelling-framework/resources/other-examples/world-music-api/api.raml";
15-
AMF.RAMLParser.parseFile(apiFile, {}, (err, model) => {
14+
let apiFile = "http://test.com/something/api.raml";
15+
let cacheDirs = {'cacheDirs': {"http://test.com/something":"/Users/antoniogarrote/Development/api-modelling-framework/resources/other-examples/world-music-api"}}
16+
AMF.RAMLParser.parseFile(apiFile, cacheDirs, (err, model) => {
1617
try {
1718
console.log("BACK FROM PARSING");
1819
console.log(err == null);

doc/java.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ After Maven has finished you should have an additional jar in the `target/amf-ja
2525

2626
Parsers can be found in the `org.raml.amf.parsers` package of the project. They can be build using the factories in `org.raml.amf.AMF`
2727

28-
``` java
28+
```java
2929
DocumentModel model = AMF.RAMLParser().parseFile(new URL("http://test.com/worldmusic/api.raml"));
3030
```
3131

@@ -35,7 +35,7 @@ Parsers can accept options, including a hash-map of URLs to local directories th
3535

3636
For instance, in the next snippet all remote references to the URLs prefixed by `http://test.com/worldmusic` will be resolved looking into the local directory `/Users/antoniogarrote/world-music-api`.
3737

38-
``` java
38+
```java
3939
HashMap<String,String> cacheDirs = new HashMap<>();
4040
cacheDirs.put("http://test.com/worldmusic","/Users/antoniogarrote/vocabs/world-music-api");
4141
ParsingOptions options = new ParsingOptions().setCacheDirs(cacheDirs);
@@ -52,7 +52,7 @@ No matter what is the actual Document Model class, the returned model will also
5252

5353
These references can be listed using the `references` method, and new instances of `DocumentModel` can be built for these references using the `modelForReference` method:
5454

55-
``` java
55+
```java
5656
for (URL ref : model.references()) {
5757
DocumentModel refModel = model.modelForReference(ref);
5858
System.out.println("Found a reference model: " + refModel);
@@ -63,7 +63,7 @@ for (URL ref : model.references()) {
6363

6464
To run the resolution algorithm and combine all the documents from the Document Model into a single Domain Model description, the method `resolve` can be invoked.
6565

66-
``` java
66+
```java
6767
DocumentModel resolvedModel = model.resolve();
6868
```
6969

@@ -75,7 +75,7 @@ Fragments return the encoded Domain Model element using the `encodes` method fro
7575
Modules returns the list of declared Domain Model elements using the `declares` method from the `org.raml.amf.core.document.DeclaresDomainModel` interface.
7676
Documents can use both methods to retrieve the top level encoded element and the list of declared elements in the root element.
7777

78-
``` java
78+
```java
7979
if (model instanceof EncodesDomainModel) {
8080
System.out.println(model.encodes());
8181
}
@@ -93,7 +93,7 @@ if (targetModel instanceof DeclaresDomainModel) {
9393
The Domain Model includes Java bean classes for all elements in the AMF Domain Model.
9494
These getters and setters can be used to navigate and mutate the model. Please, refer to the [documentation](https://raml-org.github.io/api-modeling-framework/doc/java/apidocs/index.html) for more details.
9595

96-
``` java
96+
```java
9797
APIDocumentation api = (APIDocumentation) model.encodes();
9898

9999
for (EndPoint endpoint : api.getEndpoints()) {
@@ -107,7 +107,7 @@ AMF includes generators capable of serialising the AMF model back into one of th
107107
Factory methods for each generator can be found in the `org.raml.amf.AMF` class.
108108

109109

110-
``` java
110+
```java
111111
// Generating RAML
112112
// Generate can accept just the model
113113
String generated = AMF.RAMLGenerator().generateString(targetModel);
@@ -133,5 +133,5 @@ System.out.println(generated);
133133
```
134134

135135
Two options are available when generating JSON-LD documents.
136-
`setFullGraph` will nest the JSON-LD graphs for the referenced documents in the model to be serialised, otherwise only URIs will be generated.
137-
`setSourceMapGeneration` enables or disables the generation of source maps JSON-LD information in the output.
136+
- `setFullGraph` will nest the JSON-LD graphs for the referenced documents in the model to be serialised, otherwise only URIs will be generated.
137+
- `setSourceMapGeneration` enables or disables the generation of source maps JSON-LD information in the output.

doc/js.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# AMF JS Programming Guide
2+
3+
JavaScript wrapping is available as a node NPM package.
4+
This package provides a JS friendly interface on top of the native ClojureScript code of the library.
5+
6+
## Compiling
7+
8+
At this moment we don't provide any artifacts in any repository to use AMF or these JS bindings, so they must be built manually with the help of Leiningen.
9+
In order to build the NPM package, you first need to generate the node version of the AMF library. This can be accomplished using the following command:
10+
11+
``` bash
12+
$ lein node
13+
```
14+
15+
When the compilation has finished the node package will be available in `output/node`. Next step is linking the package directory so it will be avaliable for NPM.
16+
17+
``` bash
18+
$ cd output/node && npm link
19+
```
20+
21+
Finally we need to compile and link the JS bindings package. From the AMF top level directory execute:
22+
23+
``` bash
24+
$ cd bindings/js
25+
$ npm link api-modeling-framework
26+
$ npm install
27+
$ tsc
28+
$ npm link
29+
```
30+
31+
Now you can go to your NPM project directory and link the AMF JS bindings:
32+
33+
``` bash
34+
$ npm link amf-js
35+
```
36+
37+
If you start a node REPL in your project now you should be able to require the library:
38+
39+
``` javascript
40+
require("amf-js")
41+
```
42+
43+
## Parsing
44+
45+
Parsers can be found in the `parsers` package of the project. They can be build using the factories in the `AMF` class
46+
47+
``` typescript
48+
let apiFile = "file:///path/to/other-examples/world-music-api/api.raml";
49+
AMF.RAMLParser.parseFile(apiFile, {}, (err, model) => {
50+
if (err != null) {
51+
console.log(`Created model from file ${model.location()}`);
52+
}
53+
});
54+
```
55+
56+
Parsers are include for RAML, OpenAPI and the JSON-LD serialisation of the AMF model.
57+
58+
Parsers can accept options, including a hash-map of URLs to local directories that will be used to resolve references in the parsed documents.
59+
60+
For instance, in the next snippet all remote references to the URLs prefixed by `http://test.com/worldmusic` will be resolved looking into the local directory.
61+
62+
```typescript
63+
let apiFile = "http://test.com/something/api.raml";
64+
let cacheDirs = {'cacheDirs': {"http://test.com/something":"file:///path/to/other-examples/world-music-api/api.raml"}};
65+
AMF.RAMLParser.parseFile(apiFile, cacheDirs, (err, model) => {
66+
if (err != null) {
67+
console.log(`Created model from file ${model.location()}`);
68+
}
69+
});
70+
```
71+
The original parsed text can be retrieved using the `rawText` method.
72+
73+
74+
## Navigating the Document Model
75+
The parsing process will return an instance of one of the subclasses of `DocumentModel`.
76+
Depending on what is the parsed file, a `Document`, a `Fragment` or a `Module` instance will be returned.
77+
78+
No matter what is the actual Document Model class, the returned model will also include references to all linked documents in the model.
79+
80+
These references can be listed using the `references` method, and new instances of `DocumentModel` can be built for these references using the `modelForReference` method:
81+
82+
```typescript
83+
let referenceModels = model
84+
.references()
85+
.map(ref => model.modelForReference(ref));
86+
```
87+
## Applying resolution
88+
89+
To run the resolution algorithm and combine all the documents from the Document Model into a single Domain Model description, the method `resolve` can be invoked.
90+
91+
```typescript
92+
const resolvedModel: DocumentModel = model.resolve();
93+
```
94+
95+
## Accessing the Domain Model
96+
97+
The parsed Domain Model can be retrieved from the Document Model instance using the appropriate accessor.
98+
99+
Fragments return the encoded Domain Model element using the `encodes` method from the `document.EncodesDomainModel` interface.
100+
Modules returns the list of declared Domain Model elements using the `declares` method from the `document.DeclaresDomainModel` interface.
101+
Documents can use both methods to retrieve the top level encoded element and the list of declared elements in the root element.
102+
103+
```typesocript
104+
if (model instanceof EncodesDomainModel) {
105+
console.log(model.encodes());
106+
}
107+
108+
if (targetModel instanceof DeclaresDomainModel) {
109+
model.declares().foreach(decl => console.log(decl);
110+
}
111+
```
112+
## Navigating and mutating the Domain Model
113+
114+
The Domain Model includes matching classes for all elements in the AMF Domain Model.
115+
These getters and setters can be used to navigate and mutate the model. Please, refer to the [documentation](https://raml-org.github.io/api-modeling-framework/doc/js/apidocs/index.html) for more details.
116+
117+
```typescript
118+
let endpoints = api.getEndPoints();
119+
120+
// updating endponts
121+
const before = endpoints.length;
122+
let newEndpoint = EndPoint.build("http://test.com/external/1");
123+
newEndpoint.setPath("/lala");
124+
125+
const afterBreakPoints = endpoints.concat([newEndpoint]);
126+
api.setEndPoints(afterBreakPoints);
127+
128+
// updated endpoints
129+
endpoints = api.getEndPoints();
130+
const after = endpoints.length;
131+
132+
console.log("Should be true " + (after = before + 1));
133+
```
134+
## Serialisation
135+
136+
AMF includes generators capable of serialising the AMF model back into one of the supported syntaxes. The method `generateString` can be used to generate a String representation, and the method `generateFile` can be used to dump the serialised model directly into a file.
137+
Factory methods for each generator can be found in the `AMF` class.
138+
139+
140+
```typescript
141+
AMF.RAMLGenerator.generateString(model, null, null, (e, r) => console.log(r != null));
142+
AMF.OpenAPIGenerator.generateString(model, null, null, (e, r) => console.log(r != null));
143+
AMF.JSONLDGenerator.generateString(model, null, {'full-graph?': true, 'source-maps?': true}, (e, r) => r != null);
144+
```
145+
146+
Two options are available when generating JSON-LD documents.
147+
- `full-graph?` will nest the JSON-LD graphs for the referenced documents in the model to be serialised, otherwise only URIs will be generated.
148+
- `source-maps?` enables or disables the generation of source maps JSON-LD information in the output.

0 commit comments

Comments
 (0)