-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor transformer to use static init block
- Loading branch information
Showing
22 changed files
with
407 additions
and
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import router, { | ||
Controller, | ||
GetChain, | ||
HttpEvent, | ||
HttpRequest, | ||
HttpRequestEmpty, | ||
HttpResponse, | ||
HttpStatusCode, | ||
PostChain | ||
} from "../../dist/runtime/index.mjs"; | ||
import {nornir, Nornir} from "@nornir/core"; | ||
|
||
interface RouteGetInput extends HttpRequestEmpty { | ||
headers: { | ||
// eslint-disable-next-line sonarjs/no-duplicate-string | ||
"content-type": "text/plain"; | ||
}; | ||
} | ||
|
||
interface RoutePostInputJSON extends HttpRequest { | ||
headers: { | ||
"content-type": "application/json"; | ||
}; | ||
body: RoutePostBodyInput; | ||
query: { | ||
test: "boolean"; | ||
}; | ||
} | ||
|
||
interface RoutePostInputCSV extends HttpRequest { | ||
headers: { | ||
"content-type": "text/csv"; | ||
/** | ||
* This is a CSV header | ||
* @example "cool,cool2" | ||
* @pattern ^[a-z]+,[a-z]+$ | ||
* @minLength 5 | ||
*/ | ||
"csv-header": string; | ||
}; | ||
body: TestStringType; | ||
} | ||
|
||
type RoutePostInput = RoutePostInputJSON | RoutePostInputCSV; | ||
|
||
/** | ||
* this is a comment | ||
*/ | ||
interface RoutePostBodyInput { | ||
/** | ||
* This is a cool property | ||
* @minLength 5 | ||
*/ | ||
cool: string; | ||
} | ||
|
||
/** | ||
* Amazing string | ||
* @pattern ^[a-z]+$ | ||
* @minLength 5 | ||
*/ | ||
type TestStringType = Nominal<string, "TestStringType">; | ||
|
||
|
||
declare class Tagged<N extends string> { | ||
protected _nominal_: N; | ||
} | ||
|
||
type Nominal<T, N extends string, E extends T & Tagged<string> = T & Tagged<N>> = (T & Tagged<N>) | E; | ||
|
||
const basePath = "/basepath"; | ||
|
||
|
||
@Controller(basePath) | ||
class TestController { | ||
/** | ||
* A simple get route | ||
* @summary Cool Route | ||
*/ | ||
@GetChain("/route") | ||
public getRoute(chain: Nornir<RouteGetInput>) { | ||
return chain | ||
.use(input => input.headers["content-type"]) | ||
.use(contentType => ({ | ||
statusCode: HttpStatusCode.Ok, | ||
body: `Content-Type: ${contentType}`, | ||
headers: { | ||
// eslint-disable-next-line sonarjs/no-duplicate-string | ||
"content-type": "text/plain" as const, | ||
}, | ||
})); | ||
} | ||
|
||
@PostChain("/route") | ||
public postRoute(chain: Nornir<RoutePostInput>) { | ||
return chain | ||
.use(contentType => ({ | ||
statusCode: HttpStatusCode.Ok, | ||
body: `Content-Type: ${contentType}`, | ||
headers: { | ||
"content-type": "text/plain" as const, | ||
}, | ||
})); | ||
} | ||
} | ||
|
||
|
||
const handler: (event: HttpEvent) => Promise<HttpResponse> = nornir<HttpEvent>() | ||
.use(router()) | ||
.build(); | ||
|
||
describe("REST tests", () => { | ||
describe("Valid requests", () => { | ||
it("Should process a basic GET request", async () => { | ||
const response = await handler({ | ||
method: "GET", | ||
path: "/basepath/route", | ||
headers: { | ||
"content-type": "text/plain", | ||
}, | ||
query: {} | ||
}); | ||
expect(response.statusCode).toEqual(HttpStatusCode.Ok); | ||
expect(response.body).toBe("Content-Type: text/plain"); | ||
expect(response.headers["content-type"]).toBe("text/plain"); | ||
}) | ||
}) | ||
}); | ||
|
This file was deleted.
Oops, something went wrong.
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
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
32 changes: 5 additions & 27 deletions
32
packages/rest/src/transform/transformers/class-transformer.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,38 +1,16 @@ | ||
import ts from "typescript"; | ||
import { ControllerMeta } from "../controller-meta"; | ||
import { separateNornirDecorators } from "../lib"; | ||
import { Project } from "../project"; | ||
import { ControllerProcessor } from "./decorator-transofmers/controller-processor"; | ||
import { ControllerProcessor } from "./processors/controller-processor"; | ||
|
||
export abstract class ClassTransformer { | ||
public static transform(project: Project, node: ts.ClassDeclaration): ts.ClassDeclaration { | ||
public static transform(project: Project, node: ts.ClassDeclaration, context: ts.TransformationContext): ts.Node { | ||
const originalDecorators = ts.getDecorators(node) || []; | ||
if (!originalDecorators) return node; | ||
const modifiers = ts.getModifiers(node) || []; | ||
|
||
const { otherDecorators, nornirDecorators } = separateNornirDecorators(project, originalDecorators); | ||
const { nornirDecorators } = separateNornirDecorators(project, originalDecorators); | ||
if (nornirDecorators.length === 0) return node; | ||
|
||
ControllerMeta.create(project, node); | ||
|
||
for (const { decorator, declaration } of nornirDecorators) { | ||
const { name } = project.checker.getTypeAtLocation(declaration.parent).symbol; | ||
const processor = CLASS_DECORATOR_PROCESSORS[name]; | ||
if (!processor) throw new Error(`No processor for decorator ${name}`); | ||
processor(project, node, decorator); | ||
} | ||
|
||
return ts.factory.createClassDeclaration( | ||
[...modifiers, ...otherDecorators], | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
node.members, | ||
); | ||
return ControllerProcessor.process(project, node, nornirDecorators, context); | ||
} | ||
} | ||
|
||
type Task = (project: Project, node: ts.ClassDeclaration, decorator: ts.Decorator) => void; | ||
|
||
const CLASS_DECORATOR_PROCESSORS: Record<string, Task> = { | ||
Controller: ControllerProcessor.process, | ||
}; |
Oops, something went wrong.