fix: 加固 monorepo 质量门禁
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ErrorCode, ok } from "./index";
|
||||
import { describe, expect, expectTypeOf, it } from "vitest";
|
||||
import { ErrorCode, ok, type SuccessResponse } from "./index";
|
||||
|
||||
describe("contracts", () => {
|
||||
it("creates the uniform response envelope", () => {
|
||||
@@ -10,4 +10,27 @@ describe("contracts", () => {
|
||||
requestId: "req-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves literal success response types", () => {
|
||||
const response = ok({ id: "1" }, "req-1");
|
||||
|
||||
expectTypeOf(response).toEqualTypeOf<SuccessResponse<{ id: string }>>();
|
||||
expectTypeOf(response.code).toEqualTypeOf<ErrorCode.OK>();
|
||||
expectTypeOf(response.message).toEqualTypeOf<"success">();
|
||||
});
|
||||
|
||||
it("defines the complete error-code contract", () => {
|
||||
expect(Object.values(ErrorCode)).toEqual([
|
||||
"OK",
|
||||
"AUTH_TOKEN_EXPIRED",
|
||||
"AUTH_REFRESH_REUSED",
|
||||
"BOTTLE_DAILY_LIMIT",
|
||||
"BOTTLE_POOL_EMPTY",
|
||||
"BOTTLE_LEASE_EXPIRED",
|
||||
"CONTENT_REJECTED",
|
||||
"USER_BLOCKED",
|
||||
"CONVERSATION_FORBIDDEN",
|
||||
"MESSAGE_DUPLICATE",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,7 +11,14 @@ export enum ErrorCode {
|
||||
MESSAGE_DUPLICATE = "MESSAGE_DUPLICATE",
|
||||
}
|
||||
|
||||
export const ok = <T>(data: T, requestId: string) => ({
|
||||
export interface SuccessResponse<T> {
|
||||
code: ErrorCode.OK;
|
||||
message: "success";
|
||||
data: T;
|
||||
requestId: string;
|
||||
}
|
||||
|
||||
export const ok = <T>(data: T, requestId: string): SuccessResponse<T> => ({
|
||||
code: ErrorCode.OK,
|
||||
message: "success",
|
||||
data,
|
||||
|
||||
Reference in New Issue
Block a user