refactor: standardize runtime guard patterns with lodash-es (#154)

* refactor(ui): replace runtime type guards with lodash-es predicates

* refactor(runtime): migrate typeof checks to lodash-es in core modules

* refactor(ipc): standardize runtime guards with lodash-es

* test(refactor): align runtime guard checks with lodash-es

* docs(agents): add lodash-es runtime guard skill summary
This commit is contained in:
vin authored and GitHub committed 2026-04-12 11:41:56 +08:00
1 parent 0223e8d807
commit bc35b96b5d
33 files changed
+303 -208

No files matched your search

+3 -2
View File
@@ -6,17 +6,18 @@ const injectCloudAccountsFailureScript = `
(() => {
const START_ORPC_SERVER = 'start-orpc-server';
const originalPostMessage = window.postMessage.bind(window);
const isStringValue = (value) => Object.prototype.toString.call(value) === '[object String]';
window.postMessage = function (message, targetOrigin, transfer) {
if (message === START_ORPC_SERVER && Array.isArray(transfer) && transfer[0]) {
const serverPort = transfer[0];
if (typeof serverPort.start === 'function') {
if (serverPort.start instanceof Function) {
serverPort.start();
}
serverPort.onmessage = (event) => {
try {
const request = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
const request = isStringValue(event.data) ? JSON.parse(event.data) : event.data;
const requestId = request && (request.i || request.id);
const requestUrl = (request && request.p && request.p.u) || '';
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
import { isEmpty, isString } from 'lodash-es';
import { transformClaudeRequestIn } from '../../lib/antigravity/ClaudeRequestMapper';
import { ProxyService } from '../../server/modules/proxy/proxy.service';
@@ -34,7 +35,7 @@ class TestableProxyService extends ProxyService {
function getEnv(name: string, fallback?: string): string {
const value = process.env[name];
if (value && value.trim() !== '') {
if (isString(value) && !isEmpty(value.trim())) {
return value;
}
if (fallback !== undefined) {
@@ -46,7 +47,7 @@ function getEnv(name: string, fallback?: string): string {
function getEnvFromList(names: string[], fallback?: string): string {
for (const name of names) {
const value = process.env[name];
if (value && value.trim() !== '') {
if (isString(value) && !isEmpty(value.trim())) {
return value;
}
}
@@ -60,7 +61,7 @@ function getLiveHeaders(apiKey?: string): Record<string, string> {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (apiKey && apiKey.trim() !== '') {
if (isString(apiKey) && !isEmpty(apiKey.trim())) {
headers.Authorization = apiKey.startsWith('Bearer ') ? apiKey : `Bearer ${apiKey}`;
}
return headers;
@@ -271,7 +272,7 @@ async function validateRuntimeAnthropicRequestFromRealTokenManager(): Promise<vo
const captured = capturedBody as Record<string, unknown>;
const tokenProjectId = selectedToken?.token?.project_id;
const normalizedTokenProjectId =
typeof tokenProjectId === 'string' && tokenProjectId.trim() !== ''
isString(tokenProjectId) && !isEmpty(tokenProjectId.trim())
? tokenProjectId.trim()
: undefined;
+3 -2
View File
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { isString } from 'lodash-es';
// Mock module to test timeout behavior
describe('GoogleAPIService Timeout', () => {
@@ -92,13 +93,13 @@ describe('GoogleAPIService OAuth clients', () => {
});
afterEach(() => {
if (typeof originalOauthClientsEnv === 'string') {
if (isString(originalOauthClientsEnv)) {
process.env.ANTIGRAVITY_OAUTH_CLIENTS = originalOauthClientsEnv;
} else {
delete process.env.ANTIGRAVITY_OAUTH_CLIENTS;
}
if (typeof originalActiveOauthClientEnv === 'string') {
if (isString(originalActiveOauthClientEnv)) {
process.env.ANTIGRAVITY_OAUTH_CLIENT_KEY = originalActiveOauthClientEnv;
} else {
delete process.env.ANTIGRAVITY_OAUTH_CLIENT_KEY;
+2 -1
View File
@@ -1,11 +1,12 @@
import '@testing-library/jest-dom';
import * as matchers from '@testing-library/jest-dom/matchers';
import { isUndefined } from 'lodash-es';
import { expect, vi } from 'vitest';
expect.extend(matchers);
// Mock window and localStorage for Node environment
if (typeof window === 'undefined') {
if (isUndefined((globalThis as { window?: unknown }).window)) {
global.window = {
matchMedia: vi.fn().mockImplementation((query) => ({
matches: false,