Initial commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { t } from "@repo/core-shared/trpc/init";
|
||||
import { defineErrorMiddleware } from "@repo/core-shared/trpc/define-error-middleware";
|
||||
|
||||
import { {{pascalCase entity}}NotFoundError } from "../../entities/errors/{{kebabCase entity}}";
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
|
||||
export const {{camelCase name}}Procedure = t.procedure.use(
|
||||
defineErrorMiddleware([
|
||||
[InputParseError, "BAD_REQUEST"],
|
||||
[{{pascalCase entity}}NotFoundError, "NOT_FOUND"],
|
||||
]),
|
||||
);
|
||||
@@ -0,0 +1,93 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { injectable } from "inversify";
|
||||
import { {{camelCase name}}Container } from "@/di/container";
|
||||
import { {{pascalCase name}}Module } from "@/di/module";
|
||||
import { {{constantCase name}}_SYMBOLS } from "@/di/symbols";
|
||||
import { get{{pascalCase entity}}UseCase } from "@/application/use-cases/get-{{kebabCase entity}}.use-case";
|
||||
import { get{{pascalCase entity}}Controller } from "@/interface-adapters/controllers/get-{{kebabCase entity}}.controller";
|
||||
import { {{camelCase name}}Router } from "@/integrations/api/router";
|
||||
|
||||
describe("{{camelCase name}}Router", () => {
|
||||
beforeEach(() => {
|
||||
{{camelCase name}}Container.unbindAll();
|
||||
{{camelCase name}}Container.load({{pascalCase name}}Module);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
{{camelCase name}}Container.unbindAll();
|
||||
});
|
||||
|
||||
it("exposes the get{{pascalCase entity}} procedure", () => {
|
||||
const names = Object.keys({{camelCase name}}Router._def.procedures);
|
||||
expect(names).toContain("get{{pascalCase entity}}");
|
||||
});
|
||||
|
||||
it("get{{pascalCase entity}} returns the seeded {{camelCase entity}}", async () => {
|
||||
const caller = {{camelCase name}}Router.createCaller({});
|
||||
const result = await caller.get{{pascalCase entity}}({ id: "seed-1" });
|
||||
expect(result.id).toBe("seed-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("{{camelCase name}}Router (error mapping)", () => {
|
||||
beforeEach(() => {
|
||||
{{camelCase name}}Container.unbindAll();
|
||||
{{camelCase name}}Container.load({{pascalCase name}}Module);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
{{camelCase name}}Container.unbindAll();
|
||||
});
|
||||
|
||||
it("translates InputParseError → BAD_REQUEST when extra fields are passed", async () => {
|
||||
const caller = {{camelCase name}}Router.createCaller({});
|
||||
try {
|
||||
await caller.get{{pascalCase entity}}({
|
||||
id: "seed-1",
|
||||
unexpected: "field",
|
||||
} as unknown as { id: string });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("BAD_REQUEST");
|
||||
}
|
||||
});
|
||||
|
||||
it("translates {{pascalCase entity}}NotFoundError → NOT_FOUND when repository returns null", async () => {
|
||||
@injectable()
|
||||
class Null{{pascalCase entity}}Repository {
|
||||
async get{{pascalCase entity}}() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
{{camelCase name}}Container.unbindAll();
|
||||
{{camelCase name}}Container
|
||||
.bind({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)
|
||||
.to(Null{{pascalCase entity}}Repository);
|
||||
{{camelCase name}}Container
|
||||
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase)
|
||||
.toDynamicValue((ctx) =>
|
||||
get{{pascalCase entity}}UseCase(
|
||||
ctx.container.get({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository),
|
||||
),
|
||||
);
|
||||
{{camelCase name}}Container
|
||||
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller)
|
||||
.toDynamicValue((ctx) =>
|
||||
get{{pascalCase entity}}Controller(
|
||||
ctx.container.get({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase),
|
||||
),
|
||||
);
|
||||
|
||||
const caller = {{camelCase name}}Router.createCaller({});
|
||||
try {
|
||||
await caller.get{{pascalCase entity}}({ id: "anything" });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("NOT_FOUND");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { router } from "@repo/core-shared/trpc/init";
|
||||
|
||||
import { {{camelCase name}}Container } from "../../di/container";
|
||||
import { {{constantCase name}}_SYMBOLS } from "../../di/symbols";
|
||||
|
||||
import { get{{pascalCase entity}}InputSchema } from "../../application/use-cases/get-{{kebabCase entity}}.use-case";
|
||||
import type { IGet{{pascalCase entity}}Controller } from "../../interface-adapters/controllers/get-{{kebabCase entity}}.controller";
|
||||
|
||||
import { {{camelCase name}}Procedure } from "./procedures";
|
||||
|
||||
export const {{camelCase name}}Router = router({
|
||||
get{{pascalCase entity}}: {{camelCase name}}Procedure
|
||||
.input(get{{pascalCase entity}}InputSchema)
|
||||
.query(({ input }) => {
|
||||
const ctrl = {{camelCase name}}Container.get<IGet{{pascalCase entity}}Controller>(
|
||||
{{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller,
|
||||
);
|
||||
return ctrl(input);
|
||||
}),
|
||||
});
|
||||
|
||||
export type {{pascalCase name}}Router = typeof {{camelCase name}}Router;
|
||||
@@ -0,0 +1,7 @@
|
||||
// Payload CMS integration barrel for @repo/{{kebabCase name}}.
|
||||
// Re-export this feature's collections and globals here as you add them
|
||||
// under ./collections and ./globals. See packages/navigation/src/integrations/cms
|
||||
// for the canonical shape. The `<gen:job-tasks>` anchor below is required by
|
||||
// `pnpm turbo gen job` and `pnpm turbo gen event` — do not remove it.
|
||||
export {};
|
||||
// <gen:job-tasks>
|
||||
Reference in New Issue
Block a user