Skip to content

Commit

Permalink
feat(core/create): adds optional validation to application
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 16, 2019
1 parent 8ea5a22 commit 6061a2d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/core/src/create/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@ import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { PublicError, CollectionError } from '~/errors';
import { error } from '../types';
import { isElementService, validate, isServiceImplementation } from '~/inspect';
import {
isElementService,
validate,
isServiceImplementation,
isTreeImplementation
} from '~/inspect';
import { replace } from '~/transform';

export interface ApplicationCreateOptions {
/**
* Whether the collection should be validated. Default: `true`.
*/
validate?: boolean;
}

/**
* Validates and prepares a collection to be used by an adapter. Returns a new collection with:
* - `ServerError` and `ClientError` error types, if non existent, for internal usage.
* - All of its services errors rethrown as a `PublicError`s, if they're not already one, for `CollectionImplementation`s.
* - Intercepts merged into their services, for `CollectionImplementation`s.
*/
export function application<T extends CollectionTree>(
collection: T
collection: T,
options?: ApplicationCreateOptions
): ApplicationCollection<T> {
const opts = Object.assign({ validate: true }, options);

// adds global errors
const errors = {
ServerError: error({ code: 'ServerError' }),
Expand All @@ -31,7 +46,11 @@ export function application<T extends CollectionTree>(
};

// if not an implementation, return as is
if (!validate(application)) return application;
if (opts.validate) {
if (!validate(application)) return application;
} else {
if (!isTreeImplementation(application)) return application;
}

application = intercepts(
application,
Expand Down

0 comments on commit 6061a2d

Please sign in to comment.