Skip to content

Commit

Permalink
feat: export provider output types
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Sep 28, 2024
1 parent b143842 commit 421d6db
Show file tree
Hide file tree
Showing 26 changed files with 647 additions and 605 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ JS package for communicating with the Tauri backend. Published to npm as [`zebar

1. Add a new provider under [`client-api/src/providers/<YOUR_PROVIDER>`](https://github.com/glzr-io/zebar/tree/main/packages/client-api/src/providers).
2. Modify [`create-provider.ts`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/providers/create-provider.ts) to add the new provider to the `ProviderConfigMap` and `ProviderMap` types, and to create the provider in the switch statement within `createProvider`.
3. Export the provider's types from [`client-api/src/providers/index.ts`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/providers/index.ts).

2. **Add the backend logic for the provider.**

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Provider } from '../create-base-provider';

export interface BatteryProviderConfig {
type: 'battery';

/**
* How often this provider refreshes in milliseconds.
*/
refreshInterval?: number;
}

export type BatteryProvider = Provider<
BatteryProviderConfig,
BatteryOutput
>;

export interface BatteryOutput {
chargePercent: number;
cycleCount: number;
healthPercent: number;
powerConsumption: number;
state: 'discharging' | 'charging' | 'full' | 'empty' | 'unknown';
isCharging: boolean;
timeTillEmpty: number | null;
timeTillFull: number | null;
voltage: number | null;
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
import { z } from 'zod';

import {
createBaseProvider,
type Provider,
} from '../create-base-provider';
import { createBaseProvider } from '../create-base-provider';
import { onProviderEmit } from '~/desktop';

export interface BatteryProviderConfig {
type: 'battery';

/**
* How often this provider refreshes in milliseconds.
*/
refreshInterval?: number;
}
import type {
BatteryOutput,
BatteryProvider,
BatteryProviderConfig,
} from './battery-provider-types';

const batteryProviderConfigSchema = z.object({
type: z.literal('battery'),
refreshInterval: z.coerce.number().default(60 * 1000),
});

export type BatteryProvider = Provider<
BatteryProviderConfig,
BatteryOutput
>;

export interface BatteryOutput {
chargePercent: number;
cycleCount: number;
healthPercent: number;
powerConsumption: number;
state: 'discharging' | 'charging' | 'full' | 'empty' | 'unknown';
isCharging: boolean;
timeTillEmpty: number | null;
timeTillFull: number | null;
voltage: number | null;
}

export function createBatteryProvider(
config: BatteryProviderConfig,
): BatteryProvider {
Expand Down
20 changes: 20 additions & 0 deletions packages/client-api/src/providers/cpu/cpu-provider-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Provider } from '../create-base-provider';

export interface CpuProviderConfig {
type: 'cpu';

/**
* How often this provider refreshes in milliseconds.
*/
refreshInterval?: number;
}

export type CpuProvider = Provider<CpuProviderConfig, CpuOutput>;

export interface CpuOutput {
frequency: number;
usage: number;
logicalCoreCount: number;
physicalCoreCount: number;
vendor: string;
}
29 changes: 6 additions & 23 deletions packages/client-api/src/providers/cpu/create-cpu-provider.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import { z } from 'zod';

import {
createBaseProvider,
type Provider,
} from '../create-base-provider';
import { createBaseProvider } from '../create-base-provider';
import { onProviderEmit } from '~/desktop';

export interface CpuProviderConfig {
type: 'cpu';

/**
* How often this provider refreshes in milliseconds.
*/
refreshInterval?: number;
}
import type {
CpuOutput,
CpuProvider,
CpuProviderConfig,
} from './cpu-provider-types';

const cpuProviderConfigSchema = z.object({
type: z.literal('cpu'),
refreshInterval: z.coerce.number().default(5 * 1000),
});

export type CpuProvider = Provider<CpuProviderConfig, CpuOutput>;

export interface CpuOutput {
frequency: number;
usage: number;
logicalCoreCount: number;
physicalCoreCount: number;
vendor: string;
}

export function createCpuProvider(config: CpuProviderConfig): CpuProvider {
const mergedConfig = cpuProviderConfigSchema.parse(config);

Expand Down
107 changes: 52 additions & 55 deletions packages/client-api/src/providers/create-provider.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
import {
createBatteryProvider,
type BatteryProvider,
type BatteryProviderConfig,
} from './battery/create-battery-provider';
import {
createCpuProvider,
type CpuProvider,
type CpuProviderConfig,
} from './cpu/create-cpu-provider';
import {
createDateProvider,
type DateProvider,
type DateProviderConfig,
} from './date/create-date-provider';
import {
createGlazeWmProvider,
type GlazeWmProvider,
type GlazeWmProviderConfig,
} from './glazewm/create-glazewm-provider';
import {
createHostProvider,
type HostProvider,
type HostProviderConfig,
} from './host/create-host-provider';
import {
createIpProvider,
type IpProvider,
type IpProviderConfig,
} from './ip/create-ip-provider';
import {
createKomorebiProvider,
type KomorebiProvider,
type KomorebiProviderConfig,
} from './komorebi/create-komorebi-provider';
import {
createMemoryProvider,
type MemoryProvider,
type MemoryProviderConfig,
} from './memory/create-memory-provider';
import {
createNetworkProvider,
type NetworkProvider,
type NetworkProviderConfig,
} from './network/create-network-provider';
import {
createWeatherProvider,
type WeatherProvider,
type WeatherProviderConfig,
} from './weather/create-weather-provider';
import {
createKeyboardProvider,
type KeyboardProvider,
type KeyboardProviderConfig,
} from './keyboard/create-keyboard-provider';
import { createBatteryProvider } from './battery/create-battery-provider';
import type {
BatteryProviderConfig,
BatteryProvider,
} from './battery/battery-provider-types';
import { createCpuProvider } from './cpu/create-cpu-provider';
import type {
CpuProviderConfig,
CpuProvider,
} from './cpu/cpu-provider-types';
import { createDateProvider } from './date/create-date-provider';
import type {
DateProviderConfig,
DateProvider,
} from './date/date-provider-types';
import { createGlazeWmProvider } from './glazewm/create-glazewm-provider';
import type {
GlazeWmProviderConfig,
GlazeWmProvider,
} from './glazewm/glazewm-provider-types';
import { createHostProvider } from './host/create-host-provider';
import type {
HostProviderConfig,
HostProvider,
} from './host/host-provider-types';
import { createIpProvider } from './ip/create-ip-provider';
import type { IpProviderConfig, IpProvider } from './ip/ip-provider-types';
import { createKeyboardProvider } from './keyboard/create-keyboard-provider';
import type {
KeyboardProviderConfig,
KeyboardProvider,
} from './keyboard/keyboard-provider-types';
import { createKomorebiProvider } from './komorebi/create-komorebi-provider';
import type {
KomorebiProviderConfig,
KomorebiProvider,
} from './komorebi/komorebi-provider-types';
import { createMemoryProvider } from './memory/create-memory-provider';
import type {
MemoryProviderConfig,
MemoryProvider,
} from './memory/memory-provider-types';
import { createNetworkProvider } from './network/create-network-provider';
import type {
NetworkProviderConfig,
NetworkProvider,
} from './network/network-provider-types';
import { createWeatherProvider } from './weather/create-weather-provider';
import type {
WeatherProviderConfig,
WeatherProvider,
} from './weather/weather-provider-types';

export interface ProviderConfigMap {
battery: BatteryProviderConfig;
Expand Down
75 changes: 5 additions & 70 deletions packages/client-api/src/providers/date/create-date-provider.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,11 @@
import { DateTime } from 'luxon';
import { z } from 'zod';

import {
createBaseProvider,
type Provider,
} from '../create-base-provider';

export interface DateProviderConfig {
type: 'date';

/**
* How often this provider refreshes in milliseconds.
*/
refreshInterval?: number;

/**
* Either a UTC offset (eg. `UTC+8`) or an IANA timezone (eg.
* `America/New_York`). Affects the output of {@link DateOutput.formatted}.
*
* A full list of available IANA timezones can be found [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List).
*/
timezone?: string;

/**
* An ISO-639-1 locale, which is either a 2-letter language code
* (eg. `en`) or a 4-letter language + country code (eg. `en-gb`).
* Affects the output of {@link DateOutput.formatted}.
*
* A full list of ISO-639-1 locales can be found [here](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes#Table).
*/
locale?: string;

/**
* Formatting of the current date into a custom string format. Affects
* the output of {@link DateOutput.formatted}.
*
* Refer to [table of tokens](https://moment.github.io/luxon/#/formatting?id=table-of-tokens)
* for available date/time tokens.
*
* @example
* "yyyy LLL dd" -> "2023 Feb 13"
* "HH 'hours and' mm 'minutes'" -> "20 hours and 55 minutes"
*/
formatting?: string;
}
import { createBaseProvider } from '../create-base-provider';
import type {
DateProvider,
DateProviderConfig,
} from './date-provider-types';

const dateProviderConfigSchema = z.object({
type: z.literal('date'),
Expand All @@ -53,33 +15,6 @@ const dateProviderConfigSchema = z.object({
formatting: z.string().default('EEE d MMM t'),
});

export type DateProvider = Provider<DateProviderConfig, DateOutput>;

export interface DateOutput {
/**
* Current date/time as a formatted string.
*/
formatted: string;

/**
* Current date/time as a JavaScript `Date` object. Uses `new Date()` under
* the hood.
**/
new: Date;

/**
* Current date/time as milliseconds since epoch. Uses `Date.now()` under the
* hood.
**/
now: number;

/**
* Current date/time as an ISO-8601 string (eg.
* `2017-04-22T20:47:05.335-04:00`). Uses `date.toISOString()` under the hood.
**/
iso: string;
}

export function createDateProvider(
config: DateProviderConfig,
): DateProvider {
Expand Down
Loading

0 comments on commit 421d6db

Please sign in to comment.