Skip to content

Commit

Permalink
fix: fixes core dependent packages
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Nov 24, 2019
1 parent 6317dca commit c8ba0a7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 97 deletions.
8 changes: 4 additions & 4 deletions packages/intercepts/src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
intercept,
PublicError,
InterceptImplementation,
ServiceElementKind
ServiceKind,
Intercept
} from '@karmic/core';
import { tap } from 'rxjs/operators';
import { Observable } from 'rxjs';
Expand All @@ -13,7 +13,7 @@ export type LogStatus = 'success' | 'error' | 'complete' | 'unsubscribe';

export interface LogData {
route: string;
kind: ServiceElementKind;
kind: ServiceKind;
status: LogStatus;
context: any;
request: object;
Expand Down Expand Up @@ -82,7 +82,7 @@ export function logging(
};
}

return intercept({
return new Intercept({
factory: () => (data, context, info, next) => {
const start = Date.now();
const observable = next(data);
Expand Down
37 changes: 20 additions & 17 deletions packages/intercepts/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
InterceptImplementation,
intercept,
error,
ServiceErrorsImplementation,
PublicError,
ElementItem,
ErrorTypeUnion,
ErrorTypeImplementation,
item,
validator
validator,
Intercept,
ServiceExceptionsImplementation,
ExceptionUnion,
ExceptionImplementation,
Exception
} from '@karmic/core';
import { switchMap } from 'rxjs/operators';
import { of, throwError } from 'rxjs';
Expand All @@ -20,14 +20,14 @@ export interface ValidationOptions {
* If a `ValidationError` is passed, it will be used to fail instead.
* Default: `true`.
*/
request?: ElementItem<ErrorTypeUnion<'ClientInvalid'>> | boolean;
request?: ElementItem<ExceptionUnion<'ClientInvalid'>> | boolean;
/**
* If `false`, it won't validate responses.
* If `true`, it will validate responses and use a default `ValidationError` to fail.
* If a `ValidationError` is passed, it will be used to fail instead.
* Default: `false`.
*/
response?: ElementItem<ErrorTypeUnion> | boolean;
response?: ElementItem<ExceptionUnion> | boolean;
}

/**
Expand All @@ -38,32 +38,35 @@ export function validation(
): InterceptImplementation<any, any, any> {
const opts = Object.assign({ request: true, response: false }, options);

const requestError: ElementItem<ErrorTypeImplementation> | null = opts.request
const requestError: ElementItem<ExceptionImplementation> | null = opts.request
? opts.request === true
? item('RequestValidationError', error({ label: 'ClientInvalid' }))
? item(
'RequestValidationError',
new Exception({ label: 'ClientInvalid' })
)
: opts.request
: null;
const responseError: ElementItem<
ErrorTypeImplementation
ExceptionImplementation
> | null = opts.response
? opts.response === true
? item('ResponseValidationError', error({ label: 'ServerError' }))
? item('ResponseValidationError', new Exception({ label: 'ServerError' }))
: opts.response
: null;

const errors: ServiceErrorsImplementation = [];
const exceptions: ServiceExceptionsImplementation = [];
if (requestError) {
if (requestError.item.label !== 'ClientInvalid') {
throw Error(
`Request error must have label ClientInvalid: ${requestError.item.label}`
);
}
errors.push(requestError);
exceptions.push(requestError);
}
if (responseError) errors.push(responseError);
if (responseError) exceptions.push(responseError);

return intercept({
errors,
return new Intercept({
exceptions,
factory: (schemas) => {
const validateRequest = validator.compile(schemas.request);
const validateResponse = validator.compile(schemas.response);
Expand Down
17 changes: 8 additions & 9 deletions packages/rest/src/server/RESTServer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
CollectionTreeDeclaration,
CollectionTreeImplementation,
application,
toUnary,
filter,
isElementService,
isServiceSubscription,
CollectionError
Application,
Collection
} from '@karmic/core';
import { createDefaults } from './defaults';
import { ServerRouter } from './ServerRouter';
Expand All @@ -27,11 +25,12 @@ export class RESTServer {
options?: RESTServerOptions
) {
this.options = Object.assign(createDefaults(), options);
const app = application(

const instance = new Collection(collection);
const app = new Application(
this.options.subscriptions
? toUnary(collection)
: filter(
collection,
? instance.toUnary()
: instance.filter(
(element) =>
!isElementService(element) || !isServiceSubscription(element)
),
Expand Down Expand Up @@ -66,7 +65,7 @@ export class RESTServer {
const item = mapError(err);
const error = item
? item.error
: new CollectionError(this.declaration, 'ServerError', err, true);
: new Collection(this.declaration).error('ServerError', err, true);

return {
status: item ? item.status : 500,
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/server/map-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorLabel, PublicError } from '@karmic/core';
import { ExceptionLabel, PublicError } from '@karmic/core';

export const hash: { [P in ErrorLabel]: number } = {
export const hash: { [P in ExceptionLabel]: number } = {
// Client
ClientError: 400,
ClientUnauthorized: 401,
Expand Down
55 changes: 22 additions & 33 deletions packages/rpc/src/reproduce.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import {
ElementItem,
ExceptionUnion,
CollectionTreeUnion,
CollectionTreeImplementation,
toImplementation,
Exception,
Collection,
isServiceQuery,
Service,
isServiceMutation,
isServiceSubscription,
query,
mutation,
subscription,
error,
intercepts,
intercept,
PublicError,
CollectionError,
collections,
types,
ElementItem,
ErrorTypeUnion,
reference
Intercept,
PublicError
} from '@karmic/core';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { RPCClient } from './client';

export interface RPCReproduceOptions {
proxyError: ElementItem<ErrorTypeUnion<'ServerGateway'>>;
proxyException: ElementItem<ExceptionUnion<'ServerGateway'>>;
}

/**
Expand All @@ -38,54 +31,50 @@ export async function reproduce(
): Promise<CollectionTreeImplementation> {
const opts = Object.assign(
{
proxyError: {
proxyException: {
name: 'ProxyError',
item: error({ label: 'ServerGateway' })
item: new Exception({ label: 'ServerGateway' })
}
},
options
);

const implementation = collections(
toImplementation(await collection, (service, info) => {
const implementation = Collection.merge(
new Collection(await collection).toImplementation((service, info) => {
if (isServiceQuery(service)) {
return query({
return Service.query({
...service,
resolve: (data: any) => client.unary(info.route.join(':'), data)
});
} else if (isServiceMutation(service)) {
return mutation({
return Service.mutation({
...service,
resolve: (data: any) => client.unary(info.route.join(':'), data)
});
} else if (isServiceSubscription(service)) {
return subscription({
return Service.subscription({
...service,
resolve: (data: any) => client.stream(info.route.join(':'), data)
});
} else {
throw Error(`Invalid service kind: ${JSON.stringify(service)}`);
}
}),
types({ [opts.proxyError.name]: opts.proxyError.item })
Collection.exceptions({
[opts.proxyException.name]: opts.proxyException.item
})
);

return intercepts(
implementation,
intercept({
errors: reference(implementation, [opts.proxyError.name]),
return implementation.intercept(
new Intercept({
exceptions: implementation.reference([opts.proxyException.name]),
factory: () => (data, context, info, next) => {
return next(data).pipe(
catchError((err) => {
return throwError(
err instanceof PublicError
? err
: new CollectionError(
implementation,
opts.proxyError.name,
err,
true
)
: implementation.error(opts.proxyException.name, err, true)
);
})
);
Expand Down
24 changes: 12 additions & 12 deletions packages/rpc/src/server/RPCServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {
CollectionTreeImplementation,
application,
CollectionTreeDeclaration,
services,
query,
collections,
types
CollectionTreeImplementation,
Application,
Collection,
Service
} from '@karmic/core';
import { RPCServerOptions, RPCServerConnection } from './types';
import { createDefaults } from './defaults';
Expand All @@ -20,17 +18,19 @@ export class RPCServer {
options?: RPCServerOptions
) {
const opts = Object.assign(createDefaults(), options);
const app = application(
collections(
const app = new Application(
Collection.merge(
collection,
types({ [opts.complete.name]: opts.complete.item })
Collection.exceptions({ [opts.complete.name]: opts.complete.item })
),
options && options.fallback
? { fallback: options.fallback, children: opts.children }
: { children: opts.children }
);
const dapp = application(
services({ declaration: query({ resolve: () => app.declaration }) })
const dapp = new Application(
Collection.services({
declaration: Service.query({ resolve: () => app.declaration })
})
);

this.declaration = app.declaration;
Expand All @@ -41,7 +41,7 @@ export class RPCServer {
kind: 'query',
request: '',
response: '',
errors: []
exceptions: []
},
resolve: app.fallback
},
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc/src/server/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { RPCServerOptions } from './types';
import { DataOutput, DataInput } from '~/types';
import { error } from '@karmic/core';
import { Exception } from '@karmic/core';

export function createDefaults(): Required<Omit<RPCServerOptions, 'fallback'>> {
return {
children: true,
complete: {
name: 'EarlyCompleteError',
item: error({ label: 'ServerError' })
item: new Exception({ label: 'ServerError' })
},
parser: {
serialize(data: object): DataOutput {
Expand Down
16 changes: 8 additions & 8 deletions packages/rpc/src/server/errors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
PublicError,
ErrorLabel,
CollectionError,
ExceptionLabel,
CollectionTreeUnion,
ElementItem,
ErrorTypeUnion
ExceptionUnion,
PublicError,
Collection
} from '@karmic/core';
import { RPCError } from '~/types';
import { ErrorCodes } from '~/errors';

export const hash: { [P in ErrorLabel]: number } = {
export const hash: { [P in ExceptionLabel]: number } = {
// Client
ClientError: -32000,
ClientUnauthorized: -32001,
Expand Down Expand Up @@ -43,13 +43,13 @@ export type RPCErrorProviderType =

export function createErrorProvider(
collection: CollectionTreeUnion,
complete: ElementItem<ErrorTypeUnion>
complete: ElementItem<ExceptionUnion>
): ErrorProvider {
const instance = new Collection(collection);
function ensure(error: PublicErrorProviderType): PublicError {
return error instanceof PublicError
? error
: new CollectionError(
collection,
: instance.error(
error === 'EarlyComplete' ? complete.name : 'ServerError',
null,
true
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/server/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
QueryServiceImplementation,
ElementItem,
ErrorTypeUnion
ExceptionUnion
} from '@karmic/core';
import { DataOutput, DataInput, DataParser } from '~/types';
import { Observable } from 'rxjs';
Expand All @@ -17,10 +17,10 @@ export interface RPCServerOptions {
*/
parser?: DataParser;
/**
* An error to emit if a stream completes before emitting any values.
* An exception to emit if a stream completes before emitting any values.
* Defaults to an *EarlyCompleteError* `ServerError`.
*/
complete?: ElementItem<ErrorTypeUnion>;
complete?: ElementItem<ExceptionUnion>;
/**
* A fallback service for adapters to use when the route is non existent.
* Defaults to a `ClientNotFound` error throwing service.
Expand Down

0 comments on commit c8ba0a7

Please sign in to comment.