-
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: input package and new route system
- Loading branch information
Showing
58 changed files
with
1,026 additions
and
59 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {FieldSelectType, validate} from "@typesafeunit/input"; | ||
import {ActionContext, ActionState} from "@typesafeunit/unit"; | ||
import {RouteAction} from "../interfaces"; | ||
import {IRouteContext} from "../Route"; | ||
import {Resolver} from "./Resolver"; | ||
|
||
export class Payload<A extends RouteAction> { | ||
public readonly resolver: Resolver<A>; | ||
public readonly type: FieldSelectType<ActionState<A>>; | ||
|
||
constructor(type: FieldSelectType<ActionState<A>>, resolver: Resolver<A>) { | ||
this.type = type; | ||
this.resolver = resolver; | ||
} | ||
|
||
public async validate(context: IRouteContext<ActionContext<A>>): Promise<ActionState<A>> { | ||
return validate<ActionState<A>>(this.type, await this.resolver.resolve(context)); | ||
} | ||
} |
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,37 @@ | ||
import {ActionContext, ActionState} from "@typesafeunit/unit"; | ||
import {isFunction, isObject} from "@typesafeunit/util"; | ||
import {RouteAction} from "../interfaces"; | ||
import {IRouteContext} from "../Route"; | ||
|
||
type ResolverFn<A extends RouteAction> = (context: IRouteContext<ActionContext<A>>) => ActionState<A> | unknown; | ||
type ResolverType<A extends RouteAction> = ActionState<A> | ResolverFn<A>; | ||
|
||
type ResolverList<A extends RouteAction> = { | ||
[K in keyof ActionState<A>]-?: ResolverType<A>; | ||
}; | ||
|
||
export class Resolver<A extends RouteAction> { | ||
readonly resolvers: ResolverList<A>; | ||
|
||
constructor(resolvers: ResolverList<A>) { | ||
this.resolvers = resolvers; | ||
} | ||
|
||
public async resolve(context: IRouteContext<ActionContext<A>>): Promise<ActionState<A>> { | ||
const state = {}; | ||
const {resolvers} = this; | ||
if (isFunction(resolvers)) { | ||
Object.assign(state, await resolvers(context)); | ||
} else if (isObject(resolvers)) { | ||
for (const [name, resolver] of Object.entries(resolvers)) { | ||
if (isFunction(resolver)) { | ||
Reflect.set(state, name, await resolver(context)); | ||
} else { | ||
Reflect.set(state, name, resolver); | ||
} | ||
} | ||
} | ||
|
||
return state as ActionState<A>; | ||
} | ||
} |
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,2 @@ | ||
export * from "./Resolver"; | ||
export * from "./Payload"; |
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,35 @@ | ||
import {ActionCtor} from "@typesafeunit/unit"; | ||
import {ILogable} from "@typesafeunit/util"; | ||
import {RouteAction} from "../interfaces"; | ||
import {Payload} from "../Payload"; | ||
import {IRoute, IRouteMatcher, RouteMatcherFactory, RouteNew, RouteNewArgs} from "./interfaces"; | ||
|
||
export class Route<A extends RouteAction = RouteAction> implements IRoute<A>, ILogable<{ route: string }> { | ||
public readonly route: string; | ||
public readonly action: ActionCtor<A>; | ||
public readonly payload?: Payload<A>; | ||
readonly #matcher: IRouteMatcher; | ||
|
||
constructor(matcher: RouteMatcherFactory, ...[route, action, payload]: RouteNewArgs<A>) { | ||
this.route = route; | ||
this.action = action as ActionCtor<A>; | ||
this.payload = payload as Payload<A>; | ||
this.#matcher = matcher(route); | ||
} | ||
|
||
public static create(matcher: RouteMatcherFactory): RouteNew { | ||
return <A extends RouteAction>(...args: RouteNewArgs<A>) => new Route<A>(matcher, ...args); | ||
} | ||
|
||
public getLogValue(): { route: string } { | ||
return {route: this.route}; | ||
} | ||
|
||
public test(route: string): boolean { | ||
return this.#matcher.test(route); | ||
} | ||
|
||
public match(route: string): Record<string, string> { | ||
return this.#matcher.match(route); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import {IRequest} from "../../interfaces"; | ||
import {IRequest, IRequestTransform} from "../../interfaces"; | ||
|
||
export const JSONTransform = async <T>(request: IRequest): Promise<T> => { | ||
request.headers.assert("content-type", ["application/json"]); | ||
const buffer = await request.getBuffer(); | ||
return JSON.parse(buffer.toString("utf-8")); | ||
}; | ||
|
||
export const fromJsonRequest: IRequestTransform<unknown> = Object.assign( | ||
(buffer: Buffer) => JSON.parse(buffer.toString("utf-8")), | ||
{type: "application/json"}, | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./Route"; | ||
export * from "./Transport"; | ||
export * from "./Application"; | ||
export * from "./Payload"; | ||
export * from "./interfaces"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Payload} from "@typesafeunit/app"; | ||
import TestInputStateValidationRoute, { | ||
resolver, | ||
type, | ||
} from "@typesafeunit/test/src/actions/TestInputStateValidationRoute"; | ||
import {MainContext} from "../../../test/src/context/MainContext"; | ||
import {Request} from "../../../test/src/transport/Request"; | ||
import {Application} from "../../src"; | ||
|
||
describe("Route", () => { | ||
const bd = new Date("2020-11-08T12:18:06.051Z").toISOString(); | ||
const headers = {"Content-Type": "application/json", "Authorization": "s5sChdDmOPmFLtEEsq4L"}; | ||
const request = new Request("/test", headers, JSON.stringify({bd, name: "Peter"})); | ||
const failRequest = new Request("/test", headers, JSON.stringify({})); | ||
|
||
test("Payload", async () => { | ||
const payload = new Payload(type, resolver); | ||
const context = {request, args: new Map<string, string>(Object.entries(headers)), context: {}}; | ||
await expect(payload.validate(context)) | ||
.resolves | ||
.toMatchSnapshot(); | ||
}); | ||
|
||
test("Success", async () => { | ||
const app = await Application.factory(new MainContext(), [TestInputStateValidationRoute]); | ||
await app.handle(request); | ||
expect(request).toMatchSnapshot(); | ||
}); | ||
|
||
test("Fails", async () => { | ||
const app = await Application.factory(new MainContext(), [TestInputStateValidationRoute]); | ||
expect(app.handle(failRequest)).rejects.toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.