Skip to content

Commit

Permalink
feat(core): makes query, mutation, and subscription static Service me…
Browse files Browse the repository at this point in the history
…thods only take implementations
  • Loading branch information
rafamel committed Nov 24, 2019
1 parent 6592633 commit abc1dea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
40 changes: 20 additions & 20 deletions packages/core/src/classes/Service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,43 @@ import { switchMap } from 'rxjs/operators';
import { isServiceImplementation } from '~/inspect';

export class Service<
T = false,
K extends ServiceKind = ServiceKind,
T = void,
I = any,
O = any,
C = any
> extends Element<ServiceUnion> {
public static ensure<
T = false,
K extends ServiceKind = ServiceKind,
T = void,
I = any,
O = any,
C = any
>(service: ServiceInput<K, T, I, O, C>): Service<K, T, I, O, C> {
>(service: ServiceInput<T, K, I, O, C>): Service<T, K, I, O, C> {
return service instanceof Service ? service : new Service(service as any);
}
public static query<T = void, I = any, O = any, C = any>(
query?: ServiceQueryInput<T, I, O, C>
): Service<'query', T, I, O, C> {
public static query<I = any, O = any, C = any>(
query: ServiceQueryInput<true, I, O, C>
): Service<true, 'query', I, O, C> {
return new Service({ kind: 'query', ...query });
}
public static mutation<T = void, I = any, O = any, C = any>(
mutation?: ServiceMutationInput<T, I, O, C>
): Service<'mutation', T, I, O, C> {
public static mutation<I = any, O = any, C = any>(
mutation: ServiceMutationInput<true, I, O, C>
): Service<true, 'mutation', I, O, C> {
return new Service({ kind: 'mutation', ...mutation });
}
public static subscription<T = void, I = any, O = any, C = any>(
subscription?: ServiceSubscriptionInput<T, I, O, C>
): Service<'subscription', T, I, O, C> {
public static subscription<I = any, O = any, C = any>(
subscription: ServiceSubscriptionInput<true, I, O, C>
): Service<true, 'subscription', I, O, C> {
return new Service({ kind: 'subscription', ...subscription });
}
public readonly kind: K;
public readonly request: string | AbstractSchema;
public readonly response: string | AbstractSchema;
public readonly exceptions: ServiceExceptionsUnion;
public readonly resolve: ServiceElement<K, T, I, O, C>['resolve'];
public readonly intercepts: ServiceElement<K, T, I, O, C>['intercepts'];
public constructor(service: ServiceInput<K, T, I, O, C>) {
public readonly resolve: ServiceElement<T, K, I, O, C>['resolve'];
public readonly intercepts: ServiceElement<T, K, I, O, C>['intercepts'];
public constructor(service: ServiceInput<T, K, I, O, C>) {
super(service.kind);
this.request = service.request || new Schema(null, { type: 'object' });
this.response = service.response || new Schema(null, { type: 'null' });
Expand All @@ -83,10 +83,10 @@ export class Service<
};
}

this.resolve = resolve as ServiceElement<K, T, I, O, C>['resolve'];
this.resolve = resolve as ServiceElement<T, K, I, O, C>['resolve'];
this.intercepts = intercepts as ServiceElement<
K,
T,
K,
I,
O,
C
Expand All @@ -97,7 +97,7 @@ export class Service<
this: ServiceImplementation,
intercepts: InterceptImplementation | InterceptImplementation[],
options?: ServiceInterceptOptions
): Service<K, T, I, O, C> {
): Service<T, K, I, O, C> {
if (!isServiceImplementation(this)) {
throw Error(`Intercepts can only be applied to service implementations`);
}
Expand All @@ -112,7 +112,7 @@ export class Service<
: (this.intercepts || []).concat(arr)
});
}
public element(): ServiceElement<K, T, I, O, C> {
public element(): ServiceElement<T, K, I, O, C> {
return (this.resolve
? {
kind: this.kind,
Expand All @@ -127,6 +127,6 @@ export class Service<
exceptions: this.exceptions,
request: this.request,
response: this.response
}) as ServiceElement<K, T, I, O, C>;
}) as ServiceElement<T, K, I, O, C>;
}
}
42 changes: 23 additions & 19 deletions packages/core/src/classes/Service/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { Observable } from 'rxjs';
import { Service } from './Service';

type Input = Omit<Partial<ServiceUnion>, 'resolve'>;
type Maybe<T, U> = T extends Function ? U : void;
type Falsy = false | null | undefined | void | 0 | '';
type Maybe<T, U> = T extends Falsy ? void : U;
type Resolve<K extends ServiceKind, I, O, C> = K extends 'query' | 'mutation'
? UnaryServiceResolveImplementation<I, O, C>
: K extends 'subscription'
Expand All @@ -25,17 +26,17 @@ type Resolve<K extends ServiceKind, I, O, C> = K extends 'query' | 'mutation'

/* Main */
export type ServiceConstructor = <
T = false,
K extends ServiceKind = ServiceKind,
T = void,
I = any,
O = any,
C = any
>(
service: ServiceInput<K, T, I, O, C>
) => Service<K, T, I, O, C>;
service: ServiceInput<T, K, I, O, C>
) => Service<T, K, I, O, C>;

export type ServiceInput<K extends ServiceKind, T, I, O, C> =
| Service<K, T, I, O, C>
export type ServiceInput<T, K extends ServiceKind, I, O, C> =
| Service<T, K, I, O, C>
| ServiceDeclaration
| ServiceImplementation<I, O, C>
| (Record<'kind', K> &
Expand All @@ -45,26 +46,29 @@ export type ServiceInput<K extends ServiceKind, T, I, O, C> =
| ServiceQueryInput<T, I, O, C>
));

export type ServiceElement<K extends ServiceKind, T, I, O, C> = ServiceUnion &
export type ServiceElement<T, K extends ServiceKind, I, O, C> = ServiceUnion &
Record<'kind', K> &
Record<'resolve', Maybe<T, Resolve<K, I, O, C>>> &
Record<'intercepts', Maybe<T, InterceptImplementation[]>>;

/* Input */
export type ServiceQueryInput<T, I, O, C> = Input & {
kind?: QueryServiceKind;
resolve?: T | ServiceUnaryResolveInput<I, O, C>;
};
export type ServiceQueryInput<T, I, O, C> = Input &
Record<'resolve', Function> & {
kind?: QueryServiceKind;
resolve: T | ServiceUnaryResolveInput<I, O, C>;
};

export type ServiceMutationInput<T, I, O, C> = Input & {
kind?: MutationServiceKind;
resolve?: T | ServiceUnaryResolveInput<I, O, C>;
};
export type ServiceMutationInput<T, I, O, C> = Input &
Record<'resolve', Function> & {
kind?: MutationServiceKind;
resolve: T | ServiceUnaryResolveInput<I, O, C>;
};

export type ServiceSubscriptionInput<T, I, O, C> = Input & {
kind?: SubscriptionServiceKind;
resolve?: T | ServiceStreamResolveInput<I, O, C>;
};
export type ServiceSubscriptionInput<T, I, O, C> = Input &
Record<'resolve', Function> & {
kind?: SubscriptionServiceKind;
resolve: T | ServiceStreamResolveInput<I, O, C>;
};

export type ServiceUnaryResolveInput<I = any, O = any, C = any> = (
data: I,
Expand Down

0 comments on commit abc1dea

Please sign in to comment.