UNPKG

@connectrpc/connect-query

Version:

TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.

54 lines 2.12 kB
// Copyright 2021-2023 The Connect Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Pass this value as an input to signal that you want to disable the query. */ export const disableQuery = Symbol("disableQuery"); /** * Throws an error with the provided message when the condition is `false` */ export function assert(condition, message) { if (!condition) { throw new Error(`Invalid assertion: ${message}`); } } /** * Verifies that the provided input is a valid AbortController */ export const isAbortController = (input) => { if (typeof input === "object" && input !== null && "signal" in input && typeof input.signal === "object" && input.signal !== null && "aborted" in input.signal && typeof input.signal.aborted === "boolean" && "abort" in input && typeof input.abort === "function" // note, there are more things in this interface, but I stop the check here at `context.signal.aborted` and `context.abort` because (as off November 2022) that's all that connect-web is using (in `callback-client.ts`). ) { return true; } return false; }; /** * This helper makes sure that the Class for the original data is returned, even if what's provided is a partial message or a plain JavaScript object representing the underlying values. */ export const createProtobufSafeUpdater = (methodSig, updater) => (prev) => { if (typeof updater === "function") { return new methodSig.O(updater(prev)); } return new methodSig.O(updater); }; //# sourceMappingURL=utils.js.map