Skip to content

Commit 87e2e2b

Browse files
committed
Fix TypeScript issues
1 parent 8de22e3 commit 87e2e2b

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

src/apiUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApiError } from './error'
2-
import type { ApiResponse } from "./types";
2+
import type { ApiResponse, SuccessResponseData } from "./types";
33

4-
export const successResponse = (data: object, message: string) => {
4+
export const successResponse = (data: SuccessResponseData, message: string) => {
55
const response: ApiResponse = {
66
success: true,
77
data,

src/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const logSleepRoute = async (req: BunRequest) => {
5454
export const getSleepRoute = async (req: BunRequest) => {
5555
const sheetsObj = await getSheetsObj()
5656

57-
const result = await getObjectArray(
57+
const result: SheetsSleepEntry[] = await getObjectArray(
5858
sheetsObj,
5959
process.env.SPREADSHEET_ID!,
6060
process.env.SPREADSHEET_RANGE!,

src/views/js/api.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ApiResponse, GetLastSleepRouteResponse } from '../../types.js';
12
import { getApiKey } from './params.js'
23

34
export const getSleepEntries = async () => {
@@ -13,12 +14,14 @@ export const getLastSleepEntry = async () => {
1314
const apiKey = getApiKey();
1415
const url = getEndpointUrl("api/sleep/last", apiKey);
1516

16-
return await fetch(url)
17+
const response = await fetch(url)
1718
.then(res => res.json())
1819
.catch(err => console.error(err));
20+
21+
return response as ApiResponse<GetLastSleepRouteResponse>;
1922
};
2023

21-
export const submitSleepEntry = async (position) => {
24+
export const submitSleepEntry = async (position: GeolocationPosition) => {
2225
const json = {
2326
coords: {
2427
latitude: position.coords.latitude,
@@ -48,7 +51,7 @@ export const submitSleepEntry = async (position) => {
4851
.catch(err => console.error(err));
4952
};
5053

51-
export const replaceLastSleepEntry = async (position) => {
54+
export const replaceLastSleepEntry = async (position: GeolocationPosition) => {
5255
const json = {
5356
coords: {
5457
latitude: position.coords.latitude,
@@ -78,7 +81,7 @@ export const replaceLastSleepEntry = async (position) => {
7881
.catch(err => console.error(err));
7982
}
8083

81-
const getEndpointUrl = (endpoint, apiKey) => {
84+
const getEndpointUrl = (endpoint: string, apiKey?: string) => {
8285
const url = new URL(endpoint, window.location.href);
8386
if (apiKey) {
8487
url.searchParams.append('apiKey', apiKey);

src/views/js/params.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
export const getApiKey = () => {
22
const searchParams = new URLSearchParams(window.location.search);
33
if (!searchParams.has('apiKey')) {
4-
document.getElementById('text').innerHTML = 'No API key provided';
5-
return null;
4+
const textElement = document.getElementById('text');
5+
if (textElement) {
6+
textElement.innerHTML = 'No API key provided';
7+
}
8+
return;
69
}
710
const apiKey = searchParams.get('apiKey');
8-
return apiKey;
11+
return apiKey ?? undefined;
912
}
1013

1114
export const getAutoLog = () => {

src/views/js/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export const formatDuration = duration => {
1+
export const formatDuration = (duration: number) => {
22
const secondsDiff = duration / 1000;
33

4-
const hours = parseInt(secondsDiff / 3600);
5-
const minutes = parseInt((secondsDiff / 60) % 60);
6-
const seconds = parseInt(secondsDiff % 60);
4+
const hours = Math.floor(secondsDiff / 3600);
5+
const minutes = Math.floor((secondsDiff / 60) % 60);
6+
const seconds = Math.floor(secondsDiff % 60);
77

88
// const hour = { singular: ' hour', plural: ' hours' };
99
// const minute = { singular: ' minute', plural: ' minutes' };
@@ -20,15 +20,15 @@ export const formatDuration = duration => {
2020
// return hoursString + " " + minutesString + " " + secondsString;
2121

2222
return (hours == 0 ? "" : hoursString) + " " +
23-
((hours == 0 & minutes == 0) ? "" : minutesString) + " " +
23+
((hours == 0 && minutes == 0) ? "" : minutesString) + " " +
2424
secondsString;
2525
}
2626

27-
export const prettyObjectString = object => Object.entries(object)
27+
export const prettyObjectString = (object: Record<string, any>) => Object.entries(object)
2828
.map(([key, value]) => `${key}: ${value}`)
2929
.join(",<br>");
3030

31-
export const printPosition = position => {
31+
export const printPosition = (position: GeolocationPosition) => {
3232
console.log(`Timestamp: ${new Date(position.timestamp)} (${position.timestamp})`);
3333
console.log('Your current position is:');
3434
console.log(`Latitude: ${position.coords.latitude}`);

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "@tsconfig/bun/tsconfig.json"
2+
"extends": "@tsconfig/bun/tsconfig.json",
3+
"compilerOptions": {
4+
"lib": ["ESNext", "DOM"]
5+
}
36
}

0 commit comments

Comments
 (0)