@wix/essentials 0.1.8 → 0.1.10

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/auth.d.ts CHANGED
@@ -1,21 +1,67 @@
1
1
  import { RESTFunctionDescriptor } from '@wix/sdk-types';
2
2
  export declare function elevate<T extends RESTFunctionDescriptor>(restModule: T): T;
3
+ /**
4
+ * Returns the information encoded in the currently active token in API backend extensions.
5
+ *
6
+ * When developing API backend extensions you might need to access information about the session that is making the request.
7
+ *
8
+ * This can include the user ID, the site ID, the instance ID, and more. This information is encoded in the token that is sent with the request
9
+ * and can be accessed using this function.
10
+ * @returns A promise that resolves to the token info.
11
+ * @example
12
+ * ```ts
13
+ * // In an API backend extension in a Wix CLI project
14
+ * import { auth } from '@wix/essentials';
15
+ *
16
+ * export async function GET(req: Request) {
17
+ * const tokenInfo = await auth.getTokenInfo();
18
+ *
19
+ * if (tokenInfo.subjectType === 'USER') {
20
+ * return new Response(`Hello user ${tokenInfo.subjectId}`);
21
+ * } else if (tokenInfo.subjectType === 'APP') {
22
+ * return new Response('Hello app');
23
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
24
+ * return new Response(`Hello member ${tokenInfo.subjectId}`);
25
+ * } else {
26
+ * return new Response(`Hello visitor ${tokenInfo.subjectId}`);
27
+ * }
28
+ * }
29
+ * ```
30
+ * @example
31
+ * ```ts
32
+ * // In a Velo web method
33
+ * import { auth } from '@wix/essentials';
34
+ * import { Permissions, webMethod } from 'wix-web-module';
35
+ *
36
+ * export const sayHello = webMethod(Permimissions.Anyone, async () => {
37
+ * const tokenInfo = await auth.getTokenInfo();
38
+ *
39
+ * if (tokenInfo.subjectType === 'USER') {
40
+ * return `Hello user ${tokenInfo.subjectId}`;
41
+ * } else if (tokenInfo.subjectType === 'APP') {
42
+ * return 'Hello app';
43
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
44
+ * return `Hello member ${tokenInfo.subjectId}`;
45
+ * } else {
46
+ * return `Hello visitor ${tokenInfo.subjectId}`;
47
+ * }
48
+ */
3
49
  export declare const getTokenInfo: (() => Promise<{
4
50
  active: boolean;
5
- subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
51
+ subjectType: "APP" | "USER" | "MEMBER" | "VISITOR" | "UNKNOWN";
6
52
  subjectId: string;
7
53
  exp: number;
8
54
  iat: number;
9
- clientId?: string | undefined;
55
+ clientId?: string;
10
56
  siteId: string;
11
- instanceId?: string | undefined;
57
+ instanceId?: string;
12
58
  }>) & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => () => Promise<{
13
59
  active: boolean;
14
- subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
60
+ subjectType: "APP" | "USER" | "MEMBER" | "VISITOR" | "UNKNOWN";
15
61
  subjectId: string;
16
62
  exp: number;
17
63
  iat: number;
18
- clientId?: string | undefined;
64
+ clientId?: string;
19
65
  siteId: string;
20
- instanceId?: string | undefined;
66
+ instanceId?: string;
21
67
  }>);
package/build/auth.js CHANGED
@@ -2,6 +2,52 @@ import { createRESTModule } from '@wix/sdk-runtime/rest-modules';
2
2
  export function elevate(restModule) {
3
3
  return createRESTModule(restModule, true);
4
4
  }
5
+ /**
6
+ * Returns the information encoded in the currently active token in API backend extensions.
7
+ *
8
+ * When developing API backend extensions you might need to access information about the session that is making the request.
9
+ *
10
+ * This can include the user ID, the site ID, the instance ID, and more. This information is encoded in the token that is sent with the request
11
+ * and can be accessed using this function.
12
+ * @returns A promise that resolves to the token info.
13
+ * @example
14
+ * ```ts
15
+ * // In an API backend extension in a Wix CLI project
16
+ * import { auth } from '@wix/essentials';
17
+ *
18
+ * export async function GET(req: Request) {
19
+ * const tokenInfo = await auth.getTokenInfo();
20
+ *
21
+ * if (tokenInfo.subjectType === 'USER') {
22
+ * return new Response(`Hello user ${tokenInfo.subjectId}`);
23
+ * } else if (tokenInfo.subjectType === 'APP') {
24
+ * return new Response('Hello app');
25
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
26
+ * return new Response(`Hello member ${tokenInfo.subjectId}`);
27
+ * } else {
28
+ * return new Response(`Hello visitor ${tokenInfo.subjectId}`);
29
+ * }
30
+ * }
31
+ * ```
32
+ * @example
33
+ * ```ts
34
+ * // In a Velo web method
35
+ * import { auth } from '@wix/essentials';
36
+ * import { Permissions, webMethod } from 'wix-web-module';
37
+ *
38
+ * export const sayHello = webMethod(Permimissions.Anyone, async () => {
39
+ * const tokenInfo = await auth.getTokenInfo();
40
+ *
41
+ * if (tokenInfo.subjectType === 'USER') {
42
+ * return `Hello user ${tokenInfo.subjectId}`;
43
+ * } else if (tokenInfo.subjectType === 'APP') {
44
+ * return 'Hello app';
45
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
46
+ * return `Hello member ${tokenInfo.subjectId}`;
47
+ * } else {
48
+ * return `Hello visitor ${tokenInfo.subjectId}`;
49
+ * }
50
+ */
5
51
  export const getTokenInfo = createRESTModule((restModuleOpts) => {
6
52
  return async () => {
7
53
  if (!restModuleOpts.getActiveToken) {
@@ -1,16 +1,16 @@
1
1
  import { DocumentNode, GraphQLFormattedError } from 'graphql';
2
2
  import { RESTFunctionDescriptor } from '@wix/sdk-types';
3
3
  export declare const fetchWithAuth: typeof fetch & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => typeof fetch);
4
- export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
4
+ export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables, opts?: {
5
5
  apiVersion: string;
6
6
  }) => Promise<{
7
7
  data: Result;
8
- errors?: GraphQLFormattedError[] | undefined;
9
- }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
8
+ errors?: GraphQLFormattedError[];
9
+ }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables, opts?: {
10
10
  apiVersion: string;
11
11
  }) => Promise<{
12
12
  data: Result;
13
- errors?: GraphQLFormattedError[] | undefined;
13
+ errors?: GraphQLFormattedError[];
14
14
  }>)>;
15
15
  export type TypedQueryInput<Result = {
16
16
  [key: string]: any;
package/build/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- export { auth, httpClient };
3
+ import * as webMethods from './web-methods.js';
4
+ export { auth, httpClient, webMethods };
4
5
  export { i18n } from './i18n.js';
package/build/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- export { auth, httpClient };
3
+ import * as webMethods from './web-methods.js';
4
+ export { auth, httpClient, webMethods };
4
5
  export { i18n } from './i18n.js';
@@ -0,0 +1,8 @@
1
+ type Handler<T extends unknown[], R> = (...args: T) => R;
2
+ export declare function webMethod<T extends unknown[], R>(permission: Permissions, handler: Handler<T, R>): Handler<T, Promise<Awaited<R>>>;
3
+ export declare enum Permissions {
4
+ Anyone = "anyone",
5
+ Admin = "admin",
6
+ SiteMember = "site-member"
7
+ }
8
+ export {};
@@ -0,0 +1,14 @@
1
+ // https://dev.wix.com/docs/velo/api-reference/wix-web-module/introduction
2
+ export function webMethod(permission, handler) {
3
+ return {
4
+ handler,
5
+ permission,
6
+ // The type assertion is necessary so developers using this get the types they expect
7
+ };
8
+ }
9
+ export var Permissions;
10
+ (function (Permissions) {
11
+ Permissions["Anyone"] = "anyone";
12
+ Permissions["Admin"] = "admin";
13
+ Permissions["SiteMember"] = "site-member";
14
+ })(Permissions || (Permissions = {}));
@@ -1,21 +1,67 @@
1
1
  import { RESTFunctionDescriptor } from '@wix/sdk-types';
2
2
  export declare function elevate<T extends RESTFunctionDescriptor>(restModule: T): T;
3
+ /**
4
+ * Returns the information encoded in the currently active token in API backend extensions.
5
+ *
6
+ * When developing API backend extensions you might need to access information about the session that is making the request.
7
+ *
8
+ * This can include the user ID, the site ID, the instance ID, and more. This information is encoded in the token that is sent with the request
9
+ * and can be accessed using this function.
10
+ * @returns A promise that resolves to the token info.
11
+ * @example
12
+ * ```ts
13
+ * // In an API backend extension in a Wix CLI project
14
+ * import { auth } from '@wix/essentials';
15
+ *
16
+ * export async function GET(req: Request) {
17
+ * const tokenInfo = await auth.getTokenInfo();
18
+ *
19
+ * if (tokenInfo.subjectType === 'USER') {
20
+ * return new Response(`Hello user ${tokenInfo.subjectId}`);
21
+ * } else if (tokenInfo.subjectType === 'APP') {
22
+ * return new Response('Hello app');
23
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
24
+ * return new Response(`Hello member ${tokenInfo.subjectId}`);
25
+ * } else {
26
+ * return new Response(`Hello visitor ${tokenInfo.subjectId}`);
27
+ * }
28
+ * }
29
+ * ```
30
+ * @example
31
+ * ```ts
32
+ * // In a Velo web method
33
+ * import { auth } from '@wix/essentials';
34
+ * import { Permissions, webMethod } from 'wix-web-module';
35
+ *
36
+ * export const sayHello = webMethod(Permimissions.Anyone, async () => {
37
+ * const tokenInfo = await auth.getTokenInfo();
38
+ *
39
+ * if (tokenInfo.subjectType === 'USER') {
40
+ * return `Hello user ${tokenInfo.subjectId}`;
41
+ * } else if (tokenInfo.subjectType === 'APP') {
42
+ * return 'Hello app';
43
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
44
+ * return `Hello member ${tokenInfo.subjectId}`;
45
+ * } else {
46
+ * return `Hello visitor ${tokenInfo.subjectId}`;
47
+ * }
48
+ */
3
49
  export declare const getTokenInfo: (() => Promise<{
4
50
  active: boolean;
5
- subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
51
+ subjectType: "APP" | "USER" | "MEMBER" | "VISITOR" | "UNKNOWN";
6
52
  subjectId: string;
7
53
  exp: number;
8
54
  iat: number;
9
- clientId?: string | undefined;
55
+ clientId?: string;
10
56
  siteId: string;
11
- instanceId?: string | undefined;
57
+ instanceId?: string;
12
58
  }>) & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => () => Promise<{
13
59
  active: boolean;
14
- subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
60
+ subjectType: "APP" | "USER" | "MEMBER" | "VISITOR" | "UNKNOWN";
15
61
  subjectId: string;
16
62
  exp: number;
17
63
  iat: number;
18
- clientId?: string | undefined;
64
+ clientId?: string;
19
65
  siteId: string;
20
- instanceId?: string | undefined;
66
+ instanceId?: string;
21
67
  }>);
package/cjs/build/auth.js CHANGED
@@ -1,11 +1,57 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTokenInfo = exports.elevate = void 0;
3
+ exports.getTokenInfo = void 0;
4
+ exports.elevate = elevate;
4
5
  const rest_modules_1 = require("@wix/sdk-runtime/rest-modules");
5
6
  function elevate(restModule) {
6
7
  return (0, rest_modules_1.createRESTModule)(restModule, true);
7
8
  }
8
- exports.elevate = elevate;
9
+ /**
10
+ * Returns the information encoded in the currently active token in API backend extensions.
11
+ *
12
+ * When developing API backend extensions you might need to access information about the session that is making the request.
13
+ *
14
+ * This can include the user ID, the site ID, the instance ID, and more. This information is encoded in the token that is sent with the request
15
+ * and can be accessed using this function.
16
+ * @returns A promise that resolves to the token info.
17
+ * @example
18
+ * ```ts
19
+ * // In an API backend extension in a Wix CLI project
20
+ * import { auth } from '@wix/essentials';
21
+ *
22
+ * export async function GET(req: Request) {
23
+ * const tokenInfo = await auth.getTokenInfo();
24
+ *
25
+ * if (tokenInfo.subjectType === 'USER') {
26
+ * return new Response(`Hello user ${tokenInfo.subjectId}`);
27
+ * } else if (tokenInfo.subjectType === 'APP') {
28
+ * return new Response('Hello app');
29
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
30
+ * return new Response(`Hello member ${tokenInfo.subjectId}`);
31
+ * } else {
32
+ * return new Response(`Hello visitor ${tokenInfo.subjectId}`);
33
+ * }
34
+ * }
35
+ * ```
36
+ * @example
37
+ * ```ts
38
+ * // In a Velo web method
39
+ * import { auth } from '@wix/essentials';
40
+ * import { Permissions, webMethod } from 'wix-web-module';
41
+ *
42
+ * export const sayHello = webMethod(Permimissions.Anyone, async () => {
43
+ * const tokenInfo = await auth.getTokenInfo();
44
+ *
45
+ * if (tokenInfo.subjectType === 'USER') {
46
+ * return `Hello user ${tokenInfo.subjectId}`;
47
+ * } else if (tokenInfo.subjectType === 'APP') {
48
+ * return 'Hello app';
49
+ * } else if (tokenInfo.subjectType === 'MEMBER') {
50
+ * return `Hello member ${tokenInfo.subjectId}`;
51
+ * } else {
52
+ * return `Hello visitor ${tokenInfo.subjectId}`;
53
+ * }
54
+ */
9
55
  exports.getTokenInfo = (0, rest_modules_1.createRESTModule)((restModuleOpts) => {
10
56
  return async () => {
11
57
  if (!restModuleOpts.getActiveToken) {
@@ -1,16 +1,16 @@
1
1
  import { DocumentNode, GraphQLFormattedError } from 'graphql';
2
2
  import { RESTFunctionDescriptor } from '@wix/sdk-types';
3
3
  export declare const fetchWithAuth: typeof fetch & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => typeof fetch);
4
- export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
4
+ export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables, opts?: {
5
5
  apiVersion: string;
6
6
  }) => Promise<{
7
7
  data: Result;
8
- errors?: GraphQLFormattedError[] | undefined;
9
- }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
8
+ errors?: GraphQLFormattedError[];
9
+ }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables, opts?: {
10
10
  apiVersion: string;
11
11
  }) => Promise<{
12
12
  data: Result;
13
- errors?: GraphQLFormattedError[] | undefined;
13
+ errors?: GraphQLFormattedError[];
14
14
  }>)>;
15
15
  export type TypedQueryInput<Result = {
16
16
  [key: string]: any;
@@ -1,4 +1,5 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- export { auth, httpClient };
3
+ import * as webMethods from './web-methods.js';
4
+ export { auth, httpClient, webMethods };
4
5
  export { i18n } from './i18n.js';
@@ -15,18 +15,30 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
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
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.i18n = exports.httpClient = exports.auth = void 0;
36
+ exports.i18n = exports.webMethods = exports.httpClient = exports.auth = void 0;
27
37
  const auth = __importStar(require("./auth.js"));
28
38
  exports.auth = auth;
29
39
  const httpClient = __importStar(require("./http-client.js"));
30
40
  exports.httpClient = httpClient;
41
+ const webMethods = __importStar(require("./web-methods.js"));
42
+ exports.webMethods = webMethods;
31
43
  var i18n_js_1 = require("./i18n.js");
32
44
  Object.defineProperty(exports, "i18n", { enumerable: true, get: function () { return i18n_js_1.i18n; } });
@@ -0,0 +1,8 @@
1
+ type Handler<T extends unknown[], R> = (...args: T) => R;
2
+ export declare function webMethod<T extends unknown[], R>(permission: Permissions, handler: Handler<T, R>): Handler<T, Promise<Awaited<R>>>;
3
+ export declare enum Permissions {
4
+ Anyone = "anyone",
5
+ Admin = "admin",
6
+ SiteMember = "site-member"
7
+ }
8
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Permissions = void 0;
4
+ exports.webMethod = webMethod;
5
+ // https://dev.wix.com/docs/velo/api-reference/wix-web-module/introduction
6
+ function webMethod(permission, handler) {
7
+ return {
8
+ handler,
9
+ permission,
10
+ // The type assertion is necessary so developers using this get the types they expect
11
+ };
12
+ }
13
+ var Permissions;
14
+ (function (Permissions) {
15
+ Permissions["Anyone"] = "anyone";
16
+ Permissions["Admin"] = "admin";
17
+ Permissions["SiteMember"] = "site-member";
18
+ })(Permissions || (exports.Permissions = Permissions = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/essentials",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "license": "UNLICENSED",
5
5
  "main": "cjs/build/index.js",
6
6
  "module": "build/index.mjs",
@@ -13,6 +13,9 @@
13
13
  "./package.json": "./package.json"
14
14
  },
15
15
  "sideEffects": false,
16
+ "keywords": [
17
+ "wix-sdk-module=backend,page,public"
18
+ ],
16
19
  "files": [
17
20
  "build",
18
21
  "cjs"
@@ -32,26 +35,26 @@
32
35
  "*.{js,ts}": "yarn lint"
33
36
  },
34
37
  "dependencies": {
35
- "@wix/sdk-runtime": "^0.3.21",
36
- "@wix/sdk-types": "^1.12.3"
38
+ "@wix/sdk-runtime": "^0.3.24",
39
+ "@wix/sdk-types": "^1.12.4"
37
40
  },
38
41
  "optionalDependencies": {
39
42
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
40
43
  },
41
44
  "devDependencies": {
42
45
  "@types/is-ci": "^3.0.4",
43
- "@types/node": "^20.10.6",
44
- "@vitest/ui": "^1.5.0",
45
- "@wix/sdk": "1.14.1",
46
- "eslint": "^8.56.0",
46
+ "@types/node": "^20.17.9",
47
+ "@vitest/ui": "^1.6.0",
48
+ "@wix/sdk": "1.14.4",
49
+ "eslint": "^8.57.1",
47
50
  "eslint-config-sdk": "0.0.0",
48
51
  "graphql": "^16.8.0",
49
52
  "is-ci": "^3.0.1",
50
53
  "jsdom": "^22.1.0",
51
- "msw": "^2.4.5",
52
- "typescript": "^5.3.3",
53
- "vitest": "^1.5.0",
54
- "vitest-teamcity-reporter": "^0.3.0"
54
+ "msw": "^2.6.6",
55
+ "typescript": "^5.7.2",
56
+ "vitest": "^1.6.0",
57
+ "vitest-teamcity-reporter": "^0.3.1"
55
58
  },
56
59
  "eslintConfig": {
57
60
  "extends": "sdk"
@@ -70,5 +73,5 @@
70
73
  ]
71
74
  }
72
75
  },
73
- "falconPackageHash": "93165f2988c7cfab1c5bffb6a15c467d48775c9205707b5ed63f41e1"
76
+ "falconPackageHash": "89b3fc54d75286af87e57b252a4f02bc1fc55b7fc2d44a71e9faf0b4"
74
77
  }