mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-20 07:26:19 +00:00
🆕 Create Custom Bage & Security update
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
const STORAGE_KEY = 'nitro.access.token';
|
||||
const EXPIRES_KEY = 'nitro.access.token.exp';
|
||||
|
||||
export const setAccessToken = (token: string | null | undefined, expiresAt?: number | null): void =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if(token && typeof token === 'string')
|
||||
{
|
||||
window.localStorage.setItem(STORAGE_KEY, token);
|
||||
if(typeof expiresAt === 'number' && expiresAt > 0) window.localStorage.setItem(EXPIRES_KEY, String(expiresAt));
|
||||
else window.localStorage.removeItem(EXPIRES_KEY);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.localStorage.removeItem(STORAGE_KEY);
|
||||
window.localStorage.removeItem(EXPIRES_KEY);
|
||||
}
|
||||
}
|
||||
catch {}
|
||||
};
|
||||
|
||||
export const getAccessToken = (): string =>
|
||||
{
|
||||
try { return window.localStorage.getItem(STORAGE_KEY) ?? ''; }
|
||||
catch { return ''; }
|
||||
};
|
||||
|
||||
export const getAccessTokenExpiresAt = (): number =>
|
||||
{
|
||||
try
|
||||
{
|
||||
const raw = window.localStorage.getItem(EXPIRES_KEY);
|
||||
if(!raw) return 0;
|
||||
const value = parseInt(raw, 10);
|
||||
return Number.isFinite(value) ? value : 0;
|
||||
}
|
||||
catch { return 0; }
|
||||
};
|
||||
|
||||
export const clearAccessToken = (): void =>
|
||||
{
|
||||
setAccessToken(null);
|
||||
};
|
||||
|
||||
export const persistAccessTokenFromPayload = (payload: Record<string, unknown> | null | undefined): void =>
|
||||
{
|
||||
if(!payload) return;
|
||||
const token = typeof payload.accessToken === 'string' ? payload.accessToken : '';
|
||||
const expiresAt = typeof payload.accessTokenExpiresAt === 'number' ? payload.accessTokenExpiresAt : null;
|
||||
if(token) setAccessToken(token, expiresAt);
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export * from './accessToken';
|
||||
@@ -224,11 +224,8 @@ export class AvatarEditorThumbnailsHelper
|
||||
|
||||
const texture = avatarImage.processAsTexture(AvatarSetType.HEAD, false);
|
||||
const sprite = new NitroSprite(texture);
|
||||
|
||||
if(isDisabled) sprite.filters = [ AvatarEditorThumbnailsHelper.ALPHA_FILTER ];
|
||||
|
||||
const frame = AvatarEditorThumbnailsHelper.findOpaqueBoundsFrame(sprite, texture.width, texture.height);
|
||||
|
||||
const imageUrl = await TextureUtils.generateImageUrl({
|
||||
target: sprite,
|
||||
frame
|
||||
@@ -257,7 +254,6 @@ export class AvatarEditorThumbnailsHelper
|
||||
const width = data.width;
|
||||
const height = data.height;
|
||||
if(!pixels || width <= 0 || height <= 0) return new NitroRectangle(0, 0, fallbackWidth, fallbackHeight);
|
||||
|
||||
const ALPHA_THRESHOLD = 8;
|
||||
|
||||
let minX = width;
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import { GetConfiguration, GetLocalizationManager } from '@nitrots/nitro-renderer';
|
||||
import { getAccessToken } from '../auth';
|
||||
|
||||
export interface CustomBadgeRecord
|
||||
{
|
||||
badgeId: string;
|
||||
badgeCode: string;
|
||||
name: string;
|
||||
description: string;
|
||||
dateCreated: number;
|
||||
dateEdit: number;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface CustomBadgeListResponse
|
||||
{
|
||||
badges: CustomBadgeRecord[];
|
||||
max: number;
|
||||
badgeWidth: number;
|
||||
badgeHeight: number;
|
||||
maxBadgeSizeBytes: number;
|
||||
priceBadge?: number;
|
||||
currencyType?: number;
|
||||
}
|
||||
|
||||
export interface CustomBadgeError
|
||||
{
|
||||
error: string;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
const interpolate = (value: string): string =>
|
||||
{
|
||||
try { return GetConfiguration().interpolate(value); }
|
||||
catch { return value; }
|
||||
};
|
||||
|
||||
const getConfigUrl = (key: string, fallback: string): string =>
|
||||
interpolate(GetConfiguration().getValue<string>(key, fallback));
|
||||
|
||||
const buildUrl = (key: string, fallback: string, badgeId?: string): string =>
|
||||
{
|
||||
const template = getConfigUrl(key, fallback);
|
||||
if(!badgeId) return template;
|
||||
if(template.includes('%badgeId%')) return template.replace(/%badgeId%/g, encodeURIComponent(badgeId));
|
||||
return template + (template.endsWith('/') ? '' : '/') + encodeURIComponent(badgeId);
|
||||
};
|
||||
|
||||
const authHeaders = (): Record<string, string> =>
|
||||
{
|
||||
const headers: Record<string, string> = {
|
||||
'Accept': 'application/json',
|
||||
'X-Requested-With': 'NitroCustomBadges'
|
||||
};
|
||||
const token = getAccessToken();
|
||||
if(token) headers['Authorization'] = `Bearer ${ token }`;
|
||||
return headers;
|
||||
};
|
||||
|
||||
const parseJson = async <T>(response: Response): Promise<T> =>
|
||||
{
|
||||
const text = await response.text();
|
||||
if(!text) return {} as T;
|
||||
try { return JSON.parse(text) as T; }
|
||||
catch { throw new Error('Invalid response from server.'); }
|
||||
};
|
||||
|
||||
const throwOnError = async (response: Response): Promise<void> =>
|
||||
{
|
||||
if(response.ok) return;
|
||||
const payload = await parseJson<CustomBadgeError>(response);
|
||||
const message = payload?.error || `Request failed (${ response.status }).`;
|
||||
const err = new Error(message) as Error & { status: number; code?: string };
|
||||
err.status = response.status;
|
||||
if(payload?.code) err.code = payload.code;
|
||||
throw err;
|
||||
};
|
||||
|
||||
export const fetchCustomBadges = async (): Promise<CustomBadgeListResponse> =>
|
||||
{
|
||||
const url = buildUrl('badges.custom.list.endpoint', '/api/badges/custom');
|
||||
const response = await fetch(url, { method: 'GET', credentials: 'include', headers: authHeaders() });
|
||||
await throwOnError(response);
|
||||
return parseJson<CustomBadgeListResponse>(response);
|
||||
};
|
||||
|
||||
export const createCustomBadge = async (body: { name: string; description: string; image: string }): Promise<CustomBadgeRecord> =>
|
||||
{
|
||||
const url = buildUrl('badges.custom.create.endpoint', '/api/badges/custom');
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { ...authHeaders(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
await throwOnError(response);
|
||||
return parseJson<CustomBadgeRecord>(response);
|
||||
};
|
||||
|
||||
export const updateCustomBadge = async (badgeId: string, body: { name: string; description: string; image: string }): Promise<CustomBadgeRecord> =>
|
||||
{
|
||||
const url = buildUrl('badges.custom.update.endpoint', '/api/badges/custom/%badgeId%', badgeId);
|
||||
const response = await fetch(url, {
|
||||
method: 'PUT',
|
||||
credentials: 'include',
|
||||
headers: { ...authHeaders(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
await throwOnError(response);
|
||||
return parseJson<CustomBadgeRecord>(response);
|
||||
};
|
||||
|
||||
export const deleteCustomBadge = async (badgeId: string): Promise<void> =>
|
||||
{
|
||||
const url = buildUrl('badges.custom.delete.endpoint', '/api/badges/custom/%badgeId%', badgeId);
|
||||
const response = await fetch(url, { method: 'DELETE', credentials: 'include', headers: authHeaders() });
|
||||
await throwOnError(response);
|
||||
};
|
||||
|
||||
export const isCustomBadgeCode = (code: string | null | undefined): boolean =>
|
||||
{
|
||||
if(!code) return false;
|
||||
return /^CUST[A-Z0-9]{5}-\d+$/.test(code);
|
||||
};
|
||||
|
||||
let customBadgeTextsLoadPromise: Promise<void> | null = null;
|
||||
|
||||
const injectTextsIntoLocalization = (texts: Record<string, string> | null | undefined): void =>
|
||||
{
|
||||
if(!texts) return;
|
||||
let manager: ReturnType<typeof GetLocalizationManager> | null = null;
|
||||
try { manager = GetLocalizationManager(); }
|
||||
catch { return; }
|
||||
if(!manager || typeof manager.setValue !== 'function') return;
|
||||
for(const key of Object.keys(texts))
|
||||
{
|
||||
const value = texts[key];
|
||||
if(typeof value === 'string') manager.setValue(key, value);
|
||||
}
|
||||
};
|
||||
|
||||
export const ensureCustomBadgeTexts = (): Promise<void> =>
|
||||
{
|
||||
if(customBadgeTextsLoadPromise) return customBadgeTextsLoadPromise;
|
||||
customBadgeTextsLoadPromise = (async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
const url = buildUrl('badges.custom.texts.endpoint', '/api/badges/custom/texts');
|
||||
const response = await fetch(url, { method: 'GET', credentials: 'include', headers: { 'Accept': 'application/json' } });
|
||||
if(!response.ok) return;
|
||||
const payload = await parseJson<{ texts: Record<string, string> }>(response);
|
||||
injectTextsIntoLocalization(payload.texts);
|
||||
}
|
||||
catch {}
|
||||
})();
|
||||
return customBadgeTextsLoadPromise;
|
||||
};
|
||||
|
||||
export const refreshCustomBadgeTexts = (): Promise<void> =>
|
||||
{
|
||||
customBadgeTextsLoadPromise = null;
|
||||
return ensureCustomBadgeTexts();
|
||||
};
|
||||
|
||||
export const setCustomBadgeText = (badgeId: string, name: string, description: string): void =>
|
||||
{
|
||||
injectTextsIntoLocalization({
|
||||
[`badge_name_${ badgeId }`]: name || badgeId,
|
||||
[`badge_desc_${ badgeId }`]: description || ''
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export * from './CustomBadgeApi';
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from './GetRendererVersion';
|
||||
export * from './GetUIVersion';
|
||||
export * from './achievements';
|
||||
export * from './auth';
|
||||
export * from './avatar';
|
||||
export * from './badges';
|
||||
export * from './camera';
|
||||
export * from './campaign';
|
||||
export * from './catalog';
|
||||
|
||||
Reference in New Issue
Block a user