pl-api: add server directory client

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-08-30 19:28:12 +02:00
parent 977872ff62
commit db19522a54
8 changed files with 112 additions and 1 deletions

View file

@ -0,0 +1,52 @@
import { directoryCategorySchema, directoryLanguageSchema, directoryServerSchema, directoryStatisticsPeriodSchema } from './entities';
import { filteredArray } from './entities/utils';
import request from './request';
interface Params {
language?: string;
category?: string;
region?: 'europe' | 'north_america' | 'south_america' | 'africa' | 'asia' | 'oceania';
ownership?: 'juridicial' | 'natural';
registrations?: 'instant' | 'manual';
}
class PlApiDirectoryClient {
accessToken = undefined;
baseURL: string;
public request = request.bind(this) as typeof request;
constructor(baseURL: string) {
this.baseURL = baseURL;
}
async getStatistics() {
const response = await this.request('/statistics');
return filteredArray(directoryStatisticsPeriodSchema).parse(response.json);
}
async getCategories(params?: Params) {
const response = await this.request('/categories', { params });
return filteredArray(directoryCategorySchema).parse(response.json);
}
async getLanguages(params?: Params) {
const response = await this.request('/categories', { params });
return filteredArray(directoryLanguageSchema).parse(response.json);
}
async getServers(params?: Params) {
const response = await this.request('/servers', { params });
return filteredArray(directoryServerSchema).parse(response.json);
}
}
export {
PlApiDirectoryClient,
PlApiDirectoryClient as default,
};

View file

@ -0,0 +1,10 @@
import { z } from 'zod';
const directoryCategorySchema = z.object({
category: z.string(),
servers_count: z.coerce.number().nullable().catch(null),
});
type DirectoryCategory = z.infer<typeof directoryCategorySchema>;
export { directoryCategorySchema, type DirectoryCategory };

View file

@ -0,0 +1,11 @@
import { z } from 'zod';
const directoryLanguageSchema = z.object({
locale: z.string(),
language: z.string(),
servers_count: z.coerce.number().nullable().catch(null),
});
type DirectoryLanguage = z.infer<typeof directoryLanguageSchema>;
export { directoryLanguageSchema, type DirectoryLanguage };

View file

@ -0,0 +1,21 @@
import { z } from 'zod';
const directoryServerSchema = z.object({
domain: z.string(),
version: z.string(),
description: z.string(),
languages: z.array(z.string()),
region: z.string(),
categories: z.array(z.string()),
proxied_thumbnail: z.string().url().nullable().catch(null),
blurhash: z.string().nullable().catch(null),
total_users: z.coerce.number(),
last_week_users: z.coerce.number(),
approval_required: z.boolean(),
language: z.string(),
category: z.string(),
});
type DirectoryServer = z.infer<typeof directoryServerSchema>;
export { directoryServerSchema, type DirectoryServer };

View file

@ -0,0 +1,12 @@
import { z } from 'zod';
const directoryStatisticsPeriodSchema = z.object({
period: z.string().date(),
server_count: z.coerce.number().nullable().catch(null),
user_count: z.coerce.number().nullable().catch(null),
active_user_count: z.coerce.number().nullable().catch(null),
});
type DirectoryStatisticsPeriod = z.infer<typeof directoryStatisticsPeriodSchema>;
export { directoryStatisticsPeriodSchema, type DirectoryStatisticsPeriod };

View file

@ -28,6 +28,10 @@ export * from './chat-message';
export * from './context';
export * from './conversation';
export * from './custom-emoji';
export * from './directory/category';
export * from './directory/language';
export * from './directory/server';
export * from './directory/statistics-period';
export * from './domain-block';
export * from './emoji-reaction';
export * from './extended-description';

View file

@ -1,4 +1,5 @@
export { PlApiClient } from './client';
export { PlApiDirectoryClient } from './directory-client';
export { type Response as PlApiResponse } from './request';
export * from './entities';
export * from './features';

View file

@ -42,7 +42,7 @@ interface RequestBody<Params = Record<string, any>> {
type RequestMeta = Pick<RequestBody, 'idempotencyKey' | 'onUploadProgress' | 'signal'>;
function request<T = any>(this: PlApiClient, input: URL | RequestInfo, {
function request<T = any>(this: Pick<PlApiClient, 'accessToken' | 'baseURL'>, input: URL | RequestInfo, {
body,
method = body ? 'POST' : 'GET',
params,