@truenas/api-client 1.0.4 → 1.0.6

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/dist/index.js CHANGED
@@ -157,6 +157,16 @@ var TrueNasApi = class {
157
157
  this.initializeJobEventsSubscription();
158
158
  }
159
159
  call(method, params) {
160
+ return this.dispatch(method, params);
161
+ }
162
+ /**
163
+ * Send a JSON-RPC request and emit its result.
164
+ *
165
+ * Shared by {@link call} and the query verbs, which type the same wire call
166
+ * against different directories — `call` against the hand-maintained one,
167
+ * the verbs against the generated one.
168
+ */
169
+ dispatch(method, params) {
160
170
  const message = createJsonRpcMessage(method, params);
161
171
  this.connection.ws.next(message);
162
172
  const messageId = message.id ?? "";
@@ -172,6 +182,58 @@ var TrueNasApi = class {
172
182
  take(1)
173
183
  );
174
184
  }
185
+ /**
186
+ * Query a collection and emit the matching entries.
187
+ *
188
+ * ```typescript
189
+ * api.query('user.query') // UserEntry[]
190
+ * api.query('user.query', [['uid', '>', 1000]]) // UserEntry[]
191
+ * api.query('user.query', [], { select: ['id', 'username'] })
192
+ * // Pick<UserEntry, 'id' | 'username'>[]
193
+ * ```
194
+ *
195
+ * The precise result type comes from reading the options *literal*. Options
196
+ * annotated as `QueryListOptions<E>` lose that, and the result degrades to
197
+ * `Partial<E>[]` — not only when a `select` is present, but whenever the
198
+ * annotation merely permits one:
199
+ *
200
+ * ```typescript
201
+ * const opts: QueryListOptions<UserEntry> = { limit: 10 };
202
+ * api.query('user.query', [], opts); // Partial<UserEntry>[]
203
+ *
204
+ * const opts = { limit: 10 } satisfies QueryListOptions<UserEntry>;
205
+ * api.query('user.query', [], opts); // UserEntry[]
206
+ * ```
207
+ *
208
+ * That is imprecise, never unsound: `Partial<E>` is a supertype of `E`, so a
209
+ * field is only ever reported as *possibly* missing, never as present when it
210
+ * is not. Reach for `satisfies` over an annotation to keep the precision —
211
+ * the checking is the same, the inferred type is narrower.
212
+ *
213
+ * `count` and `get` are rejected: they would change the shape of the
214
+ * response, which is {@link queryCount} and {@link queryOne}'s job.
215
+ */
216
+ query(method, filters, options) {
217
+ return this.dispatch(method, [filters ?? [], options ?? {}]);
218
+ }
219
+ /**
220
+ * Query a collection and emit the single matching entry.
221
+ *
222
+ * Middleware errors unless exactly one entry matches, so this rejects
223
+ * `limit` and `offset` as well as the shape switches.
224
+ */
225
+ queryOne(method, filters, options) {
226
+ return this.dispatch(method, [filters ?? [], { ...options, get: true }]);
227
+ }
228
+ /**
229
+ * Emit the number of entries matching the filters.
230
+ *
231
+ * Takes no options: `select` and `order_by` cannot affect a count, and
232
+ * `limit` / `offset` would silently cap it.
233
+ */
234
+ queryCount(method, filters) {
235
+ return this.dispatch(method, [filters ?? [], { count: true }]);
236
+ }
175
237
  /**
176
238
  * Makes an API call and returns the job ID from the websocket event.
177
239
  * Used for v26 where API calls return null but job events contain the job ID.
@@ -3427,7 +3489,14 @@ function instantiateClientForVersion(version, opts, logger) {
3427
3489
  version: version.version,
3428
3490
  versionKey
3429
3491
  });
3430
- return new Client(uuid, hostnames, version, enabled, systemName, logger);
3492
+ return new Client(
3493
+ uuid,
3494
+ hostnames,
3495
+ version,
3496
+ enabled,
3497
+ systemName,
3498
+ logger
3499
+ );
3431
3500
  }
3432
3501
  function errorMessageOrDefault(error, fallback) {
3433
3502
  if (error instanceof Error) {