Skip to content

Commit b16289e

Browse files
committed
feat: introduce client and server entrypoints for callback handling
- Added `createCallback` for client-side usage, allowing encrypted callback data to be sent via URL hash or query parameters. - Introduced `createServerCallback` for server-side usage, ensuring safe parsing and URL generation without browser dependencies. - Updated `package.json` to include new exports for client and server entrypoints. - Enhanced README with usage examples for both client and server implementations. - Refactored build scripts to support type generation and JavaScript bundling. - Updated dependencies and lock files to ensure compatibility with the new structure.
1 parent 28cb038 commit b16289e

19 files changed

Lines changed: 7589 additions & 587 deletions

README.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,17 @@ pnpm add @unraid/shared-callbacks
1818
## Usage
1919

2020
```typescript
21-
import { createCallbackStore, CallbackActionsStore } from '@unraid/shared-callbacks';
22-
23-
// Define your callback actions store
24-
const useCallbackActions = (): CallbackActionsStore => ({
25-
saveCallbackData: (decryptedData) => {
26-
// Handle the decrypted callback data
27-
console.log(decryptedData);
28-
},
21+
import { createCallback } from '@unraid/shared-callbacks';
22+
23+
const callback = createCallback({
2924
encryptionKey: 'your-encryption-key',
30-
sendType: 'forUpc'
25+
// Optional: when true (default), encrypted data is stored in the hash
26+
// instead of the `data` query parameter.
27+
useHash: true,
3128
});
3229

33-
// Create the callback store
34-
const callbackStore = createCallbackStore(useCallbackActions);
35-
36-
// Use the store to send callbacks
37-
callbackStore.send('https://example.com/callback', [
30+
// Send encrypted callbacks (client-only)
31+
callback.send('https://example.com/callback', [
3832
{
3933
type: 'signIn',
4034
apiKey: 'your-api-key',
@@ -44,8 +38,8 @@ callbackStore.send('https://example.com/callback', [
4438
}
4539
]);
4640

47-
// Watch for incoming callbacks
48-
callbackStore.watcher();
41+
// Watch for incoming callbacks (client-only)
42+
const decrypted = callback.watcher();
4943
```
5044

5145
## API
@@ -82,7 +76,7 @@ interface CallbackActionsStore {
8276

8377
By default, encrypted callback data is now placed in the URL hash (fragment) rather than a query parameter to help prevent sensitive data from being sent in referrers.
8478

85-
You can control this behavior via the `CallbackConfig` passed to `useCallback`:
79+
You can control this behavior via the `CallbackConfig` passed to `createCallback`:
8680

8781
```ts
8882
interface CallbackConfig {
@@ -96,3 +90,26 @@ interface CallbackConfig {
9690
```
9791

9892
Parsing helpers (`parse`, `watcher`) support both formats and will read encrypted data from either the hash or the `data` query parameter.
93+
94+
### Server / client entrypoints
95+
96+
To make SSR / Workers usage explicit and safe, the package also exposes split entrypoints:
97+
98+
- `@unraid/shared-callbacks/client` – exports `createCallback` (send, watcher, parse, generateUrl) and all types. This is intended for browser/client-only code.
99+
- `@unraid/shared-callbacks/client` – exports `createCallback` (send, watcher, parse, generateUrl) and all types. This is intended for browser/client-only code.
100+
- `@unraid/shared-callbacks/server` – exports `createServerCallback`, which exposes only `parse` and `generateUrl` and never touches browser globals.
101+
102+
Example server usage:
103+
104+
```ts
105+
import {
106+
createServerCallback,
107+
type CallbackConfig,
108+
} from '@unraid/shared-callbacks/server';
109+
110+
const config: CallbackConfig = {
111+
encryptionKey: 'your-encryption-key',
112+
};
113+
114+
const { parse, generateUrl } = createServerCallback(config);
115+
```

dist/client.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { CallbackConfig, QueryPayloads, SendPayloads, WatcherOptions, SignIn, SignOut, OemSignOut, Troubleshoot, Recover, Replace, TrialExtend, TrialStart, Purchase, Redeem, Renew, Upgrade, UpdateOs, DowngradeOs, Manage, MyKeys, LinkKey, Activate, AccountActionTypes, AccountKeyActionTypes, PurchaseActionTypes, ServerActionTypes, ServerState, ServerData, UserInfo, ExternalSignIn, ExternalSignOut, ExternalKeyActions, ExternalUpdateOsAction, ServerPayload, ServerTroubleshoot, ExternalActions, UpcActions, ExternalPayload, UpcPayload } from "./types.js";
2+
export declare const createCallback: (config: CallbackConfig) => {
3+
send: (url: string, payload: SendPayloads, redirectType?: "newTab" | "replace" | null, sendType?: string, sender?: string) => void;
4+
parse: (data: string, options?: {
5+
isDataURIEncoded?: boolean;
6+
}) => QueryPayloads;
7+
watcher: (options?: WatcherOptions) => QueryPayloads | undefined;
8+
generateUrl: (url: string, payload: SendPayloads, sendType?: string, sender?: string) => string;
9+
};
10+
/**
11+
* Backwards-compatible alias for older consumers.
12+
* This no longer returns a shared singleton; it is a plain factory.
13+
*/
14+
export declare const useCallback: (config: CallbackConfig) => {
15+
send: (url: string, payload: SendPayloads, redirectType?: "newTab" | "replace" | null, sendType?: string, sender?: string) => void;
16+
parse: (data: string, options?: {
17+
isDataURIEncoded?: boolean;
18+
}) => QueryPayloads;
19+
watcher: (options?: WatcherOptions) => QueryPayloads | undefined;
20+
generateUrl: (url: string, payload: SendPayloads, sendType?: string, sender?: string) => string;
21+
};
22+
export type { CallbackConfig, QueryPayloads, SendPayloads, WatcherOptions, SignIn, SignOut, OemSignOut, Troubleshoot, Recover, Replace, TrialExtend, TrialStart, Purchase, Redeem, Renew, Upgrade, UpdateOs, DowngradeOs, Manage, MyKeys, LinkKey, Activate, AccountActionTypes, AccountKeyActionTypes, PurchaseActionTypes, ServerActionTypes, ServerState, ServerData, UserInfo, ExternalSignIn, ExternalSignOut, ExternalKeyActions, ExternalUpdateOsAction, ServerPayload, ServerTroubleshoot, ExternalActions, UpcActions, ExternalPayload, UpcPayload, };

0 commit comments

Comments
 (0)