Skip to content

Commit fc3e0ae

Browse files
Merge pull request #10 from bchainhub/update/contrib-10
Fixed types
2 parents 85db7b4 + 2cd0f61 commit fc3e0ae

File tree

9 files changed

+40
-51
lines changed

9 files changed

+40
-51
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@sveltejs/vite-plugin-svelte": "^3.1.2",
5151
"@types/eslint": "^9.6.1",
5252
"@types/node": "^20",
53-
"eslint": "^9.9.1",
53+
"eslint": "^9.10.0",
5454
"eslint-config-prettier": "^9.1.0",
5555
"eslint-plugin-svelte": "^2.43.0",
5656
"globals": "^15.9.0",
@@ -79,6 +79,7 @@
7979
"payto-rl": "^1.0.1",
8080
"postcss": "^8.4.45",
8181
"tailwindcss": "^3.4.10",
82+
"txms.js": "^1.3.1",
8283
"uuid": "^10.0.0"
8384
}
8485
}

src/app.d.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { D1Database, KVNamespace, R2Bucket } from '@cloudflare/workers-types';
1+
type D1Database = import('@cloudflare/workers-types').D1Database;
2+
type KVNamespace = import('@cloudflare/workers-types').KVNamespace;
3+
type R2Bucket = import('@cloudflare/workers-types').R2Bucket;
24

35
interface Config {
46
title: string;
@@ -56,6 +58,24 @@ type MenuItem = NavbarItem & {
5658
action?: () => void;
5759
};
5860

61+
interface User {
62+
name?: string | null;
63+
email?: string | null;
64+
image?: string | null;
65+
id?: string | null; // Optional user ID (can be from OAuth provider or internal)
66+
role?: string | null; // Optional role for authorization
67+
[key: string]: any; // Allow other properties as needed for flexibility
68+
}
69+
70+
// Define the session type to include user and session-specific properties
71+
interface Session {
72+
user?: User | null; // User information associated with the session
73+
expires: string; // Expiration date as an ISO string
74+
accessToken?: string; // Optional access token for OAuth/JWT providers
75+
refreshToken?: string; // Optional refresh token for OAuth/JWT providers
76+
[key: string]: any; // Allow extensions for future changes or addons
77+
}
78+
5979
declare namespace App {
6080
interface Locals {
6181
db?: D1Database;
@@ -69,7 +89,8 @@ declare namespace App {
6989
}
7090

7191
interface PageData {
72-
config: Config;
92+
session: Session | null;
93+
config?: Config;
7394
}
7495

7596
interface Platform {
@@ -80,17 +101,7 @@ declare namespace App {
80101
env?: Env;
81102
// Cloudflare-specific properties
82103
cf?: {
83-
asn?: string; // Autonomous System Number
84-
asOrganization?: string; // Organization name of ASN
85-
city?: string; // City of the request origin
86-
continent?: string; // Continent of the request origin
87-
country?: string; // Country code (ISO 3166-1 Alpha 2)
88-
latitude?: string; // Latitude of the request origin
89-
longitude?: string; // Longitude of the request origin
90-
postalCode?: string; // Postal code of the request origin
91-
region?: string; // Region name
92-
regionCode?: string; // Region code
93-
timezone?: string; // Timezone of the request origin
104+
[key: string]: string | undefined;
94105
};
95106
}
96107
}

src/lib/components/Header.svelte

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
];
2323
2424
const handleSelect = (event: CustomEvent<{ label: string; action: () => void; }>) => {
25-
console.log(`Selected item: ${event.detail.label}`);
2625
event.detail.action();
2726
};
2827
@@ -46,7 +45,7 @@
4645
};
4746
4847
const rotateTheme = () => {
49-
let prefersDark = defaultMode;
48+
let prefersDark = defaultMode === 'dark' ? true : false;
5049
if (typeof window !== 'undefined') {
5150
prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
5251
}
@@ -87,11 +86,11 @@
8786
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
8887
document.documentElement.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
8988
} else {
90-
document.documentElement.setAttribute('data-theme', theme);
89+
document.documentElement.setAttribute('data-theme', theme ?? '');
9190
}
92-
localStorage.setItem('theme', theme);
91+
localStorage.setItem('theme', theme ?? '');
9392
} else {
94-
localStorage.setItem('theme', theme);
93+
localStorage.setItem('theme', theme ?? '');
9594
}
9695
};
9796

src/lib/helpers/db.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ import { env } from '$env/dynamic/private';
22
import type { D1Database } from '@cloudflare/workers-types';
33
import type { RequestEvent } from '@sveltejs/kit';
44

5-
interface ExtendedPlatform extends Readonly<App.Platform> {
6-
env: {
7-
[key: string]: D1Database;
8-
};
9-
}
10-
115
/**
126
* Retrieves and validates the database instance from the RequestEvent object.
137
* @param event - The RequestEvent object provided by SvelteKit.
148
* @returns The database instance.
159
* @throws Will throw an error if the database name is not defined or the database instance is not found.
1610
*/
1711
export function getDatabaseInstance(event: RequestEvent): D1Database {
18-
const platform = event.platform as ExtendedPlatform;
19-
if (!platform) {
12+
if (!event.platform) {
2013
throw new Error("Platform is undefined.");
2114
}
2215

@@ -25,7 +18,7 @@ export function getDatabaseInstance(event: RequestEvent): D1Database {
2518
throw new Error("Database name not defined.");
2619
}
2720

28-
const db = platform.env[dbName] as D1Database | undefined;
21+
const db = (event.platform?.env as Record<string, D1Database | undefined>)?.[dbName];
2922

3023
if (!db) {
3124
throw new Error("Database not found.");

src/lib/helpers/kv.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ import { env } from '$env/dynamic/private';
22
import type { KVNamespace } from '@cloudflare/workers-types';
33
import type { RequestEvent } from '@sveltejs/kit';
44

5-
interface ExtendedPlatform extends Readonly<App.Platform> {
6-
env: {
7-
[key: string]: KVNamespace;
8-
};
9-
}
10-
115
/**
126
* Retrieves and validates the KV namespace instance from the RequestEvent object.
137
* @param event - The RequestEvent object provided by SvelteKit.
148
* @returns The KV namespace instance.
159
* @throws Will throw an error if the KV namespace is not defined or not found.
1610
*/
1711
export function getKVNamespace(event: RequestEvent): KVNamespace {
18-
const platform = event.platform as ExtendedPlatform;
19-
if (!platform) {
12+
if (!event.platform) {
2013
throw new Error("Platform is undefined.");
2114
}
2215

@@ -25,7 +18,7 @@ export function getKVNamespace(event: RequestEvent): KVNamespace {
2518
throw new Error("KV namespace name not defined.");
2619
}
2720

28-
const kv = platform.env[kvName] as KVNamespace | undefined;
21+
const kv = (event.platform?.env as Record<string, KVNamespace | undefined>)?.[kvName];
2922

3023
if (!kv) {
3124
throw new Error("KV namespace not found.");

src/lib/helpers/r2.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ import { env } from '$env/dynamic/private';
22
import type { R2Bucket } from '@cloudflare/workers-types';
33
import type { RequestEvent } from '@sveltejs/kit';
44

5-
interface ExtendedPlatform extends Readonly<App.Platform> {
6-
env: {
7-
[key: string]: R2Bucket;
8-
};
9-
}
10-
115
/**
126
* Retrieves and validates the R2 bucket instance from the RequestEvent object.
137
* @param event - The RequestEvent object provided by SvelteKit.
148
* @returns The R2 bucket instance.
159
* @throws Will throw an error if the R2 bucket binding is not defined or not found.
1610
*/
1711
export function getR2Bucket(event: RequestEvent): R2Bucket {
18-
const platform = event.platform as ExtendedPlatform;
19-
if (!platform) {
12+
if (!event.platform) {
2013
throw new Error("Platform is undefined.");
2114
}
2215

@@ -28,7 +21,7 @@ export function getR2Bucket(event: RequestEvent): R2Bucket {
2821
throw new Error("R2 bucket name not defined.");
2922
}
3023

31-
const bucket = platform.env[bucketName] as R2Bucket | undefined;
24+
const bucket = (event.platform?.env as Record<string, R2Bucket | undefined>)?.[bucketName];
3225

3326
if (!bucket) {
3427
throw new Error("R2 bucket not found.");

src/routes/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<li><a href="https://github.com/bchainhub/ican.js" target="_blank" rel="noopener noreferrer">ICAN / IBAN Validation</a></li>
2222
<li><a href="https://github.com/bchainhub/exchange-rounding" target="_blank" rel="noopener noreferrer">Exchange Number Format</a></li>
2323
<li><a href="https://github.com/bchainhub/payto-rl" target="_blank" rel="noopener noreferrer">PayTo resource locators</a></li>
24+
<li><a href="https://txms.info" target="_blank" rel="noopener noreferrer">TxMS - 0G connectivity for transaction transmission</a></li>
2425
<li><a href="https://github.com/uuidjs/uuid" target="_blank" rel="noopener noreferrer">UUID</a></li>
2526
</ul>
2627
</div>

src/routes/db/init/+server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const GET: RequestHandler = async ({ platform, locals }) => {
1818
await up(locals.db);
1919

2020
// Additional fields for the Authenticator table
21-
await locals.db.prepare(`
21+
await locals.db?.prepare(`
2222
CREATE TABLE IF NOT EXISTS "Authenticator" (
2323
"id" TEXT NOT NULL PRIMARY KEY,
2424
"credentialID" TEXT NOT NULL,
@@ -34,15 +34,15 @@ export const GET: RequestHandler = async ({ platform, locals }) => {
3434
`).run();
3535

3636
// Index for the credentialID field
37-
await locals.db.prepare(`
37+
await locals.db?.prepare(`
3838
CREATE UNIQUE INDEX IF NOT EXISTS "Authenticator_credentialID_key" ON "Authenticator"("credentialID");
3939
`).run();
4040

4141
// Additional fields for the User table:
4242
// coreId - The Core ID of the user (free item)
4343
// isActive - Whether the user Core ID is updated (user is activated)
4444
// isVerified - Whether the user Core ID is KYC verified (free item)
45-
await locals.db.prepare(`
45+
await locals.db?.prepare(`
4646
ALTER TABLE users ADD COLUMN coreId CHAR(44);
4747
CREATE INDEX idx_coreid ON users (coreId);
4848
ALTER TABLE users ADD COLUMN isActive BOOLEAN DEFAULT 0;

src/site.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Config } from './app';
21
export const config: Config = {
32
title: "MOTA",
43
url: "http://localhost:3000",
@@ -109,7 +108,6 @@ export const config: Config = {
109108
metadata: [
110109
{ name: "viewport", content: "width=device-width, initial-scale=1.0" },
111110
{ name: "theme-color", content: "#25c19f" },
112-
{ name: "apple-mobile-web-app-capable", content: "yes" },
113111
{ name: "description", content: "This is SvetleKit Boilerplate website" },
114112
{ name: "keywords", content: "website, sveltekit, vite, cloudflare" },
115113
{ property: "og:type", content: "website" }

0 commit comments

Comments
 (0)