Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use typed client in most places, convert to start time + end time #21

Merged
merged 23 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CLI (Check)

on: [push]

jobs:
# run format, lint, and test
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: oven-sh/setup-bun@v2
- run: bun install --frozen-lockfile
- run: bun check
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"scripts": {
"format": "biome format --write ./src",
"lint": "biome lint --write ./src",
"check": "biome check ./src",
"check-fix": "biome check ./src --apply",
"check": "biome check ./src && tsc --noEmit --project .",
"dev": "IS_DEVELOPMENT_CLI_ENV=true bun run src/index.ts",
"release": "bun run src/scripts/release.ts",
"prod": "bun run src/index.ts",
Expand All @@ -28,7 +29,7 @@
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
"typescript": "^5.6.2"
},
"version": "0.0.29"
}
47 changes: 0 additions & 47 deletions src/api/index.ts

This file was deleted.

90 changes: 0 additions & 90 deletions src/api/orders.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/api/quoting.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/api/types.ts

This file was deleted.

14 changes: 11 additions & 3 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import createClient from "openapi-fetch";
import createClient, { type Client } from "openapi-fetch";
import { getAuthToken, loadConfig } from "./helpers/config";
import type { paths } from "./schema"; // generated by openapi-typescript

let __client: Client<paths, `${string}/${string}`> | undefined;

export const apiClient = async () => {
const config = await loadConfig();
if (__client) {
return __client;
}

return createClient<paths>({
const config = await loadConfig();
__client = createClient<paths>({
baseUrl: config.api_url,
headers: {
Authorization: `Bearer ${await getAuthToken()}`,
"Content-Type": "application/json",
},
});

return __client;
};
8 changes: 4 additions & 4 deletions src/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { getCommandBase } from "./command";
import { clearAuthFromConfig } from "./config";

export function logAndQuit(message: string) {
export function logAndQuit(message: string): never {
console.error(message);
process.exit(1);
}

export function logLoginMessageAndQuit() {
export function logLoginMessageAndQuit(): never {
const base = getCommandBase();
const loginCommand = `${base} login`;

logAndQuit(`You need to login first.\n\n\t$ ${loginCommand}\n`);
}

export async function logSessionTokenExpiredAndQuit() {
export async function logSessionTokenExpiredAndQuit(): Promise<never> {
await clearAuthFromConfig();
logAndQuit("\nYour session has expired. Please login again.");
}

export function failedToConnect() {
export function failedToConnect(): never {
logAndQuit(
"Failed to connect to the server. Please check your internet connection and try again.",
);
Expand Down
24 changes: 23 additions & 1 deletion src/helpers/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { Nullable } from "../types/empty";

export type Epoch = number;

const MILLS_PER_EPOCH = 1000 * 60;
const MILLS_PER_EPOCH = 1000 * 60; // 1 minute
const EPOCHS_PER_HOUR = (3600 * 1000) / MILLS_PER_EPOCH;

export function currentEpoch(): Epoch {
return Math.floor(Date.now() / MILLS_PER_EPOCH);
Expand All @@ -14,6 +15,27 @@ export function epochToDate(epoch: Epoch): Date {
return new Date(epoch * MILLS_PER_EPOCH);
}

export function roundStartDate(startDate: Date): Date {
const now = currentEpoch();
const startEpoch = dateToEpoch(startDate);
if (startEpoch <= now + 1) {
return epochToDate(now + 1);
} else {
return epochToDate(roundEpochUpToHour(startEpoch));
}
}

export function roundEndDate(endDate: Date): Date {
return epochToDate(roundEpochUpToHour(dateToEpoch(endDate)));
}

function dateToEpoch(date: Date): number {
return Math.ceil(date.getTime() / MILLS_PER_EPOCH);
}
function roundEpochUpToHour(epoch: number): number {
return Math.ceil(epoch / EPOCHS_PER_HOUR) * EPOCHS_PER_HOUR;
}

// -- currency

export type Cents = number;
Expand Down
Loading
Loading