Skip to content

Commit

Permalink
feat(core): allows intercepts and scopes functions to take a Collecti…
Browse files Browse the repository at this point in the history
…onTree
  • Loading branch information
rafamel committed Oct 13, 2019
1 parent b39c5c6 commit 7bf98da
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
14 changes: 9 additions & 5 deletions packages/core/src/create/intercepts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
InputInterceptHook,
InterceptImplementation,
InputIntercept,
CollectionTreeImplementation,
CreateInterceptsOptions
CreateInterceptsOptions,
CollectionTree
} from '~/types';
import { from, Observable } from 'rxjs';
import { switchMap, mergeMap } from 'rxjs/operators';
Expand All @@ -12,10 +12,11 @@ import {
traverse,
isElementService,
emptyIntercept,
mergeServiceErrors
mergeServiceErrors,
isServiceImplementation
} from '~/utils';

export function intercepts<T extends CollectionTreeImplementation>(
export function intercepts<T extends CollectionTree>(
collection: T,
intercepts: InterceptImplementation[],
options?: CreateInterceptsOptions
Expand All @@ -27,7 +28,10 @@ export function intercepts<T extends CollectionTreeImplementation>(
collection,
{ deep: true, children: true, inline: true },
(element) => {
if (!isElementService(element)) return;
if (!isElementService(element) || !isServiceImplementation(element)) {
return;
}

element.intercepts = opts.prepend
? intercepts.concat(element.intercepts || [])
: (element.intercepts || []).concat(intercepts);
Expand Down
27 changes: 8 additions & 19 deletions packages/core/src/create/scopes.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import { CollectionTreeImplementation, ScopeTreeImplementation } from '~/types';
import { ScopeCreate, CollectionTree, ExtractScopeCreate } from '~/types';
import { emptyCollection, emptyScope } from '~/utils';

export function scope<T extends CollectionTreeImplementation, N extends string>(
export function scope<T extends CollectionTree, N extends string>(
name: N,
collection: T
): CollectionTreeImplementation<
T['types'],
{},
{ [P in N]: ScopeTreeImplementation<T['services'], T['scopes']> }
> {
): ScopeCreate<T, N> {
const { types, ...other } = collection;

return {
...emptyCollection(),
types,
scopes: {
[name]: { ...emptyScope(), ...other }
} as any
};
}
} as any;
}

export function extract<
T extends CollectionTreeImplementation,
N extends keyof T['scopes']
>(
export function extract<T extends CollectionTree, N extends keyof T['scopes']>(
collection: T,
name: N & string
): CollectionTreeImplementation<
T['types'],
T['scopes'][N]['services'],
T['scopes'][N]['scopes']
> {
): ExtractScopeCreate<T, N> {
const { types } = collection;
const { services, scopes } = collection.scopes[name];
return {
...emptyCollection(),
types,
services,
scopes
};
} as any;
}
57 changes: 52 additions & 5 deletions packages/core/src/types/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
InterceptImplementation,
CollectionTreeImplementation,
CollectionTreeApplication,
CollectionTree
CollectionTree,
ScopeTree,
QueryService,
MutationService,
SubscriptionService,
ScopeTreeImplementation
} from './collection';
import { Observable } from './observable';
import { Schema } from './schema';
Expand All @@ -23,13 +28,12 @@ export interface CreateInterceptsOptions {
prepend?: boolean;
}

// Collection
// Input
export type InputCollection =
| Partial<CollectionTree>
| Partial<CollectionTreeImplementation>
| Partial<CollectionTreeApplication>;

// Services
export interface InputQueryService<I = any, O = any> {
types?: InputServiceTypes;
intercepts?: Array<InterceptImplementation<I, O>>;
Expand All @@ -52,12 +56,10 @@ export interface InputServiceTypes {
response?: string | Schema | ResponseTypeImplementation;
}

// Types
export type InputErrorType = Omit<ErrorTypeImplementation, 'kind'>;
export type InputRequestType = Omit<RequestTypeImplementation, 'kind'>;
export type InputResponseType = Omit<ResponseTypeImplementation, 'kind'>;

// Intercepts
export interface InputIntercept<I = any, O = any> {
errors?: ServiceErrorsImplementation;
factory: InputInterceptFactory<I, O>;
Expand Down Expand Up @@ -86,3 +88,48 @@ export type InputInterceptHookResolve<T = any> = (
data: T,
context: any
) => T | Promise<T>;

// Create
export type ScopeCreate<
T extends CollectionTree,
N extends string
> = T extends CollectionTreeImplementation
? CollectionTreeImplementation<
T['types'],
{},
{ [P in N]: ScopeTreeImplementation<T['services'], T['scopes']> }
>
: CollectionTree<
QueryService,
MutationService,
SubscriptionService,
T['types'],
{},
{
[P in N]: ScopeTree<
QueryService,
MutationService,
SubscriptionService,
T['services'],
T['scopes']
>;
}
>;

export type ExtractScopeCreate<
T extends CollectionTree,
N extends keyof T['scopes']
> = T extends CollectionTreeImplementation
? CollectionTreeImplementation<
T['types'],
T['scopes'][N]['services'],
T['scopes'][N]['scopes']
>
: CollectionTree<
QueryService,
MutationService,
SubscriptionService,
T['types'],
T['scopes'][N]['services'],
T['scopes'][N]['scopes']
>;

0 comments on commit 7bf98da

Please sign in to comment.