-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): rewrites Types as Exception, Schema, and Children
- Loading branch information
Showing
57 changed files
with
1,621 additions
and
1,343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ExceptionLabel } from '~/types'; | ||
|
||
export class PublicError extends Error { | ||
public id: string; | ||
public label: ExceptionLabel; | ||
public source?: Error; | ||
public constructor( | ||
id: string, | ||
label: ExceptionLabel, | ||
source?: Error | null, | ||
message?: string | null, | ||
clear?: boolean | ||
) { | ||
super(message || ''); | ||
this.id = id; | ||
this.label = label; | ||
this.source = source || undefined; | ||
if (clear) this.stack = `${this.name}: ${this.message}`; | ||
} | ||
public get name(): string { | ||
return 'PublicError'; | ||
} | ||
public get root(): PublicError { | ||
return this.source instanceof PublicError && this.source !== this | ||
? this.source.root | ||
: this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 25 additions & 37 deletions
62
packages/core/src/classes/Application/helpers/get-routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,42 @@ | ||
import { | ||
CollectionTreeImplementation, | ||
ServiceElementImplementation | ||
} from '~/types'; | ||
import { CollectionTreeImplementation, ServiceImplementation } from '~/types'; | ||
import { | ||
ApplicationCreateOptionsMapFn, | ||
ApplicationRoutes | ||
} from '../definitions'; | ||
import { Collection } from '../../Collection'; | ||
import { | ||
isElementService, | ||
isElementType, | ||
isTypeResponse, | ||
isElementTree | ||
isElementTree, | ||
isTreeCollection | ||
} from '~/inspect/is'; | ||
|
||
export function getRoutes( | ||
collection: Collection<CollectionTreeImplementation>, | ||
map: ApplicationCreateOptionsMapFn | ||
): ApplicationRoutes { | ||
const responses: ApplicationRoutes = {}; | ||
const routes: any = collection.replace((element, info, next): any => { | ||
if (isElementService(element)) { | ||
return map(element as ServiceElementImplementation, info); | ||
} | ||
if (isElementType(element)) { | ||
if (!isTypeResponse(element) || !element.children) return element; | ||
|
||
const entries = Object.entries(element.children); | ||
if (!entries.length) return element; | ||
|
||
if (info.route.length > 1) { | ||
throw Error(`Type with children not at root: ${info.path}`); | ||
const routes: any = collection.replace( | ||
(element, { path, route }, next): any => { | ||
if (isElementService(element)) { | ||
if (!route) { | ||
throw Error(`Expected route on path: ${path}`); | ||
} | ||
return map(element as ServiceImplementation, { path, route }); | ||
} | ||
const name = info.route[info.route.length - 1]; | ||
const children: ApplicationRoutes = {}; | ||
for (const [key, service] of entries) { | ||
children[key] = map(service as ServiceElementImplementation, { | ||
route: info.route.concat([key]), | ||
path: info.path.concat(['children', key]) | ||
}); | ||
} | ||
responses[name] = { ...children }; | ||
return element; | ||
} | ||
|
||
element = next(element); | ||
return isElementTree(element) | ||
? { ...element.scopes, ...element.services } | ||
: element; | ||
}); | ||
element = next(element); | ||
if (!isElementTree(element)) return element; | ||
return isTreeCollection(element) | ||
? { | ||
...element.scopes, | ||
...element.services, | ||
...element.children | ||
} | ||
: { | ||
...element.scopes, | ||
...element.services | ||
}; | ||
} | ||
); | ||
|
||
return { ...responses, ...routes }; | ||
return routes; | ||
} |
20 changes: 7 additions & 13 deletions
20
packages/core/src/classes/Application/helpers/intercept-response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 1 addition & 9 deletions
10
packages/core/src/classes/Application/helpers/remove-children.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,8 @@ | ||
import { CollectionTreeImplementation } from '~/types'; | ||
import { Collection } from '../../Collection'; | ||
import { isElementType, isTypeResponse } from '~/inspect/is'; | ||
|
||
export function removeChildren( | ||
collection: Collection<CollectionTreeImplementation> | ||
): Collection<CollectionTreeImplementation> { | ||
return collection.replace((element, info, next) => { | ||
element = next(element); | ||
if (isElementType(element) && isTypeResponse(element) && element.children) { | ||
const { children, ...other } = element; | ||
return other; | ||
} | ||
return element; | ||
}); | ||
return new Collection({ ...collection, children: {} }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ChildrenUnion, ChildrenServicesUnion } from '~/types'; | ||
import { Element } from './Element'; | ||
|
||
export type ChildrenCreateInput<A extends ChildrenServicesUnion> = Omit< | ||
ChildrenUnion<A>, | ||
'kind' | ||
>; | ||
|
||
export class Children<T extends ChildrenUnion = ChildrenUnion> extends Element< | ||
T | ||
> { | ||
public static create<A extends ChildrenServicesUnion>( | ||
children: ChildrenCreateInput<A> | ||
): Children<ChildrenUnion<A>> { | ||
return new Children({ | ||
kind: 'children', | ||
...children | ||
}); | ||
} | ||
public readonly schemas: T['schemas']; | ||
public readonly services: T['services']; | ||
public constructor(exception: T) { | ||
super(exception.kind); | ||
this.schemas = exception.schemas; | ||
this.services = exception.services; | ||
} | ||
public element(): T { | ||
return { | ||
kind: this.kind, | ||
schemas: this.schemas, | ||
services: this.services | ||
} as T; | ||
} | ||
} |
Oops, something went wrong.