bigbuffet-rw/packages/pl-api/lib/directory-client.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as v from 'valibot';
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 v.parse(filteredArray(directoryStatisticsPeriodSchema), response.json);
}
async getCategories(params?: Params) {
const response = await this.request('/categories', { params });
return v.parse(filteredArray(directoryCategorySchema), response.json);
}
async getLanguages(params?: Params) {
const response = await this.request('/categories', { params });
return v.parse(filteredArray(directoryLanguageSchema), response.json);
}
async getServers(params?: Params) {
const response = await this.request('/servers', { params });
return v.parse(filteredArray(directoryServerSchema), response.json);
}
}
export {
PlApiDirectoryClient,
PlApiDirectoryClient as default,
};