|
4 | 4 | <ul>
|
5 | 5 | <li><a href="#what-are-deployment-modes-">What are deployment modes?</a></li>
|
6 | 6 | <li><a href="#can-exposed-api-be-asynchronous-">Can exposed API be asynchronous?</a></li>
|
| 7 | +<li><a href="#are-there-limitations-on-exposed-api-signatures-">Are there limitations on exposed API signatures?</a></li> |
7 | 8 | <li><a href="#where-to-put-asynchronous-initialization-">Where to put asynchronous initialization?</a></li>
|
8 | 9 | <li><a href="#can-initialization-code-be-configured-">Can initialization code be configured?</a></li>
|
9 | 10 | <li><a href="#how-can-service-definition-be-more-modular-">How can service definition be more modular?</a></li>
|
@@ -77,6 +78,54 @@ <h2 id="can-exposed-api-be-asynchronous-">Can exposed API be asynchronous?</h2>
|
77 | 78 | })
|
78 | 79 | })
|
79 | 80 | </code></pre>
|
| 81 | +<h2 id="are-there-limitations-on-exposed-api-signatures-">Are there limitations on exposed API signatures?</h2> |
| 82 | +<hr /> |
| 83 | +<p>Despite all our efforts, yes. Hopefully main cases are covered.</p> |
| 84 | +<p>Because parameters will be stringified when sent to server:</p> |
| 85 | +<ul> |
| 86 | +<li>they could be <code>null</code> or <code>undefined</code></li> |
| 87 | +<li>they could be booleans, numbers, strings, arrays, and plain object (contains only properties of the previous types)</li> |
| 88 | +<li>they could be of other types (Date, Error, RegExp, custom classes...) but will boils down to the output of their <code>toString()</code> method</li> |
| 89 | +<li>they could be <code>Buffer</code> or <code>Stream</code> (see bellow). All paremeter but the first will be ignored</li> |
| 90 | +</ul> |
| 91 | +<p>In particular, don't use functions as parameters.</p> |
| 92 | +<p>Same limitations applies to API returned object.</p> |
| 93 | +<p>You can use destructuring, rest parameters and even default values:</p> |
| 94 | +<pre><code class="lang-js">async withExoticParameters ([a, b], {c: {d}} = {}, ...other) { |
| 95 | + return [a, b, d, ...other] |
| 96 | +} |
| 97 | +</code></pre> |
| 98 | +<p>To use <code>Buffer</code> input parameter:</p> |
| 99 | +<ul> |
| 100 | +<li>decorate API with <code>hasBufferInput</code></li> |
| 101 | +<li>use only one parameter (others will be set to <code>undefined</code>)<pre><code class="lang-js"> const api = { |
| 102 | + async bufferHandling (buffer) { |
| 103 | + assert(Buffer.isBuffer(buffer)) |
| 104 | + return Buffer.concat([buffer, new Uint8Array([3, 4])]) |
| 105 | + } |
| 106 | + } |
| 107 | + // decorate |
| 108 | + apis.bufferHandling.hasBufferInput = true |
| 109 | +</code></pre> |
| 110 | +</li> |
| 111 | +</ul> |
| 112 | +<p>To use <code>Stream</code> input parameter:</p> |
| 113 | +<ul> |
| 114 | +<li>decorate API with <code>hasStreamInput</code></li> |
| 115 | +<li>use only one parameter (others will be set to <code>undefined</code>)<pre><code class="lang-js"> const api = { |
| 116 | + async streamHandling (stream) { |
| 117 | + assert(stream instanceof Readable) |
| 118 | + const prefix = new BufferList() |
| 119 | + prefix.append('here is a prefix -- ', 'utf8') |
| 120 | + return multistream([prefix, stream]) |
| 121 | + } |
| 122 | + } |
| 123 | + // decorate |
| 124 | + apis.streamHandling.hasStreamInput = true |
| 125 | +</code></pre> |
| 126 | +</li> |
| 127 | +</ul> |
| 128 | +<p>You can return <code>Stream</code> and <code>Buffer</code> without any decoration, but don't nest them in objects (they will be stringified).</p> |
80 | 129 | <h2 id="where-to-put-asynchronous-initialization-">Where to put asynchronous initialization?</h2>
|
81 | 130 | <hr />
|
82 | 131 | <p>To serve this purpose, the <code>init()</code> function can be either synchronous or return a Promise.</p>
|
|
0 commit comments