move to nuxt

This commit is contained in:
Hayden 2022-09-01 14:32:03 -08:00
parent 890eb55d27
commit 26ecb5a9d4
93 changed files with 5273 additions and 4749 deletions

View file

@ -0,0 +1,17 @@
import { Requests } from '../../requests';
// <
// TGetResult,
// TPostData,
// TPostResult,
// TPutData = TPostData,
// TPutResult = TPostResult,
// TDeleteResult = void
// >
export class BaseAPI {
http: Requests;
constructor(requests: Requests) {
this.http = requests;
}
}

View file

@ -0,0 +1,3 @@
export type Results<T> = {
items: T[];
};

View file

@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest';
import { UrlBuilder } from '.';
describe('UrlBuilder', () => {
it('basic query parameter', () => {
const result = UrlBuilder('/test', { a: 'b' });
expect(result).toBe('/api/v1/test?a=b');
});
it('multiple query parameters', () => {
const result = UrlBuilder('/test', { a: 'b', c: 'd' });
expect(result).toBe('/api/v1/test?a=b&c=d');
});
it('no query parameters', () => {
const result = UrlBuilder('/test');
expect(result).toBe('/api/v1/test');
});
it('list-like query parameters', () => {
const result = UrlBuilder('/test', { a: ['b', 'c'] });
expect(result).toBe('/api/v1/test?a=b&a=c');
});
});

View file

@ -0,0 +1,2 @@
export { BaseAPI } from './base-api';
export { UrlBuilder } from './urls';

View file

@ -0,0 +1,31 @@
export const prefix = '/api/v1';
export type QueryValue =
| string
| string[]
| number
| number[]
| boolean
| null
| undefined;
export function UrlBuilder(
rest: string,
params: Record<string, QueryValue> = {}
): string {
// we use a stub base URL to leverage the URL class
const url = new URL(prefix + rest, 'http://localhost.com');
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value) {
url.searchParams.append(key, String(item));
}
} else {
url.searchParams.append(key, String(value));
}
}
// we return the path only, without the base URL
return url.toString().replace('http://localhost.com', '');
}

View file

@ -0,0 +1,37 @@
import { BaseAPI, UrlBuilder } from '../base';
import { type Results } from '../base/base-types';
export type LocationCreate = {
name: string;
description: string;
};
export type Location = LocationCreate & {
id: string;
groupId: string;
createdAt: string;
updatedAt: string;
};
export type LocationUpdate = LocationCreate;
export class LocationsApi extends BaseAPI {
async getAll() {
return this.http.get<Results<Location>>(UrlBuilder('/locations'));
}
async create(location: LocationCreate) {
return this.http.post<LocationCreate, Location>(UrlBuilder('/locations'), location);
}
async get(id: string) {
return this.http.get<Location>(UrlBuilder(`/locations/${id}`));
}
async delete(id: string) {
return this.http.delete<void>(UrlBuilder(`/locations/${id}`));
}
async update(id: string, location: LocationUpdate) {
return this.http.put<LocationUpdate, Location>(UrlBuilder(`/locations/${id}`), location);
}
}

View file

@ -0,0 +1,39 @@
import { BaseAPI, UrlBuilder } from "./base";
export type LoginResult = {
token: string;
expiresAt: string;
};
export type LoginPayload = {
username: string;
password: string;
};
export type RegisterPayload = {
user: {
email: string;
password: string;
name: string;
};
groupName: string;
};
export class PublicApi extends BaseAPI {
public login(username: string, password: string) {
return this.http.post<LoginPayload, LoginResult>(
UrlBuilder("/users/login"),
{
username,
password,
}
);
}
public register(payload: RegisterPayload) {
return this.http.post<RegisterPayload, LoginResult>(
UrlBuilder("/users/register"),
payload
);
}
}

34
frontend/lib/api/user.ts Normal file
View file

@ -0,0 +1,34 @@
import { Requests } from "~~/lib/requests";
import { BaseAPI, UrlBuilder } from "./base";
import { LocationsApi } from "./classes/locations";
export type Result<T> = {
item: T;
};
export type User = {
name: string;
email: string;
isSuperuser: boolean;
id: number;
};
export class UserApi extends BaseAPI {
locations: LocationsApi;
constructor(requests: Requests) {
super(requests);
this.locations = new LocationsApi(requests);
Object.freeze(this);
}
public self() {
return this.http.get<Result<User>>(UrlBuilder("/users/self"));
}
public logout() {
return this.http.post<object, void>(UrlBuilder("/users/logout"), {});
}
}