jsgui3-server 0.0.131 → 0.0.133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -768,6 +768,33 @@ const server = new Server({
|
|
|
768
768
|
|
|
769
769
|
Data models can be shared within a single client instance, enabling synchronized UI controls.
|
|
770
770
|
|
|
771
|
+
### Publishing functions with `server.publish`
|
|
772
|
+
|
|
773
|
+
The server can publish plain JavaScript functions as HTTP endpoints. When a function is published, request bodies are parsed and passed to the function, and the function's return value determines the response MIME type. The API base path (for example, `/api`) is prefixed automatically, so only provide the route suffix to `publish()`.
|
|
774
|
+
|
|
775
|
+
- If the function returns a string, the response is sent as `text/plain; charset=UTF-8`.
|
|
776
|
+
- If the function returns an object (including arrays), it is serialized as JSON with `application/json`.
|
|
777
|
+
- If the function returns a Promise, it is awaited and then handled by the two rules above.
|
|
778
|
+
|
|
779
|
+
Input payload handling:
|
|
780
|
+
- `Content-Type: application/json` bodies are parsed to an object and passed as the single argument.
|
|
781
|
+
- `Content-Type: text/plain` bodies are passed as a UTF-8 string.
|
|
782
|
+
|
|
783
|
+
Minimal example:
|
|
784
|
+
|
|
785
|
+
```javascript
|
|
786
|
+
// Inside your server bootstrap (after constructing Server)
|
|
787
|
+
server.on('ready', () => {
|
|
788
|
+
// Returns text/plain at GET/POST /api/hello
|
|
789
|
+
server.publish('hello', name => `Hello ${name || 'world'}`);
|
|
790
|
+
|
|
791
|
+
// Returns application/json at GET/POST /api/sum
|
|
792
|
+
server.publish('sum', ({ a, b }) => ({ sum: a + b }));
|
|
793
|
+
});
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
This behavior is implemented by the function publisher, which inspects the function result type to choose the correct headers and serialization.
|
|
797
|
+
|
|
771
798
|
## CSS Architecture and Styling
|
|
772
799
|
|
|
773
800
|
### CSS Definition Patterns
|
|
@@ -21,6 +21,17 @@ if (require.main === module) {
|
|
|
21
21
|
return new Date().toISOString();
|
|
22
22
|
});
|
|
23
23
|
|
|
24
|
+
server.publish('user', () => {
|
|
25
|
+
return { id: 1, name: 'John Doe' };
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
server.publish('users', () => {
|
|
29
|
+
return [
|
|
30
|
+
{ id: 1, name: 'John Doe' },
|
|
31
|
+
{ id: 2, name: 'Jane Doe' }
|
|
32
|
+
];
|
|
33
|
+
});
|
|
34
|
+
|
|
24
35
|
server.start(8088);
|
|
25
36
|
|
|
26
37
|
}
|
package/package.json
CHANGED
|
@@ -145,7 +145,7 @@ class Function_Publisher extends HTTP_Publisher {
|
|
|
145
145
|
const tfr = tf(fn_res);
|
|
146
146
|
//console.log('fn_res', fn_res);
|
|
147
147
|
|
|
148
|
-
//
|
|
148
|
+
//
|
|
149
149
|
|
|
150
150
|
if (tfr === 'p') {
|
|
151
151
|
// promise
|
|
@@ -174,8 +174,22 @@ class Function_Publisher extends HTTP_Publisher {
|
|
|
174
174
|
res.end(fn_res);
|
|
175
175
|
|
|
176
176
|
|
|
177
|
-
} else {
|
|
177
|
+
} else if (tfr === 'o' || tfr === 'a') {
|
|
178
|
+
// Just write it as a string for the moment I think?
|
|
179
|
+
// Or always encode as JSON?
|
|
178
180
|
|
|
181
|
+
// text/plain;charset=UTF-8
|
|
182
|
+
|
|
183
|
+
res.writeHead(200, {
|
|
184
|
+
'Content-Type': 'application/json'//,
|
|
185
|
+
//'Transfer-Encoding': 'chunked',
|
|
186
|
+
//'Trailer': 'Content-MD5'
|
|
187
|
+
});
|
|
188
|
+
res.end(JSON.stringify(fn_res));
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
} else {
|
|
192
|
+
console.log('tfr', tfr);
|
|
179
193
|
console.trace();
|
|
180
194
|
throw 'NYI';
|
|
181
195
|
}
|