UNPKG

@dossierhq/core

Version:

The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.

186 lines 7.05 kB
/// <reference types="./PublishedDossierClient.d.ts" /> import { ErrorType, notOk, ok, } from '../ErrorResult.js'; import { convertJsonConnection, convertJsonEdge, convertJsonPublishedEntity, convertJsonResult, } from './JsonUtils.js'; import { executeOperationPipeline, } from './SharedClient.js'; export const PublishedDossierClientOperationName = { getEntities: 'getEntities', getEntitiesSample: 'getEntitiesSample', getEntitiesTotalCount: 'getEntitiesTotalCount', getEntity: 'getEntity', getEntityList: 'getEntityList', getSchemaSpecification: 'getSchemaSpecification', }; class BasePublishedDossierClient { context; pipeline; constructor({ context, pipeline, }) { this.context = context; this.pipeline = pipeline; } getSchemaSpecification() { return this.executeOperation({ name: PublishedDossierClientOperationName.getSchemaSpecification, args: [], modifies: false, }); } getEntity(reference) { return this.executeOperation({ name: PublishedDossierClientOperationName.getEntity, args: [reference], modifies: false, }); } getEntityList(references) { return this.executeOperation({ name: PublishedDossierClientOperationName.getEntityList, args: [references], modifies: false, }); } getEntities(query, paging) { return this.executeOperation({ name: PublishedDossierClientOperationName.getEntities, args: [query, paging], modifies: false, }); } getEntitiesTotalCount(query) { return this.executeOperation({ name: PublishedDossierClientOperationName.getEntitiesTotalCount, args: [query], modifies: false, }); } getEntitiesSample(query, options) { return this.executeOperation({ name: PublishedDossierClientOperationName.getEntitiesSample, args: [query, options], modifies: false, }); } toExceptionClient() { return new PublishedExceptionClientWrapper(this); } async executeOperation(operation) { let context; if (typeof this.context === 'function') { const contextResult = await this.context(); if (contextResult.isError()) { if (contextResult.isErrorType(ErrorType.Generic)) { return contextResult; } //TODO maybe operation should have a list of supported error types? return notOk.GenericUnexpectedError(contextResult); } context = contextResult.value.context; } else { context = this.context; } return await executeOperationPipeline(context, this.pipeline, operation); } } class PublishedExceptionClientWrapper { client; constructor(client) { this.client = client; } async getSchemaSpecification() { return (await this.client.getSchemaSpecification()).valueOrThrow(); } async getEntity(reference) { return (await this.client.getEntity(reference)).valueOrThrow(); } async getEntityList(references) { return (await this.client.getEntityList(references)).valueOrThrow(); } async getEntities(query, paging) { return (await this.client.getEntities(query, paging)).valueOrThrow(); } async getEntitiesTotalCount(query) { return (await this.client.getEntitiesTotalCount(query)).valueOrThrow(); } async getEntitiesSample(query, options) { return (await this.client.getEntitiesSample(query, options)).valueOrThrow(); } } export function createBasePublishedDossierClient(option) { return new BasePublishedDossierClient(option); } export async function executeJsonPublishedDossierClientOperation(publishedClient, operationName, operationArgs) { const name = operationName; switch (name) { case PublishedDossierClientOperationName.getEntities: { const [query, paging] = operationArgs; return await publishedClient.getEntities(query, paging); } case PublishedDossierClientOperationName.getEntitiesSample: { const [query, options] = operationArgs; return await publishedClient.getEntitiesSample(query, options); } case PublishedDossierClientOperationName.getEntitiesTotalCount: { const [query] = operationArgs; return await publishedClient.getEntitiesTotalCount(query); } case PublishedDossierClientOperationName.getEntity: { const [reference] = operationArgs; return await publishedClient.getEntity(reference); } case PublishedDossierClientOperationName.getEntityList: { const [references] = operationArgs; return await publishedClient.getEntityList(references); } case PublishedDossierClientOperationName.getSchemaSpecification: { return await publishedClient.getSchemaSpecification(); } default: { name; return notOk.BadRequest(`Unknown operation ${operationName}`); } } } export function convertJsonPublishedDossierClientResult(operationName, jsonResult) { if (jsonResult.isError()) { //TODO check expected types return jsonResult; } const { value } = jsonResult; switch (operationName) { case PublishedDossierClientOperationName.getEntities: { const result = ok(convertJsonConnection(value, convertJsonPublishedEntityEdge)); return result; } case PublishedDossierClientOperationName.getEntitiesSample: { const payload = value; const result = ok({ ...payload, items: payload.items.map(convertJsonPublishedEntity), }); return result; } case PublishedDossierClientOperationName.getEntitiesTotalCount: return ok(value); case PublishedDossierClientOperationName.getEntity: { const result = ok(convertJsonPublishedEntity(value)); return result; } case PublishedDossierClientOperationName.getEntityList: { const result = ok(value.map((jsonItemResult) => { const itemResult = convertJsonResult(jsonItemResult); return itemResult.isOk() ? itemResult.map(convertJsonPublishedEntity) : itemResult; })); return result; } case PublishedDossierClientOperationName.getSchemaSpecification: return ok(value); default: { operationName; return notOk.Generic(`Unknown operation ${operationName}`); } } } function convertJsonPublishedEntityEdge(edge) { return convertJsonEdge(edge, convertJsonPublishedEntity); } //# sourceMappingURL=PublishedDossierClient.js.map