chore(frontend): introduce prettier + eslint and prune unused code
- Add Prettier 3 with prettier-plugin-organize-imports (sorts/removes unused imports) - Add ESLint 10 flat config with typescript-eslint + eslint-plugin-vue + eslint-config-prettier - Add root scripts: format, format:check, lint, lint:fix - Reformat 257 files across admin-web, ops-web, desktop-client, packages - Remove unused locals/exports flagged by --noUnusedLocals/--noUnusedParameters - Fix duplicate localTabLabel key, surrogate-pair regex u-flag, NBSP literal in regex - Skip server/ (Go) and apps/browser-extension/ (deprecated per ADR) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,4 +11,3 @@
|
||||
".": "./src/index.ts"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,164 +3,164 @@ import axios, {
|
||||
type AxiosInstance,
|
||||
type AxiosRequestConfig,
|
||||
type InternalAxiosRequestConfig,
|
||||
} from "axios";
|
||||
} from 'axios'
|
||||
|
||||
import type { ApiEnvelope, ApiErrorEnvelope, AuthTokens } from "@geo/shared-types";
|
||||
import type { ApiEnvelope, ApiErrorEnvelope, AuthTokens } from '@geo/shared-types'
|
||||
|
||||
export interface AuthBindings {
|
||||
getAccessToken: () => string | null;
|
||||
getRefreshToken: () => string | null;
|
||||
setTokens: (tokens: AuthTokens) => void;
|
||||
clearTokens: () => void;
|
||||
refresh: (refreshToken: string) => Promise<AuthTokens>;
|
||||
getAccessToken: () => string | null
|
||||
getRefreshToken: () => string | null
|
||||
setTokens: (tokens: AuthTokens) => void
|
||||
clearTokens: () => void
|
||||
refresh: (refreshToken: string) => Promise<AuthTokens>
|
||||
}
|
||||
|
||||
export interface CreateApiClientOptions {
|
||||
baseURL?: string;
|
||||
timeoutMs?: number;
|
||||
auth?: AuthBindings;
|
||||
baseURL?: string
|
||||
timeoutMs?: number
|
||||
auth?: AuthBindings
|
||||
}
|
||||
|
||||
export class ApiClientError extends Error {
|
||||
status?: number;
|
||||
code: number;
|
||||
detail?: string;
|
||||
requestId?: string;
|
||||
status?: number
|
||||
code: number
|
||||
detail?: string
|
||||
requestId?: string
|
||||
|
||||
constructor(input: {
|
||||
message: string;
|
||||
code?: number;
|
||||
status?: number;
|
||||
detail?: string;
|
||||
requestId?: string;
|
||||
message: string
|
||||
code?: number
|
||||
status?: number
|
||||
detail?: string
|
||||
requestId?: string
|
||||
}) {
|
||||
super(input.message);
|
||||
this.name = "ApiClientError";
|
||||
this.code = input.code ?? 50000;
|
||||
this.status = input.status;
|
||||
this.detail = input.detail;
|
||||
this.requestId = input.requestId;
|
||||
super(input.message)
|
||||
this.name = 'ApiClientError'
|
||||
this.code = input.code ?? 50000
|
||||
this.status = input.status
|
||||
this.detail = input.detail
|
||||
this.requestId = input.requestId
|
||||
}
|
||||
}
|
||||
|
||||
export interface ApiClient {
|
||||
raw: AxiosInstance;
|
||||
get: <T>(url: string, config?: AxiosRequestConfig) => Promise<T>;
|
||||
post: <T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) => Promise<T>;
|
||||
put: <T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) => Promise<T>;
|
||||
remove: <T>(url: string, config?: AxiosRequestConfig) => Promise<T>;
|
||||
raw: AxiosInstance
|
||||
get: <T>(url: string, config?: AxiosRequestConfig) => Promise<T>
|
||||
post: <T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) => Promise<T>
|
||||
put: <T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) => Promise<T>
|
||||
remove: <T>(url: string, config?: AxiosRequestConfig) => Promise<T>
|
||||
}
|
||||
|
||||
function normalizeError(error: unknown): ApiClientError {
|
||||
if (error instanceof ApiClientError) {
|
||||
return error;
|
||||
return error
|
||||
}
|
||||
|
||||
if (axios.isAxiosError(error)) {
|
||||
const payload = error.response?.data as Partial<ApiErrorEnvelope> | undefined;
|
||||
const payload = error.response?.data as Partial<ApiErrorEnvelope> | undefined
|
||||
return new ApiClientError({
|
||||
message: payload?.message ?? error.message ?? "network_error",
|
||||
message: payload?.message ?? error.message ?? 'network_error',
|
||||
code: payload?.code ?? 50000,
|
||||
status: error.response?.status,
|
||||
detail: payload?.detail,
|
||||
requestId: payload?.request_id,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return new ApiClientError({ message: error.message });
|
||||
return new ApiClientError({ message: error.message })
|
||||
}
|
||||
|
||||
return new ApiClientError({ message: "unknown_error" });
|
||||
return new ApiClientError({ message: 'unknown_error' })
|
||||
}
|
||||
|
||||
function unwrapEnvelope<T>(response: { data: ApiEnvelope<T> }): T {
|
||||
return response.data.data;
|
||||
return response.data.data
|
||||
}
|
||||
|
||||
export function createApiClient(options: CreateApiClientOptions = {}): ApiClient {
|
||||
const client = axios.create({
|
||||
baseURL: options.baseURL ?? "",
|
||||
baseURL: options.baseURL ?? '',
|
||||
timeout: options.timeoutMs ?? 15000,
|
||||
withCredentials: false,
|
||||
});
|
||||
})
|
||||
|
||||
const auth = options.auth;
|
||||
let refreshPromise: Promise<AuthTokens> | null = null;
|
||||
const auth = options.auth
|
||||
let refreshPromise: Promise<AuthTokens> | null = null
|
||||
|
||||
client.interceptors.request.use((config) => {
|
||||
if (!auth) {
|
||||
return config;
|
||||
return config
|
||||
}
|
||||
|
||||
const accessToken = auth.getAccessToken();
|
||||
const accessToken = auth.getAccessToken()
|
||||
if (accessToken) {
|
||||
config.headers = config.headers ?? {};
|
||||
(config.headers as Record<string, string>).Authorization = `Bearer ${accessToken}`;
|
||||
config.headers = config.headers ?? {}
|
||||
;(config.headers as Record<string, string>).Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
return config
|
||||
})
|
||||
|
||||
client.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error: AxiosError<ApiErrorEnvelope>) => {
|
||||
if (auth && error.response?.status === 401 && error.config) {
|
||||
const originalRequest = error.config as InternalAxiosRequestConfig & {
|
||||
_retry?: boolean;
|
||||
};
|
||||
const refreshToken = auth.getRefreshToken();
|
||||
_retry?: boolean
|
||||
}
|
||||
const refreshToken = auth.getRefreshToken()
|
||||
|
||||
if (refreshToken && !originalRequest._retry) {
|
||||
originalRequest._retry = true;
|
||||
originalRequest._retry = true
|
||||
|
||||
try {
|
||||
if (!refreshPromise) {
|
||||
refreshPromise = auth
|
||||
.refresh(refreshToken)
|
||||
.then((tokens) => {
|
||||
auth.setTokens(tokens);
|
||||
return tokens;
|
||||
auth.setTokens(tokens)
|
||||
return tokens
|
||||
})
|
||||
.finally(() => {
|
||||
refreshPromise = null;
|
||||
});
|
||||
refreshPromise = null
|
||||
})
|
||||
}
|
||||
|
||||
const nextTokens = await refreshPromise;
|
||||
originalRequest.headers = originalRequest.headers ?? {};
|
||||
(originalRequest.headers as Record<string, string>).Authorization =
|
||||
`Bearer ${nextTokens.accessToken}`;
|
||||
return client.request(originalRequest);
|
||||
const nextTokens = await refreshPromise
|
||||
originalRequest.headers = originalRequest.headers ?? {}
|
||||
;(originalRequest.headers as Record<string, string>).Authorization =
|
||||
`Bearer ${nextTokens.accessToken}`
|
||||
return client.request(originalRequest)
|
||||
} catch (refreshError) {
|
||||
const normalizedRefreshError = normalizeError(refreshError);
|
||||
const normalizedRefreshError = normalizeError(refreshError)
|
||||
if (normalizedRefreshError.status === 401) {
|
||||
auth.clearTokens();
|
||||
auth.clearTokens()
|
||||
}
|
||||
throw normalizedRefreshError;
|
||||
throw normalizedRefreshError
|
||||
}
|
||||
}
|
||||
|
||||
auth.clearTokens();
|
||||
auth.clearTokens()
|
||||
}
|
||||
|
||||
throw normalizeError(error);
|
||||
throw normalizeError(error)
|
||||
},
|
||||
);
|
||||
)
|
||||
|
||||
return {
|
||||
raw: client,
|
||||
async get<T>(url: string, config?: AxiosRequestConfig) {
|
||||
return unwrapEnvelope(await client.get<ApiEnvelope<T>>(url, config));
|
||||
return unwrapEnvelope(await client.get<ApiEnvelope<T>>(url, config))
|
||||
},
|
||||
async post<T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) {
|
||||
return unwrapEnvelope(await client.post<ApiEnvelope<T>>(url, body, config));
|
||||
return unwrapEnvelope(await client.post<ApiEnvelope<T>>(url, body, config))
|
||||
},
|
||||
async put<T, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig) {
|
||||
return unwrapEnvelope(await client.put<ApiEnvelope<T>>(url, body, config));
|
||||
return unwrapEnvelope(await client.put<ApiEnvelope<T>>(url, body, config))
|
||||
},
|
||||
async remove<T>(url: string, config?: AxiosRequestConfig) {
|
||||
return unwrapEnvelope(await client.delete<ApiEnvelope<T>>(url, config));
|
||||
return unwrapEnvelope(await client.delete<ApiEnvelope<T>>(url, config))
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user