mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
feat: api
This commit is contained in:
1 parent
2dc4f12246
commit
7e11c1e5ce
26 files changed
+149
-866
No files matched your search
@@ -1,5 +0,0 @@
|
||||
import { RemoteMetadataAccessor } from 'packages/core/src';
|
||||
|
||||
export const remoteMetadata = new RemoteMetadataAccessor(
|
||||
'/api/internal/catalog',
|
||||
);
|
||||
@@ -1,6 +0,0 @@
|
||||
import { fetchMetadata } from '@blitzkit/closed';
|
||||
import { SocketMetadataAccessor } from 'packages/core/src';
|
||||
|
||||
export const socketMetadata = new SocketMetadataAccessor(
|
||||
await fetchMetadata(),
|
||||
).injectBlitzkit();
|
||||
@@ -1,11 +0,0 @@
|
||||
import type { MetadataAccessor } from 'packages/core/src';
|
||||
|
||||
let accessor: MetadataAccessor;
|
||||
|
||||
if (import.meta.env.SSR) {
|
||||
accessor = await import('./metadata.socket').then((m) => m.socketMetadata);
|
||||
} else {
|
||||
accessor = await import('./metadata.remote').then((m) => m.remoteMetadata);
|
||||
}
|
||||
|
||||
export const metadata = accessor;
|
||||
@@ -1,11 +0,0 @@
|
||||
let base: string;
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
base = 'http://localhost:4321';
|
||||
} else {
|
||||
base = import.meta.env.SITE;
|
||||
}
|
||||
|
||||
export function api(path: string) {
|
||||
return `${base}/api/${path}`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { BlitzkitClientAPI } from '@blitzkit/core';
|
||||
|
||||
export const api = new BlitzkitClientAPI();
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { BlitzkitAPI } from 'packages/core/src';
|
||||
|
||||
let api: BlitzkitAPI;
|
||||
|
||||
if (import.meta.env.SSR) {
|
||||
api = await import('./server').then((module) => module.api);
|
||||
} else {
|
||||
api = await import('./client').then((module) => module.api);
|
||||
}
|
||||
|
||||
export { api };
|
||||
@@ -0,0 +1,6 @@
|
||||
import { fetchMetadata } from '@blitzkit/closed';
|
||||
import { BlitzkitServerAPI, MetadataAccessor } from '@blitzkit/core';
|
||||
|
||||
const metadata = await fetchMetadata();
|
||||
const accessor = new MetadataAccessor(metadata);
|
||||
export const api = new BlitzkitServerAPI(accessor);
|
||||
@@ -1,11 +1,10 @@
|
||||
import { BlitzkitAllAvatarsComponent } from '@protos/blitzkit_static_all_avatars_component';
|
||||
import { MagnifyingGlassIcon } from '@radix-ui/react-icons';
|
||||
import { Flex, Grid, Spinner, TextField } from '@radix-ui/themes';
|
||||
import fuzzysort from 'fuzzysort';
|
||||
import { debounce, times } from 'lodash-es';
|
||||
import { AvatarGroup } from 'packages/website-ue/src/components/Avatars/Group';
|
||||
import { PageWrapper } from 'packages/website-ue/src/components/PageWrapper';
|
||||
import { metadata } from 'packages/website-ue/src/core/blitz/metadata';
|
||||
import { api } from 'packages/website-ue/src/core/blitzkit/api';
|
||||
import {
|
||||
LocaleProvider,
|
||||
useLocale,
|
||||
@@ -14,13 +13,7 @@ import {
|
||||
import type { MaybeSkeletonComponentProps } from 'packages/website-ue/src/types/maybeSkeletonComponentProps';
|
||||
import { useCallback, useMemo, useRef, useState } from 'react';
|
||||
|
||||
const allAvatars = await metadata
|
||||
.get('BlitzkitAllAvatarsEntity.blitzkit_all_avatars')
|
||||
.then((entity) =>
|
||||
entity
|
||||
.get(BlitzkitAllAvatarsComponent, 'blitzkitAllAvatarsComponent')
|
||||
.avatars.reverse(),
|
||||
);
|
||||
const { avatars } = await api.avatars();
|
||||
|
||||
export function Page({ localeData }: LocaleAcceptorProps) {
|
||||
return (
|
||||
@@ -39,7 +32,7 @@ function Content({ skeleton }: MaybeSkeletonComponentProps) {
|
||||
const [search, setSearch] = useState<string | null>(null);
|
||||
const [searching, setSearching] = useState(false);
|
||||
const groups = useMemo(() => {
|
||||
const avatars = allAvatars
|
||||
const avatarsSorted = avatars
|
||||
.map((avatar) => ({
|
||||
avatar,
|
||||
name: gameStrings[avatar.name] as string | undefined,
|
||||
@@ -52,9 +45,9 @@ function Content({ skeleton }: MaybeSkeletonComponentProps) {
|
||||
? -1
|
||||
: a.name.localeCompare(b.name),
|
||||
);
|
||||
const groups = new Map<string | undefined, typeof avatars>();
|
||||
const groups = new Map<string | undefined, typeof avatarsSorted>();
|
||||
|
||||
for (const avatar of avatars) {
|
||||
for (const avatar of avatarsSorted) {
|
||||
if (groups.has(avatar.name)) {
|
||||
groups.get(avatar.name)!.push(avatar);
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
import { avatarsTool } from 'packages/website-ue/src/constants/tools';
|
||||
import { metadata } from 'packages/website-ue/src/core/blitz/metadata';
|
||||
import { api } from 'packages/website-ue/src/core/blitzkit/api';
|
||||
import ToolLayout from 'packages/website-ue/src/layouts/ToolLayout.astro';
|
||||
import { Page } from './_index';
|
||||
|
||||
@@ -8,11 +8,11 @@ export { getStaticPaths } from '../index.astro';
|
||||
|
||||
const { locale } = Astro.props;
|
||||
|
||||
const gameStrings = await metadata.stringsPartial(locale, [
|
||||
const gameStrings = await api.metadata.stringsPartial(locale, [
|
||||
'ProfileAvatarEntity__*',
|
||||
]);
|
||||
---
|
||||
|
||||
<ToolLayout tool={avatarsTool} {locale} >
|
||||
<ToolLayout tool={avatarsTool} {locale}>
|
||||
<Page localeData={{ locale, gameStrings }} client:load />
|
||||
</ToolLayout>
|
||||
@@ -1,29 +1,28 @@
|
||||
import { ProfileAvatarComponent } from '@protos/blitz_static_profile_avatar_component';
|
||||
import type { APIRoute, GetStaticPaths, GetStaticPathsItem } from 'astro';
|
||||
import { socketMetadata } from 'packages/website-ue/src/core/blitz/metadata.socket';
|
||||
import type { BlitzKitAvatar } from '@protos/blitzkit';
|
||||
import type { APIRoute, GetStaticPaths } from 'astro';
|
||||
import { api } from 'packages/website-ue/src/core/blitzkit/api';
|
||||
|
||||
export const getStaticPaths: GetStaticPaths = async () => {
|
||||
const paths: GetStaticPathsItem[] = [];
|
||||
|
||||
for (const item in socketMetadata.items) {
|
||||
if (!item.startsWith('ProfileAvatarEntity.')) continue;
|
||||
|
||||
const id = await socketMetadata
|
||||
.get(item)
|
||||
.then((item) => item.undiscriminatedId());
|
||||
|
||||
paths.push({ params: { id } });
|
||||
}
|
||||
|
||||
return paths;
|
||||
const { avatars } = await api.avatars();
|
||||
return avatars.map((avatar) => ({
|
||||
params: { id: avatar.id },
|
||||
props: { avatar },
|
||||
}));
|
||||
};
|
||||
|
||||
export const GET: APIRoute<{}, { id: string }> = async ({ params }) => {
|
||||
const item = await socketMetadata.get(`ProfileAvatarEntity.${params.id}`);
|
||||
export const GET: APIRoute<
|
||||
{ avatar: BlitzKitAvatar },
|
||||
{ id: string }
|
||||
> = async ({ params }) => {
|
||||
const item = api.metadata.get(`ProfileAvatarEntity.${params.id}`);
|
||||
const avatar = item.get(ProfileAvatarComponent, 'profileAvatarComponent');
|
||||
|
||||
try {
|
||||
return await fetch(avatar.avatar);
|
||||
const response = await fetch(avatar.avatar);
|
||||
const buffer = await response.arrayBuffer();
|
||||
|
||||
return new Response(buffer, { headers: response.headers });
|
||||
} catch {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { BlitzKitAvatars } from '@protos/blitzkit';
|
||||
import type { APIRoute } from 'astro';
|
||||
import { api } from 'packages/website-ue/src/core/blitzkit/api';
|
||||
|
||||
export const GET: APIRoute = async () => {
|
||||
return new Response(BlitzKitAvatars.encode(await api.avatars()).finish());
|
||||
};
|
||||
@@ -1,19 +0,0 @@
|
||||
import type { APIRoute, GetStaticPaths, GetStaticPathsItem } from 'astro';
|
||||
import { socketMetadata } from 'packages/website-ue/src/core/blitz/metadata.socket';
|
||||
|
||||
export const getStaticPaths: GetStaticPaths = () => {
|
||||
const paths: GetStaticPathsItem[] = [];
|
||||
|
||||
for (const item in socketMetadata.items) {
|
||||
paths.push({ params: { item } });
|
||||
}
|
||||
|
||||
return paths;
|
||||
};
|
||||
|
||||
export const GET: APIRoute<{}, { item: string }> = async ({ params }) => {
|
||||
return new Response(
|
||||
await socketMetadata.get(params.item).then((item) => item.encode()),
|
||||
{ headers: { 'Content-Type': 'application/octet-stream' } },
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user