48877c05cc
I introduced some subtle linter issues when I added the APIError class and added the TypeScript mock generator. This commit addresses them, so the linter doesn't yell about the TypeScript generated sources. Change-Id: Icc7dfa4169a228b1a5144d4a292f4350ee5ef9f0
154 lines
4.5 KiB
TypeScript
154 lines
4.5 KiB
TypeScript
// AUTOGENERATED BY private/apigen
|
|
// DO NOT EDIT.
|
|
|
|
import { HttpClient } from '@/utils/httpClient';
|
|
import { Time, UUID } from '@/types/common';
|
|
|
|
export class DocsGetResponseItem {
|
|
id: UUID;
|
|
path: string;
|
|
date: Time;
|
|
metadata: Metadata;
|
|
last_retrievals?: DocsGetResponseItemLastRetrievals;
|
|
}
|
|
|
|
export class DocsGetResponseItemLastRetrievalsItem {
|
|
user: string;
|
|
when: Time;
|
|
}
|
|
|
|
export class DocsUpdateContentRequest {
|
|
content: string;
|
|
}
|
|
|
|
export class DocsUpdateContentResponse {
|
|
id: UUID;
|
|
date: Time;
|
|
pathParam: string;
|
|
body: string;
|
|
}
|
|
|
|
export class Document {
|
|
id: UUID;
|
|
date: Time;
|
|
pathParam: string;
|
|
body: string;
|
|
version: Version;
|
|
}
|
|
|
|
export class Metadata {
|
|
owner: string;
|
|
tags?: string[][];
|
|
}
|
|
|
|
export class UsersCreateRequestItem {
|
|
name: string;
|
|
surname: string;
|
|
email: string;
|
|
}
|
|
|
|
export class Version {
|
|
date: Time;
|
|
number: number;
|
|
}
|
|
|
|
export type DocsGetResponse = Array<DocsGetResponseItem>
|
|
|
|
export type DocsGetResponseItemLastRetrievals = Array<DocsGetResponseItemLastRetrievalsItem>
|
|
|
|
export type UsersCreateRequest = Array<UsersCreateRequestItem>
|
|
|
|
export type UsersGetResponse = Array<UsersCreateRequestItem>
|
|
|
|
class APIError extends Error {
|
|
constructor(
|
|
public readonly msg: string,
|
|
public readonly responseStatusCode?: number,
|
|
) {
|
|
super(msg);
|
|
}
|
|
}
|
|
|
|
export class DocumentsHttpApiV0 {
|
|
private readonly http: HttpClient = new HttpClient();
|
|
private readonly ROOT_PATH: string = '/api/v0/docs';
|
|
|
|
public async get(): Promise<DocsGetResponse> {
|
|
const fullPath = `${this.ROOT_PATH}/`;
|
|
const response = await this.http.get(fullPath);
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as DocsGetResponse);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
|
|
public async getOne(path: string): Promise<Document> {
|
|
const fullPath = `${this.ROOT_PATH}/${path}`;
|
|
const response = await this.http.get(fullPath);
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as Document);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
|
|
public async getTag(path: string, tagName: string): Promise<string[]> {
|
|
const fullPath = `${this.ROOT_PATH}/${path}/${tagName}`;
|
|
const response = await this.http.get(fullPath);
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as string[]);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
|
|
public async getVersions(path: string): Promise<Version[]> {
|
|
const fullPath = `${this.ROOT_PATH}/${path}`;
|
|
const response = await this.http.get(fullPath);
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as Version[]);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
|
|
public async updateContent(request: DocsUpdateContentRequest, path: string, id: UUID, date: Time): Promise<DocsUpdateContentResponse> {
|
|
const u = new URL(`${this.ROOT_PATH}/${path}`);
|
|
u.searchParams.set('id', id);
|
|
u.searchParams.set('date', date);
|
|
const fullPath = u.toString();
|
|
const response = await this.http.post(fullPath, JSON.stringify(request));
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as DocsUpdateContentResponse);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
}
|
|
|
|
export class UsersHttpApiV0 {
|
|
private readonly http: HttpClient = new HttpClient();
|
|
private readonly ROOT_PATH: string = '/api/v0/users';
|
|
|
|
public async get(): Promise<UsersGetResponse> {
|
|
const fullPath = `${this.ROOT_PATH}/`;
|
|
const response = await this.http.get(fullPath);
|
|
if (response.ok) {
|
|
return response.json().then((body) => body as UsersGetResponse);
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
|
|
public async create(request: UsersCreateRequest): Promise<void> {
|
|
const fullPath = `${this.ROOT_PATH}/`;
|
|
const response = await this.http.post(fullPath, JSON.stringify(request));
|
|
if (response.ok) {
|
|
return;
|
|
}
|
|
const err = await response.json();
|
|
throw new APIError(err.error, response.status);
|
|
}
|
|
}
|