@svadmin/pusher 0.0.1 → 0.2.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/package.json CHANGED
@@ -1,22 +1,48 @@
1
1
  {
2
2
  "name": "@svadmin/pusher",
3
- "version": "0.0.1",
4
- "description": "Placeholder for @svadmin/pusher: the package is published from CI with npm trusted publishing. Install a released version instead.",
3
+ "version": "0.2.1",
4
+ "description": "Pusher LiveProvider adapter for svadmin",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "src/**/*.ts",
9
+ "!src/**/*.test.*",
10
+ "!src/**/*.spec.*"
11
+ ],
12
+ "main": "src/index.ts",
13
+ "types": "src/index.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./src/index.ts",
17
+ "default": "./src/index.ts"
18
+ }
19
+ },
20
+ "peerDependencies": {
21
+ "@svadmin/core": "^0.76.0"
22
+ },
23
+ "keywords": [
24
+ "svadmin",
25
+ "pusher",
26
+ "live-provider",
27
+ "realtime"
28
+ ],
5
29
  "license": "MIT",
30
+ "author": "zuohuadong",
6
31
  "repository": {
7
32
  "type": "git",
8
33
  "url": "git+https://github.com/vibeunion/svadmin.git",
9
34
  "directory": "packages/pusher"
10
35
  },
11
- "homepage": "https://github.com/vibeunion/svadmin/tree/main/packages/pusher#readme",
36
+ "homepage": "https://github.com/vibeunion/svadmin#readme",
12
37
  "bugs": {
13
38
  "url": "https://github.com/vibeunion/svadmin/issues"
14
39
  },
15
- "publishConfig": {
16
- "access": "public"
17
- },
18
- "files": [
19
- "package.json",
20
- "README.md"
21
- ]
40
+ "svadmin": {
41
+ "capabilities": [
42
+ "live"
43
+ ],
44
+ "core": "^0.55.0",
45
+ "stability": "stable",
46
+ "migration": null
47
+ }
22
48
  }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { createPusherLiveProvider } from './pusher-live-provider';
2
+ export type {
3
+ PusherChannelLike,
4
+ PusherClientLike,
5
+ PusherLiveOptions,
6
+ } from './pusher-live-provider';
@@ -0,0 +1,89 @@
1
+ import type { LiveProvider, LiveEvent } from '@svadmin/core';
2
+
3
+ export interface PusherChannelLike {
4
+ bind: (eventName: string, callback: (data: unknown) => void) => void;
5
+ unbind: (eventName: string, callback?: (data: unknown) => void) => void;
6
+ unbind_all?: () => void;
7
+ }
8
+
9
+ export interface PusherClientLike {
10
+ subscribe: (channelName: string) => PusherChannelLike;
11
+ unsubscribe: (channelName: string) => void;
12
+ trigger?: (channelName: string, eventName: string, data: unknown) => void;
13
+ connection?: {
14
+ bind: (event: string, callback: () => void) => void;
15
+ unbind: (event: string, callback?: () => void) => void;
16
+ };
17
+ }
18
+
19
+ export interface PusherLiveOptions {
20
+ channelPrefix?: string;
21
+ }
22
+
23
+ type PusherEventType = 'created' | 'updated' | 'deleted';
24
+ const eventTypes: Record<PusherEventType, LiveEvent['type']> = {
25
+ created: 'INSERT',
26
+ updated: 'UPDATE',
27
+ deleted: 'DELETE',
28
+ };
29
+ const pusherEventTypes: Record<LiveEvent['type'], PusherEventType> = {
30
+ INSERT: 'created',
31
+ UPDATE: 'updated',
32
+ DELETE: 'deleted',
33
+ };
34
+
35
+ function toPayload(data: unknown): Record<string, unknown> {
36
+ if (data && typeof data === 'object' && !Array.isArray(data)) {
37
+ return data as Record<string, unknown>;
38
+ }
39
+ return { value: data };
40
+ }
41
+
42
+ export function createPusherLiveProvider(
43
+ pusher: PusherClientLike,
44
+ options: PusherLiveOptions = {},
45
+ ): LiveProvider {
46
+ const prefix = options.channelPrefix ?? 'svadmin';
47
+
48
+ return {
49
+ subscribe({ resource, callback }): () => void {
50
+ const channelName = `${prefix}-${resource}`;
51
+ const channel = pusher.subscribe(channelName);
52
+
53
+ const handler = (type: PusherEventType) => (data: unknown) => {
54
+ const event: LiveEvent = {
55
+ type: eventTypes[type],
56
+ resource,
57
+ payload: toPayload(data),
58
+ };
59
+ callback(event);
60
+ };
61
+
62
+ const handleCreated = handler('created');
63
+ const handleUpdated = handler('updated');
64
+ const handleDeleted = handler('deleted');
65
+
66
+ channel.bind('created', handleCreated);
67
+ channel.bind('updated', handleUpdated);
68
+ channel.bind('deleted', handleDeleted);
69
+
70
+ return () => {
71
+ channel.unbind('created', handleCreated);
72
+ channel.unbind('updated', handleUpdated);
73
+ channel.unbind('deleted', handleDeleted);
74
+ pusher.unsubscribe(channelName);
75
+ };
76
+ },
77
+
78
+ unsubscribe({ resource }): void {
79
+ const channelName = `${prefix}-${resource}`;
80
+ pusher.unsubscribe(channelName);
81
+ },
82
+
83
+ publish(event: LiveEvent): void {
84
+ if (!pusher.trigger) return;
85
+ const channelName = `${prefix}-${event.resource}`;
86
+ pusher.trigger(channelName, pusherEventTypes[event.type], event.payload);
87
+ },
88
+ };
89
+ }
package/README.md DELETED
@@ -1,6 +0,0 @@
1
- # @svadmin/pusher
2
-
3
- This version is a placeholder published only to register the package name and
4
- enable npm trusted publishing from CI. It contains no source code.
5
-
6
- Install a released version (`>= 0.2.1`) instead.