Skip to content

Commit

Permalink
feat(core): adds ensure static method to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Nov 24, 2019
1 parent c8ba0a7 commit 8b54c5d
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/classes/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Application {

const services: ApplicationServices = {};

new Collection(this.declaration).traverse((element, info, next) => {
Collection.ensure(this.declaration).traverse((element, info, next) => {
next();
if (isElementService(element)) {
if (!info.route) throw Error(`Expected route on path: ${info.path}`);
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/classes/Children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export type ChildrenInput<A extends ChildrenServicesUnion> = Optional<
export class Children<
A extends ChildrenServicesUnion = ChildrenServicesUnion
> extends Element<ChildrenUnion> {
public static ensure<A extends ChildrenServicesUnion = ChildrenServicesUnion>(
children: ChildrenUnion<A>
): Children<A> {
return children instanceof Children ? children : new Children(children);
}
public readonly schemas: ChildrenSchemasUnion;
public readonly services: A;
public constructor(children: ChildrenInput<A>) {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/classes/Collection/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export class Collection<
D extends ServicesRecordUnion = {},
E extends ScopesRecordUnion = {}
> extends Element<CollectionTreeUnion> {
public static ensure<T extends CollectionTreeUnion>(
collection: T
): CollectionInstance<T> {
return collection instanceof Collection
? collection
: new Collection(collection);
}
public static exceptions<T extends ExceptionsRecordUnion>(
exceptions: T
): Collection<T, {}, {}, {}, {}> {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/classes/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export type ExceptionInput<L extends ExceptionLabel> = Optional<
export class Exception<
L extends ExceptionLabel = ExceptionLabel
> extends Element<ExceptionUnion> {
public static ensure<L extends ExceptionLabel = ExceptionLabel>(
exception: ExceptionUnion<L>
): Exception<L> {
return exception instanceof Exception
? exception
: new Exception(exception);
}
public readonly label: L;
public readonly description: string | undefined;
public constructor(exception: ExceptionInput<L>) {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/classes/Intercept/Intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { mergeServiceExceptions } from '~/transform/merge';
export class Intercept<I = any, O = any, C = any> extends Element<
InterceptImplementation
> {
public static ensure<I = any, O = any, C = any>(
intercept: InterceptImplementation<I, O, C>
): Intercept<I, O, C> {
return intercept instanceof Intercept
? intercept
: new Intercept(intercept);
}
/**
* Exposes a simpler api to create intercepts to be run *before* the `resolve` function of a `ServiceImplementation` has been called to act on the incoming `data`.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/classes/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export type SchemaConstructor = (
export type SchemaInput = Optional<SchemaUnion, 'kind'>;

export class Schema extends Element<SchemaUnion> {
static ensure(schema: SchemaUnion): Schema {
return schema instanceof Schema ? schema : new Schema(schema);
}
static json(
input?: SchemaInput | boolean | null,
schema?: JSONSchema
Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/classes/Service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ export class Service<
O = any,
C = any
> extends Element<ServiceUnion> {
static query<T = void, I = any, O = any, C = any>(
public static ensure<
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> {
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> {
return new Service({ kind: 'query', ...query });
}
static mutation<T = void, I = any, O = any, C = any>(
public static mutation<T = void, I = any, O = any, C = any>(
mutation?: ServiceMutationInput<T, I, O, C>
): Service<'mutation', T, I, O, C> {
return new Service({ kind: 'mutation', ...mutation });
}
static subscription<T = void, I = any, O = any, C = any>(
public static subscription<T = void, I = any, O = any, C = any>(
subscription?: ServiceSubscriptionInput<T, I, O, C>
): Service<'subscription', T, I, O, C> {
return new Service({ kind: 'subscription', ...subscription });
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generate/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function client(
};

let content = '';
const instance = new Collection(await collection);
const instance = Collection.ensure(await collection);

if (opts.typescript) {
content += await typings(collection, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generate/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function typings(
'/* This file was automatically generated. DO NOT MODIFY IT BY HAND. */\n\n';
}

const { schemas } = new Collection(await collection).lift();
const { schemas } = Collection.ensure(await collection).lift();
for (const [key, value] of Object.entries(schemas)) {
content += await compile(value.schema, key, { bannerComment: '' });
content += '\n';
Expand Down
5 changes: 3 additions & 2 deletions packages/rest/src/server/RESTServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class RESTServer {
) {
this.options = Object.assign(createDefaults(), options);

const instance = new Collection(collection);
const instance = Collection.ensure(collection);
const app = new Application(
this.options.subscriptions
? instance.toUnary()
Expand All @@ -37,6 +37,7 @@ export class RESTServer {
options && options.fallback ? { fallback: options.fallback } : {}
);

this.declaration = app.declaration;
this.router = new ServerRouter(
app.flatten('/'),
app.fallback,
Expand Down Expand Up @@ -65,7 +66,7 @@ export class RESTServer {
const item = mapError(err);
const error = item
? item.error
: new Collection(this.declaration).error('ServerError', err, true);
: Collection.ensure(this.declaration).error('ServerError', err, true);

return {
status: item ? item.status : 500,
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/reproduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function reproduce(
);

const implementation = Collection.merge(
new Collection(await collection).toImplementation((service, info) => {
Collection.ensure(await collection).toImplementation((service, info) => {
if (isServiceQuery(service)) {
return Service.query({
...service,
Expand Down

0 comments on commit 8b54c5d

Please sign in to comment.