@wix/realtime 0.1.9 → 1.0.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/build/cjs/index.d.ts +1 -0
- package/{cjs/build → build/cjs}/index.js +1 -1
- package/build/es/index.d.ts +1 -0
- package/build/es/index.js +37 -0
- package/build/tsconfig.cjs.tsbuildinfo +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +48 -45
- package/README.md +0 -2
- package/build/index.d.ts +0 -1
- package/build/index.js +0 -1
- package/build/site/index.d.ts +0 -5
- package/build/site/index.js +0 -10
- package/build/site/permissions-router.d.ts +0 -9
- package/build/site/permissions-router.js +0 -36
- package/build/site/publisher.d.ts +0 -6
- package/build/site/publisher.js +0 -51
- package/build/site/request.d.ts +0 -8
- package/build/site/request.js +0 -18
- package/build/site/types.d.ts +0 -36
- package/build/site/types.js +0 -5
- package/cjs/build/index.d.ts +0 -1
- package/cjs/build/site/index.d.ts +0 -5
- package/cjs/build/site/index.js +0 -14
- package/cjs/build/site/permissions-router.d.ts +0 -9
- package/cjs/build/site/permissions-router.js +0 -40
- package/cjs/build/site/publisher.d.ts +0 -6
- package/cjs/build/site/publisher.js +0 -54
- package/cjs/build/site/request.d.ts +0 -8
- package/cjs/build/site/request.js +0 -21
- package/cjs/build/site/types.d.ts +0 -36
- package/cjs/build/site/types.js +0 -8
- package/cjs/package.json +0 -3
- package/cjs/tsconfig.cjs.tsbuildinfo +0 -1
- package/docs/docs-ts-model.json +0 -305
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as site from '@wix/sdk_realtime_site';
|
|
@@ -34,4 +34,4 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.site = void 0;
|
|
37
|
-
exports.site = __importStar(require("
|
|
37
|
+
exports.site = __importStar(require("@wix/sdk_realtime_site"));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as site from '@wix/sdk_realtime_site';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.site = void 0;
|
|
37
|
+
exports.site = __importStar(require("@wix/sdk_realtime_site"));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/index.ts"],"version":"5.7.3"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/index.ts"],"version":"5.7.3"}
|
package/package.json
CHANGED
|
@@ -1,62 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/realtime",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"registry": "https://registry.npmjs.org/",
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"wixSdkBackendModule",
|
|
10
|
+
"wixSdkPageModule",
|
|
11
|
+
"wixSdkPublicModule"
|
|
12
|
+
],
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"main": "./build/cjs/index.js",
|
|
15
|
+
"types": "./build/es/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./build/es/index.js",
|
|
19
|
+
"require": "./build/cjs/index.js",
|
|
20
|
+
"types": "./build/es/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
"./meta": {
|
|
24
|
+
"import": "./build/es/meta.js",
|
|
25
|
+
"require": "./build/cjs/meta.js",
|
|
26
|
+
"types": "./build/es/meta.d.ts"
|
|
27
|
+
},
|
|
28
|
+
"./service-plugins": {
|
|
29
|
+
"import": "./build/es/service-plugins.js",
|
|
30
|
+
"require": "./build/cjs/service-plugins.js",
|
|
31
|
+
"types": "./build/es/service-plugins.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./dashboard": {
|
|
34
|
+
"import": "./build/es/dashboard.js",
|
|
35
|
+
"require": "./build/cjs/dashboard.js",
|
|
36
|
+
"types": "./build/es/dashboard.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
8
39
|
"files": [
|
|
9
40
|
"build",
|
|
10
|
-
"cjs"
|
|
11
|
-
"docs/docs-ts-model.json"
|
|
41
|
+
"cjs"
|
|
12
42
|
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "tsc -b tsconfig.json tsconfig.cjs.json",
|
|
15
|
-
"docs": "docs-ts-model --config ./docs/cli-config.json",
|
|
16
|
-
"test": "tsx --test test/*.spec.ts",
|
|
17
|
-
"lint": "eslint",
|
|
18
|
-
"lint:fix": "eslint --max-warnings=0 . --fix",
|
|
19
|
-
"typecheck": "tsc --noEmit"
|
|
20
|
-
},
|
|
21
43
|
"dependencies": {
|
|
22
|
-
"@wix/
|
|
44
|
+
"@wix/sdk_realtime_site": "1.0.1"
|
|
23
45
|
},
|
|
24
46
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"@types/chance": "^1",
|
|
27
|
-
"@types/express": "^5.0.0",
|
|
28
|
-
"@types/node": "^20.14.2",
|
|
29
|
-
"@wix/docs-ts-model": "^1.35.0",
|
|
30
|
-
"@wix/sdk": "^1.10.0",
|
|
31
|
-
"@wix/sdk-types": "^1.8.0",
|
|
32
|
-
"chance": "^1.1.11",
|
|
33
|
-
"eslint": "9.x",
|
|
34
|
-
"eslint-plugin-prettier": "^5.1.3",
|
|
35
|
-
"fetch-mock": "^9.11.0",
|
|
36
|
-
"globals": "^15.4.0",
|
|
37
|
-
"node-fetch": "^3.3.2",
|
|
38
|
-
"prettier": "^3.3.1",
|
|
39
|
-
"tsx": "^4.15.1",
|
|
40
|
-
"typescript": "^5.7.3",
|
|
41
|
-
"typescript-eslint": "^8.24.1"
|
|
47
|
+
"typescript": "^5.7.3"
|
|
42
48
|
},
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -b tsconfig.json tsconfig.cjs.json",
|
|
51
|
+
"test": ":"
|
|
46
52
|
},
|
|
47
53
|
"wix": {
|
|
48
54
|
"artifact": {
|
|
49
55
|
"groupId": "com.wixpress.sdk",
|
|
50
|
-
"artifactId": "realtime"
|
|
56
|
+
"artifactId": "realtime",
|
|
57
|
+
"isRootPublicSdk": true
|
|
51
58
|
},
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
],
|
|
56
|
-
"postDependenciesBuild": [
|
|
57
|
-
"typecheck"
|
|
58
|
-
]
|
|
59
|
+
"sdkDependency": {
|
|
60
|
+
"fqdnNamespace": "",
|
|
61
|
+
"fqdn": ""
|
|
59
62
|
}
|
|
60
63
|
},
|
|
61
|
-
"falconPackageHash": "
|
|
64
|
+
"falconPackageHash": "66890f8e2cfea64578d11d9b5bd68fc4a1cffff77c29c7d69bcb144a"
|
|
62
65
|
}
|
package/README.md
DELETED
package/build/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as site from './site/index.js';
|
package/build/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as site from './site/index.js';
|
package/build/site/index.d.ts
DELETED
package/build/site/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createRESTModule } from '@wix/sdk-runtime/rest-modules';
|
|
2
|
-
import { createPublisher } from './publisher.js';
|
|
3
|
-
import { PermissionsRouter } from './permissions-router.js';
|
|
4
|
-
const permissionsRouter = new PermissionsRouter();
|
|
5
|
-
const publish = createRESTModule((httpClient) => createPublisher({
|
|
6
|
-
httpClient,
|
|
7
|
-
apiUrl: process.env.DUPLEXER_API_URL ||
|
|
8
|
-
'https://manage.wix.com/_api/wix-duplexer-api',
|
|
9
|
-
}), true);
|
|
10
|
-
export { permissionsRouter, publish };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Channel, PermissionHandler, Subscriber } from './types.js';
|
|
2
|
-
export declare class PermissionsRouter {
|
|
3
|
-
private readonly routes;
|
|
4
|
-
private defaultHandler;
|
|
5
|
-
constructor();
|
|
6
|
-
add(channel: Channel, permissionHandler: PermissionHandler): void;
|
|
7
|
-
default(permissionHandler: PermissionHandler): void;
|
|
8
|
-
check(channel: Channel, subscriber: Subscriber): Promise<import("./types.js").Permissions>;
|
|
9
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export class PermissionsRouter {
|
|
2
|
-
routes;
|
|
3
|
-
defaultHandler;
|
|
4
|
-
constructor() {
|
|
5
|
-
this.routes = new Map();
|
|
6
|
-
this.defaultHandler = () => Promise.resolve({
|
|
7
|
-
read: true,
|
|
8
|
-
write: false,
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
add(channel, permissionHandler) {
|
|
12
|
-
if (!this.routes.has(channel.name)) {
|
|
13
|
-
this.routes.set(channel.name, new Map());
|
|
14
|
-
}
|
|
15
|
-
const resourceId = typeof channel.resourceId === 'string' ? channel.resourceId : '';
|
|
16
|
-
this.routes.get(channel.name)?.set(resourceId, permissionHandler);
|
|
17
|
-
}
|
|
18
|
-
default(permissionHandler) {
|
|
19
|
-
this.defaultHandler = permissionHandler;
|
|
20
|
-
}
|
|
21
|
-
check(channel, subscriber) {
|
|
22
|
-
if (this.routes.has(channel.name)) {
|
|
23
|
-
const channelNameRoutes = this.routes.get(channel.name);
|
|
24
|
-
if (typeof channel.resourceId === 'string' &&
|
|
25
|
-
channelNameRoutes?.has(channel.resourceId)) {
|
|
26
|
-
const handler = channelNameRoutes.get(channel.resourceId);
|
|
27
|
-
return handler(channel, subscriber);
|
|
28
|
-
}
|
|
29
|
-
if (channelNameRoutes?.has('')) {
|
|
30
|
-
const handler = channelNameRoutes.get('');
|
|
31
|
-
return handler(channel, subscriber);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return this.defaultHandler(channel, subscriber);
|
|
35
|
-
}
|
|
36
|
-
}
|
package/build/site/publisher.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { getCurrentRequest } from './request.js';
|
|
2
|
-
import { transformError } from '@wix/sdk-runtime/transform-error';
|
|
3
|
-
export function createPublisher({ apiUrl, httpClient, }) {
|
|
4
|
-
return async function (channel, payload, { users: recipients, includePublisher } = {}) {
|
|
5
|
-
const isAddRevisionToChannelNameInRealtimeBackendEnabled = await shouldAddRevisionToChannelNameInRealtimeBackend();
|
|
6
|
-
const { userId, visitorId, viewMode, siteRevision } = getCurrentRequest();
|
|
7
|
-
const publisher = includePublisher
|
|
8
|
-
? { uid: userId || visitorId }
|
|
9
|
-
: undefined;
|
|
10
|
-
let channels;
|
|
11
|
-
if (viewMode?.toLowerCase() !== 'site') {
|
|
12
|
-
channels = [`@preview-${channel.name}`];
|
|
13
|
-
}
|
|
14
|
-
else if (isAddRevisionToChannelNameInRealtimeBackendEnabled) {
|
|
15
|
-
channels = [`${siteRevision}-${channel.name}`];
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
channels = [channel.name];
|
|
19
|
-
}
|
|
20
|
-
try {
|
|
21
|
-
await httpClient.fetchWithAuth(`${apiUrl}/v3/publish`, {
|
|
22
|
-
method: 'POST',
|
|
23
|
-
headers: {
|
|
24
|
-
'Content-Type': 'application/json',
|
|
25
|
-
},
|
|
26
|
-
body: JSON.stringify({
|
|
27
|
-
channels,
|
|
28
|
-
resourceId: channel.resourceId,
|
|
29
|
-
event: 'message',
|
|
30
|
-
payload: { payload },
|
|
31
|
-
publisher,
|
|
32
|
-
recipients,
|
|
33
|
-
}),
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
catch (err) {
|
|
37
|
-
const transformedError = transformError(err);
|
|
38
|
-
throw transformedError;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
const shouldAddRevisionToChannelNameInRealtimeBackend = async () => {
|
|
43
|
-
// const currentRequest = getCurrentRequest();
|
|
44
|
-
// const petriClient = getPetriClient({ url: currentRequest.petriUrl });
|
|
45
|
-
// const result = await petriClient.conductExperiment(
|
|
46
|
-
// 'specs.core-services.AddRevisionToChannelNameInRealtime',
|
|
47
|
-
// 'false',
|
|
48
|
-
// );
|
|
49
|
-
// return result.toString() === 'true';
|
|
50
|
-
return false;
|
|
51
|
-
};
|
package/build/site/request.d.ts
DELETED
package/build/site/request.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export function getCurrentRequest() {
|
|
2
|
-
const maybeRequest = extractRequestFromContext();
|
|
3
|
-
const headers = maybeRequest?.headers ?? {};
|
|
4
|
-
return {
|
|
5
|
-
serverIdentity: headers['x-wix-server-identity'],
|
|
6
|
-
userId: headers['x-wix-user-id'],
|
|
7
|
-
visitorId: headers['x-wix-aid'],
|
|
8
|
-
viewMode: headers['x-wix-view-mode'],
|
|
9
|
-
siteRevision: headers['x-wix-site-revision'],
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function extractRequestFromContext() {
|
|
13
|
-
if (global.__requestContext?.getStore()) {
|
|
14
|
-
return global.__requestContext?.getStore()?.request;
|
|
15
|
-
}
|
|
16
|
-
// @ts-expect-error domain is a custom property
|
|
17
|
-
return process.domain?.webMethodInfo?.request;
|
|
18
|
-
}
|
package/build/site/types.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Request as ExpressRequest } from 'express';
|
|
2
|
-
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
export type Request = ExpressRequest;
|
|
4
|
-
declare global {
|
|
5
|
-
var __requestContext: AsyncLocalStorage<{
|
|
6
|
-
request: Request;
|
|
7
|
-
}> | undefined;
|
|
8
|
-
}
|
|
9
|
-
export declare enum ViewMode {
|
|
10
|
-
Preview = "Preview",
|
|
11
|
-
Public = "Site"
|
|
12
|
-
}
|
|
13
|
-
export interface Channel {
|
|
14
|
-
name: string;
|
|
15
|
-
resourceId?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface PublishOptions {
|
|
18
|
-
includePublisher?: boolean;
|
|
19
|
-
users?: string[];
|
|
20
|
-
}
|
|
21
|
-
export type Publisher = (channel: Channel, payload: unknown, options?: PublishOptions) => Promise<unknown>;
|
|
22
|
-
export declare const enum SubscriberType {
|
|
23
|
-
USER = "USER",
|
|
24
|
-
SITEMEMBER = "MEMBER",
|
|
25
|
-
VISITOR = "VISITOR"
|
|
26
|
-
}
|
|
27
|
-
export interface Subscriber {
|
|
28
|
-
id: string;
|
|
29
|
-
type: SubscriberType;
|
|
30
|
-
}
|
|
31
|
-
export interface Permissions {
|
|
32
|
-
read: boolean;
|
|
33
|
-
write: boolean;
|
|
34
|
-
}
|
|
35
|
-
export type PermissionHandler = (channel: Channel, subscriber: Subscriber) => Promise<Permissions>;
|
|
36
|
-
export type Routes = Map<string, Map<string, PermissionHandler>>;
|
package/build/site/types.js
DELETED
package/cjs/build/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as site from './site/index.js';
|
package/cjs/build/site/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.publish = exports.permissionsRouter = void 0;
|
|
4
|
-
const rest_modules_1 = require("@wix/sdk-runtime/rest-modules");
|
|
5
|
-
const publisher_js_1 = require("./publisher.js");
|
|
6
|
-
const permissions_router_js_1 = require("./permissions-router.js");
|
|
7
|
-
const permissionsRouter = new permissions_router_js_1.PermissionsRouter();
|
|
8
|
-
exports.permissionsRouter = permissionsRouter;
|
|
9
|
-
const publish = (0, rest_modules_1.createRESTModule)((httpClient) => (0, publisher_js_1.createPublisher)({
|
|
10
|
-
httpClient,
|
|
11
|
-
apiUrl: process.env.DUPLEXER_API_URL ||
|
|
12
|
-
'https://manage.wix.com/_api/wix-duplexer-api',
|
|
13
|
-
}), true);
|
|
14
|
-
exports.publish = publish;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Channel, PermissionHandler, Subscriber } from './types.js';
|
|
2
|
-
export declare class PermissionsRouter {
|
|
3
|
-
private readonly routes;
|
|
4
|
-
private defaultHandler;
|
|
5
|
-
constructor();
|
|
6
|
-
add(channel: Channel, permissionHandler: PermissionHandler): void;
|
|
7
|
-
default(permissionHandler: PermissionHandler): void;
|
|
8
|
-
check(channel: Channel, subscriber: Subscriber): Promise<import("./types.js").Permissions>;
|
|
9
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PermissionsRouter = void 0;
|
|
4
|
-
class PermissionsRouter {
|
|
5
|
-
routes;
|
|
6
|
-
defaultHandler;
|
|
7
|
-
constructor() {
|
|
8
|
-
this.routes = new Map();
|
|
9
|
-
this.defaultHandler = () => Promise.resolve({
|
|
10
|
-
read: true,
|
|
11
|
-
write: false,
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
add(channel, permissionHandler) {
|
|
15
|
-
if (!this.routes.has(channel.name)) {
|
|
16
|
-
this.routes.set(channel.name, new Map());
|
|
17
|
-
}
|
|
18
|
-
const resourceId = typeof channel.resourceId === 'string' ? channel.resourceId : '';
|
|
19
|
-
this.routes.get(channel.name)?.set(resourceId, permissionHandler);
|
|
20
|
-
}
|
|
21
|
-
default(permissionHandler) {
|
|
22
|
-
this.defaultHandler = permissionHandler;
|
|
23
|
-
}
|
|
24
|
-
check(channel, subscriber) {
|
|
25
|
-
if (this.routes.has(channel.name)) {
|
|
26
|
-
const channelNameRoutes = this.routes.get(channel.name);
|
|
27
|
-
if (typeof channel.resourceId === 'string' &&
|
|
28
|
-
channelNameRoutes?.has(channel.resourceId)) {
|
|
29
|
-
const handler = channelNameRoutes.get(channel.resourceId);
|
|
30
|
-
return handler(channel, subscriber);
|
|
31
|
-
}
|
|
32
|
-
if (channelNameRoutes?.has('')) {
|
|
33
|
-
const handler = channelNameRoutes.get('');
|
|
34
|
-
return handler(channel, subscriber);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return this.defaultHandler(channel, subscriber);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.PermissionsRouter = PermissionsRouter;
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createPublisher = createPublisher;
|
|
4
|
-
const request_js_1 = require("./request.js");
|
|
5
|
-
const transform_error_1 = require("@wix/sdk-runtime/transform-error");
|
|
6
|
-
function createPublisher({ apiUrl, httpClient, }) {
|
|
7
|
-
return async function (channel, payload, { users: recipients, includePublisher } = {}) {
|
|
8
|
-
const isAddRevisionToChannelNameInRealtimeBackendEnabled = await shouldAddRevisionToChannelNameInRealtimeBackend();
|
|
9
|
-
const { userId, visitorId, viewMode, siteRevision } = (0, request_js_1.getCurrentRequest)();
|
|
10
|
-
const publisher = includePublisher
|
|
11
|
-
? { uid: userId || visitorId }
|
|
12
|
-
: undefined;
|
|
13
|
-
let channels;
|
|
14
|
-
if (viewMode?.toLowerCase() !== 'site') {
|
|
15
|
-
channels = [`@preview-${channel.name}`];
|
|
16
|
-
}
|
|
17
|
-
else if (isAddRevisionToChannelNameInRealtimeBackendEnabled) {
|
|
18
|
-
channels = [`${siteRevision}-${channel.name}`];
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
channels = [channel.name];
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
await httpClient.fetchWithAuth(`${apiUrl}/v3/publish`, {
|
|
25
|
-
method: 'POST',
|
|
26
|
-
headers: {
|
|
27
|
-
'Content-Type': 'application/json',
|
|
28
|
-
},
|
|
29
|
-
body: JSON.stringify({
|
|
30
|
-
channels,
|
|
31
|
-
resourceId: channel.resourceId,
|
|
32
|
-
event: 'message',
|
|
33
|
-
payload: { payload },
|
|
34
|
-
publisher,
|
|
35
|
-
recipients,
|
|
36
|
-
}),
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
const transformedError = (0, transform_error_1.transformError)(err);
|
|
41
|
-
throw transformedError;
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
const shouldAddRevisionToChannelNameInRealtimeBackend = async () => {
|
|
46
|
-
// const currentRequest = getCurrentRequest();
|
|
47
|
-
// const petriClient = getPetriClient({ url: currentRequest.petriUrl });
|
|
48
|
-
// const result = await petriClient.conductExperiment(
|
|
49
|
-
// 'specs.core-services.AddRevisionToChannelNameInRealtime',
|
|
50
|
-
// 'false',
|
|
51
|
-
// );
|
|
52
|
-
// return result.toString() === 'true';
|
|
53
|
-
return false;
|
|
54
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCurrentRequest = getCurrentRequest;
|
|
4
|
-
function getCurrentRequest() {
|
|
5
|
-
const maybeRequest = extractRequestFromContext();
|
|
6
|
-
const headers = maybeRequest?.headers ?? {};
|
|
7
|
-
return {
|
|
8
|
-
serverIdentity: headers['x-wix-server-identity'],
|
|
9
|
-
userId: headers['x-wix-user-id'],
|
|
10
|
-
visitorId: headers['x-wix-aid'],
|
|
11
|
-
viewMode: headers['x-wix-view-mode'],
|
|
12
|
-
siteRevision: headers['x-wix-site-revision'],
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function extractRequestFromContext() {
|
|
16
|
-
if (global.__requestContext?.getStore()) {
|
|
17
|
-
return global.__requestContext?.getStore()?.request;
|
|
18
|
-
}
|
|
19
|
-
// @ts-expect-error domain is a custom property
|
|
20
|
-
return process.domain?.webMethodInfo?.request;
|
|
21
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Request as ExpressRequest } from 'express';
|
|
2
|
-
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
export type Request = ExpressRequest;
|
|
4
|
-
declare global {
|
|
5
|
-
var __requestContext: AsyncLocalStorage<{
|
|
6
|
-
request: Request;
|
|
7
|
-
}> | undefined;
|
|
8
|
-
}
|
|
9
|
-
export declare enum ViewMode {
|
|
10
|
-
Preview = "Preview",
|
|
11
|
-
Public = "Site"
|
|
12
|
-
}
|
|
13
|
-
export interface Channel {
|
|
14
|
-
name: string;
|
|
15
|
-
resourceId?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface PublishOptions {
|
|
18
|
-
includePublisher?: boolean;
|
|
19
|
-
users?: string[];
|
|
20
|
-
}
|
|
21
|
-
export type Publisher = (channel: Channel, payload: unknown, options?: PublishOptions) => Promise<unknown>;
|
|
22
|
-
export declare const enum SubscriberType {
|
|
23
|
-
USER = "USER",
|
|
24
|
-
SITEMEMBER = "MEMBER",
|
|
25
|
-
VISITOR = "VISITOR"
|
|
26
|
-
}
|
|
27
|
-
export interface Subscriber {
|
|
28
|
-
id: string;
|
|
29
|
-
type: SubscriberType;
|
|
30
|
-
}
|
|
31
|
-
export interface Permissions {
|
|
32
|
-
read: boolean;
|
|
33
|
-
write: boolean;
|
|
34
|
-
}
|
|
35
|
-
export type PermissionHandler = (channel: Channel, subscriber: Subscriber) => Promise<Permissions>;
|
|
36
|
-
export type Routes = Map<string, Map<string, PermissionHandler>>;
|
package/cjs/build/site/types.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ViewMode = void 0;
|
|
4
|
-
var ViewMode;
|
|
5
|
-
(function (ViewMode) {
|
|
6
|
-
ViewMode["Preview"] = "Preview";
|
|
7
|
-
ViewMode["Public"] = "Site";
|
|
8
|
-
})(ViewMode || (exports.ViewMode = ViewMode = {}));
|
package/cjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/site/index.ts","../src/site/permissions-router.ts","../src/site/publisher.ts","../src/site/request.ts","../src/site/types.ts"],"version":"5.7.3"}
|
package/docs/docs-ts-model.json
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"typeDocModel": {
|
|
3
|
-
"id": 0,
|
|
4
|
-
"name": "@wix/realtime",
|
|
5
|
-
"variant": "project",
|
|
6
|
-
"kind": 1,
|
|
7
|
-
"flags": {},
|
|
8
|
-
"children": [
|
|
9
|
-
{
|
|
10
|
-
"id": 1,
|
|
11
|
-
"name": "site",
|
|
12
|
-
"variant": "declaration",
|
|
13
|
-
"kind": 4,
|
|
14
|
-
"flags": {},
|
|
15
|
-
"children": [
|
|
16
|
-
{
|
|
17
|
-
"id": 2,
|
|
18
|
-
"name": "permissionsRouter",
|
|
19
|
-
"variant": "declaration",
|
|
20
|
-
"kind": 32,
|
|
21
|
-
"flags": {
|
|
22
|
-
"isConst": true
|
|
23
|
-
},
|
|
24
|
-
"sources": [
|
|
25
|
-
{
|
|
26
|
-
"fileName": "src/site/index.ts",
|
|
27
|
-
"line": 6,
|
|
28
|
-
"character": 6,
|
|
29
|
-
"url": "https://github.com/wix-private/wix-sdk-realtime/blob/4be1697/packages/realtime/src/site/index.ts#L6"
|
|
30
|
-
}
|
|
31
|
-
],
|
|
32
|
-
"type": {
|
|
33
|
-
"type": "reference",
|
|
34
|
-
"target": {
|
|
35
|
-
"sourceFileName": "src/site/permissions-router.ts",
|
|
36
|
-
"qualifiedName": "PermissionsRouter"
|
|
37
|
-
},
|
|
38
|
-
"name": "PermissionsRouter",
|
|
39
|
-
"package": "@wix/realtime"
|
|
40
|
-
},
|
|
41
|
-
"defaultValue": "..."
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
"id": 3,
|
|
45
|
-
"name": "publish",
|
|
46
|
-
"variant": "declaration",
|
|
47
|
-
"kind": 64,
|
|
48
|
-
"flags": {},
|
|
49
|
-
"sources": [
|
|
50
|
-
{
|
|
51
|
-
"fileName": "src/site/index.ts",
|
|
52
|
-
"line": 7,
|
|
53
|
-
"character": 6,
|
|
54
|
-
"url": "https://github.com/wix-private/wix-sdk-realtime/blob/4be1697/packages/realtime/src/site/index.ts#L7"
|
|
55
|
-
}
|
|
56
|
-
],
|
|
57
|
-
"signatures": [
|
|
58
|
-
{
|
|
59
|
-
"id": 4,
|
|
60
|
-
"name": "publish",
|
|
61
|
-
"variant": "signature",
|
|
62
|
-
"kind": 4096,
|
|
63
|
-
"flags": {},
|
|
64
|
-
"sources": [
|
|
65
|
-
{
|
|
66
|
-
"fileName": "src/site/types.ts",
|
|
67
|
-
"line": 27,
|
|
68
|
-
"character": 24,
|
|
69
|
-
"url": "https://github.com/wix-private/wix-sdk-realtime/blob/4be1697/packages/realtime/src/site/types.ts#L27"
|
|
70
|
-
}
|
|
71
|
-
],
|
|
72
|
-
"parameters": [
|
|
73
|
-
{
|
|
74
|
-
"id": 5,
|
|
75
|
-
"name": "channel",
|
|
76
|
-
"variant": "param",
|
|
77
|
-
"kind": 32768,
|
|
78
|
-
"flags": {},
|
|
79
|
-
"type": {
|
|
80
|
-
"type": "reference",
|
|
81
|
-
"target": {
|
|
82
|
-
"sourceFileName": "src/site/types.ts",
|
|
83
|
-
"qualifiedName": "Channel"
|
|
84
|
-
},
|
|
85
|
-
"name": "Channel",
|
|
86
|
-
"package": "@wix/realtime"
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
"id": 6,
|
|
91
|
-
"name": "payload",
|
|
92
|
-
"variant": "param",
|
|
93
|
-
"kind": 32768,
|
|
94
|
-
"flags": {},
|
|
95
|
-
"type": {
|
|
96
|
-
"type": "intrinsic",
|
|
97
|
-
"name": "unknown"
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
"id": 7,
|
|
102
|
-
"name": "options",
|
|
103
|
-
"variant": "param",
|
|
104
|
-
"kind": 32768,
|
|
105
|
-
"flags": {
|
|
106
|
-
"isOptional": true
|
|
107
|
-
},
|
|
108
|
-
"type": {
|
|
109
|
-
"type": "reference",
|
|
110
|
-
"target": {
|
|
111
|
-
"sourceFileName": "src/site/types.ts",
|
|
112
|
-
"qualifiedName": "PublishOptions"
|
|
113
|
-
},
|
|
114
|
-
"name": "PublishOptions",
|
|
115
|
-
"package": "@wix/realtime"
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
],
|
|
119
|
-
"type": {
|
|
120
|
-
"type": "reference",
|
|
121
|
-
"target": {
|
|
122
|
-
"sourceFileName": "../../node_modules/typescript/lib/lib.es5.d.ts",
|
|
123
|
-
"qualifiedName": "Promise"
|
|
124
|
-
},
|
|
125
|
-
"typeArguments": [
|
|
126
|
-
{
|
|
127
|
-
"type": "intrinsic",
|
|
128
|
-
"name": "unknown"
|
|
129
|
-
}
|
|
130
|
-
],
|
|
131
|
-
"name": "Promise",
|
|
132
|
-
"package": "typescript"
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
]
|
|
136
|
-
}
|
|
137
|
-
],
|
|
138
|
-
"groups": [
|
|
139
|
-
{
|
|
140
|
-
"title": "Variables",
|
|
141
|
-
"children": [
|
|
142
|
-
2
|
|
143
|
-
]
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
"title": "Functions",
|
|
147
|
-
"children": [
|
|
148
|
-
3
|
|
149
|
-
]
|
|
150
|
-
}
|
|
151
|
-
],
|
|
152
|
-
"sources": [
|
|
153
|
-
{
|
|
154
|
-
"fileName": "src/site/index.ts",
|
|
155
|
-
"line": 1,
|
|
156
|
-
"character": 0,
|
|
157
|
-
"url": "https://github.com/wix-private/wix-sdk-realtime/blob/4be1697/packages/realtime/src/site/index.ts#L1"
|
|
158
|
-
}
|
|
159
|
-
]
|
|
160
|
-
}
|
|
161
|
-
],
|
|
162
|
-
"groups": [
|
|
163
|
-
{
|
|
164
|
-
"title": "Namespaces",
|
|
165
|
-
"children": [
|
|
166
|
-
1
|
|
167
|
-
]
|
|
168
|
-
}
|
|
169
|
-
],
|
|
170
|
-
"packageName": "@wix/realtime",
|
|
171
|
-
"symbolIdMap": {}
|
|
172
|
-
},
|
|
173
|
-
"appendices": {
|
|
174
|
-
"/": [
|
|
175
|
-
{
|
|
176
|
-
"type": "GUIDE",
|
|
177
|
-
"title": "Introduction",
|
|
178
|
-
"src": "# Introduction\nUsing the Realtime API you can create channels and channel resources where messages\n can be published. Site visitors who subscribe to a channel receive the published\n messages published to that channel. When a message is received you can handle the message any\n way you choose, such as displaying the contents of the message or changing the state\n of page elements depending on the contents of the message. You can also manage the\n permissions of who can read from specific channels.\n\n The `wix-realtime-backend` module is used in conjunction with the \n [`wix-realtime-frontend`](wix-realtime-frontend.html) module.\n\n Get hands-on experience with the Realtime API on our [Hello Realtime](https://www.wix.com/velo/example/hello-realtime) example page. \n\n Typical uses of the Realtime API include implementing a messaging system or\n as an alternative to using a polling mechanism.\n\n The Realtime API uses the following terminology:\n\n + **Channel**: A specific channel on which messages are published. Each channel\n has a unique name. Site visitors can subscribe to a channel to receive the \n messages that are published on it. When site visitors subscribe to a channel, they do not \n receive messages published to a resource on that same channel.\n + **Channel resource**: A subchannel on which messages are published. Subchannels\n can share a name with a channel and other subchannels, but they have a unique\n resource ID. When site visitors subscribe to a channel resource, they do not \n receive messages published to that same channel without a specified resource.\n Channel resources inherit their parent channel's permissions, unless specified\n otherwise.\n + **Message**: A message published on a channel or channel resource. A message can\n be of any type, such as a string, number, or object. Channel subscribers receive\n the messages published on the channel.\n + **Publish**: Sending a message over a channel or channel resource.\n + **Subscribe**: A site visitor subscribes to receive the messages published on a \n channel or channel resource.\n + **Permissions router**: The permissions router is used to define the permissions\n you want to grant to subscribers on channels and channel resources.\n + **Permissions handler**: Handles a permissions check for a specific user on a \n specific channel or channel resource.\n \n For a tutorial on using the Realtime API, see \n [Sending Messages with the Realtime API](https://support.wix.com/en/article/velo-tutorial-sending-messages-with-the-realtime-api).\n\n To use the Realtime API in the backend, import `wixRealtimeBackend` from the `wix-realtime-backend` module:\n\n ```javascript\n import wixRealtimeBackend from 'wix-realtime-backend';\n ```"
|
|
179
|
-
}
|
|
180
|
-
],
|
|
181
|
-
"/site/publish": [
|
|
182
|
-
{
|
|
183
|
-
"type": "EXAMPLE",
|
|
184
|
-
"title": "Publish to a channel",
|
|
185
|
-
"src": "import wixRealtimeBackend from 'wix-realtime-backend';\n\n// ...\n\nconst channel = {\"name\": \"someChannel\"};\n\nwixRealtimeBackend.publish(channel, \"My message\")\n .then(() => {\n // published\n });"
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
"type": "EXAMPLE",
|
|
189
|
-
"title": "Publish to a resource on a channel",
|
|
190
|
-
"src": "import wixRealtimeBackend from 'wix-realtime-backend';\n\n// ...\n\nconst channel = {\n \"name\": \"someChannel\",\n \"resourceId\": \"someId\"\n};\n\nwixRealtimeBackend.publish(channel, \"My message\")\n .then(() => {\n // published\n });"
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
"type": "EXAMPLE",
|
|
194
|
-
"title": "Publish to specific users on a channel and include publisher information",
|
|
195
|
-
"src": "import wixRealtimeBackend from 'wix-realtime-backend';\n\n// ...\n\nconst channel = {\"name\": \"someChannel\"};\n\nconst message = {\n \"firstKey\": \"firstValue\",\n \"secondKey\": \"secondValue\"\n};\n\nconst user1 = // get ID of first user to publish to\nconst user2 = // get ID of second user to publish to\n\nconst options = {\n \"users\" : [user1, user2],\n \"includePublisher\": true\n};\n\nwixRealtimeBackend.publish(channel, message, options)\n .then( () => {\n // published\n } );"
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
"type": "EXAMPLE",
|
|
199
|
-
"title": "Send and receive messages across multiple channels and channel resources",
|
|
200
|
-
"src": "import { subscribe } from 'wix-realtime-frontend';\nimport { publishMessage } from 'backend/realtime';\n\nconst channelA = { name: 'a' };\nconst channelB = { name: 'b' };\nconst channelB1 = { name: 'b', resourceId: '1' };\nconst channelB2 = { name: 'b', resourceId: '2' };\n\n$w.onReady(function () {\n subscribeToChannels();\n setupPublishButtons();\n});\n\nfunction subscribeToChannels() {\n subscribe(channelA, messageHandler);\n subscribe(channelB, messageHandler);\n subscribe(channelB1, messageHandler);\n subscribe(channelB2, messageHandler);\n}\n\nfunction setupPublishButtons() {\n $w('#publishButtonA').onClick(event => {\n publish(event.target, channelA);\n });\n\n $w('#publishButtonB').onClick(event => {\n publish(event.target, channelB);\n });\n\n $w('#publishButtonB1').onClick(event => {\n publish(event.target, channelB1);\n });\n\n $w('#publishButtonB2').onClick(event => {\n publish(event.target, channelB2);\n });\n}\n\nasync function publish(button, channel) {\n if ($w('#message').value) {\n button.disable();\n $w('#message').disable();\n\n await publishMessage(channel, $w('#message').value);\n\n $w('#message').value = undefined;\n $w('#message').enable();\n button.enable();\n }\n}\n\nfunction messageHandler(message, channel) {\n let textBoxId;\n\n if (channel.name === 'a') {\n textBoxId = '#receivedMessagesA';\n } else { // Channel B - check resource ID\n if (channel.resourceId === '1') {\n textBoxId = '#receivedMessagesB1';\n } else if (channel.resourceId === '2') {\n textBoxId = '#receivedMessagesB2';\n } else {\n textBoxId = '#receivedMessagesB';\n }\n }\n\n $w(textBoxId).value += (message.payload + '\\n');\n}",
|
|
201
|
-
"description": "You can test out the code in our [example template](https://manage.wix.com/edit-template/from?originTemplateId=a527f79a-9a33-46f9-a246-2d6c8dc37e5e&templateId=3a469df1-5a74-47ae-bb5e-5e21d6a4d5ef&autoDevMode=true)."
|
|
202
|
-
}
|
|
203
|
-
],
|
|
204
|
-
"/realtime_check_permission": [
|
|
205
|
-
{
|
|
206
|
-
"type": "EXAMPLE",
|
|
207
|
-
"title": "Grant permissions for a channel based on subscriber type",
|
|
208
|
-
"src": "// In realtime-permissions.js\n\nexport function realtime_check_permission(channel, subscriber) {\n // set default permissions\n let permissions = {\"read\": true};\n \n if(channel.name === \"MembersOnly\") {\n if(subscriber.type === \"Member\"){\n permissions.read = true; \n }\n else {\n permissions.read = false;\n }\n }\n \n return permissions;\n}"
|
|
209
|
-
},
|
|
210
|
-
{
|
|
211
|
-
"type": "EXAMPLE",
|
|
212
|
-
"title": "Grant permissions for a channel based on subscriber type using the permissions router",
|
|
213
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\npermissionsRouter.default( (channel, subscriber) => {\n return {\"read\": true};\n} );\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {\n if(subscriber.type === \"Member\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": false};\n }\n});\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
"type": "EXAMPLE",
|
|
217
|
-
"title": "Grant permissions based on user data",
|
|
218
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\n\nexport async function realtime_check_permission(channel, subscriber) {\n // set default permissions\n let permissions = { \"read\": true };\n\n if(channel.name === \"BobsOnly\") {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n if(member.contactDetails.firstName === \"Bob\") {\n permissions.read = true;\n }\n else {\n permissions.read = false;\n }\n }\n \n return permissions;\n}"
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
"type": "EXAMPLE",
|
|
222
|
-
"title": "Grant permissions based on user data using the permissions router",
|
|
223
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\nimport { permissionsRouter } from 'wix-realtime-backend';\n\npermissionsRouter.default((channel, subscriber) => {\n return {\"read\": true};\n});\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n \n if(channel.resourceId === \"BobsOnly\") {\n if(member.contactDetails.firstName === \"Bob\") {\n return { \"read\": true };\n }\n else {\n return { \"read\": false };\n }\n }\n else {\n return { \"read\": true };\n }\n} );\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel, subscriber);\n}"
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
"type": "EXAMPLE",
|
|
227
|
-
"title": "Check permissions across multiple channels and channel resources",
|
|
228
|
-
"src": "import { subscribe } from 'wix-realtime-frontend';\nimport { publishMessage } from 'backend/realtime';\n\nconst updatesChannel = {name: 'updates'};\nconst updatesMembersChannel = {name: 'updates', resourceId: 'members'};\nconst updatesAdminsChannel = {name: 'updates', resourceId: 'admins'};\nconst updatesGuestsChannel = {name: 'updates', resourceId: 'guests'};\n\n$w.onReady(function () {\n subscribeToChannels();\n setupPublishButtons();\n});\n\nfunction subscribeToChannels() {\n subscribe(updatesChannel, messageHandler)\n .catch(() => console.log('Not subscribed to updates channel'));\n subscribe(updatesMembersChannel, messageHandler)\n .catch(() => console.log('Not subscribed to members channel'));\n subscribe(updatesAdminsChannel, messageHandler)\n .catch(() => console.log('Not subscribed to admins channel'));\n subscribe(updatesGuestsChannel, messageHandler)\n .catch(() => console.log('Not subscribed to guest channel'));\n}\n\nfunction messageHandler(message, channel) {\n const channelName = channel.name;\n const channelResource = channel.resourceId ? `-${channel.resourceId}` : '';\n let newMessageLine = `${channelName}${channelResource}: ${message.payload}\\n`;\n $w('#receivedMessages').value += newMessageLine;\n}\n\nfunction setupPublishButtons() {\n $w('#updateButton').onClick(event => {\n publish(event.target, updatesChannel);\n });\n\n $w('#memberUpdateButton').onClick(event => {\n publish(event.target, updatesMembersChannel);\n });\n\n $w('#adminUpdateButton').onClick(event => {\n publish(event.target, updatesAdminsChannel);\n });\n\n $w('#guestUpdateButton').onClick(event => {\n publish(event.target, updatesGuestsChannel);\n });\n}\n\nasync function publish(button, channel) {\n if ($w('#message').value) {\n button.disable();\n $w('#message').disable();\n\n await publishMessage(channel, $w('#message').value);\n\n $w('#message').value = undefined;\n $w('#message').enable();\n button.enable();\n }\n}",
|
|
229
|
-
"description": "You can test out the code in our [example template](https://manage.wix.com/edit-template/from?originTemplateId=a527f79a-9a33-46f9-a246-2d6c8dc37e5e&templateId=3a469df1-5a74-47ae-bb5e-5e21d6a4d5ef&autoDevMode=true)."
|
|
230
|
-
}
|
|
231
|
-
],
|
|
232
|
-
"/permissionsRouter": [
|
|
233
|
-
{
|
|
234
|
-
"type": "EXAMPLE",
|
|
235
|
-
"title": "Grant permissions for a channel based on subscriber type using the permissions router",
|
|
236
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\npermissionsRouter.default( (channel, subscriber) => {\n return {\"read\": true};\n} );\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {\n if(subscriber.type === \"Member\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": false};\n }\n});\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
237
|
-
},
|
|
238
|
-
{
|
|
239
|
-
"type": "EXAMPLE",
|
|
240
|
-
"title": "Grant permissions based on user data using the permissions router",
|
|
241
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\nimport { permissionsRouter } from 'wix-realtime-backend';\n\npermissionsRouter.default((channel, subscriber) => {\n return {\"read\": true};\n});\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n \n if(channel.resourceId === \"BobsOnly\") {\n if(member.contactDetails.firstName === \"Bob\") {\n return { \"read\": true };\n }\n else {\n return { \"read\": false };\n }\n }\n else {\n return { \"read\": true };\n }\n} );\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel, subscriber);\n}"
|
|
242
|
-
}
|
|
243
|
-
],
|
|
244
|
-
"/PermissionsRouter": [
|
|
245
|
-
{
|
|
246
|
-
"type": "GUIDE",
|
|
247
|
-
"title": "Introduction",
|
|
248
|
-
"src": "# Introduction\nUse the permissions router to define the permissions you want to grant to subscribers\n on channels or channel resources. You can set permissions on the channel and channel\n resource level. If you do not define permissions for a particular channel resource,\n that resource inherits the permissions of its parent channel.\n\n Typically you use the permissions router in the **realtime-permissions.js** file in\n your site's backend to:\n\n + Set default permissions for all channels and channel resources where no explicit\n permissions are defined.\n + Add permissions logic for specific channels or channel resources.\n + Check the permissions for a specific subscriber on a specific channel.\n\n > **Note:** Although it is recommended, you do not need to use the permissions router to set permissions. You can also define all your permissions logic in the [`realtime_check_permission()`](#realtime_check_permission) function."
|
|
249
|
-
}
|
|
250
|
-
],
|
|
251
|
-
"/PermissionsRouter/add": [
|
|
252
|
-
{
|
|
253
|
-
"type": "EXAMPLE",
|
|
254
|
-
"title": "Set a permissions handler for a specific channel",
|
|
255
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\n// ...\n\nconst adminAnnouncements = {\"name\": \"announcements\"}\n\npermissionsRouter.add( adminAnnouncements, (channel, subscriber) => {\n // add permissions check logic and return\n // permissions for announcements channel\n if(subscriber.type === \"Admin\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": true};\n }\n} );"
|
|
256
|
-
},
|
|
257
|
-
{
|
|
258
|
-
"type": "EXAMPLE",
|
|
259
|
-
"title": "Grant permissions for a channel based on subscriber type using the permissions router",
|
|
260
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\npermissionsRouter.default( (channel, subscriber) => {\n return {\"read\": true};\n} );\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {\n if(subscriber.type === \"Member\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": false};\n }\n});\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
261
|
-
},
|
|
262
|
-
{
|
|
263
|
-
"type": "EXAMPLE",
|
|
264
|
-
"title": "Grant permissions based on user data using the permissions router",
|
|
265
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\nimport { permissionsRouter } from 'wix-realtime-backend';\n\npermissionsRouter.default((channel, subscriber) => {\n return {\"read\": true};\n});\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n \n if(channel.resourceId === \"BobsOnly\") {\n if(member.contactDetails.firstName === \"Bob\") {\n return { \"read\": true };\n }\n else {\n return { \"read\": false };\n }\n }\n else {\n return { \"read\": true };\n }\n} );\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel, subscriber);\n}"
|
|
266
|
-
}
|
|
267
|
-
],
|
|
268
|
-
"/PermissionsRouter/check": [
|
|
269
|
-
{
|
|
270
|
-
"type": "EXAMPLE",
|
|
271
|
-
"title": "Check permission for a subscriber to a channel",
|
|
272
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\n// ...\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
273
|
-
},
|
|
274
|
-
{
|
|
275
|
-
"type": "EXAMPLE",
|
|
276
|
-
"title": "Grant permissions for a channel based on subscriber type using the permissions router",
|
|
277
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\npermissionsRouter.default( (channel, subscriber) => {\n return {\"read\": true};\n} );\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {\n if(subscriber.type === \"Member\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": false};\n }\n});\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
278
|
-
},
|
|
279
|
-
{
|
|
280
|
-
"type": "EXAMPLE",
|
|
281
|
-
"title": "Grant permissions based on user data using the permissions router",
|
|
282
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\nimport { permissionsRouter } from 'wix-realtime-backend';\n\npermissionsRouter.default((channel, subscriber) => {\n return {\"read\": true};\n});\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n \n if(channel.resourceId === \"BobsOnly\") {\n if(member.contactDetails.firstName === \"Bob\") {\n return { \"read\": true };\n }\n else {\n return { \"read\": false };\n }\n }\n else {\n return { \"read\": true };\n }\n} );\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel, subscriber);\n}"
|
|
283
|
-
}
|
|
284
|
-
],
|
|
285
|
-
"/PermissionsRouter/default": [
|
|
286
|
-
{
|
|
287
|
-
"type": "EXAMPLE",
|
|
288
|
-
"title": "Set a default permissions handler",
|
|
289
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\n// ...\n\npermissionsRouter.default( (channel, subscriber) => {\n // return default permissions\n return {\"read\": true};\n} );"
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
"type": "EXAMPLE",
|
|
293
|
-
"title": "Grant permissions for a channel based on subscriber type using the permissions router",
|
|
294
|
-
"src": "// In realtime-permissions.js\n\nimport {permissionsRouter} from 'wix-realtime-backend';\n\npermissionsRouter.default( (channel, subscriber) => {\n return {\"read\": true};\n} );\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {\n if(subscriber.type === \"Member\") {\n return {\"read\": true};\n }\n else {\n return {\"read\": false};\n }\n});\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel,subscriber);\n}"
|
|
295
|
-
},
|
|
296
|
-
{
|
|
297
|
-
"type": "EXAMPLE",
|
|
298
|
-
"title": "Grant permissions based on user data using the permissions router",
|
|
299
|
-
"src": "// In realtime-permissions.js\n\nimport { members } from `wix-members-backend`;\nimport { permissionsRouter } from 'wix-realtime-backend';\n\npermissionsRouter.default((channel, subscriber) => {\n return {\"read\": true};\n});\n\nconst membersOnlyChannel = {\"name\": \"MembersOnly\"};\n\npermissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {\n let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });\n \n if(channel.resourceId === \"BobsOnly\") {\n if(member.contactDetails.firstName === \"Bob\") {\n return { \"read\": true };\n }\n else {\n return { \"read\": false };\n }\n }\n else {\n return { \"read\": true };\n }\n} );\n\nexport function realtime_check_permission(channel, subscriber) {\n return permissionsRouter.check(channel, subscriber);\n}"
|
|
300
|
-
}
|
|
301
|
-
]
|
|
302
|
-
},
|
|
303
|
-
"standaloneTypes": {},
|
|
304
|
-
"metadata": {}
|
|
305
|
-
}
|