Template
1
0
Files
2025-11-23 22:57:43 +01:00

90 lines
2.2 KiB
TypeScript

import z from "zod";
import type { ServerErrorType } from "./errors.ts";
import type { RouteMethod } from "./route.ts";
/*
|--------------------------------------------------------------------------------
| Types
|--------------------------------------------------------------------------------
*/
const ServerErrorResponseSchema = z.object({
error: z.object({
code: z.any(),
status: z.number(),
message: z.string(),
data: z.any().optional(),
}),
});
/*
|--------------------------------------------------------------------------------
| Utilities
|--------------------------------------------------------------------------------
*/
/**
* Check if the given candidate is a valid relay error response.
*
* @param candidate - Candidate to check.
*/
export function assertServerErrorResponse(candidate: unknown): candidate is ServerErrorResponse {
return ServerErrorResponseSchema.safeParse(candidate).success;
}
/*
|--------------------------------------------------------------------------------
| Types
|--------------------------------------------------------------------------------
*/
export type RelayAdapter = {
readonly url: string;
/**
* Return the full URL from given endpoint.
*
* @param endpoint - Endpoint to get url for.
*/
getUrl(endpoint: string): string;
/**
* Send a request to the configured relay url.
*
* @param input - Request input parameters.
* @param publicKey - Key to encrypt the payload with.
*/
send(input: RelayInput, publicKey?: string): Promise<RelayResponse>;
/**
* Sends a fetch request using the given options and returns a
* raw response.
*
* @param options - Relay request options.
*/
request(input: RequestInfo | URL, init?: RequestInit): Promise<RelayResponse>;
};
export type RelayInput = {
method: RouteMethod;
endpoint: string;
query?: string;
body?: Record<string, unknown>;
headers?: Headers;
};
export type RelayResponse<TData = unknown, TError = ServerErrorType | ServerErrorResponse["error"]> =
| {
result: "success";
headers: Headers;
data: TData;
}
| {
result: "error";
headers: Headers;
error: TError;
};
export type ServerErrorResponse = z.infer<typeof ServerErrorResponseSchema>;