|
| 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