78 lines
7.5 KiB
JavaScript
78 lines
7.5 KiB
JavaScript
/** Typed all-available contract, composed with generated legacy DRF operations. */
|
|
export function addSourceExportCatalog(document) {
|
|
const schemas = document.components.schemas;
|
|
const ref = name => ({ $ref: `#/components/schemas/${name}` });
|
|
const string = { type: 'string' };
|
|
const uuid = { type: 'string', format: 'uuid' };
|
|
const date = { type: 'string', format: 'date-time' };
|
|
const integer = { type: 'integer', minimum: 0, maximum: 9007199254740991 };
|
|
const positive = { ...integer, minimum: 1 };
|
|
const year = { type: 'integer', minimum: 1, maximum: 9999, nullable: true };
|
|
const bool = { type: 'boolean' };
|
|
const enumeration = values => ({ type: 'string', enum: values });
|
|
const format = enumeration(['xlsx', 'csv', 'json']);
|
|
const nullable = schema => ({ ...schema, nullable: true });
|
|
const nil = { type: 'object', nullable: true, enum: [null] };
|
|
const array = items => ({ type: 'array', items });
|
|
const object = properties => ({ type: 'object', additionalProperties: false, properties, required: Object.keys(properties) });
|
|
const group = structuredClone(schemas.OrganizationSourceRecordExportRequest.properties.sources.items);
|
|
schemas.SourceExportBuildSummary = object({ state: enumeration(['idle', 'queued', 'running', 'success', 'failed']), started_at: nullable(date), finished_at: nullable(date), error_code: nullable(string) });
|
|
const snapshot = {
|
|
source_group: group, generation_id: nullable(uuid),
|
|
state: enumeration(['ready', 'empty', 'not_ready', 'building', 'unavailable']),
|
|
unavailable_reason: nullable(enumeration(['disabled', 'adapter_missing', 'build_failed', 'storage_unavailable'])),
|
|
generated_at: nullable(date), data_as_of: nullable(date), data_scope: enumeration(['all_available']), partitioning: enumeration(['year', 'none']),
|
|
};
|
|
schemas.SourceExportSourceSummary = object({ ...snapshot, source_slug: string, title: string, order: integer, export_enabled: bool,
|
|
records_count: nullable(integer), exported_records_count: nullable(integer), parts_count: nullable(integer), supported_formats: { ...array(format), uniqueItems: true },
|
|
default_format: nullable(format), is_stale: bool, last_build: ref('SourceExportBuildSummary') });
|
|
const envelope = (data, meta = nil) => object({ success: { type: 'boolean', enum: [true] }, data, errors: nil, meta });
|
|
schemas.SourceExportCatalogResponse = envelope(object({ schema_version: enumeration(['2']), catalog_revision: string, sources: array(ref('SourceExportSourceSummary')) }));
|
|
schemas.SourceExportDescription = object({ composition: string, origin: string, fields: string, example_dataset_key: nullable(string), example_is_synthetic: bool,
|
|
example_record: { type: 'object', additionalProperties: true, nullable: true } });
|
|
schemas.SourceExportDataset = object({ key: string, year, title: string, export_schema_version: string, records_count: integer, parts_count: integer });
|
|
schemas.SourceExportArtifact = object({ artifact_id: uuid, format, file_name: { type: 'string', pattern: '^[A-Za-z0-9._-]+$' }, mime_type: string,
|
|
size_bytes: positive, sha256: { type: 'string', pattern: '^[0-9a-f]{64}$' } });
|
|
schemas.SourceExportPart = object({ part_id: uuid, year, dataset_key: string, title: string, part_number: positive, parts_count: positive, records_count: positive,
|
|
default_format: format, variants: { ...array(ref('SourceExportArtifact')), minItems: 1 } });
|
|
schemas.SourceExportPagination = object({ page: positive, page_size: { ...positive, maximum: 200 }, total_count: integer, total_pages: positive, has_next: bool, has_previous: bool });
|
|
schemas.SourceExportSourceDetail = object({ ...snapshot, current_generation_id: nullable(uuid), description: ref('SourceExportDescription'), datasets: array(ref('SourceExportDataset')), files: array(ref('SourceExportPart')) });
|
|
schemas.SourceExportSourceDetailResponse = envelope(ref('SourceExportSourceDetail'), object({ pagination: ref('SourceExportPagination') }));
|
|
schemas.SourceExportAuthenticationError = object({ detail: string });
|
|
schemas.SourceExportError = object({ detail: string, code: string, field: nullable(string), retryable: bool, request_id: uuid });
|
|
schemas.SourceExportFileTicketRequest = object({ selection_kind: enumeration(['file']), source_group: group, generation_id: uuid, part_id: uuid, format });
|
|
schemas.SourceExportArchiveTicketRequest = object({ selection_kind: enumeration(['source_archive']), source_group: group, generation_id: uuid, format });
|
|
schemas.OrganizationSourceRecordExportRequest.additionalProperties = false;
|
|
schemas.SourceRecordExportTicket.additionalProperties = false;
|
|
schemas.SourceRecordExportTicket.required = Object.keys(schemas.SourceRecordExportTicket.properties);
|
|
schemas.SourceExportSelectionTicket = object({ ticket: { type: 'string', minLength: 47, maxLength: 47 }, file_name: string, expires_in: { type: 'integer', enum: [300] },
|
|
selection_kind: enumeration(['file', 'source_archive']), source_group: group, generation_id: uuid, format, mime_type: string, size_bytes: nullable(positive), parts_count: positive });
|
|
schemas.SourceRecordExportDownloadRequest = object({ ticket: { type: 'string', minLength: 1, maxLength: 128 } });
|
|
const base = '/api/v2/organization-source-records/';
|
|
const json = schema => ({ 'application/json': { schema } });
|
|
const response = (schema, description = 'Success') => ({ description, content: json(schema) });
|
|
const errorResponses = Object.fromEntries([400,403,404,409,410,429,503,500].map(code => [code, response(ref('SourceExportError'), 'Export request failed')]));
|
|
for (const [endpoint, schema] of [['export-catalog/', 'SourceExportCatalogResponse'], ['export-catalog/{source_group}/', 'SourceExportSourceDetailResponse']]) {
|
|
const operation = document.paths[base + endpoint].get;
|
|
operation.responses = { ...errorResponses, 200: response(ref(schema)), 401: response(ref('SourceExportAuthenticationError'), 'Authentication required') };
|
|
operation.parameters = endpoint.includes('{') ? [
|
|
{ name: 'source_group', in: 'path', required: true, schema: group },
|
|
{ name: 'generation_id', in: 'query', schema: uuid },
|
|
{ name: 'page', in: 'query', schema: { ...positive, default: 1 } },
|
|
{ name: 'page_size', in: 'query', schema: { ...positive, maximum: 200, default: 50 } },
|
|
] : [];
|
|
}
|
|
const ticket = document.paths[base + 'export-ticket/'].post;
|
|
ticket.requestBody = { required: true, content: json({ oneOf: ['OrganizationSourceRecordExportRequest', 'SourceExportFileTicketRequest', 'SourceExportArchiveTicketRequest'].map(ref) }) };
|
|
ticket.responses = { ...errorResponses, 201: response({ oneOf: ['SourceRecordExportTicket', 'SourceExportSelectionTicket'].map(ref) }), 401: response(ref('SourceExportAuthenticationError'), 'Authentication required') };
|
|
const download = document.paths[base + 'export-download/'].post;
|
|
download.requestBody = { required: true, content: { 'application/x-www-form-urlencoded': { schema: ref('SourceRecordExportDownloadRequest') } } };
|
|
download.responses = { ...errorResponses, 200: { description: 'Pinned artifact or streaming ZIP64', content: Object.fromEntries([
|
|
'application/zip', 'application/json', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
].map(mime => [mime, { schema: { type: 'string', format: 'binary' } }])) } };
|
|
for (const [status, legacy] of [[400, 'SourceRecordExportInvalidTicket'], [410, 'SourceRecordExportTicketExpired'], [503, 'SourceRecordExportUnavailable']]) {
|
|
download.responses[status] = response({ oneOf: [ref(legacy), ref('SourceExportError')] }, 'Legacy or all-available export failure');
|
|
}
|
|
|
|
}
|