feat(organizations): migrate source storage to polymorphic records
This commit is contained in:
257
ts_client/src/client.ts
Normal file
257
ts_client/src/client.ts
Normal file
@@ -0,0 +1,257 @@
|
||||
import type {
|
||||
ApiErrorPayload,
|
||||
Organization,
|
||||
OrganizationDetailParams,
|
||||
OrganizationListParams,
|
||||
OrganizationListResponse,
|
||||
QueryListValue,
|
||||
JsonValue,
|
||||
} from "./types.js";
|
||||
|
||||
export type FetchLike = (
|
||||
input: RequestInfo,
|
||||
init?: RequestInit,
|
||||
) => Promise<Response>;
|
||||
|
||||
export interface MostovikOrganizationsClientOptions {
|
||||
baseUrl: string | URL;
|
||||
fetch?: FetchLike;
|
||||
headers?: HeadersInit;
|
||||
accessToken?: string;
|
||||
}
|
||||
|
||||
export interface ApiRequestOptions {
|
||||
headers?: HeadersInit;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export class ApiClientError extends Error {
|
||||
readonly status: number;
|
||||
readonly payload: ApiErrorPayload;
|
||||
|
||||
constructor(message: string, status: number, payload: ApiErrorPayload) {
|
||||
super(message);
|
||||
this.name = "ApiClientError";
|
||||
this.status = status;
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
||||
|
||||
type QueryParamValue = string | number | boolean | null | undefined;
|
||||
type QueryParam = QueryParamValue | readonly QueryParamValue[];
|
||||
type QueryParams = Record<string, QueryParam>;
|
||||
|
||||
export class MostovikOrganizationsClient {
|
||||
private readonly baseUrl: string;
|
||||
private readonly fetchImpl: FetchLike;
|
||||
private readonly defaultHeaders: HeadersInit | undefined;
|
||||
private readonly accessToken: string | undefined;
|
||||
|
||||
constructor(options: MostovikOrganizationsClientOptions) {
|
||||
this.baseUrl = normalizeBaseUrl(options.baseUrl);
|
||||
this.fetchImpl = options.fetch ?? defaultFetch();
|
||||
this.defaultHeaders = options.headers;
|
||||
this.accessToken = options.accessToken;
|
||||
}
|
||||
|
||||
listOrganizations(
|
||||
params: OrganizationListParams = {},
|
||||
options: ApiRequestOptions = {},
|
||||
): Promise<OrganizationListResponse> {
|
||||
return this.request<OrganizationListResponse>(
|
||||
"api/v2/organizations/",
|
||||
params as QueryParams,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
getOrganization(
|
||||
uid: string,
|
||||
params: OrganizationDetailParams = {},
|
||||
options: ApiRequestOptions = {},
|
||||
): Promise<Organization> {
|
||||
return this.request<Organization>(
|
||||
`api/v2/organizations/${encodeURIComponent(uid)}/`,
|
||||
params as QueryParams,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
private async request<T>(
|
||||
path: string,
|
||||
params: QueryParams,
|
||||
options: ApiRequestOptions,
|
||||
): Promise<T> {
|
||||
const url = this.buildUrl(path, params);
|
||||
const init: RequestInit = {
|
||||
method: "GET",
|
||||
headers: this.buildHeaders(options.headers),
|
||||
};
|
||||
if (options.signal !== undefined) {
|
||||
init.signal = options.signal;
|
||||
}
|
||||
|
||||
const response = await this.fetchImpl(url.toString(), init);
|
||||
const payload = await parseResponsePayload(response);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorPayload = toApiErrorPayload(payload, response.status);
|
||||
throw new ApiClientError(
|
||||
`GET ${url.pathname} failed with HTTP ${response.status}`,
|
||||
response.status,
|
||||
errorPayload,
|
||||
);
|
||||
}
|
||||
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
private buildUrl(path: string, params: QueryParams): URL {
|
||||
const url = new URL(path, this.baseUrl);
|
||||
appendQueryParams(url, params);
|
||||
return url;
|
||||
}
|
||||
|
||||
private buildHeaders(requestHeaders?: HeadersInit): Headers {
|
||||
const headers = new Headers(this.defaultHeaders);
|
||||
headers.set("Accept", "application/json");
|
||||
|
||||
if (this.accessToken !== undefined && !headers.has("Authorization")) {
|
||||
headers.set("Authorization", `Bearer ${this.accessToken}`);
|
||||
}
|
||||
|
||||
if (requestHeaders !== undefined) {
|
||||
new Headers(requestHeaders).forEach((value, key) => {
|
||||
headers.set(key, value);
|
||||
});
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeBaseUrl(baseUrl: string | URL): string {
|
||||
const value = String(baseUrl);
|
||||
return value.endsWith("/") ? value : `${value}/`;
|
||||
}
|
||||
|
||||
function defaultFetch(): FetchLike {
|
||||
if (typeof globalThis.fetch !== "function") {
|
||||
throw new Error(
|
||||
"No fetch implementation is available. Pass fetch in client options.",
|
||||
);
|
||||
}
|
||||
|
||||
return (input, init) => globalThis.fetch(input, init);
|
||||
}
|
||||
|
||||
function appendQueryParams(url: URL, params: QueryParams): void {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (isQueryParamArray(value)) {
|
||||
value.forEach((item) => appendQueryParam(url, key, item));
|
||||
return;
|
||||
}
|
||||
|
||||
appendQueryParam(url, key, value);
|
||||
});
|
||||
}
|
||||
|
||||
function isQueryParamArray(value: QueryParam): value is readonly QueryParamValue[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
function appendQueryParam(url: URL, key: string, value: QueryParamValue): void {
|
||||
if (value === undefined || value === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
url.searchParams.append(key, String(value));
|
||||
}
|
||||
|
||||
async function parseResponsePayload(response: Response): Promise<JsonValue | null> {
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
const body = await response.text();
|
||||
if (body === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (contentType.includes("application/json")) {
|
||||
return JSON.parse(body) as JsonValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(body) as JsonValue;
|
||||
} catch {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
function toApiErrorPayload(
|
||||
payload: JsonValue | null,
|
||||
status: number,
|
||||
): ApiErrorPayload {
|
||||
if (
|
||||
isJsonObject(payload) &&
|
||||
payload.success === false &&
|
||||
payload.data === null &&
|
||||
Array.isArray(payload.errors)
|
||||
) {
|
||||
const meta = payload.meta ?? null;
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
errors: payload.errors.map(toApiErrorDetail),
|
||||
meta: isJsonObject(meta) ? meta : null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
errors: [
|
||||
{
|
||||
code: `http_${status}`,
|
||||
message: "HTTP request failed",
|
||||
details: {
|
||||
payload,
|
||||
},
|
||||
},
|
||||
],
|
||||
meta: null,
|
||||
};
|
||||
}
|
||||
|
||||
function toApiErrorDetail(value: JsonValue): {
|
||||
code: string;
|
||||
message: string;
|
||||
details?: { readonly [key: string]: JsonValue };
|
||||
} {
|
||||
if (isJsonObject(value)) {
|
||||
const details = value.details ?? null;
|
||||
const code = typeof value.code === "string" ? value.code : "error";
|
||||
const message =
|
||||
typeof value.message === "string" ? value.message : JSON.stringify(value);
|
||||
if (isJsonObject(details)) {
|
||||
return {
|
||||
code,
|
||||
message,
|
||||
details,
|
||||
};
|
||||
}
|
||||
return {
|
||||
code,
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: "error",
|
||||
message: String(value),
|
||||
};
|
||||
}
|
||||
|
||||
function isJsonObject(value: JsonValue | null): value is {
|
||||
readonly [key: string]: JsonValue;
|
||||
} {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
8
ts_client/src/index.ts
Normal file
8
ts_client/src/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export {
|
||||
ApiClientError,
|
||||
MostovikOrganizationsClient,
|
||||
type ApiRequestOptions,
|
||||
type FetchLike,
|
||||
type MostovikOrganizationsClientOptions,
|
||||
} from "./client.js";
|
||||
export * from "./types.js";
|
||||
343
ts_client/src/types.ts
Normal file
343
ts_client/src/types.ts
Normal file
@@ -0,0 +1,343 @@
|
||||
export type UuidString = string;
|
||||
export type IsoDateString = string;
|
||||
export type IsoDateTimeString = string;
|
||||
export type DecimalString = string;
|
||||
|
||||
export type JsonPrimitive = string | number | boolean | null;
|
||||
export type JsonValue =
|
||||
| JsonPrimitive
|
||||
| JsonValue[]
|
||||
| JsonObject;
|
||||
export type JsonObject = { readonly [key: string]: JsonValue };
|
||||
export type GenericSourcePayload = JsonObject;
|
||||
|
||||
export interface ApiErrorPayload {
|
||||
success: false;
|
||||
data: null;
|
||||
errors: ApiErrorDetail[];
|
||||
meta: ApiErrorMeta | null;
|
||||
}
|
||||
|
||||
export interface ApiErrorDetail {
|
||||
code: string;
|
||||
message: string;
|
||||
details?: JsonObject;
|
||||
}
|
||||
|
||||
export interface ApiErrorMeta {
|
||||
request_id?: string;
|
||||
}
|
||||
|
||||
export const ORGANIZATION_DATA_SOURCES = [
|
||||
"arbitration",
|
||||
"contracts",
|
||||
"fas_goz",
|
||||
"fedresurs_bankruptcy",
|
||||
"fns_reports",
|
||||
"fstec",
|
||||
"industrial",
|
||||
"industrial_products",
|
||||
"inspections",
|
||||
"manufactures",
|
||||
"procurements",
|
||||
"procurements_44fz",
|
||||
"procurements_223fz",
|
||||
"unfair_suppliers",
|
||||
"vacancies",
|
||||
] as const;
|
||||
|
||||
export type OrganizationDataSource = (typeof ORGANIZATION_DATA_SOURCES)[number];
|
||||
|
||||
export type OrganizationGenericInternalSource =
|
||||
| "arbitration"
|
||||
| "contracts"
|
||||
| "fas_goz"
|
||||
| "fedresurs_bankruptcy"
|
||||
| "fstec"
|
||||
| "procurements_44fz"
|
||||
| "procurements_223fz"
|
||||
| "trudvsem"
|
||||
| "unfair_suppliers";
|
||||
|
||||
export type OrganizationOrderingField =
|
||||
| "uid"
|
||||
| "name"
|
||||
| "inn"
|
||||
| "kpp"
|
||||
| "ogrn"
|
||||
| "ogrip";
|
||||
|
||||
export type OrganizationOrdering =
|
||||
| OrganizationOrderingField
|
||||
| `-${OrganizationOrderingField}`;
|
||||
|
||||
export type QueryListValue<T> = T | readonly T[];
|
||||
|
||||
export interface OrganizationDataSelectionParams {
|
||||
/** Ограничивает блок `data` указанными источниками. В клиенте лучше передавать массив вместо CSV-строки. */
|
||||
data?: QueryListValue<OrganizationDataSource>;
|
||||
/** Alias для `data`: явно задает набор источников, которые нужно вернуть в `data`. */
|
||||
data_sources?: QueryListValue<OrganizationDataSource>;
|
||||
/** Исключает указанные источники из блока `data`. */
|
||||
exclude_data?: QueryListValue<OrganizationDataSource>;
|
||||
/** Alias для `exclude_data`: явно задает набор источников, которые нужно исключить из `data`. */
|
||||
exclude_data_sources?: QueryListValue<OrganizationDataSource>;
|
||||
}
|
||||
|
||||
export interface OrganizationListParams extends OrganizationDataSelectionParams {
|
||||
/** Номер страницы пагинации. Backend default: 1. */
|
||||
page?: number;
|
||||
/** Размер страницы. Backend default: 20, максимум 100. */
|
||||
page_size?: number;
|
||||
/** Поиск по наименованию, ИНН, КПП, ОГРН и ОГРИП. */
|
||||
search?: string;
|
||||
/** Сортировка по `uid`, `name`, `inn`, `kpp`, `ogrn`, `ogrip`; префикс `-` включает обратный порядок. */
|
||||
ordering?: OrganizationOrdering;
|
||||
/** Фильтр по части полного наименования организации. */
|
||||
name?: string;
|
||||
/** Точный фильтр по ИНН. */
|
||||
inn?: string;
|
||||
/** Точный фильтр по КПП. */
|
||||
kpp?: string;
|
||||
/** Точный фильтр по ОГРН. */
|
||||
ogrn?: string;
|
||||
/** Точный фильтр по ОГРИП. */
|
||||
ogrip?: string;
|
||||
/** UUID реестра: возвращает организации с активным участием в этом реестре. */
|
||||
registry?: UuidString;
|
||||
/** Фильтр по части наименования реестра. */
|
||||
registry_name?: string;
|
||||
/** Наличие активного участия в любом реестре. Для list backend по умолчанию применяет `true`, если параметр не передан. */
|
||||
has_registry?: boolean;
|
||||
/** Наличие данных источника `industrial`. */
|
||||
has_industrial?: boolean;
|
||||
/** Наличие данных источника `industrial_products`. */
|
||||
has_industrial_products?: boolean;
|
||||
/** Наличие данных источника `manufactures`. */
|
||||
has_manufactures?: boolean;
|
||||
/** Наличие данных источника `inspections`. */
|
||||
has_inspections?: boolean;
|
||||
/** Наличие данных источника `procurements`. */
|
||||
has_procurements?: boolean;
|
||||
/** Наличие данных источника `procurements_44fz`. */
|
||||
has_procurements_44fz?: boolean;
|
||||
/** Наличие данных источника `procurements_223fz`. */
|
||||
has_procurements_223fz?: boolean;
|
||||
/** Наличие данных источника `contracts`. */
|
||||
has_contracts?: boolean;
|
||||
/** Наличие данных источника `unfair_suppliers`. */
|
||||
has_unfair_suppliers?: boolean;
|
||||
/** Наличие данных источника `fas_goz`. */
|
||||
has_fas_goz?: boolean;
|
||||
/** Наличие данных источника `arbitration`. */
|
||||
has_arbitration?: boolean;
|
||||
/** Наличие данных источника `fedresurs_bankruptcy`. */
|
||||
has_fedresurs_bankruptcy?: boolean;
|
||||
/** Наличие данных источника `fstec`. */
|
||||
has_fstec?: boolean;
|
||||
/** Наличие данных публичного API v2 источника `vacancies` (внутренний backend source: `trudvsem`). */
|
||||
has_vacancies?: boolean;
|
||||
/** @deprecated Внутренний alias backend для `has_vacancies`; используйте `has_vacancies`. */
|
||||
has_trudvsem?: boolean;
|
||||
/** Наличие данных источника `fns_reports`. */
|
||||
has_fns_reports?: boolean;
|
||||
}
|
||||
|
||||
export interface OrganizationDetailParams extends OrganizationDataSelectionParams {}
|
||||
|
||||
export interface OrganizationListResponse {
|
||||
success: true;
|
||||
data: Organization[];
|
||||
errors: null;
|
||||
meta: {
|
||||
pagination: PagePagination;
|
||||
};
|
||||
}
|
||||
|
||||
export interface PagePagination {
|
||||
page: number;
|
||||
page_size: number;
|
||||
total_count: number;
|
||||
total_pages: number;
|
||||
has_next: boolean;
|
||||
has_previous: boolean;
|
||||
}
|
||||
|
||||
export interface Organization {
|
||||
uid: UuidString;
|
||||
name: string;
|
||||
normalized_name: string;
|
||||
inn: string;
|
||||
kpp: string;
|
||||
ogrn: string;
|
||||
ogrip: string;
|
||||
data: OrganizationData;
|
||||
data_sources: OrganizationDataSourceSummary[];
|
||||
registries: OrganizationRegistry[];
|
||||
}
|
||||
|
||||
export interface OrganizationRegistry {
|
||||
id: UuidString;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface OrganizationDataSourceSummary {
|
||||
source: OrganizationDataSource;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export type OrganizationData = Partial<OrganizationDataBySource>;
|
||||
|
||||
export interface OrganizationDataBySource {
|
||||
arbitration: GenericOrganizationSourceRecord[];
|
||||
contracts: GenericOrganizationSourceRecord[];
|
||||
fas_goz: GenericOrganizationSourceRecord[];
|
||||
fedresurs_bankruptcy: GenericOrganizationSourceRecord[];
|
||||
fns_reports: FinancialReportRecord[];
|
||||
fstec: GenericOrganizationSourceRecord[];
|
||||
industrial: IndustrialCertificateRecord[];
|
||||
industrial_products: IndustrialProductRecord[];
|
||||
inspections: InspectionRecord[];
|
||||
manufactures: ManufacturerRecord[];
|
||||
procurements: ProcurementRecord[];
|
||||
procurements_44fz: GenericOrganizationSourceRecord[];
|
||||
procurements_223fz: GenericOrganizationSourceRecord[];
|
||||
unfair_suppliers: GenericOrganizationSourceRecord[];
|
||||
vacancies: GenericOrganizationSourceRecord[];
|
||||
}
|
||||
|
||||
export interface OrganizationTimedRecord {
|
||||
id: number;
|
||||
load_batch: number;
|
||||
registry_organization: number | null;
|
||||
created_at: IsoDateTimeString;
|
||||
updated_at: IsoDateTimeString;
|
||||
}
|
||||
|
||||
export interface IndustrialCertificateRecord extends OrganizationTimedRecord {
|
||||
issue_date: string;
|
||||
issue_date_normalized: IsoDateString | null;
|
||||
certificate_number: string;
|
||||
expiry_date: string;
|
||||
expiry_date_normalized: IsoDateString | null;
|
||||
certificate_file_url: string;
|
||||
organisation_name: string;
|
||||
inn: string;
|
||||
ogrn: string;
|
||||
}
|
||||
|
||||
export interface IndustrialProductRecord extends OrganizationTimedRecord {
|
||||
full_organisation_name: string;
|
||||
ogrn: string;
|
||||
inn: string;
|
||||
registry_number: string;
|
||||
product_name: string;
|
||||
product_model: string;
|
||||
okpd2_code: string;
|
||||
tnved_code: string;
|
||||
regulatory_document: string;
|
||||
}
|
||||
|
||||
export interface ManufacturerRecord extends OrganizationTimedRecord {
|
||||
full_legal_name: string;
|
||||
inn: string;
|
||||
ogrn: string;
|
||||
address: string;
|
||||
}
|
||||
|
||||
export interface InspectionRecord extends OrganizationTimedRecord {
|
||||
registration_number: string;
|
||||
inn: string;
|
||||
ogrn: string;
|
||||
organisation_name: string;
|
||||
control_authority: string;
|
||||
inspection_type: string;
|
||||
inspection_form: string;
|
||||
start_date: string;
|
||||
start_date_normalized: IsoDateString | null;
|
||||
end_date: string;
|
||||
end_date_normalized: IsoDateString | null;
|
||||
status: string;
|
||||
legal_basis: string;
|
||||
result: string;
|
||||
is_federal_law_248: boolean;
|
||||
data_year: number | null;
|
||||
data_month: number | null;
|
||||
}
|
||||
|
||||
export interface ProcurementRecord extends OrganizationTimedRecord {
|
||||
purchase_number: string;
|
||||
purchase_name: string;
|
||||
customer_inn: string;
|
||||
customer_kpp: string;
|
||||
customer_ogrn: string;
|
||||
customer_name: string;
|
||||
max_price: string;
|
||||
max_price_amount: DecimalString | null;
|
||||
currency_code: string;
|
||||
placement_method: string;
|
||||
publish_date: string;
|
||||
publish_date_normalized: IsoDateString | null;
|
||||
end_date: string;
|
||||
end_date_normalized: IsoDateString | null;
|
||||
status: string;
|
||||
law_type: string;
|
||||
purchase_object_info: string;
|
||||
href: string;
|
||||
region_code: string;
|
||||
data_year: number | null;
|
||||
data_month: number | null;
|
||||
}
|
||||
|
||||
export interface GenericOrganizationSourceRecord extends OrganizationTimedRecord {
|
||||
source: OrganizationGenericInternalSource;
|
||||
external_id: string;
|
||||
inn: string;
|
||||
ogrn: string;
|
||||
organisation_name: string;
|
||||
title: string;
|
||||
record_date: string;
|
||||
amount: DecimalString | null;
|
||||
status: string;
|
||||
url: string;
|
||||
payload: GenericSourcePayload;
|
||||
}
|
||||
|
||||
export type FinancialReportStatus =
|
||||
| "failed"
|
||||
| "pending"
|
||||
| "processing"
|
||||
| "success";
|
||||
|
||||
export type FinancialReportSource = "api" | "file_watch";
|
||||
|
||||
export interface FinancialReportRecord {
|
||||
id: number;
|
||||
external_id: string;
|
||||
ogrn: string;
|
||||
registry_organization: number | null;
|
||||
file_name: string;
|
||||
file_hash: string;
|
||||
load_batch: number;
|
||||
status: FinancialReportStatus;
|
||||
source: FinancialReportSource;
|
||||
error_message: string;
|
||||
created_at: IsoDateTimeString;
|
||||
updated_at: IsoDateTimeString;
|
||||
lines_count: number;
|
||||
lines: FinancialReportLinesByYear;
|
||||
}
|
||||
|
||||
export type FinancialReportSection = "active" | "balance" | "passive" | `form_${string}`;
|
||||
|
||||
export type FinancialReportLinesByYear = Record<
|
||||
string,
|
||||
Partial<Record<FinancialReportSection, Record<string, FinancialReportLineValue>>>
|
||||
>;
|
||||
|
||||
export interface FinancialReportLineValue {
|
||||
form_code: string;
|
||||
name: string;
|
||||
period_start: number | null;
|
||||
period_end: number | null;
|
||||
}
|
||||
Reference in New Issue
Block a user