decentralised message store

feat: http response helpers

serenity c5c0b2aa a1f1a2e8

Changed files
+88
src
lib
types
utils
+19
src/lib/types/http/errors.ts
···
+
import { z } from "zod";
+
+
export const HttpGeneralErrorType = {
+
TYPE_ERROR: "Type error",
+
PARAMS_ERROR: "Missing required params",
+
SERVER_ERROR: "Something went wrong on the server",
+
};
+
export const httpGeneralErrorTypeSchema = z.enum(HttpGeneralErrorType);
+
export type HttpGeneralErrorType = z.infer<typeof httpGeneralErrorTypeSchema>;
+
+
export const httpErrorTypeSchema = z.union([httpGeneralErrorTypeSchema]);
+
export type HttpErrorType = z.infer<typeof httpErrorTypeSchema>;
+
+
export const httpResponseErrorInfoSchema = z.object({
+
message: z.string(),
+
type: z.optional(httpErrorTypeSchema),
+
details: z.optional(z.unknown()),
+
});
+
export type HttpResponseErrorInfo = z.infer<typeof httpResponseErrorInfoSchema>;
+39
src/lib/types/http/responses.ts
···
+
import { httpResponseErrorInfoSchema } from "@/lib/types/http/errors";
+
import { z } from "zod";
+
+
export const HttpResponseStatusType = {
+
SUCCESS: "success",
+
ERROR: "error",
+
} as const;
+
export const httpResponseStatusTypeSchema = z.enum(HttpResponseStatusType);
+
export type HttpResponseStatusType = z.infer<
+
typeof httpResponseStatusTypeSchema
+
>;
+
+
export const handshakeResponseSchema = z.object({
+
shardToken: z.string(),
+
});
+
export type HandshakeResponse = z.infer<typeof handshakeResponseSchema>;
+
+
export const httpResponseDataSchema = z.union([handshakeResponseSchema]);
+
export type HttpResponseData = z.infer<typeof httpResponseDataSchema>;
+
+
const httpResponseBaseSchema = z.object({
+
status: httpResponseStatusTypeSchema,
+
data: z.optional(httpResponseDataSchema),
+
error: z.optional(httpResponseErrorInfoSchema),
+
});
+
+
export const httpSuccessResponseSchema = httpResponseBaseSchema.safeExtend({
+
status: z.literal(HttpResponseStatusType.SUCCESS),
+
data: httpResponseDataSchema,
+
error: z.undefined(),
+
});
+
export type HttpSuccessResponse = z.infer<typeof httpSuccessResponseSchema>;
+
+
export const httpErrorResponseSchema = httpResponseBaseSchema.safeExtend({
+
status: z.literal(HttpResponseStatusType.ERROR),
+
error: httpResponseErrorInfoSchema,
+
data: z.undefined(),
+
});
+
export type HttpErrorResponse = z.infer<typeof httpErrorResponseSchema>;
+30
src/lib/utils/http/responses.ts
···
+
import type {
+
HttpErrorResponse,
+
HttpResponseData,
+
} from "@/lib/types/http/responses";
+
import { HttpResponseStatusType } from "@/lib/types/http/responses";
+
+
export const newSuccessResponse = (data: HttpResponseData) => {
+
const response = {
+
status: HttpResponseStatusType.SUCCESS,
+
data,
+
};
+
return new Response(JSON.stringify(response), {
+
status: 200,
+
headers: {
+
"Content-Type": "application/json",
+
},
+
});
+
};
+
+
export const newErrorResponse = (
+
httpCode: number,
+
errorObj: HttpErrorResponse,
+
) => {
+
return new Response(JSON.stringify(errorObj), {
+
status: httpCode,
+
headers: {
+
"Content-Type": "application/json",
+
},
+
});
+
};