@connectrpc/connect-query
Version:
TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.
60 lines • 2.42 kB
JavaScript
;
// 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.
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProtobufSafeUpdater = exports.isAbortController = exports.assert = exports.disableQuery = void 0;
/**
* Pass this value as an input to signal that you want to disable the query.
*/
exports.disableQuery = Symbol("disableQuery");
/**
* Throws an error with the provided message when the condition is `false`
*/
function assert(condition, message) {
if (!condition) {
throw new Error(`Invalid assertion: ${message}`);
}
}
exports.assert = assert;
/**
* Verifies that the provided input is a valid AbortController
*/
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;
};
exports.isAbortController = isAbortController;
/**
* 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.
*/
const createProtobufSafeUpdater = (methodSig, updater) => (prev) => {
if (typeof updater === "function") {
return new methodSig.O(updater(prev));
}
return new methodSig.O(updater);
};
exports.createProtobufSafeUpdater = createProtobufSafeUpdater;
//# sourceMappingURL=utils.js.map