Skip to content

Commit

Permalink
feat(core): sets default response type as null; maps undefined to nul…
Browse files Browse the repository at this point in the history
…l on services
  • Loading branch information
rafamel committed Oct 24, 2019
1 parent 1de6069 commit 6577ec6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ApplicationServices
} from '~/types';
import { validate, traverse, isElementService, atPath } from '~/inspect';
import { addInterceptErrors } from './intercept-errors';
import { addInterceptResponse } from './intercept-response';
import { mergeIntercepts } from './merge-intercepts';
import { handleChildren } from './handle-children';
import { getRoutes } from './get-routes';
Expand Down Expand Up @@ -35,9 +35,10 @@ export type ApplicationCreateMapFn<I = any, O = any, C = any> = (

/**
* Validates and prepares a collection to be used:
* - Adds `ServerError` and `ClientError` error types, if non existent, to the collection declaration.
* - Handles children adequately according to whether they have children services.
* - Merges service intercepts into each route resolver, and makes them fail with `PublicError`s, if they don't already do.
* - Merges service intercepts into each route resolver.
* - Ensures services fail with a `PublicError` and resolve with `null` for empty responses.
* - Ensures `ServerError` and `ClientError` error types exist on the collection declaration.
* - Names and lifts inline types to the collection root if they have children services.
*/
export function application<T extends CollectionTreeImplementation>(
collection: T,
Expand All @@ -59,7 +60,7 @@ export function application<T extends CollectionTreeImplementation>(
let tree: CollectionTreeImplementation = collection;
if (opts.validate) validate(tree, { as: 'implementation' });
tree = handleChildren(tree, opts.children ? 'lift' : 'remove');
tree = addInterceptErrors(tree);
tree = addInterceptResponse(tree);
tree = mergeIntercepts(tree);

const declaration = toDeclaration(tree);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CollectionTreeImplementation } from '~/types';
import { catchError } from 'rxjs/operators';
import { catchError, map } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { PublicError, CollectionError } from '~/errors';
import { error, intercepts, intercept } from '~/create';

export function addInterceptErrors(
export function addInterceptResponse(
collection: CollectionTreeImplementation
): CollectionTreeImplementation {
const errors = {
Expand All @@ -27,6 +27,7 @@ export function addInterceptErrors(
),
factory: () => (data, context, info, next) => {
return next(data).pipe(
map((value) => (value === undefined ? null : value)),
catchError((err) =>
throwError(
err instanceof PublicError
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/create/implement/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ function parseTypes(types: ServiceInputTypes = {}): ServiceTypesImplementation {
request:
isElement(types.request) || typeof types.request === 'string'
? types.request
: request({ schema: types.request || {} }),
: request({ schema: types.request || { type: 'object' } }),
response:
isElement(types.response) || typeof types.response === 'string'
? types.response
: response({ schema: types.response || {} })
: response({ schema: types.response || { type: 'null' } })
};
}
3 changes: 2 additions & 1 deletion packages/core/src/inspect/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { normalize } from '~/transform/normalize';
import { isTreeImplementation } from './is';

// TODO: validate collection object (ajv) + check schemas are valid

// TODO: children services must have a request schema equal or as a subset of the type they belong to
// TODO: all request types must be of type object
export interface ValidateInspectOptions {
/**
* If specified, it will also throw if the collection is not a full implementation or solely a declaration, in each case. Default: `null`.
Expand Down

0 comments on commit 6577ec6

Please sign in to comment.