Skip to content

Commit

Permalink
feat(core): parametizes elements for services types
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Sep 15, 2019
1 parent 4b0b764 commit 38b1d89
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 128 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/create/collection/empty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CollectionTree } from '~/types';
import { AppCollectionTree } from '~/types';

export function emptyCollection(): CollectionTree {
export function emptyCollection(): AppCollectionTree {
return {
types: { error: {}, request: {}, response: {} },
services: { query: {}, mutation: {}, subscription: {} },
Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/create/collection/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { CollectionTree, Service, Tree, ServiceItem, InputHook } from '~/types';
import {
ServiceItem,
InputHook,
AppCollectionTree,
AppTree,
AppService
} from '~/types';
import { query, mutation, subscription } from '../service';
import { services as createServices } from '../services';
import { mergeCollectionArray } from './merge';
import { emptyCollection } from './empty';

export function setCollectionHooks(
collection: CollectionTree,
collection: AppCollectionTree,
hooks: InputHook | InputHook[]
): CollectionTree {
): AppCollectionTree {
const empty = emptyCollection();

return mergeCollectionArray([
Expand All @@ -25,11 +31,11 @@ export function setCollectionHooks(
]);
}

export function setServicesHooks<T extends Service>(
kind: keyof Tree['services'],
export function setServicesHooks<T extends AppService>(
kind: keyof AppTree['services'],
services: { [key: string]: T },
hooks: InputHook | InputHook[]
): CollectionTree {
): AppCollectionTree {
const entries = Object.entries(services);
return createServices(
entries.reduce(
Expand All @@ -40,8 +46,8 @@ export function setServicesHooks<T extends Service>(
);
}

export function setServiceHooks<T extends Service>(
kind: keyof Tree['services'],
export function setServiceHooks<T extends AppService>(
kind: keyof AppTree['services'],
service: T,
hooks: InputHook | InputHook[]
): ServiceItem<T> {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/create/collection/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CollectionTree, InputHook } from '~/types';
import { InputHook, AppCollectionTree } from '~/types';
import { mergeCollectionArray } from './merge';
import { emptyCollection } from './empty';
import { setCollectionHooks } from './hooks';

export function collection(
collections?: CollectionTree | CollectionTree[],
collections?: AppCollectionTree | AppCollectionTree[],
hooks?: InputHook | InputHook[]
): CollectionTree {
): AppCollectionTree {
const collection = Array.isArray(collections)
? mergeCollectionArray(collections)
: collections || emptyCollection();
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/create/collection/merge.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { CollectionTree, ScopeTree } from '~/types';
import { AppCollectionTree, AppScopeTree } from '~/types';
import { emptyCollection } from './empty';

export function mergeCollectionArray(
collections: CollectionTree[]
): CollectionTree {
collections: AppCollectionTree[]
): AppCollectionTree {
return collections.reduce(
(acc, collection) => mergeCollections(acc, collection),
emptyCollection()
);
}

export function mergeCollections(
a: CollectionTree,
b: CollectionTree
): CollectionTree {
a: AppCollectionTree,
b: AppCollectionTree
): AppCollectionTree {
const { types: aTypes, ...aScope } = a;
const { types: bTypes, ...bScope } = b;
return {
Expand All @@ -26,7 +26,7 @@ export function mergeCollections(
};
}

export function mergeScopes(a: ScopeTree, b: ScopeTree): ScopeTree {
export function mergeScopes(a: AppScopeTree, b: AppScopeTree): AppScopeTree {
const scopeKeys = {
a: Object.keys(a.scopes),
b: Object.keys(b.scopes)
Expand All @@ -45,7 +45,7 @@ export function mergeScopes(a: ScopeTree, b: ScopeTree): ScopeTree {
...a.scopes,
...b.scopes,
...nonUniqueScopeKeys.reduce(
(acc: { [key: string]: ScopeTree }, x) =>
(acc: { [key: string]: AppScopeTree }, x) =>
Object.assign(acc, mergeScopes(a.scopes[x], b.scopes[x])),
{}
)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/create/scope.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CollectionTree, InputHook } from '~/types';
import { InputHook, AppCollectionTree } from '~/types';
import { collection as createCollection } from './collection';

export function scope(
name: string,
collection: CollectionTree,
collection: AppCollectionTree,
hooks?: InputHook | InputHook[]
): CollectionTree {
): AppCollectionTree {
const { types, ...other } = collection;
const tree = createCollection();
tree.types = types;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/create/service/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ServiceItem, InputHook, Service } from '~/types';
import { ServiceItem, InputHook, AppService } from '~/types';
import { Observable, from } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

export function setServiceHooks<T extends Service>(
export function setServiceHooks<T extends AppService>(
service: ServiceItem<T>,
hooks: InputHook | InputHook[]
): ServiceItem<T> {
const arr = Array.isArray(hooks) ? hooks : [hooks];
return arr.reduce((acc, hook) => setServiceHooks(acc, hook), service);
}

export function setServiceHook<T extends Service>(
export function setServiceHook<T extends AppService>(
service: ServiceItem<T>,
hook: InputHook
): ServiceItem<T> {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/create/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
QueryService,
MutationService,
SubscriptionService,
ServiceItem,
InputService,
InputHook
InputHook,
QueryService,
MutationService,
SubscriptionService
} from '~/types';
import { inputServiceToItem } from './input-to-item';
import { setServiceHooks } from './hooks';
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/create/service/input-to-item.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
Service,
ServiceItem,
CollectionTree,
InputService,
ServiceItemTypes,
ErrorCode
ErrorCode,
AppService
} from '~/types';

export function inputServiceToItem<T extends Service>(
export function inputServiceToItem<T extends AppService>(
kind: keyof Required<CollectionTree>['services'],
service: InputService<T>
): ServiceItem<T> {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/create/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { CollectionTree, InputHook, InputServices } from '~/types';
import { InputHook, InputServices, AppCollectionTree } from '~/types';
import { itemToCollection } from './item-to-collection';
import { collection } from '../collection';
import { scope as createScope } from '../scope';

export function services(
services: InputServices,
hooks?: InputHook | InputHook[]
): CollectionTree;
): AppCollectionTree;
export function services(
name: string,
services: InputServices,
hooks?: InputHook | InputHook[]
): CollectionTree;
): AppCollectionTree;
export function services(
name: string,
scope: boolean,
services: InputServices,
hooks?: InputHook | InputHook[]
): CollectionTree;
export function services(...args: any[]): CollectionTree {
): AppCollectionTree;
export function services(...args: any[]): AppCollectionTree {
const hasName = typeof args[0] === 'string';
const hasScope = typeof args[1] === 'boolean';
const name: string = hasName ? args[0] : '';
Expand All @@ -34,7 +34,7 @@ export function services(...args: any[]): CollectionTree {
? args[2]
: args[1];

const collections: CollectionTree[] = [];
const collections: AppCollectionTree[] = [];
const entries = Object.entries(services);
for (const [key, item] of entries) {
collections.push(itemToCollection(name, key, item));
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/create/services/item-to-collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServiceItem, CollectionTree } from '~/types';
import { ServiceItem, AppCollectionTree } from '~/types';
import { collection } from '../collection';
import camelcase from 'camelcase';
import { request, response, error } from '../type';
Expand All @@ -7,8 +7,8 @@ export function itemToCollection(
prefix: string,
name: string,
item: ServiceItem
): CollectionTree {
const collections: CollectionTree[] = [];
): AppCollectionTree {
const collections: AppCollectionTree[] = [];
const service = {
...item.service,
types: { ...item.service.types }
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/create/type.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {
CollectionTree,
InputRequest,
InputResponse,
InputError
InputError,
AppCollectionTree
} from '~/types';
import { collection } from './collection';

export function error(name: string, type: InputError): CollectionTree {
export function error(name: string, type: InputError): AppCollectionTree {
const tree = collection();
tree.types.error[name] = type;
return tree;
}

export function request(name: string, type: InputRequest): CollectionTree {
export function request(name: string, type: InputRequest): AppCollectionTree {
const tree = collection();
tree.types.request[name] = type;
return tree;
}

export function response(name: string, type: InputResponse): CollectionTree {
export function response(name: string, type: InputResponse): AppCollectionTree {
const tree = collection();
tree.types.response[name] = {
...type,
Expand Down
67 changes: 0 additions & 67 deletions packages/core/src/types/collection.ts

This file was deleted.

59 changes: 59 additions & 0 deletions packages/core/src/types/collection/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
Service,
Element,
Tree,
Type,
CollectionTree,
ScopeTree,
TreeTypes,
TreeServices,
ErrorType,
RequestType,
ResponseType,
ResponseTypeChildren
} from './collection';
import { QueryService, MutationService, SubscriptionService } from './services';

// Groups
export type AppElement = Element<
QueryService,
MutationService,
SubscriptionService
>;
export type AppTree = Tree<QueryService, MutationService, SubscriptionService>;
export type AppType = Type<QueryService, SubscriptionService>;
export type AppService<I = any, O = any> = Service<
QueryService<I, O>,
MutationService<I, O>,
SubscriptionService<I, O>
>;

// Tree
export type AppCollectionTree = CollectionTree<
QueryService,
MutationService,
SubscriptionService
>;
export type AppScopeTree = ScopeTree<
QueryService,
MutationService,
SubscriptionService
>;
export type AppTreeTypes = TreeTypes<QueryService, SubscriptionService>;
export type AppTreeServices = TreeServices<
QueryService,
MutationService,
SubscriptionService
>;

// Types
export type AppErrorType = ErrorType;
export type AppRequestType = RequestType;
export type AppResponseType<I = any, O = any> = ResponseType<
QueryService<I, O>,
SubscriptionService<I, O>
>;
export type AppResponseTypeChildren<I = any, O = any> = ResponseTypeChildren<
QueryService<I, O>,
SubscriptionService<I, O>
>;

0 comments on commit 38b1d89

Please sign in to comment.