lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -1,82 +1,91 @@
|
||||
import { SnowLumaAbortError } from '../errors';
|
||||
|
||||
export interface RequestAbortState {
|
||||
signal: AbortSignal;
|
||||
cleanup(): void;
|
||||
isTimedOut(): boolean;
|
||||
isExternallyAborted(): boolean;
|
||||
signal: AbortSignal;
|
||||
cleanup(): void;
|
||||
isTimedOut(): boolean;
|
||||
isExternallyAborted(): boolean;
|
||||
}
|
||||
|
||||
export function createRequestAbortState(
|
||||
timeoutMs: number,
|
||||
externalSignal?: AbortSignal,
|
||||
timeoutMs: number,
|
||||
externalSignal?: AbortSignal,
|
||||
): RequestAbortState {
|
||||
const controller = new AbortController();
|
||||
let timedOut = false;
|
||||
let externallyAborted = false;
|
||||
const controller = new AbortController();
|
||||
let timedOut = false;
|
||||
let externallyAborted = false;
|
||||
|
||||
const onExternalAbort = (): void => {
|
||||
externallyAborted = true;
|
||||
controller.abort(externalSignal?.reason);
|
||||
};
|
||||
const onExternalAbort = (): void => {
|
||||
externallyAborted = true;
|
||||
controller.abort(externalSignal?.reason);
|
||||
};
|
||||
|
||||
if (externalSignal?.aborted) {
|
||||
onExternalAbort();
|
||||
} else {
|
||||
externalSignal?.addEventListener('abort', onExternalAbort, { once: true });
|
||||
}
|
||||
if (externalSignal?.aborted) {
|
||||
onExternalAbort();
|
||||
} else {
|
||||
externalSignal?.addEventListener('abort', onExternalAbort, { once: true });
|
||||
}
|
||||
|
||||
const timer = timeoutMs > 0
|
||||
? setTimeout(() => {
|
||||
timedOut = true;
|
||||
controller.abort();
|
||||
}, timeoutMs)
|
||||
: null;
|
||||
const timer =
|
||||
timeoutMs > 0
|
||||
? setTimeout(() => {
|
||||
timedOut = true;
|
||||
controller.abort();
|
||||
}, timeoutMs)
|
||||
: null;
|
||||
|
||||
return {
|
||||
signal: controller.signal,
|
||||
cleanup() {
|
||||
if (timer) clearTimeout(timer);
|
||||
externalSignal?.removeEventListener('abort', onExternalAbort);
|
||||
},
|
||||
isTimedOut() {
|
||||
return timedOut;
|
||||
},
|
||||
isExternallyAborted() {
|
||||
return externallyAborted || externalSignal?.aborted === true;
|
||||
},
|
||||
};
|
||||
return {
|
||||
signal: controller.signal,
|
||||
cleanup() {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
externalSignal?.removeEventListener('abort', onExternalAbort);
|
||||
},
|
||||
isTimedOut() {
|
||||
return timedOut;
|
||||
},
|
||||
isExternallyAborted() {
|
||||
return externallyAborted || externalSignal?.aborted === true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function throwIfAborted(signal?: AbortSignal, message = 'SnowLuma request aborted'): void {
|
||||
if (signal?.aborted) {
|
||||
throw new SnowLumaAbortError(message, { cause: signal.reason });
|
||||
}
|
||||
if (signal?.aborted) {
|
||||
throw new SnowLumaAbortError(message, { cause: signal.reason });
|
||||
}
|
||||
}
|
||||
|
||||
export function isAbortError(error: unknown): boolean {
|
||||
return typeof error === 'object'
|
||||
&& error !== null
|
||||
&& 'name' in error
|
||||
&& (error as { name?: unknown }).name === 'AbortError';
|
||||
return (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'name' in error &&
|
||||
(error as { name?: unknown }).name === 'AbortError'
|
||||
);
|
||||
}
|
||||
|
||||
export function raceWithAbort<T>(
|
||||
promise: Promise<T>,
|
||||
signal?: AbortSignal,
|
||||
message = 'SnowLuma request aborted',
|
||||
promise: Promise<T>,
|
||||
signal?: AbortSignal,
|
||||
message = 'SnowLuma request aborted',
|
||||
): Promise<T> {
|
||||
if (!signal) return promise;
|
||||
throwIfAborted(signal, message);
|
||||
if (!signal) {
|
||||
return promise;
|
||||
}
|
||||
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const onAbort = (): void => {
|
||||
reject(new SnowLumaAbortError(message, { cause: signal.reason }));
|
||||
};
|
||||
throwIfAborted(signal, message);
|
||||
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
promise.then(resolve, reject).finally(() => {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const onAbort = (): void => {
|
||||
reject(new SnowLumaAbortError(message, { cause: signal.reason }));
|
||||
};
|
||||
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
promise.then(resolve, reject).finally(() => {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,64 +1,89 @@
|
||||
import type { ApiResponse, JsonObject, JsonValue } from '../types/index';
|
||||
|
||||
export function isJsonObject(value: unknown): value is JsonObject {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function asJsonValue(value: unknown): JsonValue {
|
||||
if (value === null) return null;
|
||||
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
||||
return value;
|
||||
}
|
||||
if (Array.isArray(value)) return value.map(asJsonValue);
|
||||
if (isJsonObject(value)) {
|
||||
const out: JsonObject = {};
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if (item === undefined) continue;
|
||||
out[key] = asJsonValue(item);
|
||||
if (value === null) {
|
||||
return null;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
return String(value);
|
||||
|
||||
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(asJsonValue);
|
||||
}
|
||||
|
||||
if (isJsonObject(value)) {
|
||||
const out: JsonObject = {};
|
||||
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if (item === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
out[key] = asJsonValue(item);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
return String(value);
|
||||
}
|
||||
|
||||
export function assertApiResponse(value: unknown): ApiResponse {
|
||||
if (!isJsonObject(value)) {
|
||||
throw new Error('response is not a JSON object');
|
||||
}
|
||||
if (!isJsonObject(value)) {
|
||||
throw new Error('response is not a JSON object');
|
||||
}
|
||||
|
||||
const status = value.status === 'ok' ? 'ok' : value.status === 'failed' ? 'failed' : null;
|
||||
if (!status) {
|
||||
throw new Error('response status is missing or invalid');
|
||||
}
|
||||
const status = value.status === 'ok' ? 'ok' : value.status === 'failed' ? 'failed' : null;
|
||||
|
||||
const retcode = typeof value.retcode === 'number' && Number.isFinite(value.retcode)
|
||||
? value.retcode
|
||||
: status === 'ok' ? 0 : -1;
|
||||
if (!status) {
|
||||
throw new Error('response status is missing or invalid');
|
||||
}
|
||||
|
||||
return {
|
||||
status,
|
||||
retcode,
|
||||
data: asJsonValue(value.data ?? null),
|
||||
echo: value.echo === undefined ? undefined : asJsonValue(value.echo),
|
||||
wording: typeof value.wording === 'string' ? value.wording : undefined,
|
||||
};
|
||||
const retcode =
|
||||
typeof value.retcode === 'number' && Number.isFinite(value.retcode)
|
||||
? value.retcode
|
||||
: status === 'ok'
|
||||
? 0
|
||||
: -1;
|
||||
|
||||
return {
|
||||
status,
|
||||
retcode,
|
||||
data: asJsonValue(value.data ?? null),
|
||||
echo: value.echo === undefined ? undefined : asJsonValue(value.echo),
|
||||
wording: typeof value.wording === 'string' ? value.wording : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function createEcho(): string {
|
||||
const random = Math.random().toString(36).slice(2);
|
||||
return `snowluma-sdk:${Date.now().toString(36)}:${random}`;
|
||||
const random = Math.random().toString(36).slice(2);
|
||||
return `snowluma-sdk:${Date.now().toString(36)}:${random}`;
|
||||
}
|
||||
|
||||
export function echoKey(value: JsonValue | undefined): string {
|
||||
if (value === undefined) return '';
|
||||
return JSON.stringify(value);
|
||||
if (value === undefined) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
export function appendAccessToken(urlValue: string, accessToken?: string): string {
|
||||
if (!accessToken) return urlValue;
|
||||
const url = new URL(urlValue);
|
||||
if (!url.searchParams.has('access_token')) {
|
||||
url.searchParams.set('access_token', accessToken);
|
||||
}
|
||||
return url.toString();
|
||||
if (!accessToken) {
|
||||
return urlValue;
|
||||
}
|
||||
|
||||
const url = new URL(urlValue);
|
||||
|
||||
if (!url.searchParams.has('access_token')) {
|
||||
url.searchParams.set('access_token', accessToken);
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
Reference in new issue
Block a user