Skip to content

Commit cc1b499

Browse files
authored
Update readme: server API (#168)
* The Try PureScript client no longer uses a bundle of preloaded modules; fix a sentence which says it does * Document that the request body must be a module called Main * Document the formats of the various JSON responses in more detail * Document the fact that we now expose the output directory from compile.purescript.org
1 parent af550f2 commit cc1b499

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

README.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,59 @@ stack exec trypurescript 8081 $(spago sources)
7474
## Server API
7575

7676
The server is a very basic web service which wraps the PureScript compiler, allowing clients to send PureScript code to be compiled and receiving either compiled JS or error messages in response.
77+
It is hosted at <https://compile.purescript.org/>.
7778

7879
### Compile PureScript code
7980

80-
**POST /compile**
81+
#### POST /compile
8182

82-
- Request body: PureScript code
83-
- Response body: Either `{ js: "..." }` or `{ error: "..." }`
83+
- Request body: PureScript code defining a module whose name must be Main
8484
- Status code: 200 (success)
8585

86-
Note that if the code in the request body fails to compile, this is considered a success from the perspective of the API, so compilation failures will be returned with 2xx status codes.
86+
Response body on compilation success:
87+
88+
```javascript
89+
{
90+
"js": "...", // a string containing JavaScript code
91+
"warnings": [ ... ] // an array of warnings, using the same format as the
92+
// compiler's --json-errors flag
93+
}
94+
```
95+
96+
Response body on compilation failure:
97+
98+
```javascript
99+
{
100+
"error": {
101+
"tag": "CompilerErrors",
102+
"contents": [ ... ] // an array of errors, using the same format as the
103+
// compiler's --json-errors flag
104+
}
105+
}
106+
```
107+
108+
Response body on other errors (eg, the name of the module in request body was not Main, or the request body was too large)
109+
110+
```javascript
111+
{
112+
"error": {
113+
"tag": "OtherError",
114+
"contents": "..." // a string containing an error message
115+
}
116+
}
117+
```
118+
119+
Note that the API returns a 200 response in all of the above cases; in particular, if the code in the request body fails to compile and the API returns errors, this is still considered a success.
87120
Among other things, this makes it easier to use the API from another domain using CORS.
88121

89-
The output code will contain references to preloaded modules using `require` calls.
122+
The output code will contain references to any imported modules using `require` calls.
90123
To run these files in the browser, it is necessary to either use a `require` shim (such as require1k), or replace these calls and deploy a bundle of precompiled modules.
91-
The Try PureScript client uses the second approach.
124+
The Try PureScript client uses the first approach.
125+
126+
#### GET /output/:module/(index.js|foreign.js)
127+
128+
The server exposes the compiled JS for all of the modules it has access to.
129+
If the compiled JavaScript code in the response includes a `require` call such as `require(../Web.HTML/index.js)`, then the client is expected to arrange things so that this `require` call provides access to the JavaScript code available at the URL path `/output/Web.HTML/index.js`, via a shim or otherwise.
92130

93131
### Configuration
94132

@@ -99,4 +137,4 @@ The server application takes the following arguments on the command line:
99137

100138
#### Example
101139

102-
dist/build/trypurescript/trypurescript 8081 'bower_components/purescript-*/src/**/*.purs'
140+
trypurescript 8081 'bower_components/purescript-*/src/**/*.purs'

0 commit comments

Comments
 (0)