Files
radicle-explorer/httpd-client/lib/session.ts
T
Rūdolfs Ošiņš 260cd4c144 Use object instead of strictObject for httpd schema checking
This way we can deploy back-end API changes that are backwards
compatible without breaking the UI.

Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
2023-06-02 15:54:50 +02:00

77 lines
1.5 KiB
TypeScript

import type { Fetcher, RequestOptions } from "./fetcher.js";
import type { SuccessResponse } from "./shared.js";
import type { ZodSchema } from "zod";
import { number, object, string } from "zod";
import { successResponseSchema } from "./shared.js";
interface Session {
sessionId: string;
status: string;
publicKey: string;
issuedAt: number;
expiresAt: number;
}
const sessionSchema = object({
sessionId: string(),
status: string(),
publicKey: string(),
issuedAt: number(),
expiresAt: number(),
}) satisfies ZodSchema<Session>;
export class Client {
#fetcher: Fetcher;
public constructor(fetcher: Fetcher) {
this.#fetcher = fetcher;
}
public async getById(id: string, options?: RequestOptions): Promise<Session> {
return this.#fetcher.fetchOk(
{
method: "GET",
path: `sessions/${id}`,
options,
},
sessionSchema,
);
}
public async update(
id: string,
body: {
sig: string;
pk: string;
},
options?: RequestOptions,
): Promise<SuccessResponse> {
return this.#fetcher.fetchOk(
{
method: "PUT",
path: `sessions/${id}`,
body,
options,
},
successResponseSchema,
);
}
public async delete(
id: string,
options?: RequestOptions,
): Promise<SuccessResponse> {
return this.#fetcher.fetchOk(
{
method: "DELETE",
path: `sessions/${id}`,
headers: { Authorization: `Bearer ${id}` },
options,
},
successResponseSchema,
);
}
}