drop client

This commit is contained in:
Hayden 2022-08-30 16:08:21 -08:00
parent 7361dcc5f7
commit 93a4a816f9
10 changed files with 0 additions and 3289 deletions

View file

@ -1,5 +0,0 @@
import { v1ApiClient } from "./v1client";
export function getClientV1(baseUrl: string): v1ApiClient {
return new v1ApiClient(baseUrl, "v1");
}

View file

@ -1,93 +0,0 @@
import axios, { Axios } from "axios";
interface Wrap<T> {
item: T;
}
interface Status {
status: string;
message: string;
}
export interface ApiSummary {
health: boolean;
versions: string[];
title: string;
message: string;
}
export interface ApiToken {
token: string;
expiresAt: string;
}
export interface UserSelf {
id: string;
name: string;
email: string;
isSuperuser: boolean;
}
export class v1ApiClient {
version: string;
baseUrl: string;
requests: Axios;
token: string;
expires: Date;
constructor(baseUrl: string, version = "v1") {
this.version = version;
this.baseUrl = baseUrl;
this.requests = axios.create({
baseURL: `${this.baseUrl}/${this.version}`,
});
}
v1(url: string) {
return `${this.baseUrl}/api/v1${url}`;
}
api(url: string) {
return `${this.baseUrl}/api${url}`;
}
setToken(token: string, expires: Date) {
this.token = token;
this.expires = expires;
this.requests.defaults.headers.common["Authorization"] = token;
}
async login(username: string, password: string) {
const response = await this.requests.post<ApiToken>(
this.v1("/users/login"),
{
username,
password,
}
);
this.setToken(response.data.token, new Date(response.data.expiresAt));
return response;
}
async logout() {
const response = await this.requests.post<any>(this.v1("/users/logout"));
if (response.status === 200) {
this.setToken("", new Date());
}
return response;
}
async self() {
return this.requests.get<Wrap<UserSelf>>(this.v1("/users/self"));
}
async status() {
return this.requests.get<Wrap<Status>>(this.api("/status"));
}
}

3024
client/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,17 +0,0 @@
{
"devDependencies": {
"@types/expect": "^24.3.0",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.14",
"typescript": "^4.5.5",
"vitest": "^0.2.5"
},
"scripts": {
"test:ci": "TEST_SHUTDOWN_API_SERVER=true vitest --run --config ./test/vitest.config.ts",
"test:local": "TEST_SHUTDOWN_API_SERVER=false && vitest --run --config ./test/vitest.config.ts",
"test:watch": " TEST_SHUTDOWN_API_SERVER=false vitest --config ./test/vitest.config.ts"
},
"dependencies": {
"axios": "^0.25.0"
}
}

View file

@ -1,26 +0,0 @@
import { getClientV1 } from "../../client";
import { describe, it, expect } from "vitest";
import * as config from "../config";
const client = getClientV1(config.BASE_URL);
describe("GET /api/status", function () {
it("server is available", async function (done) {
try {
const res = await client.status();
expect(res.status).toBe(200);
expect(res.statusText).toBe("OK");
expect(res.data.item).toEqual({
health: true,
versions: ["v1"],
title: "Go API Template",
message: "Welcome to the Go API Template Application!",
});
done();
} catch (err) {
done(err);
}
});
});

View file

@ -1,4 +0,0 @@
export const PORT = "7745";
export const HOST = "http://127.0.0.1";
export const BASE_URL = HOST + ":" + PORT;

View file

@ -1,20 +0,0 @@
import { exec } from "child_process";
import * as config from "./config";
export const setup = () => {
console.log("Starting Client Tests");
console.log({
PORT: config.PORT,
HOST: config.HOST,
BASE_URL: config.BASE_URL,
});
};
export const teardown = () => {
if (process.env.TEST_SHUTDOWN_API_SERVER) {
const pc = exec("pkill -SIGTERM api"); // Kill background API process
pc.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
}
};

View file

@ -1,75 +0,0 @@
import { getClientV1 } from "../../client";
import { describe, it, expect } from "vitest";
import * as config from "../config";
import axios, { AxiosError } from "axios";
const client = getClientV1(config.BASE_URL);
describe("POST /api/v1/login", function () {
it("user can login", async function (done) {
try {
const res = await client.login("admin@admin.com", "admin");
expect(res.status).toBe(200);
expect(res.statusText).toBe("OK");
expect(res.data.expiresAt).exist;
expect(res.data.token).exist;
done();
} catch (err) {
done(err);
}
});
});
describe("POST /api/v1/users/logout", function () {
it("user can logout", async function (done) {
try {
const myclient = getClientV1(config.BASE_URL);
const res = await myclient.login("admin@admin.com", "admin");
expect(res.status).toBe(200);
expect(res.statusText).toBe("OK");
const res2 = await myclient.logout();
expect(res2.status).toBe(204);
expect(res2.statusText).toBe("No Content");
// Try to get self again
try {
const res3 = await myclient.self();
expect(res3.status).toBe(401);
expect(res3.statusText).toBe("Unauthorized");
} catch (e) {
if (axios.isAxiosError(e)) {
expect(e.response.status).toBe(401);
done();
} else {
done(e);
}
}
done();
} catch (err) {
done(err);
}
});
});
describe("GET /api/v1/users/self", function () {
it("user can access basic self details", async function (done) {
try {
const res = await client.self();
expect(res.status).toBe(200);
expect(res.statusText).toBe("OK");
expect(res.data.item.id).exist;
expect(res.data.item.name).toBe("Admin");
expect(res.data.item.email).toBe("admin@admin.com");
done();
} catch (err) {
done(err);
}
});
});

View file

@ -1,8 +0,0 @@
/// <reference types="vitest" />
import { defineConfig } from "vite";
export default defineConfig({
test: {
globalSetup: "./test/setup.ts",
},
});

View file

@ -1,17 +0,0 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"outDir": "build",
"sourceMap": true,
"allowJs": true,
"checkJs": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": false,
"esModuleInterop": true,
"removeComments": true
},
"include": ["client/**/*", "test/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}