-
Notifications
You must be signed in to change notification settings - Fork 2
Update README #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
engbergandreas
wants to merge
3
commits into
master
Choose a base branch
from
feature/update-readme
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update README #58
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| { | ||
| "MD003": { | ||
| "style": "atx" | ||
| }, | ||
| "MD004": { | ||
| "style": "dash" | ||
| }, | ||
| "MD007": { | ||
| "indent": 2, | ||
| "start_indented": true | ||
| }, | ||
| "MD012": false, | ||
| "MD013": false, | ||
| "MD022": false, | ||
| "MD024": { | ||
| "siblings_only": true | ||
| }, | ||
| "MD029": { | ||
| "style": "one" | ||
| }, | ||
| "MD031": false, | ||
| "MD032": false, | ||
| "MD033": false, | ||
| "MD035": { | ||
| "style": "---" | ||
| }, | ||
| "MD041": { | ||
| "allow_preamble": true | ||
| }, | ||
| "MD046": { | ||
| "style": "fenced" | ||
| }, | ||
| "MD048": { | ||
| "style": "backtick" | ||
| }, | ||
| "MD049": { | ||
| "style": "asterisk" | ||
| }, | ||
| "MD050": { | ||
| "style": "asterisk" | ||
| }, | ||
| "MD051": false, | ||
| "MD055": { | ||
| "style": "leading_and_trailing" | ||
| }, | ||
| "MD058": false, | ||
| "MD059": false, | ||
| "MD060": { | ||
| "style": "compact" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,129 @@ | ||
| # openspace-api-js | ||
| JavaScript library to interface with [OpenSpace](https://github.com/OpenSpace/OpenSpace) using sockets and WebSockets. | ||
|
|
||
| ## Work in progress | ||
| Both the API and libary are still very much work in progress, and are subject to change. | ||
| TypeScript/JavaScript library for interfacing with [OpenSpace](https://github.com/OpenSpace/OpenSpace) via TCP sockets and WebSockets. | ||
|
|
||
| ## Documentation | ||
| A [reference](https://openspace.github.io/openspace-api-js) can be found here. Examples are available below: | ||
| ## Installation | ||
|
|
||
| ### NodeJS in the terminal | ||
| https://github.com/OpenSpace/openspace-api-js/blob/master/examples/example.ts provides an example of how to connect from a NodeTS. To run it run the following in a terminal: | ||
| ### npm | ||
|
|
||
| If you are using npm, install the latest version: | ||
|
|
||
| ```sh | ||
| npm install openspace-api-js | ||
| ``` | ||
|
|
||
| ### Script tag | ||
|
|
||
| If you are including the API via a script tag, the preferred approach is to load it directly from the OpenSpace CDN: | ||
|
|
||
| ```html | ||
| <script type="text/javascript" src="https://liu-se.cdn.openspaceproject.com/api/1.0.0/openspace-api.js"></script> | ||
| ``` | ||
|
|
||
| If you require an offline-capable version, download the [latest release](https://github.com/OpenSpace/openspace-api-js/blob/master/dist/openspace-api.js) and serve it locally: | ||
|
|
||
| ```html | ||
| <script type="text/javascript" src="openspace-api.js"></script> | ||
| ``` | ||
|
|
||
| ## Migration | ||
|
|
||
| If you are upgrading from a previous version of this library, please see the [migration guide](https://docs.openspaceproject.com/releases-v0.22/building-content/api/api-migration.html). | ||
|
|
||
| ## Usage | ||
|
|
||
| The TypeScript/JavaScript API can be used in both Node.js setups as well as browser-based setups. | ||
|
|
||
| ### Node.js (TCP socket) | ||
|
|
||
| Connects to OpenSpace on port **4681** by default. | ||
|
|
||
| [TypeScript example](https://github.com/OpenSpace/openspace-api-js/blob/master/examples/example.ts) and [JavaScript example](https://github.com/OpenSpace/openspace-api-js/blob/master/examples/example.js) provide examples of how to connect from a Node.js application. To run the bundled Node.js example: | ||
|
engbergandreas marked this conversation as resolved.
|
||
|
|
||
| ```sh | ||
| npm install | ||
| npm run example | ||
| npm run examples-typescript # TypeScript | ||
| npm run examples-javascript # JavaScript | ||
| ``` | ||
|
|
||
| ### Simple website | ||
| https://github.com/OpenSpace/openspace-api-js/blob/master/examples/index.html provides an example of connecting to OpenSpace from a website. For simplicity, the example is self-contained in the index.html file. | ||
| ### Browser (WebSocket) | ||
|
|
||
| Connects to OpenSpace on port **4682** by default. | ||
|
|
||
| If you are loading the library as a plain `<script>` tag (e.g. `openspace-api.js` from the CDN or a downloaded bundle), the factory function is exposed on `window`: | ||
|
|
||
| ```html | ||
| <script src="openspace-api.js"></script> | ||
| <script> | ||
| const api = window.openspaceApi('localhost', 4682); | ||
|
|
||
| api.onConnect(async () => { | ||
| const openspace = await api.library(); | ||
| const time = await openspace.time.UTC(); | ||
| console.log('Simulation time:', time); | ||
| }); | ||
|
|
||
| api.connect(); | ||
| </script> | ||
| ``` | ||
|
|
||
| See [HTML example](https://github.com/OpenSpace/openspace-api-js/blob/master/examples/index.html) for a complete working example. For simplicity, it is self-contained in the index.html file. | ||
|
engbergandreas marked this conversation as resolved.
|
||
|
|
||
| If you are importing the library as an ES module from an npm-based project, import the entry point directly: | ||
|
|
||
| ### Web application | ||
| For a proper example of how to interact with OpenSpace from a web application using npm, webpack and modern ES versions, please refer to https://github.com/OpenSpace/openspace-api-web-example. | ||
| ```ts | ||
| import openspaceApi from 'openspace-api-js'; | ||
|
|
||
| const api = openspaceApi('localhost', 4682); | ||
| ``` | ||
|
|
||
| ## Generating types from your OpenSpace build | ||
|
|
||
| The package ships with pre-generated types targeting a specific OpenSpace version. If your build differs, you can regenerate them locally. There are two separate generators, one for topic types and one for the Lua library. Both write directly into `src/types/generated/`. | ||
|
|
||
| ### Topic types (`generate-topic-types`) | ||
|
|
||
| Reads JSON schema files from OpenSpace's `support/types/` directory and compiles them to TypeScript using `json-schema-to-typescript`. | ||
|
|
||
| **Prerequisites:** | ||
|
|
||
| - Node.js | ||
| - OpenSpace version 0.22 or later | ||
|
|
||
| **Steps:** | ||
|
|
||
| 1. Run OpenSpace `DocsWriter.exe` | ||
| 1. Locate the `support/types/` directory in your OpenSpace source tree | ||
| 1. Run: | ||
|
|
||
| ```sh | ||
| npm run generate-topic-types -- "<path-to-openspace>/support/types" | ||
| ``` | ||
|
|
||
| This writes the generated files into `src/types/generated/` and rebuilds the `AllTopics` union type used throughout the API. | ||
|
|
||
|
|
||
| ### Lua library types (`generate-lua-library`) | ||
|
|
||
| Connects to a running OpenSpace instance, fetches the full Lua API documentation via the `documentation` topic, and generates `src/types/generated/openspacelualibrary.ts`. | ||
|
|
||
| > **Why generate this instead of publishing it once?** The OpenSpace Lua API changes frequently, so these types are generated from a live instance rather than published to `@types` or DefinitelyTyped. Keeping generation in this repo means changes can be reflected immediately without going through an external approval process. | ||
|
|
||
| **Prerequisites:** | ||
|
|
||
| - Python >= 3.12 | ||
| - The [OpenSpace Python API](https://github.com/OpenSpace/openspace-api-python) must be installed and importable | ||
| - OpenSpace must be running and reachable on `localhost:4681` | ||
|
|
||
| **Steps:** | ||
|
|
||
| 1. Start OpenSpace. | ||
| 1. Run: | ||
|
|
||
| ```sh | ||
| npm run generate-lua-library | ||
| ``` | ||
|
|
||
| ### Observable Notebook | ||
| Use Obserable notebooks to interact with OpenSpace locally: | ||
| https://observablehq.com/@emiax/openspace-notebook-example | ||
| This may be useful for tinkering, GUI prototyping and testing the lua interface during development. | ||
| This installs the Python dependencies (via `pip install -r script/requirements.txt`) and runs `script/generatetypescriptfile.py`, writing the result to `src/types/generated/openspacelualibrary.ts`. | ||
|
|
||
| ## API versioning | ||
| The Lua function types in this package are generated against a specific OpenSpace version. | ||
| If your OpenSpace version differs, the API will still work at runtime but type hints | ||
| may be inaccurate. Check the package changelog to see which OpenSpace version the | ||
| types were generated against. | ||
| > **Note:** The generated file is specific to the OpenSpace version that was running when the script executed. Type hints may be inaccurate if your runtime version differs from the version used to generate them. | ||
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.