@jchat/addon-sdk 0.0.1 → 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +2 -2
- package/dist/diagnostics.mjs +110 -1
- package/dist/index.d.mts +20 -3
- package/dist/index.mjs +86 -1
- package/dist/vite.d.mts +9 -12
- package/dist/vite.mjs +185 -4
- package/package.json +16 -9
- package/src/contracts.d.ts +127 -0
- package/src/contracts.ts +132 -0
- package/src/diagnostics.d.ts +19 -0
- package/src/diagnostics.test.ts +106 -0
- package/src/diagnostics.ts +182 -0
- package/src/index.d.ts +249 -0
- package/src/index.test.ts +35 -0
- package/src/index.ts +299 -0
- package/src/vite.test.ts +62 -0
- package/src/vite.ts +289 -0
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { IconPack } from "@jchat/icon";
|
|
2
|
+
import type { Elysia } from "elysia";
|
|
3
|
+
import type { Kysely } from "kysely";
|
|
4
|
+
import type { AddonManifest, AddonPermission, MountPoint } from "./contracts";
|
|
5
|
+
export { MOUNT_POINTS, type AddonManifest, type AddonPermission, type AddonPermissionDefinition, type ConfigSchemaField, type MountPoint } from "./contracts";
|
|
6
|
+
export type AddonJsonBody = Record<string, unknown> | unknown[] | null;
|
|
7
|
+
/**
|
|
8
|
+
* @jchat/addon-sdk
|
|
9
|
+
*
|
|
10
|
+
* Lightweight SDK for Jchat addon developers. Provides TypeScript types for addon props, manifest
|
|
11
|
+
* schema, and host API access. This package is always EXTERNALISED from addon bundles — the host
|
|
12
|
+
* app provides the runtime implementation.
|
|
13
|
+
*/
|
|
14
|
+
/** Minimal user profile accessible to addon backends. */
|
|
15
|
+
export interface AddonUser {
|
|
16
|
+
/** Unique user identifier */
|
|
17
|
+
id: string;
|
|
18
|
+
/** Display name */
|
|
19
|
+
name: string;
|
|
20
|
+
/** Full display name */
|
|
21
|
+
displayName: string;
|
|
22
|
+
/** Full URL to the user's avatar image */
|
|
23
|
+
avatar: string;
|
|
24
|
+
/** User's gender identifier */
|
|
25
|
+
gender: string;
|
|
26
|
+
}
|
|
27
|
+
export interface AddonRequestUser {
|
|
28
|
+
id?: string;
|
|
29
|
+
permissions?: string[];
|
|
30
|
+
siteId?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Typed, schema-scoped database facade for an addon. The host deliberately omits raw execution,
|
|
34
|
+
* schema switching, dialect/connection access, and lifecycle methods.
|
|
35
|
+
*/
|
|
36
|
+
export type AddonDatabase<TDatabase = Record<string, Record<string, unknown>>> = Pick<Kysely<TDatabase>, "deleteFrom" | "fn" | "insertInto" | "selectFrom" | "transaction" | "updateTable">;
|
|
37
|
+
/**
|
|
38
|
+
* Context provided to an addon's server-side factory. Parameterize it with the addon's table
|
|
39
|
+
* interface: `AddonServerContext<MyAddonDatabase>`.
|
|
40
|
+
*/
|
|
41
|
+
export interface AddonServerContext<TDatabase = Record<string, Record<string, unknown>>> {
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve the encrypted settings for this addon for a specific site. Internal secrets are
|
|
44
|
+
* automatically decrypted using the host's master key.
|
|
45
|
+
*/
|
|
46
|
+
getConfig: (siteId: string) => Promise<Record<string, unknown>>;
|
|
47
|
+
/** Fetch a specific user's public profile */
|
|
48
|
+
getUser: (userId: string) => Promise<AddonUser | null>;
|
|
49
|
+
/** Efficiently fetch multiple user profiles in a single query. Returns a Map keyed by user ID. */
|
|
50
|
+
getUsers: (userIds: string[]) => Promise<Map<string, AddonUser>>;
|
|
51
|
+
/**
|
|
52
|
+
* Check whether the request user holds a given permission. Provides autocomplete for core Jchat
|
|
53
|
+
* permissions while also accepting any custom addon permission string.
|
|
54
|
+
*
|
|
55
|
+
* @param user - The user object from the request context
|
|
56
|
+
* @param permission - The permission to check, e.g., 'message.send' or 'addon.stories.view'
|
|
57
|
+
*/
|
|
58
|
+
hasPermission: (user: AddonRequestUser | null | undefined, permission: AddonPermission) => boolean;
|
|
59
|
+
/**
|
|
60
|
+
* A Kysely database instance scoped entirely to your addon's tables. You cannot access core Jchat
|
|
61
|
+
* tables (Users, Rooms, etc.) through this instance.
|
|
62
|
+
*/
|
|
63
|
+
db: AddonDatabase<TDatabase>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The entry point for your addon's server bundle. You should default-export a function matching
|
|
67
|
+
* this signature.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ;```ts
|
|
71
|
+
* import { type AddonServerFactory } from '@jchat/addon-sdk'
|
|
72
|
+
*
|
|
73
|
+
* const factory: AddonServerFactory = ({ db, hasPermission }, app) => {
|
|
74
|
+
* return app.get('/trending', async () => { ... })
|
|
75
|
+
* }
|
|
76
|
+
* export default factory
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export type AddonServerFactory<TDatabase = Record<string, Record<string, unknown>>> = (ctx: AddonServerContext<TDatabase>, app: Elysia) => Elysia;
|
|
80
|
+
/** Props passed to your addon's root React component by the Jchat host. */
|
|
81
|
+
export interface AddonProps {
|
|
82
|
+
/** The mount point where this addon instance is rendered (e.g., 'chat-sidebar') */
|
|
83
|
+
mountPoint: MountPoint;
|
|
84
|
+
/** Information about the currently logged-in user (if any) */
|
|
85
|
+
user?: {
|
|
86
|
+
id: string;
|
|
87
|
+
name: string;
|
|
88
|
+
displayName?: string;
|
|
89
|
+
avatar?: string;
|
|
90
|
+
gender?: string;
|
|
91
|
+
};
|
|
92
|
+
/** The ID of the room the user is currently viewing (if applicable) */
|
|
93
|
+
roomId?: string;
|
|
94
|
+
/** The ID of the active Jchat site */
|
|
95
|
+
siteId?: string;
|
|
96
|
+
}
|
|
97
|
+
export interface AddonApi {
|
|
98
|
+
get: <TResponse = unknown>(path: string) => Promise<TResponse>;
|
|
99
|
+
post: <TResponse = unknown>(path: string, body?: AddonJsonBody) => Promise<TResponse>;
|
|
100
|
+
put: <TResponse = unknown>(path: string, body?: AddonJsonBody) => Promise<TResponse>;
|
|
101
|
+
delete: <TResponse = unknown>(path: string) => Promise<TResponse>;
|
|
102
|
+
}
|
|
103
|
+
export type AddonEventListener = (data: unknown) => void;
|
|
104
|
+
/** A font option provided by the JChat host. */
|
|
105
|
+
export interface AddonFont {
|
|
106
|
+
/** Display label shown to users (e.g. 'Modern', 'Elegant') */
|
|
107
|
+
label: string;
|
|
108
|
+
/** Font identifier stored in the database (e.g. 'poppins', 'lato') */
|
|
109
|
+
value: string;
|
|
110
|
+
/** CSS font-family string ready for use in inline styles */
|
|
111
|
+
family: string;
|
|
112
|
+
/** Google Fonts name for loading the font via a <link> tag */
|
|
113
|
+
googleName: string;
|
|
114
|
+
/** Relative size multiplier — apply this to base font sizes to match the host's rendering */
|
|
115
|
+
scale: number;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The bridge between your addon and the Jchat host application. Use the `useAddonSDK()` hook to
|
|
119
|
+
* access this interface in your components.
|
|
120
|
+
*/
|
|
121
|
+
export interface AddonSDK {
|
|
122
|
+
/**
|
|
123
|
+
* Make authenticated API calls to your addon's backend or core Jchat endpoints. All calls
|
|
124
|
+
* automatically include the current user's sessionId and site validation headers.
|
|
125
|
+
*/
|
|
126
|
+
api: AddonApi;
|
|
127
|
+
/**
|
|
128
|
+
* Display a temporary notification toast in the host UI.
|
|
129
|
+
*
|
|
130
|
+
* @param message - The text content to display
|
|
131
|
+
* @param variant - Visual style: 'default' (blue), 'success' (green), or 'error' (red)
|
|
132
|
+
*/
|
|
133
|
+
toast: (message: string, variant?: "default" | "success" | "error") => void;
|
|
134
|
+
/**
|
|
135
|
+
* Navigate the host application to a different internal route.
|
|
136
|
+
*
|
|
137
|
+
* @param path - The target location, e.g., '/settings' or '/addons/store'
|
|
138
|
+
*/
|
|
139
|
+
navigate: (path: string) => void;
|
|
140
|
+
/**
|
|
141
|
+
* Interaction utilities for the active Slate message editor. Only available if your addon is
|
|
142
|
+
* mounted near the chat input.
|
|
143
|
+
*/
|
|
144
|
+
editor: {
|
|
145
|
+
/**
|
|
146
|
+
* Trigger a specialized editor command defined by the host.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* sdk.editor.dispatchCommand("INSERT_YOUTUBE_COMMAND", "dQw4w9WgXcQ")
|
|
150
|
+
*/
|
|
151
|
+
dispatchCommand: (command: string, payload?: unknown) => void;
|
|
152
|
+
/**
|
|
153
|
+
* Insert a low-level Slate node directly at the user's cursor position.
|
|
154
|
+
*
|
|
155
|
+
* @param node - A valid Slate Descendant or Fragment object
|
|
156
|
+
*/
|
|
157
|
+
insertNode: (node: unknown) => void;
|
|
158
|
+
/**
|
|
159
|
+
* Upload and attach a file to the current message without inserting it inline. Users will see
|
|
160
|
+
* the file in the attachment bar below the editor.
|
|
161
|
+
*/
|
|
162
|
+
addAttachment: (attachment: {
|
|
163
|
+
url: string;
|
|
164
|
+
name: string;
|
|
165
|
+
type: string;
|
|
166
|
+
}) => void;
|
|
167
|
+
/** Force the browser focus back to the editor input */
|
|
168
|
+
focus: () => void;
|
|
169
|
+
};
|
|
170
|
+
/** Event Bus for reacting to real-time application state changes. */
|
|
171
|
+
events: {
|
|
172
|
+
/**
|
|
173
|
+
* Register a persistent listener for a specific event.
|
|
174
|
+
*
|
|
175
|
+
* @param event - Event name, e.g., 'room.message', 'user.typing'
|
|
176
|
+
*/
|
|
177
|
+
on: (event: string, callback: AddonEventListener) => void;
|
|
178
|
+
/** Unregister a previously attached listener */
|
|
179
|
+
off: (event: string, callback: AddonEventListener) => void;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* The list of fonts available in this JChat deployment, as configured by the site administrator.
|
|
183
|
+
* Use this to build a font picker or apply a matching font family in your addon UI.
|
|
184
|
+
*/
|
|
185
|
+
supportedFonts?: AddonFont[];
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Seed data for your addon's tables, provided inside the addon's isolated schema.
|
|
189
|
+
*
|
|
190
|
+
* These documents are inserted into the addon's dedicated schema on installation and can be used to
|
|
191
|
+
* provide default values, references, or supporting data. The host guarantees that these documents
|
|
192
|
+
* do not conflict with core schemas or other addons.
|
|
193
|
+
*/
|
|
194
|
+
export interface AddonSeedDocument {
|
|
195
|
+
/** Addon-owned table name. Core prefixes the addon's isolated PostgreSQL schema. */
|
|
196
|
+
table: string;
|
|
197
|
+
/** Behavior when a row conflicts with an existing unique or primary key. */
|
|
198
|
+
conflict?: "error" | "ignore" | "update";
|
|
199
|
+
/** Required for update behavior; identifies the unique key used by ON CONFLICT. */
|
|
200
|
+
conflictColumns?: string[];
|
|
201
|
+
/** JSON-compatible rows inserted using parameterized values. */
|
|
202
|
+
rows: Record<string, unknown>[];
|
|
203
|
+
}
|
|
204
|
+
/** Define a manifest with full validation-friendly typing while preserving literal values. */
|
|
205
|
+
export declare function defineAddonManifest<const TManifest extends AddonManifest>(manifest: TManifest): TManifest;
|
|
206
|
+
/** Define a typed server factory without widening its addon database interface. */
|
|
207
|
+
export declare function defineAddonServer<TDatabase = Record<string, Record<string, unknown>>>(factory: AddonServerFactory<TDatabase>): AddonServerFactory<TDatabase>;
|
|
208
|
+
/**
|
|
209
|
+
* Access the host application's SDK. Provides authenticated API access, toast notifications, and
|
|
210
|
+
* navigation.
|
|
211
|
+
*
|
|
212
|
+
* This function is a shell; the real implementation is provided by the Jchat host environment at
|
|
213
|
+
* runtime.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ;```tsx
|
|
217
|
+
* import { useAddonSDK } from "@jchat/addon-sdk"
|
|
218
|
+
*
|
|
219
|
+
* export default function MyAddon({ mountPoint }: AddonProps) {
|
|
220
|
+
* const sdk = useAddonSDK()
|
|
221
|
+
*
|
|
222
|
+
* const handleClick = async () => {
|
|
223
|
+
* const data = await sdk.api.get("/trending")
|
|
224
|
+
* sdk.toast("Fetched trending posts!", "success")
|
|
225
|
+
* }
|
|
226
|
+
* }
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export declare function useAddonSDK(): AddonSDK;
|
|
230
|
+
/**
|
|
231
|
+
* Access the host application's active icon pack. Returns the icon set matching the site's
|
|
232
|
+
* configured icon pack preference.
|
|
233
|
+
*
|
|
234
|
+
* This function is a shell; the real implementation is provided by the Jchat host environment at
|
|
235
|
+
* runtime.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ;```tsx
|
|
239
|
+
* import { useIcons } from "@jchat/addon-sdk"
|
|
240
|
+
*
|
|
241
|
+
* export default function MyAddon() {
|
|
242
|
+
* const { Icons } = useIcons()
|
|
243
|
+
* return <Icons.Send className="size-5" />
|
|
244
|
+
* }
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export declare function useIcons(): {
|
|
248
|
+
Icons: IconPack;
|
|
249
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { Elysia } from "elysia"
|
|
3
|
+
import {
|
|
4
|
+
defineAddonManifest,
|
|
5
|
+
defineAddonServer,
|
|
6
|
+
MOUNT_POINTS,
|
|
7
|
+
type AddonManifest,
|
|
8
|
+
type AddonServerContext
|
|
9
|
+
} from "./index"
|
|
10
|
+
|
|
11
|
+
describe("addon SDK definition helpers", () => {
|
|
12
|
+
test("defineAddonManifest preserves the manifest", () => {
|
|
13
|
+
const manifest = {
|
|
14
|
+
id: "example-addon",
|
|
15
|
+
name: "Example addon",
|
|
16
|
+
version: "1.0.0",
|
|
17
|
+
description: "An example addon",
|
|
18
|
+
author: "Jchat",
|
|
19
|
+
compatibility: { jchat: "^1.0.0", addonSdk: "^1.0.0" },
|
|
20
|
+
permissions: ["message.read"]
|
|
21
|
+
} as const satisfies AddonManifest
|
|
22
|
+
|
|
23
|
+
expect(defineAddonManifest(manifest)).toBe(manifest)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test("keeps manifest mount points available from the public entry", () => {
|
|
27
|
+
expect(MOUNT_POINTS.CHAT_HEADER).toBe("chat-header")
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test("defineAddonServer preserves the server factory", () => {
|
|
31
|
+
const factory = (_context: AddonServerContext) => new Elysia()
|
|
32
|
+
|
|
33
|
+
expect(defineAddonServer(factory)).toBe(factory)
|
|
34
|
+
})
|
|
35
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { IconPack } from "@jchat/icon"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import type { Kysely } from "kysely"
|
|
4
|
+
import type { AddonManifest, AddonPermission, MountPoint } from "./contracts"
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
MOUNT_POINTS,
|
|
8
|
+
type AddonManifest,
|
|
9
|
+
type AddonPermission,
|
|
10
|
+
type AddonPermissionDefinition,
|
|
11
|
+
type ConfigSchemaField,
|
|
12
|
+
type MountPoint
|
|
13
|
+
} from "./contracts"
|
|
14
|
+
|
|
15
|
+
export type AddonJsonBody = Record<string, unknown> | unknown[] | null
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @jchat/addon-sdk
|
|
19
|
+
*
|
|
20
|
+
* Lightweight SDK for Jchat addon developers. Provides TypeScript types for addon props, manifest
|
|
21
|
+
* schema, and host API access. This package is always EXTERNALISED from addon bundles — the host
|
|
22
|
+
* app provides the runtime implementation.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** Minimal user profile accessible to addon backends. */
|
|
26
|
+
export interface AddonUser {
|
|
27
|
+
/** Unique user identifier */
|
|
28
|
+
id: string
|
|
29
|
+
/** Display name */
|
|
30
|
+
name: string
|
|
31
|
+
/** Full display name */
|
|
32
|
+
displayName: string
|
|
33
|
+
/** Full URL to the user's avatar image */
|
|
34
|
+
avatar: string
|
|
35
|
+
/** User's gender identifier */
|
|
36
|
+
gender: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AddonRequestUser {
|
|
40
|
+
id?: string
|
|
41
|
+
permissions?: string[]
|
|
42
|
+
siteId?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Typed, schema-scoped database facade for an addon. The host deliberately omits raw execution,
|
|
47
|
+
* schema switching, dialect/connection access, and lifecycle methods.
|
|
48
|
+
*/
|
|
49
|
+
export type AddonDatabase<TDatabase = Record<string, Record<string, unknown>>> = Pick<
|
|
50
|
+
Kysely<TDatabase>,
|
|
51
|
+
"deleteFrom" | "fn" | "insertInto" | "selectFrom" | "transaction" | "updateTable"
|
|
52
|
+
>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Context provided to an addon's server-side factory. Parameterize it with the addon's table
|
|
56
|
+
* interface: `AddonServerContext<MyAddonDatabase>`.
|
|
57
|
+
*/
|
|
58
|
+
export interface AddonServerContext<TDatabase = Record<string, Record<string, unknown>>> {
|
|
59
|
+
/**
|
|
60
|
+
* Retrieve the encrypted settings for this addon for a specific site. Internal secrets are
|
|
61
|
+
* automatically decrypted using the host's master key.
|
|
62
|
+
*/
|
|
63
|
+
getConfig: (siteId: string) => Promise<Record<string, unknown>>
|
|
64
|
+
|
|
65
|
+
/** Fetch a specific user's public profile */
|
|
66
|
+
getUser: (userId: string) => Promise<AddonUser | null>
|
|
67
|
+
|
|
68
|
+
/** Efficiently fetch multiple user profiles in a single query. Returns a Map keyed by user ID. */
|
|
69
|
+
getUsers: (userIds: string[]) => Promise<Map<string, AddonUser>>
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check whether the request user holds a given permission. Provides autocomplete for core Jchat
|
|
73
|
+
* permissions while also accepting any custom addon permission string.
|
|
74
|
+
*
|
|
75
|
+
* @param user - The user object from the request context
|
|
76
|
+
* @param permission - The permission to check, e.g., 'message.send' or 'addon.stories.view'
|
|
77
|
+
*/
|
|
78
|
+
hasPermission: (user: AddonRequestUser | null | undefined, permission: AddonPermission) => boolean
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A Kysely database instance scoped entirely to your addon's tables. You cannot access core Jchat
|
|
82
|
+
* tables (Users, Rooms, etc.) through this instance.
|
|
83
|
+
*/
|
|
84
|
+
db: AddonDatabase<TDatabase>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The entry point for your addon's server bundle. You should default-export a function matching
|
|
89
|
+
* this signature.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ;```ts
|
|
93
|
+
* import { type AddonServerFactory } from '@jchat/addon-sdk'
|
|
94
|
+
*
|
|
95
|
+
* const factory: AddonServerFactory = ({ db, hasPermission }, app) => {
|
|
96
|
+
* return app.get('/trending', async () => { ... })
|
|
97
|
+
* }
|
|
98
|
+
* export default factory
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export type AddonServerFactory<TDatabase = Record<string, Record<string, unknown>>> = (
|
|
102
|
+
ctx: AddonServerContext<TDatabase>,
|
|
103
|
+
app: Elysia
|
|
104
|
+
) => Elysia
|
|
105
|
+
|
|
106
|
+
/** Props passed to your addon's root React component by the Jchat host. */
|
|
107
|
+
export interface AddonProps {
|
|
108
|
+
/** The mount point where this addon instance is rendered (e.g., 'chat-sidebar') */
|
|
109
|
+
mountPoint: MountPoint
|
|
110
|
+
/** Information about the currently logged-in user (if any) */
|
|
111
|
+
user?: {
|
|
112
|
+
id: string
|
|
113
|
+
name: string
|
|
114
|
+
displayName?: string
|
|
115
|
+
avatar?: string
|
|
116
|
+
gender?: string
|
|
117
|
+
}
|
|
118
|
+
/** The ID of the room the user is currently viewing (if applicable) */
|
|
119
|
+
roomId?: string
|
|
120
|
+
/** The ID of the active Jchat site */
|
|
121
|
+
siteId?: string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface AddonApi {
|
|
125
|
+
get: <TResponse = unknown>(path: string) => Promise<TResponse>
|
|
126
|
+
post: <TResponse = unknown>(path: string, body?: AddonJsonBody) => Promise<TResponse>
|
|
127
|
+
put: <TResponse = unknown>(path: string, body?: AddonJsonBody) => Promise<TResponse>
|
|
128
|
+
delete: <TResponse = unknown>(path: string) => Promise<TResponse>
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type AddonEventListener = (data: unknown) => void
|
|
132
|
+
|
|
133
|
+
/** A font option provided by the JChat host. */
|
|
134
|
+
export interface AddonFont {
|
|
135
|
+
/** Display label shown to users (e.g. 'Modern', 'Elegant') */
|
|
136
|
+
label: string
|
|
137
|
+
/** Font identifier stored in the database (e.g. 'poppins', 'lato') */
|
|
138
|
+
value: string
|
|
139
|
+
/** CSS font-family string ready for use in inline styles */
|
|
140
|
+
family: string
|
|
141
|
+
/** Google Fonts name for loading the font via a <link> tag */
|
|
142
|
+
googleName: string
|
|
143
|
+
/** Relative size multiplier — apply this to base font sizes to match the host's rendering */
|
|
144
|
+
scale: number
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The bridge between your addon and the Jchat host application. Use the `useAddonSDK()` hook to
|
|
149
|
+
* access this interface in your components.
|
|
150
|
+
*/
|
|
151
|
+
export interface AddonSDK {
|
|
152
|
+
/**
|
|
153
|
+
* Make authenticated API calls to your addon's backend or core Jchat endpoints. All calls
|
|
154
|
+
* automatically include the current user's sessionId and site validation headers.
|
|
155
|
+
*/
|
|
156
|
+
api: AddonApi
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Display a temporary notification toast in the host UI.
|
|
160
|
+
*
|
|
161
|
+
* @param message - The text content to display
|
|
162
|
+
* @param variant - Visual style: 'default' (blue), 'success' (green), or 'error' (red)
|
|
163
|
+
*/
|
|
164
|
+
toast: (message: string, variant?: "default" | "success" | "error") => void
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Navigate the host application to a different internal route.
|
|
168
|
+
*
|
|
169
|
+
* @param path - The target location, e.g., '/settings' or '/addons/store'
|
|
170
|
+
*/
|
|
171
|
+
navigate: (path: string) => void
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Interaction utilities for the active Slate message editor. Only available if your addon is
|
|
175
|
+
* mounted near the chat input.
|
|
176
|
+
*/
|
|
177
|
+
editor: {
|
|
178
|
+
/**
|
|
179
|
+
* Trigger a specialized editor command defined by the host.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* sdk.editor.dispatchCommand("INSERT_YOUTUBE_COMMAND", "dQw4w9WgXcQ")
|
|
183
|
+
*/
|
|
184
|
+
dispatchCommand: (command: string, payload?: unknown) => void
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Insert a low-level Slate node directly at the user's cursor position.
|
|
188
|
+
*
|
|
189
|
+
* @param node - A valid Slate Descendant or Fragment object
|
|
190
|
+
*/
|
|
191
|
+
insertNode: (node: unknown) => void
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Upload and attach a file to the current message without inserting it inline. Users will see
|
|
195
|
+
* the file in the attachment bar below the editor.
|
|
196
|
+
*/
|
|
197
|
+
addAttachment: (attachment: { url: string; name: string; type: string }) => void
|
|
198
|
+
|
|
199
|
+
/** Force the browser focus back to the editor input */
|
|
200
|
+
focus: () => void
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Event Bus for reacting to real-time application state changes. */
|
|
204
|
+
events: {
|
|
205
|
+
/**
|
|
206
|
+
* Register a persistent listener for a specific event.
|
|
207
|
+
*
|
|
208
|
+
* @param event - Event name, e.g., 'room.message', 'user.typing'
|
|
209
|
+
*/
|
|
210
|
+
on: (event: string, callback: AddonEventListener) => void
|
|
211
|
+
|
|
212
|
+
/** Unregister a previously attached listener */
|
|
213
|
+
off: (event: string, callback: AddonEventListener) => void
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The list of fonts available in this JChat deployment, as configured by the site administrator.
|
|
218
|
+
* Use this to build a font picker or apply a matching font family in your addon UI.
|
|
219
|
+
*/
|
|
220
|
+
supportedFonts?: AddonFont[]
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Seed data for your addon's tables, provided inside the addon's isolated schema.
|
|
225
|
+
*
|
|
226
|
+
* These documents are inserted into the addon's dedicated schema on installation and can be used to
|
|
227
|
+
* provide default values, references, or supporting data. The host guarantees that these documents
|
|
228
|
+
* do not conflict with core schemas or other addons.
|
|
229
|
+
*/
|
|
230
|
+
export interface AddonSeedDocument {
|
|
231
|
+
/** Addon-owned table name. Core prefixes the addon's isolated PostgreSQL schema. */
|
|
232
|
+
table: string
|
|
233
|
+
/** Behavior when a row conflicts with an existing unique or primary key. */
|
|
234
|
+
conflict?: "error" | "ignore" | "update"
|
|
235
|
+
/** Required for update behavior; identifies the unique key used by ON CONFLICT. */
|
|
236
|
+
conflictColumns?: string[]
|
|
237
|
+
/** JSON-compatible rows inserted using parameterized values. */
|
|
238
|
+
rows: Record<string, unknown>[]
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Define a manifest with full validation-friendly typing while preserving literal values. */
|
|
242
|
+
export function defineAddonManifest<const TManifest extends AddonManifest>(
|
|
243
|
+
manifest: TManifest
|
|
244
|
+
): TManifest {
|
|
245
|
+
return manifest
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Define a typed server factory without widening its addon database interface. */
|
|
249
|
+
export function defineAddonServer<TDatabase = Record<string, Record<string, unknown>>>(
|
|
250
|
+
factory: AddonServerFactory<TDatabase>
|
|
251
|
+
): AddonServerFactory<TDatabase> {
|
|
252
|
+
return factory
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Access the host application's SDK. Provides authenticated API access, toast notifications, and
|
|
257
|
+
* navigation.
|
|
258
|
+
*
|
|
259
|
+
* This function is a shell; the real implementation is provided by the Jchat host environment at
|
|
260
|
+
* runtime.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ;```tsx
|
|
264
|
+
* import { useAddonSDK } from "@jchat/addon-sdk"
|
|
265
|
+
*
|
|
266
|
+
* export default function MyAddon({ mountPoint }: AddonProps) {
|
|
267
|
+
* const sdk = useAddonSDK()
|
|
268
|
+
*
|
|
269
|
+
* const handleClick = async () => {
|
|
270
|
+
* const data = await sdk.api.get("/trending")
|
|
271
|
+
* sdk.toast("Fetched trending posts!", "success")
|
|
272
|
+
* }
|
|
273
|
+
* }
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
export function useAddonSDK(): AddonSDK {
|
|
277
|
+
throw new Error("AddonSDK must be used within the Jchat host environment.")
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Access the host application's active icon pack. Returns the icon set matching the site's
|
|
282
|
+
* configured icon pack preference.
|
|
283
|
+
*
|
|
284
|
+
* This function is a shell; the real implementation is provided by the Jchat host environment at
|
|
285
|
+
* runtime.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ;```tsx
|
|
289
|
+
* import { useIcons } from "@jchat/addon-sdk"
|
|
290
|
+
*
|
|
291
|
+
* export default function MyAddon() {
|
|
292
|
+
* const { Icons } = useIcons()
|
|
293
|
+
* return <Icons.Send className="size-5" />
|
|
294
|
+
* }
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
export function useIcons(): { Icons: IconPack } {
|
|
298
|
+
throw new Error("useIcons must be used within the Jchat host environment.")
|
|
299
|
+
}
|
package/src/vite.test.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import crypto from "node:crypto"
|
|
3
|
+
import { createAddonServerBuildArguments, requireAddonSigningKey, signAddonManifest } from "./vite"
|
|
4
|
+
|
|
5
|
+
describe("addon package signing", () => {
|
|
6
|
+
test("builds a self-contained Bun ESM server artifact", () => {
|
|
7
|
+
expect(createAddonServerBuildArguments("src/api.ts", "build/api.js", false)).toEqual([
|
|
8
|
+
"build",
|
|
9
|
+
"src/api.ts",
|
|
10
|
+
"--target=bun",
|
|
11
|
+
"--format=esm",
|
|
12
|
+
"--outfile=build/api.js",
|
|
13
|
+
"--minify"
|
|
14
|
+
])
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test("permits unsigned database artifacts only in explicit development builds", () => {
|
|
18
|
+
expect(requireAddonSigningKey(undefined, true)).toBeUndefined()
|
|
19
|
+
expect(() => requireAddonSigningKey(undefined, false)).toThrow(
|
|
20
|
+
"require JCHAT_ADDON_SIGNING_PRIVATE_KEY"
|
|
21
|
+
)
|
|
22
|
+
expect(requireAddonSigningKey("private-key", false)).toBe("private-key")
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test("signs the canonical package digest", () => {
|
|
26
|
+
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
|
|
27
|
+
modulusLength: 2048
|
|
28
|
+
})
|
|
29
|
+
const signed = signAddonManifest(
|
|
30
|
+
{
|
|
31
|
+
id: "sample",
|
|
32
|
+
name: "Sample",
|
|
33
|
+
version: "1.0.0",
|
|
34
|
+
compatibility: { jchat: "^1.0.0", addonSdk: "^1.0.0" },
|
|
35
|
+
publisher: { id: "test" },
|
|
36
|
+
server: {
|
|
37
|
+
bundle: "api.ts",
|
|
38
|
+
migrations: ["migrations/001.sql"],
|
|
39
|
+
seeds: ["seeds/items.json"]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
[
|
|
43
|
+
{ path: "manifest.json", contents: Buffer.from("") },
|
|
44
|
+
{ path: "migrations/001.sql", contents: Buffer.from("safe") },
|
|
45
|
+
{
|
|
46
|
+
path: "seeds/items.json",
|
|
47
|
+
contents: Buffer.from('{"table":"Items","rows":[]}')
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
privateKey.export({ format: "pem", type: "pkcs8" }).toString()
|
|
51
|
+
)
|
|
52
|
+
expect(signed.signature?.digest).toHaveLength(64)
|
|
53
|
+
expect(
|
|
54
|
+
crypto.verify(
|
|
55
|
+
"sha256",
|
|
56
|
+
Buffer.from(signed.signature?.digest ?? ""),
|
|
57
|
+
publicKey,
|
|
58
|
+
Buffer.from(signed.signature?.value ?? "", "base64url")
|
|
59
|
+
)
|
|
60
|
+
).toBe(true)
|
|
61
|
+
})
|
|
62
|
+
})
|